xref: /aosp_15_r20/external/webrtc/common_audio/audio_converter.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef COMMON_AUDIO_AUDIO_CONVERTER_H_
12 #define COMMON_AUDIO_AUDIO_CONVERTER_H_
13 
14 #include <stddef.h>
15 
16 #include <memory>
17 
18 namespace webrtc {
19 
20 // Format conversion (remixing and resampling) for audio. Only simple remixing
21 // conversions are supported: downmix to mono (i.e. `dst_channels` == 1) or
22 // upmix from mono (i.e. |src_channels == 1|).
23 //
24 // The source and destination chunks have the same duration in time; specifying
25 // the number of frames is equivalent to specifying the sample rates.
26 class AudioConverter {
27  public:
28   // Returns a new AudioConverter, which will use the supplied format for its
29   // lifetime. Caller is responsible for the memory.
30   static std::unique_ptr<AudioConverter> Create(size_t src_channels,
31                                                 size_t src_frames,
32                                                 size_t dst_channels,
33                                                 size_t dst_frames);
~AudioConverter()34   virtual ~AudioConverter() {}
35 
36   AudioConverter(const AudioConverter&) = delete;
37   AudioConverter& operator=(const AudioConverter&) = delete;
38 
39   // Convert `src`, containing `src_size` samples, to `dst`, having a sample
40   // capacity of `dst_capacity`. Both point to a series of buffers containing
41   // the samples for each channel. The sizes must correspond to the format
42   // passed to Create().
43   virtual void Convert(const float* const* src,
44                        size_t src_size,
45                        float* const* dst,
46                        size_t dst_capacity) = 0;
47 
src_channels()48   size_t src_channels() const { return src_channels_; }
src_frames()49   size_t src_frames() const { return src_frames_; }
dst_channels()50   size_t dst_channels() const { return dst_channels_; }
dst_frames()51   size_t dst_frames() const { return dst_frames_; }
52 
53  protected:
54   AudioConverter();
55   AudioConverter(size_t src_channels,
56                  size_t src_frames,
57                  size_t dst_channels,
58                  size_t dst_frames);
59 
60   // Helper to RTC_CHECK that inputs are correctly sized.
61   void CheckSizes(size_t src_size, size_t dst_capacity) const;
62 
63  private:
64   const size_t src_channels_;
65   const size_t src_frames_;
66   const size_t dst_channels_;
67   const size_t dst_frames_;
68 };
69 
70 }  // namespace webrtc
71 
72 #endif  // COMMON_AUDIO_AUDIO_CONVERTER_H_
73