OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "PCMFile.h" | 11 #include "webrtc/modules/audio_coding/test/PCMFile.h" |
12 | 12 |
13 #include <ctype.h> | 13 #include <ctype.h> |
14 #include <stdio.h> | 14 #include <stdio.h> |
15 #include <string.h> | 15 #include <string.h> |
16 | 16 |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "webrtc/modules/include/module_common_types.h" | 18 #include "webrtc/modules/include/module_common_types.h" |
19 | 19 |
20 namespace webrtc { | 20 namespace webrtc { |
21 | 21 |
22 #define MAX_FILE_NAME_LENGTH_BYTE 500 | 22 #define MAX_FILE_NAME_LENGTH_BYTE 500 |
23 | 23 |
24 PCMFile::PCMFile() | 24 PCMFile::PCMFile() |
25 : pcm_file_(NULL), | 25 : pcm_file_(NULL), |
26 samples_10ms_(160), | 26 samples_10ms_(160), |
27 frequency_(16000), | 27 frequency_(16000), |
28 end_of_file_(false), | 28 end_of_file_(false), |
29 auto_rewind_(false), | 29 auto_rewind_(false), |
30 rewinded_(false), | 30 rewinded_(false), |
31 read_stereo_(false), | 31 read_stereo_(false), |
32 save_stereo_(false) { | 32 save_stereo_(false), |
33 num_10ms_blocks_to_read_(rtc::Optional<int>()) { | |
ivoc
2015/12/10 12:23:57
Can this be initialized in the .h file as well? Or
hlundin-webrtc
2015/12/10 12:49:59
In fact, it's not needed at all. The default ctor
| |
33 timestamp_ = (((uint32_t) rand() & 0x0000FFFF) << 16) | | 34 timestamp_ = (((uint32_t) rand() & 0x0000FFFF) << 16) | |
34 ((uint32_t) rand() & 0x0000FFFF); | 35 ((uint32_t) rand() & 0x0000FFFF); |
35 } | 36 } |
36 | 37 |
37 PCMFile::PCMFile(uint32_t timestamp) | 38 PCMFile::PCMFile(uint32_t timestamp) |
38 : pcm_file_(NULL), | 39 : pcm_file_(NULL), |
39 samples_10ms_(160), | 40 samples_10ms_(160), |
40 frequency_(16000), | 41 frequency_(16000), |
41 end_of_file_(false), | 42 end_of_file_(false), |
42 auto_rewind_(false), | 43 auto_rewind_(false), |
43 rewinded_(false), | 44 rewinded_(false), |
44 read_stereo_(false), | 45 read_stereo_(false), |
45 save_stereo_(false) { | 46 save_stereo_(false), |
47 num_10ms_blocks_to_read_(rtc::Optional<int>()) { | |
46 timestamp_ = timestamp; | 48 timestamp_ = timestamp; |
47 } | 49 } |
48 | 50 |
49 int16_t PCMFile::ChooseFile(std::string* file_name, int16_t max_len, | 51 int16_t PCMFile::ChooseFile(std::string* file_name, int16_t max_len, |
50 uint16_t* frequency_hz) { | 52 uint16_t* frequency_hz) { |
51 char tmp_name[MAX_FILE_NAME_LENGTH_BYTE]; | 53 char tmp_name[MAX_FILE_NAME_LENGTH_BYTE]; |
52 | 54 |
53 EXPECT_TRUE(fgets(tmp_name, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL); | 55 EXPECT_TRUE(fgets(tmp_name, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL); |
54 tmp_name[MAX_FILE_NAME_LENGTH_BYTE - 1] = '\0'; | 56 tmp_name[MAX_FILE_NAME_LENGTH_BYTE - 1] = '\0'; |
55 int16_t n = 0; | 57 int16_t n = 0; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 rewinded_ = true; | 132 rewinded_ = true; |
131 } else { | 133 } else { |
132 end_of_file_ = true; | 134 end_of_file_ = true; |
133 } | 135 } |
134 } | 136 } |
135 audio_frame.samples_per_channel_ = samples_10ms_; | 137 audio_frame.samples_per_channel_ = samples_10ms_; |
136 audio_frame.sample_rate_hz_ = frequency_; | 138 audio_frame.sample_rate_hz_ = frequency_; |
137 audio_frame.num_channels_ = channels; | 139 audio_frame.num_channels_ = channels; |
138 audio_frame.timestamp_ = timestamp_; | 140 audio_frame.timestamp_ = timestamp_; |
139 timestamp_ += samples_10ms_; | 141 timestamp_ += samples_10ms_; |
142 ++blocks_read_; | |
143 if (num_10ms_blocks_to_read_ && blocks_read_ >= *num_10ms_blocks_to_read_) | |
144 end_of_file_ = true; | |
140 return samples_10ms_; | 145 return samples_10ms_; |
141 } | 146 } |
142 | 147 |
143 void PCMFile::Write10MsData(AudioFrame& audio_frame) { | 148 void PCMFile::Write10MsData(AudioFrame& audio_frame) { |
144 if (audio_frame.num_channels_ == 1) { | 149 if (audio_frame.num_channels_ == 1) { |
145 if (!save_stereo_) { | 150 if (!save_stereo_) { |
146 if (fwrite(audio_frame.data_, sizeof(uint16_t), | 151 if (fwrite(audio_frame.data_, sizeof(uint16_t), |
147 audio_frame.samples_per_channel_, pcm_file_) != | 152 audio_frame.samples_per_channel_, pcm_file_) != |
148 static_cast<size_t>(audio_frame.samples_per_channel_)) { | 153 static_cast<size_t>(audio_frame.samples_per_channel_)) { |
149 return; | 154 return; |
(...skipping 25 matching lines...) Expand all Loading... | |
175 void PCMFile::Write10MsData(int16_t* playout_buffer, size_t length_smpls) { | 180 void PCMFile::Write10MsData(int16_t* playout_buffer, size_t length_smpls) { |
176 if (fwrite(playout_buffer, sizeof(uint16_t), length_smpls, pcm_file_) != | 181 if (fwrite(playout_buffer, sizeof(uint16_t), length_smpls, pcm_file_) != |
177 length_smpls) { | 182 length_smpls) { |
178 return; | 183 return; |
179 } | 184 } |
180 } | 185 } |
181 | 186 |
182 void PCMFile::Close() { | 187 void PCMFile::Close() { |
183 fclose(pcm_file_); | 188 fclose(pcm_file_); |
184 pcm_file_ = NULL; | 189 pcm_file_ = NULL; |
190 blocks_read_ = 0; | |
191 } | |
192 | |
193 void PCMFile::FastForward(int num_10ms_blocks) { | |
194 const int channels = read_stereo_ ? 2 : 1; | |
195 size_t num_bytes_to_move = | |
196 num_10ms_blocks * sizeof(int16_t) * samples_10ms_ * channels; | |
197 int error = fseek(pcm_file_, num_bytes_to_move, SEEK_CUR); | |
198 RTC_DCHECK_EQ(error, 0); | |
185 } | 199 } |
186 | 200 |
187 void PCMFile::Rewind() { | 201 void PCMFile::Rewind() { |
188 rewind(pcm_file_); | 202 rewind(pcm_file_); |
189 end_of_file_ = false; | 203 end_of_file_ = false; |
204 blocks_read_ = 0; | |
190 } | 205 } |
191 | 206 |
192 bool PCMFile::Rewinded() { | 207 bool PCMFile::Rewinded() { |
193 return rewinded_; | 208 return rewinded_; |
194 } | 209 } |
195 | 210 |
196 void PCMFile::SaveStereo(bool is_stereo) { | 211 void PCMFile::SaveStereo(bool is_stereo) { |
197 save_stereo_ = is_stereo; | 212 save_stereo_ = is_stereo; |
198 } | 213 } |
199 | 214 |
200 void PCMFile::ReadStereo(bool is_stereo) { | 215 void PCMFile::ReadStereo(bool is_stereo) { |
201 read_stereo_ = is_stereo; | 216 read_stereo_ = is_stereo; |
202 } | 217 } |
203 | 218 |
219 void PCMFile::SetNum10MsBlocksToRead(int value) { | |
220 num_10ms_blocks_to_read_ = rtc::Optional<int>(value); | |
221 } | |
222 | |
204 } // namespace webrtc | 223 } // namespace webrtc |
OLD | NEW |