andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef COMMON_AUDIO_AUDIO_CONVERTER_H_ |
| 12 | #define COMMON_AUDIO_AUDIO_CONVERTER_H_ |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
kwiberg | c2b785d | 2016-02-24 13:22:32 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 18 | #include "rtc_base/constructor_magic.h" |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 | [diff] [blame] | 22 | // Format conversion (remixing and resampling) for audio. Only simple remixing |
| 23 | // conversions are supported: downmix to mono (i.e. |dst_channels| == 1) or |
| 24 | // upmix from mono (i.e. |src_channels == 1|). |
| 25 | // |
| 26 | // The source and destination chunks have the same duration in time; specifying |
| 27 | // the number of frames is equivalent to specifying the sample rates. |
| 28 | class AudioConverter { |
| 29 | public: |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 | [diff] [blame] | 30 | // Returns a new AudioConverter, which will use the supplied format for its |
| 31 | // lifetime. Caller is responsible for the memory. |
kwiberg | c2b785d | 2016-02-24 13:22:32 | [diff] [blame] | 32 | static std::unique_ptr<AudioConverter> Create(size_t src_channels, |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 33 | size_t src_frames, |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 34 | size_t dst_channels, |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 35 | size_t dst_frames); |
oprypin | 67fdb80 | 2017-03-09 14:25:06 | [diff] [blame] | 36 | virtual ~AudioConverter() {} |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 | [diff] [blame] | 37 | |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 | [diff] [blame] | 38 | // Convert |src|, containing |src_size| samples, to |dst|, having a sample |
| 39 | // capacity of |dst_capacity|. Both point to a series of buffers containing |
| 40 | // the samples for each channel. The sizes must correspond to the format |
| 41 | // passed to Create(). |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 42 | virtual void Convert(const float* const* src, |
| 43 | size_t src_size, |
| 44 | float* const* dst, |
| 45 | size_t dst_capacity) = 0; |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 | [diff] [blame] | 46 | |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 47 | size_t src_channels() const { return src_channels_; } |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 48 | size_t src_frames() const { return src_frames_; } |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 49 | size_t dst_channels() const { return dst_channels_; } |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 50 | size_t dst_frames() const { return dst_frames_; } |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 | [diff] [blame] | 51 | |
| 52 | protected: |
| 53 | AudioConverter(); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 54 | AudioConverter(size_t src_channels, |
| 55 | size_t src_frames, |
| 56 | size_t dst_channels, |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 57 | size_t dst_frames); |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 | [diff] [blame] | 58 | |
henrikg | 91d6ede | 2015-09-17 07:24:34 | [diff] [blame] | 59 | // Helper to RTC_CHECK that inputs are correctly sized. |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 | [diff] [blame] | 60 | void CheckSizes(size_t src_size, size_t dst_capacity) const; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 | [diff] [blame] | 61 | |
| 62 | private: |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 63 | const size_t src_channels_; |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 64 | const size_t src_frames_; |
Peter Kasting | 6955870 | 2016-01-13 00:26:35 | [diff] [blame] | 65 | const size_t dst_channels_; |
Peter Kasting | dce40cf | 2015-08-24 21:52:23 | [diff] [blame] | 66 | const size_t dst_frames_; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 | [diff] [blame] | 67 | |
henrikg | 3c089d7 | 2015-09-16 12:37:44 | [diff] [blame] | 68 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioConverter); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace webrtc |
| 72 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 73 | #endif // COMMON_AUDIO_AUDIO_CONVERTER_H_ |