blob: c6fe08e2d070a5f48feb37062b2b95e98679fd74 [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
11#ifndef WEBRTC_COMMON_AUDIO_AUDIO_CONVERTER_H_
12#define WEBRTC_COMMON_AUDIO_AUDIO_CONVERTER_H_
13
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000014#include "webrtc/base/constructormagic.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000015#include "webrtc/base/scoped_ptr.h"
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000016
17namespace webrtc {
18
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000019// Format conversion (remixing and resampling) for audio. Only simple remixing
20// conversions are supported: downmix to mono (i.e. |dst_channels| == 1) or
21// upmix from mono (i.e. |src_channels == 1|).
22//
23// The source and destination chunks have the same duration in time; specifying
24// the number of frames is equivalent to specifying the sample rates.
25class AudioConverter {
26 public:
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000027 // Returns a new AudioConverter, which will use the supplied format for its
28 // lifetime. Caller is responsible for the memory.
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000029 static rtc::scoped_ptr<AudioConverter> Create(int src_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070030 size_t src_frames,
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000031 int dst_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070032 size_t dst_frames);
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000033 virtual ~AudioConverter() {};
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000034
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000035 // Convert |src|, containing |src_size| samples, to |dst|, having a sample
36 // capacity of |dst_capacity|. Both point to a series of buffers containing
37 // the samples for each channel. The sizes must correspond to the format
38 // passed to Create().
39 virtual void Convert(const float* const* src, size_t src_size,
40 float* const* dst, size_t dst_capacity) = 0;
41
42 int src_channels() const { return src_channels_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 size_t src_frames() const { return src_frames_; }
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000044 int dst_channels() const { return dst_channels_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -070045 size_t dst_frames() const { return dst_frames_; }
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000046
47 protected:
48 AudioConverter();
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 AudioConverter(int src_channels, size_t src_frames, int dst_channels,
50 size_t dst_frames);
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000051
52 // Helper to CHECK that inputs are correctly sized.
53 void CheckSizes(size_t src_size, size_t dst_capacity) const;
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000054
55 private:
andrew@webrtc.org58049362014-11-03 21:32:14 +000056 const int src_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070057 const size_t src_frames_;
andrew@webrtc.org58049362014-11-03 21:32:14 +000058 const int dst_channels_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070059 const size_t dst_frames_;
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000060
henrikg3c089d72015-09-16 05:37:44 -070061 RTC_DISALLOW_COPY_AND_ASSIGN(AudioConverter);
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000062};
63
64} // namespace webrtc
65
66#endif // WEBRTC_COMMON_AUDIO_AUDIO_CONVERTER_H_