/* * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #ifndef COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ #define COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_ #include <stdint.h> #include <algorithm> #include <cmath> #include <cstring> #include <limits> #include "api/audio/audio_view.h" #include "rtc_base/checks.h" namespace webrtc { limits_int16; // TODO(tommi, peah): Move these constants to their own header, e.g. // `audio_constants.h`. Also consider if they should be in api/. // Absolute highest acceptable sample rate supported for audio processing, // capture and codecs. Note that for some components some cases a lower limit // applies which typically is 48000 but in some cases is lower. constexpr int kMaxSampleRateHz = …; // Number of samples per channel for 10ms of audio at the highest sample rate. constexpr size_t kMaxSamplesPerChannel10ms = …; // The conversion functions use the following naming convention: // S16: int16_t [-32768, 32767] // Float: float [-1.0, 1.0] // FloatS16: float [-32768.0, 32768.0] // Dbfs: float [-20.0*log(10, 32768), 0] = [-90.3, 0] // The ratio conversion functions use this naming convention: // Ratio: float (0, +inf) // Db: float (-inf, +inf) static inline float S16ToFloat(int16_t v) { … } static inline int16_t FloatS16ToS16(float v) { … } static inline int16_t FloatToS16(float v) { … } static inline float FloatToFloatS16(float v) { … } static inline float FloatS16ToFloat(float v) { … } void FloatToS16(const float* src, size_t size, int16_t* dest); void S16ToFloat(const int16_t* src, size_t size, float* dest); void S16ToFloatS16(const int16_t* src, size_t size, float* dest); void FloatS16ToS16(const float* src, size_t size, int16_t* dest); void FloatToFloatS16(const float* src, size_t size, float* dest); void FloatS16ToFloat(const float* src, size_t size, float* dest); inline float DbToRatio(float v) { … } inline float DbfsToFloatS16(float v) { … } inline float FloatS16ToDbfs(float v) { … } // Copy audio from `src` channels to `dest` channels unless `src` and `dest` // point to the same address. `src` and `dest` must have the same number of // channels, and there must be sufficient space allocated in `dest`. // TODO: b/335805780 - Accept ArrayView. template <typename T> void CopyAudioIfNeeded(const T* const* src, int num_frames, int num_channels, T* const* dest) { … } // Deinterleave audio from `interleaved` to the channel buffers pointed to // by `deinterleaved`. There must be sufficient space allocated in the // `deinterleaved` buffers (`num_channel` buffers with `samples_per_channel` // per buffer). template <typename T> void Deinterleave(const InterleavedView<const T>& interleaved, const DeinterleavedView<T>& deinterleaved) { … } // Interleave audio from the channel buffers pointed to by `deinterleaved` to // `interleaved`. There must be sufficient space allocated in `interleaved` // (`samples_per_channel` * `num_channels`). template <typename T> void Interleave(const DeinterleavedView<const T>& deinterleaved, const InterleavedView<T>& interleaved) { … } // Downmixes an interleaved multichannel signal to a single channel by averaging // all channels. // TODO: b/335805780 - Accept InterleavedView and DeinterleavedView. template <typename T, typename Intermediate> void DownmixInterleavedToMonoImpl(const T* interleaved, size_t num_frames, int num_channels, T* deinterleaved) { … } // TODO: b/335805780 - Accept InterleavedView and DeinterleavedView. template <typename T> void DownmixInterleavedToMono(const T* interleaved, size_t num_frames, int num_channels, T* deinterleaved); // TODO: b/335805780 - Accept InterleavedView and DeinterleavedView. template <> void DownmixInterleavedToMono<int16_t>(const int16_t* interleaved, size_t num_frames, int num_channels, int16_t* deinterleaved); } // namespace webrtc #endif // COMMON_AUDIO_INCLUDE_AUDIO_UTIL_H_