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