blob: 24a5e72cee9040890a97e26bb0e023bc1f43869c [file] [log] [blame]
andrew@webrtc.orgaada86b2014-10-27 18:18:171/*
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 Bonadei92ea95e2017-09-15 04:47:3111#ifndef COMMON_AUDIO_AUDIO_CONVERTER_H_
12#define COMMON_AUDIO_AUDIO_CONVERTER_H_
andrew@webrtc.orgaada86b2014-10-27 18:18:1713
Yves Gerey988cc082018-10-23 10:03:0114#include <stddef.h>
kwibergc2b785d2016-02-24 13:22:3215#include <memory>
16
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "rtc_base/constructormagic.h"
andrew@webrtc.orgaada86b2014-10-27 18:18:1718
19namespace webrtc {
20
andrew@webrtc.orgaada86b2014-10-27 18:18:1721// Format conversion (remixing and resampling) for audio. Only simple remixing
22// conversions are supported: downmix to mono (i.e. |dst_channels| == 1) or
23// upmix from mono (i.e. |src_channels == 1|).
24//
25// The source and destination chunks have the same duration in time; specifying
26// the number of frames is equivalent to specifying the sample rates.
27class AudioConverter {
28 public:
andrew@webrtc.org2c29c2e2015-02-11 01:09:5029 // Returns a new AudioConverter, which will use the supplied format for its
30 // lifetime. Caller is responsible for the memory.
kwibergc2b785d2016-02-24 13:22:3231 static std::unique_ptr<AudioConverter> Create(size_t src_channels,
Peter Kastingdce40cf2015-08-24 21:52:2332 size_t src_frames,
Peter Kasting69558702016-01-13 00:26:3533 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 21:52:2334 size_t dst_frames);
oprypin67fdb802017-03-09 14:25:0635 virtual ~AudioConverter() {}
andrew@webrtc.orgaada86b2014-10-27 18:18:1736
andrew@webrtc.org2c29c2e2015-02-11 01:09:5037 // Convert |src|, containing |src_size| samples, to |dst|, having a sample
38 // capacity of |dst_capacity|. Both point to a series of buffers containing
39 // the samples for each channel. The sizes must correspond to the format
40 // passed to Create().
Yves Gerey665174f2018-06-19 13:03:0541 virtual void Convert(const float* const* src,
42 size_t src_size,
43 float* const* dst,
44 size_t dst_capacity) = 0;
andrew@webrtc.org2c29c2e2015-02-11 01:09:5045
Peter Kasting69558702016-01-13 00:26:3546 size_t src_channels() const { return src_channels_; }
Peter Kastingdce40cf2015-08-24 21:52:2347 size_t src_frames() const { return src_frames_; }
Peter Kasting69558702016-01-13 00:26:3548 size_t dst_channels() const { return dst_channels_; }
Peter Kastingdce40cf2015-08-24 21:52:2349 size_t dst_frames() const { return dst_frames_; }
andrew@webrtc.org2c29c2e2015-02-11 01:09:5050
51 protected:
52 AudioConverter();
Yves Gerey665174f2018-06-19 13:03:0553 AudioConverter(size_t src_channels,
54 size_t src_frames,
55 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 21:52:2356 size_t dst_frames);
andrew@webrtc.org2c29c2e2015-02-11 01:09:5057
henrikg91d6ede2015-09-17 07:24:3458 // Helper to RTC_CHECK that inputs are correctly sized.
andrew@webrtc.org2c29c2e2015-02-11 01:09:5059 void CheckSizes(size_t src_size, size_t dst_capacity) const;
andrew@webrtc.orgaada86b2014-10-27 18:18:1760
61 private:
Peter Kasting69558702016-01-13 00:26:3562 const size_t src_channels_;
Peter Kastingdce40cf2015-08-24 21:52:2363 const size_t src_frames_;
Peter Kasting69558702016-01-13 00:26:3564 const size_t dst_channels_;
Peter Kastingdce40cf2015-08-24 21:52:2365 const size_t dst_frames_;
andrew@webrtc.orgaada86b2014-10-27 18:18:1766
henrikg3c089d72015-09-16 12:37:4467 RTC_DISALLOW_COPY_AND_ASSIGN(AudioConverter);
andrew@webrtc.orgaada86b2014-10-27 18:18:1768};
69
70} // namespace webrtc
71
Mirko Bonadei92ea95e2017-09-15 04:47:3172#endif // COMMON_AUDIO_AUDIO_CONVERTER_H_