blob: 4afbb6d0fd7b5b59ad6e2763480b9baec08010da [file] [log] [blame]
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +00001/*
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 06:47:31 +020011#ifndef COMMON_AUDIO_AUDIO_CONVERTER_H_
12#define COMMON_AUDIO_AUDIO_CONVERTER_H_
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
kwibergc2b785d2016-02-24 05:22:32 -080016#include <memory>
17
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000018namespace webrtc {
19
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000020// Format conversion (remixing and resampling) for audio. Only simple remixing
Artem Titov96315752021-07-26 12:15:29 +020021// conversions are supported: downmix to mono (i.e. `dst_channels` == 1) or
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000022// 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.
26class AudioConverter {
27 public:
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000028 // Returns a new AudioConverter, which will use the supplied format for its
29 // lifetime. Caller is responsible for the memory.
kwibergc2b785d2016-02-24 05:22:32 -080030 static std::unique_ptr<AudioConverter> Create(size_t src_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070031 size_t src_frames,
Peter Kasting69558702016-01-12 16:26:35 -080032 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070033 size_t dst_frames);
oprypin67fdb802017-03-09 06:25:06 -080034 virtual ~AudioConverter() {}
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000035
Artem Titov6cae2d52022-01-26 15:01:10 +000036 AudioConverter(const AudioConverter&) = delete;
37 AudioConverter& operator=(const AudioConverter&) = delete;
38
Artem Titov96315752021-07-26 12:15:29 +020039 // Convert `src`, containing `src_size` samples, to `dst`, having a sample
40 // capacity of `dst_capacity`. Both point to a series of buffers containing
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000041 // the samples for each channel. The sizes must correspond to the format
42 // passed to Create().
Yves Gerey665174f2018-06-19 15:03:05 +020043 virtual void Convert(const float* const* src,
44 size_t src_size,
45 float* const* dst,
46 size_t dst_capacity) = 0;
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000047
Peter Kasting69558702016-01-12 16:26:35 -080048 size_t src_channels() const { return src_channels_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 size_t src_frames() const { return src_frames_; }
Peter Kasting69558702016-01-12 16:26:35 -080050 size_t dst_channels() const { return dst_channels_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -070051 size_t dst_frames() const { return dst_frames_; }
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000052
53 protected:
54 AudioConverter();
Yves Gerey665174f2018-06-19 15:03:05 +020055 AudioConverter(size_t src_channels,
56 size_t src_frames,
57 size_t dst_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070058 size_t dst_frames);
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000059
henrikg91d6ede2015-09-17 00:24:34 -070060 // Helper to RTC_CHECK that inputs are correctly sized.
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000061 void CheckSizes(size_t src_size, size_t dst_capacity) const;
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000062
63 private:
Peter Kasting69558702016-01-12 16:26:35 -080064 const size_t src_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070065 const size_t src_frames_;
Peter Kasting69558702016-01-12 16:26:35 -080066 const size_t dst_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070067 const size_t dst_frames_;
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000068};
69
70} // namespace webrtc
71
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020072#endif // COMMON_AUDIO_AUDIO_CONVERTER_H_