blob: 9289d73baa7e044b14fe3e8a03c7f51b8e9e04db [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
Henrik Lundin4d682082015-12-10 16:24:39 +010011#include "webrtc/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
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000017#include "testing/gtest/include/gtest/gtest.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010018#include "webrtc/modules/include/module_common_types.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000020namespace webrtc {
21
niklase@google.com470e71d2011-07-07 08:21:25 +000022#define MAX_FILE_NAME_LENGTH_BYTE 500
23
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000024PCMFile::PCMFile()
25 : pcm_file_(NULL),
26 samples_10ms_(160),
27 frequency_(16000),
28 end_of_file_(false),
29 auto_rewind_(false),
30 rewinded_(false),
31 read_stereo_(false),
32 save_stereo_(false) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000033 timestamp_ = (((uint32_t) rand() & 0x0000FFFF) << 16) |
34 ((uint32_t) rand() & 0x0000FFFF);
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
pbos@webrtc.org0946a562013-04-09 00:28:06 +000037PCMFile::PCMFile(uint32_t timestamp)
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000038 : pcm_file_(NULL),
39 samples_10ms_(160),
40 frequency_(16000),
41 end_of_file_(false),
42 auto_rewind_(false),
43 rewinded_(false),
44 read_stereo_(false),
45 save_stereo_(false) {
46 timestamp_ = timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +000047}
48
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000049int16_t PCMFile::ChooseFile(std::string* file_name, int16_t max_len,
pbos@webrtc.org0946a562013-04-09 00:28:06 +000050 uint16_t* frequency_hz) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000051 char tmp_name[MAX_FILE_NAME_LENGTH_BYTE];
52
53 EXPECT_TRUE(fgets(tmp_name, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL);
54 tmp_name[MAX_FILE_NAME_LENGTH_BYTE - 1] = '\0';
pbos@webrtc.org0946a562013-04-09 00:28:06 +000055 int16_t n = 0;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000056
57 // Removing trailing spaces.
58 while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (tmp_name[n] != 0)
59 && (n < MAX_FILE_NAME_LENGTH_BYTE)) {
60 n++;
61 }
62 if (n > 0) {
63 memmove(tmp_name, &tmp_name[n], MAX_FILE_NAME_LENGTH_BYTE - n);
64 }
65
66 // Removing trailing spaces.
pbos@webrtc.org0946a562013-04-09 00:28:06 +000067 n = (int16_t)(strlen(tmp_name) - 1);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000068 if (n >= 0) {
69 while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (n >= 0)) {
70 n--;
niklase@google.com470e71d2011-07-07 08:21:25 +000071 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000072 }
73 if (n >= 0) {
74 tmp_name[n + 1] = '\0';
75 }
niklase@google.com470e71d2011-07-07 08:21:25 +000076
pbos@webrtc.org0946a562013-04-09 00:28:06 +000077 int16_t len = (int16_t) strlen(tmp_name);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000078 if (len > max_len) {
79 return -1;
80 }
81 if (len > 0) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000082 std::string tmp_string(tmp_name, len + 1);
83 *file_name = tmp_string;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000084 }
85 printf("Enter the sampling frequency (in Hz) of the above file [%u]: ",
86 *frequency_hz);
87 EXPECT_TRUE(fgets(tmp_name, 10, stdin) != NULL);
pbos@webrtc.org0946a562013-04-09 00:28:06 +000088 uint16_t tmp_frequency = (uint16_t) atoi(tmp_name);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000089 if (tmp_frequency > 0) {
90 *frequency_hz = tmp_frequency;
91 }
92 return 0;
93}
94
pbos@webrtc.org0946a562013-04-09 00:28:06 +000095void PCMFile::Open(const std::string& file_name, uint16_t frequency,
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000096 const char* mode, bool auto_rewind) {
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000097 if ((pcm_file_ = fopen(file_name.c_str(), mode)) == NULL) {
98 printf("Cannot open file %s.\n", file_name.c_str());
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000099 ADD_FAILURE() << "Unable to read file";
100 }
101 frequency_ = frequency;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000102 samples_10ms_ = (uint16_t)(frequency_ / 100);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000103 auto_rewind_ = auto_rewind;
104 end_of_file_ = false;
105 rewinded_ = false;
106}
107
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000108int32_t PCMFile::SamplingFrequency() const {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000109 return frequency_;
110}
111
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000112uint16_t PCMFile::PayloadLength10Ms() const {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000113 return samples_10ms_;
114}
115
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000116int32_t PCMFile::Read10MsData(AudioFrame& audio_frame) {
117 uint16_t channels = 1;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000118 if (read_stereo_) {
119 channels = 2;
120 }
121
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000122 int32_t payload_size = (int32_t) fread(audio_frame.data_, sizeof(uint16_t),
123 samples_10ms_ * channels, pcm_file_);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000124 if (payload_size < samples_10ms_ * channels) {
125 for (int k = payload_size; k < samples_10ms_ * channels; k++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000126 audio_frame.data_[k] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000128 if (auto_rewind_) {
129 rewind(pcm_file_);
130 rewinded_ = true;
131 } else {
132 end_of_file_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000133 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000134 }
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000135 audio_frame.samples_per_channel_ = samples_10ms_;
136 audio_frame.sample_rate_hz_ = frequency_;
137 audio_frame.num_channels_ = channels;
138 audio_frame.timestamp_ = timestamp_;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000139 timestamp_ += samples_10ms_;
Henrik Lundin4d682082015-12-10 16:24:39 +0100140 ++blocks_read_;
141 if (num_10ms_blocks_to_read_ && blocks_read_ >= *num_10ms_blocks_to_read_)
142 end_of_file_ = true;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000143 return samples_10ms_;
144}
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000146void PCMFile::Write10MsData(AudioFrame& audio_frame) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000147 if (audio_frame.num_channels_ == 1) {
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000148 if (!save_stereo_) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000149 if (fwrite(audio_frame.data_, sizeof(uint16_t),
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000150 audio_frame.samples_per_channel_, pcm_file_) !=
151 static_cast<size_t>(audio_frame.samples_per_channel_)) {
152 return;
153 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000154 } else {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000155 int16_t* stereo_audio = new int16_t[2 * audio_frame.samples_per_channel_];
Peter Kastingdce40cf2015-08-24 14:52:23 -0700156 for (size_t k = 0; k < audio_frame.samples_per_channel_; k++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000157 stereo_audio[k << 1] = audio_frame.data_[k];
158 stereo_audio[(k << 1) + 1] = audio_frame.data_[k];
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000159 }
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000160 if (fwrite(stereo_audio, sizeof(int16_t),
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000161 2 * audio_frame.samples_per_channel_, pcm_file_) !=
162 static_cast<size_t>(2 * audio_frame.samples_per_channel_)) {
163 return;
164 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000165 delete[] stereo_audio;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000167 } else {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000168 if (fwrite(audio_frame.data_, sizeof(int16_t),
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000169 audio_frame.num_channels_ * audio_frame.samples_per_channel_,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000170 pcm_file_) !=
171 static_cast<size_t>(audio_frame.num_channels_ *
172 audio_frame.samples_per_channel_)) {
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000173 return;
174 }
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000175 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000176}
177
Peter Kastingdce40cf2015-08-24 14:52:23 -0700178void PCMFile::Write10MsData(int16_t* playout_buffer, size_t length_smpls) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000179 if (fwrite(playout_buffer, sizeof(uint16_t), length_smpls, pcm_file_) !=
180 length_smpls) {
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000181 return;
182 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000183}
184
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000185void PCMFile::Close() {
186 fclose(pcm_file_);
187 pcm_file_ = NULL;
Henrik Lundin4d682082015-12-10 16:24:39 +0100188 blocks_read_ = 0;
189}
190
191void PCMFile::FastForward(int num_10ms_blocks) {
192 const int channels = read_stereo_ ? 2 : 1;
193 long num_bytes_to_move =
194 num_10ms_blocks * sizeof(int16_t) * samples_10ms_ * channels;
195 int error = fseek(pcm_file_, num_bytes_to_move, SEEK_CUR);
196 RTC_DCHECK_EQ(error, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197}
198
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000199void PCMFile::Rewind() {
200 rewind(pcm_file_);
201 end_of_file_ = false;
Henrik Lundin4d682082015-12-10 16:24:39 +0100202 blocks_read_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203}
204
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000205bool PCMFile::Rewinded() {
206 return rewinded_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207}
208
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000209void PCMFile::SaveStereo(bool is_stereo) {
210 save_stereo_ = is_stereo;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000213void PCMFile::ReadStereo(bool is_stereo) {
214 read_stereo_ = is_stereo;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
Henrik Lundin4d682082015-12-10 16:24:39 +0100217void PCMFile::SetNum10MsBlocksToRead(int value) {
218 num_10ms_blocks_to_read_ = rtc::Optional<int>(value);
219}
220
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +0000221} // namespace webrtc