2
* Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
* Author: Adrien Beraud <adrien.beraud@wisdomvibes.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Additional permission under GNU GPL version 3 section 7:
21
* If you modify this program, or any covered work, by linking or
22
* combining it with the OpenSSL project's OpenSSL library (or a
23
* modified version of that library), containing parts covered by the
24
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
* grants you additional permission to convey the resulting work.
26
* Corresponding Source for a non-source form of such a combination
27
* shall include the source code for the parts of OpenSSL used as well
28
* as that of the covered work.
31
#ifndef _AUDIO_BUFFER_H
32
#define _AUDIO_BUFFER_H
35
#include <cstddef> // for size_t
37
#include "sfl_types.h"
42
* Default constructor.
44
AudioBuffer(size_t sample_num, unsigned channel_num, int sample_rate);
47
* Construtor from existing interleaved data (copied into the buffer). */
48
AudioBuffer(const SFLAudioSample* in, size_t sample_num, unsigned channel_num, int sample_rate);
51
* Copy constructor that by default only copies the buffer parameters (channel number, sample rate and buffer size).
52
* If copy_content is set to true, the other buffer content is also copied.
54
AudioBuffer(const AudioBuffer& other, bool copy_content = false);
59
AudioBuffer(AudioBuffer&& other) : sampleRate_(other.sampleRate_), samples_( std::move(other.samples_) ) {};
64
AudioBuffer& operator=(const AudioBuffer& other);
69
AudioBuffer& operator=(AudioBuffer&& other);
72
for(auto& c : samples_)
73
std::fill(c.begin(), c.end(), 0);
76
inline size_t size() {
77
return frames() * channels() * sizeof(SFLAudioSample);
81
* Returns the sample rate (in samples/sec) associated to this buffer.
83
int getSampleRate() const;
86
* Set the sample rate (in samples/sec) associated to this buffer.
88
void setSampleRate(int sr);
91
* Returns the number of channels in this buffer.
93
inline unsigned channels() const {
94
return samples_.size();
98
* Set the number of channels of this buffer.
100
* @param n: the new number of channels. If n < channels(), channels are removed from the buffer, otherwise the behavior depends on copy_first.
102
* @param copy_first: if set to true and n > channels(), new channels are initialised as a copy of the first channel (channel 0). If set to false, new channels are initialised to 0.
104
void setChannelNum(unsigned n, bool copy_first = false);
107
* Returns the number of (multichannel) frames in this buffer.
109
inline size_t frames() const {
110
if (not samples_.empty())
111
return samples_[0].size();
117
* Return the total number of single samples in the buffer
119
inline size_t capacity() const {
120
return frames() * channels();
124
* Resize the buffer to make it able to hold sample_num multichannel samples.
126
void resize(size_t sample_num);
129
* Set all samples in this buffer to 0. Buffer size is not changed.
134
* Resize the buffer to 0. All samples are lost but the number of channels and sample rate are kept.
139
* Return the data (audio samples) for a given channel number.
140
* Channel data can be modified but size of individual channel vectors should not be changed by the user.
142
std::vector<SFLAudioSample> *getChannel(unsigned chan);
145
* Return a pointer to the raw data in this buffer.
147
inline std::vector<std::vector<SFLAudioSample> > &getData() {
152
* Write interleaved multichannel data to the out buffer (fixed-point 16-bits).
153
* The out buffer must be at least of size capacity()*sizeof(SFLAudioSample) bytes.
155
* @returns Number of samples writen.
157
size_t interleave(SFLAudioSample* out) const;
160
* Write interleaved multichannel data to the out buffer, while samples are converted to float.
161
* The out buffer must be at least of size capacity()*sizeof(float) bytes.
163
* @returns Number of samples writen.
165
size_t interleaveFloat(float* out) const;
168
* Import interleaved multichannel data. Internal buffer is resized as needed. Function will read sample_num*channel_num elements of the in buffer.
170
void deinterleave(const SFLAudioSample* in, size_t sample_num, unsigned channel_num = 1);
173
* In-place gain transformation.
175
* @param gain: 0.0 -> 1.0 scale
177
void applyGain(double gain);
180
* Mix samples from the other buffer within this buffer (in-place simple addition).
181
* If the other buffer has more channels than this one, only the first this.channels() channels are imported.
182
* If the other buffer has less channels than this one, behavior depends on upmix.
183
* Sample rate is not considered by this function.
185
* TODO: some kind of check for overflow/saturation.
187
* @param other: the other buffer to mix in this one.
188
* @param upmix: if true, upmixing occurs when other.channels() < this.channels().
189
* If false, only the first other.channels() channels are edited in this buffer.
191
* @returns Number of samples modified.
193
size_t mix(const AudioBuffer& other, bool upmix = true);
196
* Copy sample_num samples from in (from sample sample pos_in) to this buffer (at sample sample pos_out).
197
* If sample_num is -1 (the default), the entire in buffer is copied.
199
* Buffer sample number is increased if required to hold the new requested samples.
201
size_t copy(AudioBuffer& in, int sample_num = -1, size_t pos_in = 0, size_t pos_out = 0, bool upmix = true);
204
* Copy sample_num samples from in to this buffer (at sample pos_out).
205
* Input data is treated as mono and samples are duplicated in the case of a multichannel buffer.
207
* Buffer sample number is increased if required to hold the new requested samples.
209
size_t copy(SFLAudioSample* in, size_t sample_num, size_t pos_out = 0);
214
// main buffers holding data for each channels
215
std::vector<std::vector<SFLAudioSample> > samples_;
218
#endif // _AUDIO_BUFFER_H