andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [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 06:47:31 +0200 | [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 +0000 | [diff] [blame] | 13 | |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/constructormagic.h" |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 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: |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 28 | // Returns a new AudioConverter, which will use the supplied format for its |
| 29 | // lifetime. Caller is responsible for the memory. |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 30 | static std::unique_ptr<AudioConverter> Create(size_t src_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 31 | size_t src_frames, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 32 | size_t dst_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 33 | size_t dst_frames); |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 34 | virtual ~AudioConverter() {} |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 35 | |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 36 | // Convert |src|, containing |src_size| samples, to |dst|, having a sample |
| 37 | // capacity of |dst_capacity|. Both point to a series of buffers containing |
| 38 | // the samples for each channel. The sizes must correspond to the format |
| 39 | // passed to Create(). |
| 40 | virtual void Convert(const float* const* src, size_t src_size, |
| 41 | float* const* dst, size_t dst_capacity) = 0; |
| 42 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 43 | size_t src_channels() const { return src_channels_; } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 44 | size_t src_frames() const { return src_frames_; } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 45 | size_t dst_channels() const { return dst_channels_; } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 46 | size_t dst_frames() const { return dst_frames_; } |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 47 | |
| 48 | protected: |
| 49 | AudioConverter(); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 50 | AudioConverter(size_t src_channels, size_t src_frames, size_t dst_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | size_t dst_frames); |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 52 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 53 | // Helper to RTC_CHECK that inputs are correctly sized. |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 54 | void CheckSizes(size_t src_size, size_t dst_capacity) const; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 57 | const size_t src_channels_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 58 | const size_t src_frames_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 59 | const size_t dst_channels_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 60 | const size_t dst_frames_; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 61 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 62 | RTC_DISALLOW_COPY_AND_ASSIGN(AudioConverter); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | } // namespace webrtc |
| 66 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 67 | #endif // COMMON_AUDIO_AUDIO_CONVERTER_H_ |