blob: bdb46eb5a8596e3566050cc12b90ce771249d5af [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org91b359e2012-02-28 17:26:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/test/PCMFile.h"
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <ctype.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <stdio.h>
15#include <string.h>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "test/gtest.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000019namespace webrtc {
20
niklase@google.com470e71d2011-07-07 08:21:25 +000021#define MAX_FILE_NAME_LENGTH_BYTE 500
22
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000023PCMFile::PCMFile()
24 : pcm_file_(NULL),
25 samples_10ms_(160),
26 frequency_(16000),
27 end_of_file_(false),
28 auto_rewind_(false),
29 rewinded_(false),
30 read_stereo_(false),
31 save_stereo_(false) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000032 timestamp_ = (((uint32_t) rand() & 0x0000FFFF) << 16) |
33 ((uint32_t) rand() & 0x0000FFFF);
niklase@google.com470e71d2011-07-07 08:21:25 +000034}
35
pbos@webrtc.org0946a562013-04-09 00:28:06 +000036PCMFile::PCMFile(uint32_t timestamp)
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000037 : pcm_file_(NULL),
38 samples_10ms_(160),
39 frequency_(16000),
40 end_of_file_(false),
41 auto_rewind_(false),
42 rewinded_(false),
43 read_stereo_(false),
44 save_stereo_(false) {
45 timestamp_ = timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
kwiberg65fc8b92016-08-29 10:05:24 -070048PCMFile::~PCMFile() {
49 if (pcm_file_) {
50 fclose(pcm_file_);
51 }
52}
53
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000054int16_t PCMFile::ChooseFile(std::string* file_name, int16_t max_len,
pbos@webrtc.org0946a562013-04-09 00:28:06 +000055 uint16_t* frequency_hz) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000056 char tmp_name[MAX_FILE_NAME_LENGTH_BYTE];
57
58 EXPECT_TRUE(fgets(tmp_name, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL);
59 tmp_name[MAX_FILE_NAME_LENGTH_BYTE - 1] = '\0';
pbos@webrtc.org0946a562013-04-09 00:28:06 +000060 int16_t n = 0;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000061
62 // Removing trailing spaces.
63 while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (tmp_name[n] != 0)
64 && (n < MAX_FILE_NAME_LENGTH_BYTE)) {
65 n++;
66 }
67 if (n > 0) {
68 memmove(tmp_name, &tmp_name[n], MAX_FILE_NAME_LENGTH_BYTE - n);
69 }
70
71 // Removing trailing spaces.
pbos@webrtc.org0946a562013-04-09 00:28:06 +000072 n = (int16_t)(strlen(tmp_name) - 1);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000073 if (n >= 0) {
74 while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (n >= 0)) {
75 n--;
niklase@google.com470e71d2011-07-07 08:21:25 +000076 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000077 }
78 if (n >= 0) {
79 tmp_name[n + 1] = '\0';
80 }
niklase@google.com470e71d2011-07-07 08:21:25 +000081
pbos@webrtc.org0946a562013-04-09 00:28:06 +000082 int16_t len = (int16_t) strlen(tmp_name);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000083 if (len > max_len) {
84 return -1;
85 }
86 if (len > 0) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000087 std::string tmp_string(tmp_name, len + 1);
88 *file_name = tmp_string;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000089 }
90 printf("Enter the sampling frequency (in Hz) of the above file [%u]: ",
91 *frequency_hz);
92 EXPECT_TRUE(fgets(tmp_name, 10, stdin) != NULL);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000093 uint16_t tmp_frequency = (uint16_t) atoi(tmp_name);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000094 if (tmp_frequency > 0) {
95 *frequency_hz = tmp_frequency;
96 }
97 return 0;
98}
99
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000100void PCMFile::Open(const std::string& file_name, uint16_t frequency,
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000101 const char* mode, bool auto_rewind) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +0000102 if ((pcm_file_ = fopen(file_name.c_str(), mode)) == NULL) {
103 printf("Cannot open file %s.\n", file_name.c_str());
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000104 ADD_FAILURE() << "Unable to read file";
105 }
106 frequency_ = frequency;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000107 samples_10ms_ = (uint16_t)(frequency_ / 100);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000108 auto_rewind_ = auto_rewind;
109 end_of_file_ = false;
110 rewinded_ = false;
111}
112
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000113int32_t PCMFile::SamplingFrequency() const {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000114 return frequency_;
115}
116
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000117uint16_t PCMFile::PayloadLength10Ms() const {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000118 return samples_10ms_;
119}
120
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000121int32_t PCMFile::Read10MsData(AudioFrame& audio_frame) {
122 uint16_t channels = 1;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000123 if (read_stereo_) {
124 channels = 2;
125 }
126
yujo36b1a5f2017-06-12 12:45:32 -0700127 int32_t payload_size = (int32_t) fread(audio_frame.mutable_data(),
128 sizeof(uint16_t),
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000129 samples_10ms_ * channels, pcm_file_);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000130 if (payload_size < samples_10ms_ * channels) {
yujo36b1a5f2017-06-12 12:45:32 -0700131 int16_t* frame_data = audio_frame.mutable_data();
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000132 for (int k = payload_size; k < samples_10ms_ * channels; k++) {
yujo36b1a5f2017-06-12 12:45:32 -0700133 frame_data[k] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000135 if (auto_rewind_) {
136 rewind(pcm_file_);
137 rewinded_ = true;
138 } else {
139 end_of_file_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000141 }
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000142 audio_frame.samples_per_channel_ = samples_10ms_;
143 audio_frame.sample_rate_hz_ = frequency_;
144 audio_frame.num_channels_ = channels;
145 audio_frame.timestamp_ = timestamp_;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000146 timestamp_ += samples_10ms_;
Henrik Lundin4d682082015-12-10 16:24:39 +0100147 ++blocks_read_;
148 if (num_10ms_blocks_to_read_ && blocks_read_ >= *num_10ms_blocks_to_read_)
149 end_of_file_ = true;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000150 return samples_10ms_;
151}
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
yujo36b1a5f2017-06-12 12:45:32 -0700153void PCMFile::Write10MsData(const AudioFrame& audio_frame) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000154 if (audio_frame.num_channels_ == 1) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000155 if (!save_stereo_) {
yujo36b1a5f2017-06-12 12:45:32 -0700156 if (fwrite(audio_frame.data(), sizeof(uint16_t),
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000157 audio_frame.samples_per_channel_, pcm_file_) !=
158 static_cast<size_t>(audio_frame.samples_per_channel_)) {
159 return;
160 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000161 } else {
yujo36b1a5f2017-06-12 12:45:32 -0700162 const int16_t* frame_data = audio_frame.data();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000163 int16_t* stereo_audio = new int16_t[2 * audio_frame.samples_per_channel_];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700164 for (size_t k = 0; k < audio_frame.samples_per_channel_; k++) {
yujo36b1a5f2017-06-12 12:45:32 -0700165 stereo_audio[k << 1] = frame_data[k];
166 stereo_audio[(k << 1) + 1] = frame_data[k];
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000167 }
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000168 if (fwrite(stereo_audio, sizeof(int16_t),
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000169 2 * audio_frame.samples_per_channel_, pcm_file_) !=
170 static_cast<size_t>(2 * audio_frame.samples_per_channel_)) {
171 return;
172 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000173 delete[] stereo_audio;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000175 } else {
yujo36b1a5f2017-06-12 12:45:32 -0700176 if (fwrite(audio_frame.data(), sizeof(int16_t),
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000177 audio_frame.num_channels_ * audio_frame.samples_per_channel_,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000178 pcm_file_) !=
179 static_cast<size_t>(audio_frame.num_channels_ *
180 audio_frame.samples_per_channel_)) {
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000181 return;
182 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000183 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
185
yujo36b1a5f2017-06-12 12:45:32 -0700186void PCMFile::Write10MsData(const int16_t* playout_buffer,
187 size_t length_smpls) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000188 if (fwrite(playout_buffer, sizeof(uint16_t), length_smpls, pcm_file_) !=
189 length_smpls) {
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000190 return;
191 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000192}
193
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000194void PCMFile::Close() {
195 fclose(pcm_file_);
196 pcm_file_ = NULL;
Henrik Lundin4d682082015-12-10 16:24:39 +0100197 blocks_read_ = 0;
198}
199
200void PCMFile::FastForward(int num_10ms_blocks) {
201 const int channels = read_stereo_ ? 2 : 1;
202 long num_bytes_to_move =
203 num_10ms_blocks * sizeof(int16_t) * samples_10ms_ * channels;
204 int error = fseek(pcm_file_, num_bytes_to_move, SEEK_CUR);
205 RTC_DCHECK_EQ(error, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206}
207
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000208void PCMFile::Rewind() {
209 rewind(pcm_file_);
210 end_of_file_ = false;
Henrik Lundin4d682082015-12-10 16:24:39 +0100211 blocks_read_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000214bool PCMFile::Rewinded() {
215 return rewinded_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000216}
217
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000218void PCMFile::SaveStereo(bool is_stereo) {
219 save_stereo_ = is_stereo;
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
221
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000222void PCMFile::ReadStereo(bool is_stereo) {
223 read_stereo_ = is_stereo;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
Henrik Lundin4d682082015-12-10 16:24:39 +0100226void PCMFile::SetNum10MsBlocksToRead(int value) {
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100227 num_10ms_blocks_to_read_ = value;
Henrik Lundin4d682082015-12-10 16:24:39 +0100228}
229
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000230} // namespace webrtc