diff options
Diffstat (limited to 'media/audio')
-rw-r--r-- | media/audio/audio_util.cc | 19 | ||||
-rw-r--r-- | media/audio/audio_util.h | 12 |
2 files changed, 31 insertions, 0 deletions
diff --git a/media/audio/audio_util.cc b/media/audio/audio_util.cc index 6d80c87..ed84bc1 100644 --- a/media/audio/audio_util.cc +++ b/media/audio/audio_util.cc @@ -206,4 +206,23 @@ bool DeinterleaveAudioChannel(void* source, return false; } +void InterleaveFloatToInt16(const std::vector<float*>& source, + int16* destination, + size_t number_of_frames) { + const float kScale = 32768.0f; + int channels = source.size(); + for (int i = 0; i < channels; ++i) { + float* channel_data = source[i]; + for (size_t j = 0; j < number_of_frames; ++j) { + float sample = kScale * channel_data[j]; + if (sample < -32768.0) + sample = -32768.0; + else if (sample > 32767.0) + sample = 32767.0; + + destination[j * channels + i] = static_cast<int16>(sample); + } + } +} + } // namespace media diff --git a/media/audio/audio_util.h b/media/audio/audio_util.h index 669b4fa..a6ba75f 100644 --- a/media/audio/audio_util.h +++ b/media/audio/audio_util.h @@ -5,6 +5,8 @@ #ifndef MEDIA_AUDIO_AUDIO_UTIL_H_ #define MEDIA_AUDIO_AUDIO_UTIL_H_ +#include <vector> + #include "base/basictypes.h" namespace media { @@ -62,6 +64,16 @@ bool DeinterleaveAudioChannel(void* source, int bytes_per_sample, size_t number_of_frames); +// InterleaveFloatToInt16 scales, clips, and interleaves the planar +// floating-point audio contained in |source| to the int16 |destination|. +// The floating-point data is in a canonical range of -1.0 -> +1.0. +// The size of the |source| vector determines the number of channels. +// The |destination| buffer is assumed to be large enough to hold the +// result. Thus it must be at least size: number_of_frames * source.size() +void InterleaveFloatToInt16(const std::vector<float*>& source, + int16* destination, + size_t number_of_frames); + } // namespace media #endif // MEDIA_AUDIO_AUDIO_UTIL_H_ |