niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 91b359e | 2012-02-28 17:26:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 Lundin | 4d68208 | 2015-12-10 16:24:39 +0100 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/test/PCMFile.h" |
kjellander@webrtc.org | 543c3ea | 2011-11-23 12:20:35 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <ctype.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame] | 17 | #include "webrtc/test/gtest.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/include/module_common_types.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | #define MAX_FILE_NAME_LENGTH_BYTE 500 |
| 23 | |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 24 | PCMFile::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.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 33 | timestamp_ = (((uint32_t) rand() & 0x0000FFFF) << 16) | |
| 34 | ((uint32_t) rand() & 0x0000FFFF); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
| 36 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 37 | PCMFile::PCMFile(uint32_t timestamp) |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 38 | : 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | } |
| 48 | |
kwiberg | 65fc8b9 | 2016-08-29 10:05:24 -0700 | [diff] [blame] | 49 | PCMFile::~PCMFile() { |
| 50 | if (pcm_file_) { |
| 51 | fclose(pcm_file_); |
| 52 | } |
| 53 | } |
| 54 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 55 | int16_t PCMFile::ChooseFile(std::string* file_name, int16_t max_len, |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 56 | uint16_t* frequency_hz) { |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 57 | char tmp_name[MAX_FILE_NAME_LENGTH_BYTE]; |
| 58 | |
| 59 | EXPECT_TRUE(fgets(tmp_name, MAX_FILE_NAME_LENGTH_BYTE, stdin) != NULL); |
| 60 | tmp_name[MAX_FILE_NAME_LENGTH_BYTE - 1] = '\0'; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 61 | int16_t n = 0; |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 62 | |
| 63 | // Removing trailing spaces. |
| 64 | while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (tmp_name[n] != 0) |
| 65 | && (n < MAX_FILE_NAME_LENGTH_BYTE)) { |
| 66 | n++; |
| 67 | } |
| 68 | if (n > 0) { |
| 69 | memmove(tmp_name, &tmp_name[n], MAX_FILE_NAME_LENGTH_BYTE - n); |
| 70 | } |
| 71 | |
| 72 | // Removing trailing spaces. |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 73 | n = (int16_t)(strlen(tmp_name) - 1); |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 74 | if (n >= 0) { |
| 75 | while ((isspace(tmp_name[n]) || iscntrl(tmp_name[n])) && (n >= 0)) { |
| 76 | n--; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | } |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 78 | } |
| 79 | if (n >= 0) { |
| 80 | tmp_name[n + 1] = '\0'; |
| 81 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 83 | int16_t len = (int16_t) strlen(tmp_name); |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 84 | if (len > max_len) { |
| 85 | return -1; |
| 86 | } |
| 87 | if (len > 0) { |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 +0000 | [diff] [blame] | 88 | std::string tmp_string(tmp_name, len + 1); |
| 89 | *file_name = tmp_string; |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 90 | } |
| 91 | printf("Enter the sampling frequency (in Hz) of the above file [%u]: ", |
| 92 | *frequency_hz); |
| 93 | EXPECT_TRUE(fgets(tmp_name, 10, stdin) != NULL); |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 94 | uint16_t tmp_frequency = (uint16_t) atoi(tmp_name); |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 95 | if (tmp_frequency > 0) { |
| 96 | *frequency_hz = tmp_frequency; |
| 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 101 | void PCMFile::Open(const std::string& file_name, uint16_t frequency, |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 102 | const char* mode, bool auto_rewind) { |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 +0000 | [diff] [blame] | 103 | if ((pcm_file_ = fopen(file_name.c_str(), mode)) == NULL) { |
| 104 | printf("Cannot open file %s.\n", file_name.c_str()); |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 105 | ADD_FAILURE() << "Unable to read file"; |
| 106 | } |
| 107 | frequency_ = frequency; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 108 | samples_10ms_ = (uint16_t)(frequency_ / 100); |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 109 | auto_rewind_ = auto_rewind; |
| 110 | end_of_file_ = false; |
| 111 | rewinded_ = false; |
| 112 | } |
| 113 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 114 | int32_t PCMFile::SamplingFrequency() const { |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 115 | return frequency_; |
| 116 | } |
| 117 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 118 | uint16_t PCMFile::PayloadLength10Ms() const { |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 119 | return samples_10ms_; |
| 120 | } |
| 121 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 122 | int32_t PCMFile::Read10MsData(AudioFrame& audio_frame) { |
| 123 | uint16_t channels = 1; |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 124 | if (read_stereo_) { |
| 125 | channels = 2; |
| 126 | } |
| 127 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 128 | int32_t payload_size = (int32_t) fread(audio_frame.data_, sizeof(uint16_t), |
| 129 | samples_10ms_ * channels, pcm_file_); |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 130 | if (payload_size < samples_10ms_ * channels) { |
| 131 | for (int k = payload_size; k < samples_10ms_ * channels; k++) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 132 | audio_frame.data_[k] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | } |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 134 | if (auto_rewind_) { |
| 135 | rewind(pcm_file_); |
| 136 | rewinded_ = true; |
| 137 | } else { |
| 138 | end_of_file_ = true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 140 | } |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 141 | audio_frame.samples_per_channel_ = samples_10ms_; |
| 142 | audio_frame.sample_rate_hz_ = frequency_; |
| 143 | audio_frame.num_channels_ = channels; |
| 144 | audio_frame.timestamp_ = timestamp_; |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 145 | timestamp_ += samples_10ms_; |
Henrik Lundin | 4d68208 | 2015-12-10 16:24:39 +0100 | [diff] [blame] | 146 | ++blocks_read_; |
| 147 | if (num_10ms_blocks_to_read_ && blocks_read_ >= *num_10ms_blocks_to_read_) |
| 148 | end_of_file_ = true; |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 149 | return samples_10ms_; |
| 150 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 152 | void PCMFile::Write10MsData(AudioFrame& audio_frame) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 153 | if (audio_frame.num_channels_ == 1) { |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 154 | if (!save_stereo_) { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 155 | if (fwrite(audio_frame.data_, sizeof(uint16_t), |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 156 | audio_frame.samples_per_channel_, pcm_file_) != |
| 157 | static_cast<size_t>(audio_frame.samples_per_channel_)) { |
| 158 | return; |
| 159 | } |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 160 | } else { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 161 | int16_t* stereo_audio = new int16_t[2 * audio_frame.samples_per_channel_]; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 162 | for (size_t k = 0; k < audio_frame.samples_per_channel_; k++) { |
andrew@webrtc.org | 63a5098 | 2012-05-02 23:56:37 +0000 | [diff] [blame] | 163 | stereo_audio[k << 1] = audio_frame.data_[k]; |
| 164 | stereo_audio[(k << 1) + 1] = audio_frame.data_[k]; |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 165 | } |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 166 | if (fwrite(stereo_audio, sizeof(int16_t), |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 167 | 2 * audio_frame.samples_per_channel_, pcm_file_) != |
| 168 | static_cast<size_t>(2 * audio_frame.samples_per_channel_)) { |
| 169 | return; |
| 170 | } |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 171 | delete[] stereo_audio; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | } |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 173 | } else { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 174 | if (fwrite(audio_frame.data_, sizeof(int16_t), |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 175 | audio_frame.num_channels_ * audio_frame.samples_per_channel_, |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 176 | pcm_file_) != |
| 177 | static_cast<size_t>(audio_frame.num_channels_ * |
| 178 | audio_frame.samples_per_channel_)) { |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 179 | return; |
| 180 | } |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 181 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 184 | void PCMFile::Write10MsData(int16_t* playout_buffer, size_t length_smpls) { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 185 | if (fwrite(playout_buffer, sizeof(uint16_t), length_smpls, pcm_file_) != |
| 186 | length_smpls) { |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 187 | return; |
| 188 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | } |
| 190 | |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 191 | void PCMFile::Close() { |
| 192 | fclose(pcm_file_); |
| 193 | pcm_file_ = NULL; |
Henrik Lundin | 4d68208 | 2015-12-10 16:24:39 +0100 | [diff] [blame] | 194 | blocks_read_ = 0; |
| 195 | } |
| 196 | |
| 197 | void PCMFile::FastForward(int num_10ms_blocks) { |
| 198 | const int channels = read_stereo_ ? 2 : 1; |
| 199 | long num_bytes_to_move = |
| 200 | num_10ms_blocks * sizeof(int16_t) * samples_10ms_ * channels; |
| 201 | int error = fseek(pcm_file_, num_bytes_to_move, SEEK_CUR); |
| 202 | RTC_DCHECK_EQ(error, 0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 203 | } |
| 204 | |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 205 | void PCMFile::Rewind() { |
| 206 | rewind(pcm_file_); |
| 207 | end_of_file_ = false; |
Henrik Lundin | 4d68208 | 2015-12-10 16:24:39 +0100 | [diff] [blame] | 208 | blocks_read_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | } |
| 210 | |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 211 | bool PCMFile::Rewinded() { |
| 212 | return rewinded_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | } |
| 214 | |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 215 | void PCMFile::SaveStereo(bool is_stereo) { |
| 216 | save_stereo_ = is_stereo; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | } |
| 218 | |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 219 | void PCMFile::ReadStereo(bool is_stereo) { |
| 220 | read_stereo_ = is_stereo; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Henrik Lundin | 4d68208 | 2015-12-10 16:24:39 +0100 | [diff] [blame] | 223 | void PCMFile::SetNum10MsBlocksToRead(int value) { |
| 224 | num_10ms_blocks_to_read_ = rtc::Optional<int>(value); |
| 225 | } |
| 226 | |
tina.legrand@webrtc.org | a6ecd1e | 2012-04-26 07:54:30 +0000 | [diff] [blame] | 227 | } // namespace webrtc |