| 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 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 uint16_t PCMFile::PayloadLength10Ms() const { | 118 uint16_t PCMFile::PayloadLength10Ms() const { |
| 119 return samples_10ms_; | 119 return samples_10ms_; |
| 120 } | 120 } |
| 121 | 121 |
| 122 int32_t PCMFile::Read10MsData(AudioFrame& audio_frame) { | 122 int32_t PCMFile::Read10MsData(AudioFrame& audio_frame) { |
| 123 uint16_t channels = 1; | 123 uint16_t channels = 1; |
| 124 if (read_stereo_) { | 124 if (read_stereo_) { |
| 125 channels = 2; | 125 channels = 2; |
| 126 } | 126 } |
| 127 | 127 |
| 128 int32_t payload_size = (int32_t) fread(audio_frame.data_, sizeof(uint16_t), | 128 int32_t payload_size = (int32_t) fread(audio_frame.mutable_data(), |
| 129 sizeof(uint16_t), |
| 129 samples_10ms_ * channels, pcm_file_); | 130 samples_10ms_ * channels, pcm_file_); |
| 130 if (payload_size < samples_10ms_ * channels) { | 131 if (payload_size < samples_10ms_ * channels) { |
| 132 int16_t* frame_data = audio_frame.mutable_data(); |
| 131 for (int k = payload_size; k < samples_10ms_ * channels; k++) { | 133 for (int k = payload_size; k < samples_10ms_ * channels; k++) { |
| 132 audio_frame.data_[k] = 0; | 134 frame_data[k] = 0; |
| 133 } | 135 } |
| 134 if (auto_rewind_) { | 136 if (auto_rewind_) { |
| 135 rewind(pcm_file_); | 137 rewind(pcm_file_); |
| 136 rewinded_ = true; | 138 rewinded_ = true; |
| 137 } else { | 139 } else { |
| 138 end_of_file_ = true; | 140 end_of_file_ = true; |
| 139 } | 141 } |
| 140 } | 142 } |
| 141 audio_frame.samples_per_channel_ = samples_10ms_; | 143 audio_frame.samples_per_channel_ = samples_10ms_; |
| 142 audio_frame.sample_rate_hz_ = frequency_; | 144 audio_frame.sample_rate_hz_ = frequency_; |
| 143 audio_frame.num_channels_ = channels; | 145 audio_frame.num_channels_ = channels; |
| 144 audio_frame.timestamp_ = timestamp_; | 146 audio_frame.timestamp_ = timestamp_; |
| 145 timestamp_ += samples_10ms_; | 147 timestamp_ += samples_10ms_; |
| 146 ++blocks_read_; | 148 ++blocks_read_; |
| 147 if (num_10ms_blocks_to_read_ && blocks_read_ >= *num_10ms_blocks_to_read_) | 149 if (num_10ms_blocks_to_read_ && blocks_read_ >= *num_10ms_blocks_to_read_) |
| 148 end_of_file_ = true; | 150 end_of_file_ = true; |
| 149 return samples_10ms_; | 151 return samples_10ms_; |
| 150 } | 152 } |
| 151 | 153 |
| 152 void PCMFile::Write10MsData(AudioFrame& audio_frame) { | 154 void PCMFile::Write10MsData(const AudioFrame& audio_frame) { |
| 153 if (audio_frame.num_channels_ == 1) { | 155 if (audio_frame.num_channels_ == 1) { |
| 154 if (!save_stereo_) { | 156 if (!save_stereo_) { |
| 155 if (fwrite(audio_frame.data_, sizeof(uint16_t), | 157 if (fwrite(audio_frame.data(), sizeof(uint16_t), |
| 156 audio_frame.samples_per_channel_, pcm_file_) != | 158 audio_frame.samples_per_channel_, pcm_file_) != |
| 157 static_cast<size_t>(audio_frame.samples_per_channel_)) { | 159 static_cast<size_t>(audio_frame.samples_per_channel_)) { |
| 158 return; | 160 return; |
| 159 } | 161 } |
| 160 } else { | 162 } else { |
| 163 const int16_t* frame_data = audio_frame.data(); |
| 161 int16_t* stereo_audio = new int16_t[2 * audio_frame.samples_per_channel_]; | 164 int16_t* stereo_audio = new int16_t[2 * audio_frame.samples_per_channel_]; |
| 162 for (size_t k = 0; k < audio_frame.samples_per_channel_; k++) { | 165 for (size_t k = 0; k < audio_frame.samples_per_channel_; k++) { |
| 163 stereo_audio[k << 1] = audio_frame.data_[k]; | 166 stereo_audio[k << 1] = frame_data[k]; |
| 164 stereo_audio[(k << 1) + 1] = audio_frame.data_[k]; | 167 stereo_audio[(k << 1) + 1] = frame_data[k]; |
| 165 } | 168 } |
| 166 if (fwrite(stereo_audio, sizeof(int16_t), | 169 if (fwrite(stereo_audio, sizeof(int16_t), |
| 167 2 * audio_frame.samples_per_channel_, pcm_file_) != | 170 2 * audio_frame.samples_per_channel_, pcm_file_) != |
| 168 static_cast<size_t>(2 * audio_frame.samples_per_channel_)) { | 171 static_cast<size_t>(2 * audio_frame.samples_per_channel_)) { |
| 169 return; | 172 return; |
| 170 } | 173 } |
| 171 delete[] stereo_audio; | 174 delete[] stereo_audio; |
| 172 } | 175 } |
| 173 } else { | 176 } else { |
| 174 if (fwrite(audio_frame.data_, sizeof(int16_t), | 177 if (fwrite(audio_frame.data(), sizeof(int16_t), |
| 175 audio_frame.num_channels_ * audio_frame.samples_per_channel_, | 178 audio_frame.num_channels_ * audio_frame.samples_per_channel_, |
| 176 pcm_file_) != | 179 pcm_file_) != |
| 177 static_cast<size_t>(audio_frame.num_channels_ * | 180 static_cast<size_t>(audio_frame.num_channels_ * |
| 178 audio_frame.samples_per_channel_)) { | 181 audio_frame.samples_per_channel_)) { |
| 179 return; | 182 return; |
| 180 } | 183 } |
| 181 } | 184 } |
| 182 } | 185 } |
| 183 | 186 |
| 184 void PCMFile::Write10MsData(int16_t* playout_buffer, size_t length_smpls) { | 187 void PCMFile::Write10MsData(const int16_t* playout_buffer, |
| 188 size_t length_smpls) { |
| 185 if (fwrite(playout_buffer, sizeof(uint16_t), length_smpls, pcm_file_) != | 189 if (fwrite(playout_buffer, sizeof(uint16_t), length_smpls, pcm_file_) != |
| 186 length_smpls) { | 190 length_smpls) { |
| 187 return; | 191 return; |
| 188 } | 192 } |
| 189 } | 193 } |
| 190 | 194 |
| 191 void PCMFile::Close() { | 195 void PCMFile::Close() { |
| 192 fclose(pcm_file_); | 196 fclose(pcm_file_); |
| 193 pcm_file_ = NULL; | 197 pcm_file_ = NULL; |
| 194 blocks_read_ = 0; | 198 blocks_read_ = 0; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 218 | 222 |
| 219 void PCMFile::ReadStereo(bool is_stereo) { | 223 void PCMFile::ReadStereo(bool is_stereo) { |
| 220 read_stereo_ = is_stereo; | 224 read_stereo_ = is_stereo; |
| 221 } | 225 } |
| 222 | 226 |
| 223 void PCMFile::SetNum10MsBlocksToRead(int value) { | 227 void PCMFile::SetNum10MsBlocksToRead(int value) { |
| 224 num_10ms_blocks_to_read_ = rtc::Optional<int>(value); | 228 num_10ms_blocks_to_read_ = rtc::Optional<int>(value); |
| 225 } | 229 } |
| 226 | 230 |
| 227 } // namespace webrtc | 231 } // namespace webrtc |
| OLD | NEW |