Index: media/base/audio_bus.cc |
diff --git a/media/base/audio_bus.cc b/media/base/audio_bus.cc |
index 8b7fa3dde87589dc5188791d58e6868778b3c901..85b090ec74939a84c6d86a4a4e03a7555cefe71f 100644 |
--- a/media/base/audio_bus.cc |
+++ b/media/base/audio_bus.cc |
@@ -25,12 +25,17 @@ static bool IsAligned(void* ptr) { |
(AudioBus::kChannelAlignment - 1)) == 0U; |
} |
-// Calculates the required size for an AudioBus with the given params, sets |
-// |aligned_frames| to the actual frame length of each channel array. |
+// In order to guarantee that the memory block for each channel starts at an |
+// aligned address when splitting a contiguous block of memory into one block |
+// per channel, we may have to make these blocks larger than otherwise needed. |
+// We do this by allocating space for potentially more frames than requested. |
+// This method returns the required size for the contiguous memory block |
+// in bytes and outputs the adjusted number of frames via |out_aligned_frames|. |
static int CalculateMemorySizeInternal(int channels, int frames, |
int* out_aligned_frames) { |
- // Choose a size such that each channel will be aligned by |
- // kChannelAlignment when stored in a contiguous block. |
+ // Since our internal sample format is float, we can guarantee the alignment |
+ // by making the number of frames an integer multiple of |
+ // AudioBus::kChannelAlignment / sizeof(float). |
int aligned_frames = |
((frames * sizeof(float) + AudioBus::kChannelAlignment - 1) & |
~(AudioBus::kChannelAlignment - 1)) / sizeof(float); |
@@ -248,7 +253,7 @@ int AudioBus::CalculateMemorySize(int channels, int frames) { |
void AudioBus::BuildChannelData(int channels, int aligned_frames, float* data) { |
DCHECK(IsAligned(data)); |
DCHECK_EQ(channel_data_.size(), 0U); |
- // Separate audio data out into channels for easy lookup later. Figure out |
+ // Initialize |channel_data_| with pointers into |data|. |
channel_data_.reserve(channels); |
for (int i = 0; i < channels; ++i) |
channel_data_.push_back(data + i * aligned_frames); |
@@ -300,6 +305,22 @@ void AudioBus::ToInterleaved(int frames, int bytes_per_sample, |
ToInterleavedPartial(0, frames, bytes_per_sample, dest); |
} |
+// Interleaves |audio_bus| channels() of floats into a single output linear |
+// |buffer|. |
+void AudioBus::ToInterleavedFloat(int source_offset, |
+ int destination_offset, |
+ int num_samples, |
miu
2016/06/02 20:40:21
s/num_samples/frames/ (for naming consistency with
|
+ float* buffer) const { |
+ CheckOverflow(source_offset, destination_offset, num_samples); |
+ for (int ch = 0; ch < channels(); ++ch) { |
+ const float* src = channel(ch) + source_offset; |
+ const float* const src_end = src + num_samples; |
+ float* dest = buffer + destination_offset + ch; |
+ for (; src < src_end; ++src, dest += channels()) |
+ *dest = *src; |
+ } |
+} |
+ |
// TODO(dalecurtis): See if intrinsic optimizations help any here. |
void AudioBus::ToInterleavedPartial(int start_frame, int frames, |
int bytes_per_sample, void* dest) const { |