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