audio_converter.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef COMMON_AUDIO_AUDIO_CONVERTER_H_
  11. #define COMMON_AUDIO_AUDIO_CONVERTER_H_
  12. #include <stddef.h>
  13. #include <memory>
  14. #include "rtc_base/constructor_magic.h"
  15. namespace webrtc {
  16. // Format conversion (remixing and resampling) for audio. Only simple remixing
  17. // conversions are supported: downmix to mono (i.e. |dst_channels| == 1) or
  18. // upmix from mono (i.e. |src_channels == 1|).
  19. //
  20. // The source and destination chunks have the same duration in time; specifying
  21. // the number of frames is equivalent to specifying the sample rates.
  22. class AudioConverter {
  23. public:
  24. // Returns a new AudioConverter, which will use the supplied format for its
  25. // lifetime. Caller is responsible for the memory.
  26. static std::unique_ptr<AudioConverter> Create(size_t src_channels,
  27. size_t src_frames,
  28. size_t dst_channels,
  29. size_t dst_frames);
  30. virtual ~AudioConverter() {}
  31. // Convert |src|, containing |src_size| samples, to |dst|, having a sample
  32. // capacity of |dst_capacity|. Both point to a series of buffers containing
  33. // the samples for each channel. The sizes must correspond to the format
  34. // passed to Create().
  35. virtual void Convert(const float* const* src,
  36. size_t src_size,
  37. float* const* dst,
  38. size_t dst_capacity) = 0;
  39. size_t src_channels() const { return src_channels_; }
  40. size_t src_frames() const { return src_frames_; }
  41. size_t dst_channels() const { return dst_channels_; }
  42. size_t dst_frames() const { return dst_frames_; }
  43. protected:
  44. AudioConverter();
  45. AudioConverter(size_t src_channels,
  46. size_t src_frames,
  47. size_t dst_channels,
  48. size_t dst_frames);
  49. // Helper to RTC_CHECK that inputs are correctly sized.
  50. void CheckSizes(size_t src_size, size_t dst_capacity) const;
  51. private:
  52. const size_t src_channels_;
  53. const size_t src_frames_;
  54. const size_t dst_channels_;
  55. const size_t dst_frames_;
  56. RTC_DISALLOW_COPY_AND_ASSIGN(AudioConverter);
  57. };
  58. } // namespace webrtc
  59. #endif // COMMON_AUDIO_AUDIO_CONVERTER_H_