| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef MEDIA_BASE_AUDIO_BUS_H_ | 5 #ifndef MEDIA_BASE_AUDIO_BUS_H_ |
| 6 #define MEDIA_BASE_AUDIO_BUS_H_ | 6 #define MEDIA_BASE_AUDIO_BUS_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/aligned_memory.h" | 10 #include "base/memory/aligned_memory.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "media/base/media_export.h" | 12 #include "media/base/media_export.h" |
| 13 | 13 |
| 14 namespace media { | 14 namespace media { |
| 15 class AudioParameters; | 15 class AudioParameters; |
| 16 | 16 |
| 17 // Scoped container for "busing" audio channel data around. Each channel is | 17 // Scoped container for "busing" audio channel data around. Each channel is |
| 18 // stored in planar format and guaranteed to have a 16-byte alignment. AudioBus | 18 // stored in planar format and guaranteed to be aligned by kChannelAlignment. |
| 19 // objects can be created normally or via wrapping. Normally, AudioBus will | 19 // AudioBus objects can be created normally or via wrapping. Normally, AudioBus |
| 20 // dice up a contiguous memory block for channel data. When wrapped, AudioBus | 20 // will dice up a contiguous memory block for channel data. When wrapped, |
| 21 // instead routes requests for channel data to the wrapped object. | 21 // AudioBus instead routes requests for channel data to the wrapped object. |
| 22 class MEDIA_EXPORT AudioBus { | 22 class MEDIA_EXPORT AudioBus { |
| 23 public: | 23 public: |
| 24 // Guaranteed alignment of each channel's data; use 16-byte alignment for easy |
| 25 // SSE optimizations. |
| 26 enum { kChannelAlignment = 16 }; |
| 27 |
| 24 // Creates a new AudioBus and allocates |channels| of length |frames|. Uses | 28 // Creates a new AudioBus and allocates |channels| of length |frames|. Uses |
| 25 // channels() and frames_per_buffer() if given an AudioParameters object. | 29 // channels() and frames_per_buffer() from AudioParameters if given. |
| 26 static scoped_ptr<AudioBus> Create(int channels, int frames); | 30 static scoped_ptr<AudioBus> Create(int channels, int frames); |
| 27 static scoped_ptr<AudioBus> Create(const AudioParameters& params); | 31 static scoped_ptr<AudioBus> Create(const AudioParameters& params); |
| 28 | 32 |
| 29 // Creates a new AudioBus from an existing channel vector. Does not transfer | 33 // Creates a new AudioBus from an existing channel vector. Does not transfer |
| 30 // ownership of |channel_data| to AudioBus; i.e., |channel_data| must outlive | 34 // ownership of |channel_data| to AudioBus; i.e., |channel_data| must outlive |
| 31 // the returned AudioBus. Each channel pointer must be 16-byte aligned. | 35 // the returned AudioBus. Each channel must be aligned by kChannelAlignment. |
| 32 static scoped_ptr<AudioBus> WrapVector( | 36 static scoped_ptr<AudioBus> WrapVector( |
| 33 int frames, const std::vector<float*>& channel_data); | 37 int frames, const std::vector<float*>& channel_data); |
| 34 | 38 |
| 35 // Returns a raw pointer to internal channel data. Useful for copying state | 39 // Creates a new AudioBus by wrapping an existing block of memory. Block must |
| 36 // between two AudioBus objects created with the same parameters. data_size() | 40 // be at least CalculateMemorySize() bytes in size. |data| must outlive the |
| 37 // is in bytes. Can not be used with an AudioBus constructed via wrapping. | 41 // returned AudioBus. |data| must be aligned by kChannelAlignment. |
| 38 void* data(); | 42 static scoped_ptr<AudioBus> WrapMemory(const AudioParameters& params, |
| 39 int data_size() const; | 43 void* data); |
| 44 // Returns the required memory size to use the WrapMemory() method. |
| 45 static int CalculateMemorySize(const AudioParameters& params); |
| 46 |
| 47 // Helper methods for converting an AudioBus from and to interleaved integer |
| 48 // data. Expects interleaving to be [ch0, ch1, ..., chN, ch0, ch1, ...] with |
| 49 // |bytes_per_sample| per value. Values are scaled and bias corrected during |
| 50 // conversion. ToInterleaved() will also clip values to format range. |
| 51 // Handles uint8, int16, and int32 currently. |
| 52 void FromInterleaved(const void* source, int frames, int bytes_per_sample); |
| 53 void ToInterleaved(int frames, int bytes_per_sample, void* dest) const; |
| 54 |
| 55 // Helper method for copying channel data from one AudioBus to another. Both |
| 56 // AudioBus object must have the same frames() and channels(). |
| 57 void CopyTo(AudioBus* dest) const; |
| 40 | 58 |
| 41 // Returns a raw pointer to the requested channel. Pointer is guaranteed to | 59 // Returns a raw pointer to the requested channel. Pointer is guaranteed to |
| 42 // have a 16-byte alignment. | 60 // have a 16-byte alignment. |
| 43 float* channel(int channel) { return channel_data_[channel]; } | 61 float* channel(int channel) { return channel_data_[channel]; } |
| 44 const float* channel(int channel) const { return channel_data_[channel]; } | 62 const float* channel(int channel) const { return channel_data_[channel]; } |
| 45 | 63 |
| 46 int channels() const { return channel_data_.size(); } | 64 int channels() const { return channel_data_.size(); } |
| 47 int frames() const { return frames_; } | 65 int frames() const { return frames_; } |
| 48 | 66 |
| 49 // Helper method for zeroing out all channels of audio data. | 67 // Helper method for zeroing out all channels of audio data. |
| 50 void Zero(); | 68 void Zero(); |
| 51 void ZeroFrames(int frames); | 69 void ZeroFrames(int frames); |
| 52 | 70 |
| 53 private: | 71 private: |
| 54 friend class scoped_ptr<AudioBus>; | 72 friend class scoped_ptr<AudioBus>; |
| 55 ~AudioBus(); | 73 ~AudioBus(); |
| 56 | 74 |
| 57 AudioBus(int channels, int frames); | 75 AudioBus(int channels, int frames); |
| 76 AudioBus(int channels, int frames, float* data); |
| 58 AudioBus(int frames, const std::vector<float*>& channel_data); | 77 AudioBus(int frames, const std::vector<float*>& channel_data); |
| 59 | 78 |
| 79 // Helper method for building |channel_data_| from a block of memory. |data| |
| 80 // must be at least BlockSize() bytes in size. |
| 81 void BuildChannelData(int channels, int aligned_frame, float* data); |
| 82 |
| 60 // Contiguous block of channel memory. | 83 // Contiguous block of channel memory. |
| 61 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> data_; | 84 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> data_; |
| 62 int data_size_; | |
| 63 | 85 |
| 64 std::vector<float*> channel_data_; | 86 std::vector<float*> channel_data_; |
| 65 int frames_; | 87 int frames_; |
| 66 | 88 |
| 67 DISALLOW_COPY_AND_ASSIGN(AudioBus); | 89 DISALLOW_COPY_AND_ASSIGN(AudioBus); |
| 68 }; | 90 }; |
| 69 | 91 |
| 70 } // namespace media | 92 } // namespace media |
| 71 | 93 |
| 72 #endif // MEDIA_BASE_AUDIO_BUS_H_ | 94 #endif // MEDIA_BASE_AUDIO_BUS_H_ |
| OLD | NEW |