Chromium Code Reviews| 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(int channels, int frames, void* data); |
| 39 int data_size() const; | 43 static scoped_ptr<AudioBus> WrapMemory(const AudioParameters& params, |
| 44 void* data); | |
|
Chris Rogers
2012/08/21 00:14:10
nit: my preference would be to have the void* para
| |
| 45 // Returns the required memory size to use the WrapMemory() method. | |
| 46 static int CalculateMemorySize(const AudioParameters& params); | |
| 47 | |
| 48 // Helper methods for converting an AudioBus from and to interleaved integer | |
| 49 // data. Expects interleaving to be [ch0, ch1, ..., chN, ch0, ch1, ...] with | |
| 50 // |bytes_per_sample| per value. Values are scaled and bias corrected during | |
| 51 // conversion. ToInterleaved() will also clip values to format range. | |
| 52 // Handles uint8, int16, and int32 currently. | |
| 53 void FromInterleaved(const void* source, int frames, int bytes_per_sample); | |
| 54 void ToInterleaved(int frames, int bytes_per_sample, void* dest) const; | |
|
Chris Rogers
2012/08/21 00:14:10
nit: it would be nice to have the pointer |dest| b
| |
| 55 | |
| 56 // Helper method for copying channel data from one AudioBus to another. Both | |
| 57 // AudioBus object must have the same frames() and channels(). | |
| 58 void CopyTo(AudioBus* dest) const; | |
| 40 | 59 |
| 41 // Returns a raw pointer to the requested channel. Pointer is guaranteed to | 60 // Returns a raw pointer to the requested channel. Pointer is guaranteed to |
| 42 // have a 16-byte alignment. | 61 // have a 16-byte alignment. |
| 43 float* channel(int channel) { return channel_data_[channel]; } | 62 float* channel(int channel) { return channel_data_[channel]; } |
| 44 const float* channel(int channel) const { return channel_data_[channel]; } | 63 const float* channel(int channel) const { return channel_data_[channel]; } |
| 45 | 64 |
| 46 int channels() const { return channel_data_.size(); } | 65 int channels() const { return channel_data_.size(); } |
| 47 int frames() const { return frames_; } | 66 int frames() const { return frames_; } |
| 48 | 67 |
| 49 // Helper method for zeroing out all channels of audio data. | 68 // Helper method for zeroing out all channels of audio data. |
| 50 void Zero(); | 69 void Zero(); |
| 51 void ZeroFrames(int frames); | 70 void ZeroFrames(int frames); |
| 52 | 71 |
| 53 private: | 72 private: |
| 54 friend class scoped_ptr<AudioBus>; | 73 friend class scoped_ptr<AudioBus>; |
| 55 ~AudioBus(); | 74 ~AudioBus(); |
| 56 | 75 |
| 57 AudioBus(int channels, int frames); | 76 AudioBus(int channels, int frames); |
| 77 AudioBus(int channels, int frames, float* data); | |
|
Chris Rogers
2012/08/21 00:14:10
nit: |data| looks better as 1st argument
| |
| 58 AudioBus(int frames, const std::vector<float*>& channel_data); | 78 AudioBus(int frames, const std::vector<float*>& channel_data); |
| 59 | 79 |
| 80 // Helper method for building |channel_data_| from a block of memory. |data| | |
| 81 // must be at least BlockSize() bytes in size. | |
| 82 void BuildChannelData(int channels, int aligned_frame, float* data); | |
|
Chris Rogers
2012/08/21 00:14:10
nit: |data| looks better as 1st argument
| |
| 83 | |
| 60 // Contiguous block of channel memory. | 84 // Contiguous block of channel memory. |
| 61 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> data_; | 85 scoped_ptr_malloc<float, base::ScopedPtrAlignedFree> data_; |
| 62 int data_size_; | |
| 63 | 86 |
| 64 std::vector<float*> channel_data_; | 87 std::vector<float*> channel_data_; |
| 65 int frames_; | 88 int frames_; |
| 66 | 89 |
| 67 DISALLOW_COPY_AND_ASSIGN(AudioBus); | 90 DISALLOW_COPY_AND_ASSIGN(AudioBus); |
| 68 }; | 91 }; |
| 69 | 92 |
| 70 } // namespace media | 93 } // namespace media |
| 71 | 94 |
| 72 #endif // MEDIA_BASE_AUDIO_BUS_H_ | 95 #endif // MEDIA_BASE_AUDIO_BUS_H_ |
| OLD | NEW |