OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/audio_buffer.h" | 5 #include "media/base/audio_buffer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/base/audio_bus.h" | 8 #include "media/base/audio_bus.h" |
9 #include "media/base/buffers.h" | 9 #include "media/base/buffers.h" |
10 #include "media/base/limits.h" | 10 #include "media/base/limits.h" |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 double offset = static_cast<double>(duration_.InMicroseconds()) * | 268 double offset = static_cast<double>(duration_.InMicroseconds()) * |
269 frames_to_trim / adjusted_frame_count_; | 269 frames_to_trim / adjusted_frame_count_; |
270 base::TimeDelta offset_as_time = | 270 base::TimeDelta offset_as_time = |
271 base::TimeDelta::FromMicroseconds(static_cast<int64>(offset)); | 271 base::TimeDelta::FromMicroseconds(static_cast<int64>(offset)); |
272 duration_ -= offset_as_time; | 272 duration_ -= offset_as_time; |
273 | 273 |
274 // Finally adjust the number of frames in this buffer. | 274 // Finally adjust the number of frames in this buffer. |
275 adjusted_frame_count_ -= frames_to_trim; | 275 adjusted_frame_count_ -= frames_to_trim; |
276 } | 276 } |
277 | 277 |
| 278 void AudioBuffer::TrimRange(int start, int end) { |
| 279 const int frames_to_trim = end - start; |
| 280 CHECK_GE(frames_to_trim, 0); |
| 281 CHECK_LE(frames_to_trim, adjusted_frame_count_); |
| 282 |
| 283 const int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format_); |
| 284 const int frames_to_copy = adjusted_frame_count_ - frames_to_trim; |
| 285 switch (sample_format_) { |
| 286 case kSampleFormatPlanarS16: |
| 287 case kSampleFormatPlanarF32: |
| 288 // Planar data must be shifted per channel. |
| 289 for (int ch = 0; ch < channel_count_; ++ch) { |
| 290 memcpy(channel_data_[ch] + start * bytes_per_channel, |
| 291 channel_data_[ch] + end * bytes_per_channel, |
| 292 bytes_per_channel * frames_to_copy); |
| 293 } |
| 294 break; |
| 295 case kSampleFormatU8: |
| 296 case kSampleFormatS16: |
| 297 case kSampleFormatS32: |
| 298 case kSampleFormatF32: { |
| 299 // Interleaved data can be shifted all at once. |
| 300 const int frame_size = channel_count_ * bytes_per_channel; |
| 301 memcpy(channel_data_[0] + start * frame_size, |
| 302 channel_data_[0] + end * frame_size, |
| 303 frame_size * frames_to_copy); |
| 304 break; |
| 305 } |
| 306 case kUnknownSampleFormat: |
| 307 NOTREACHED() << "Invalid sample format!"; |
| 308 } |
| 309 |
| 310 // Trim the leftover data off the end of the buffer and update duration. |
| 311 TrimEnd(frames_to_trim); |
| 312 } |
| 313 |
278 } // namespace media | 314 } // namespace media |
OLD | NEW |