~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/src/audio/audiobuffer.h

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
 
3
 *  Author: Adrien Beraud <adrien.beraud@wisdomvibes.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 *  Additional permission under GNU GPL version 3 section 7:
 
20
 *
 
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.
 
29
 */
 
30
 
 
31
#ifndef _AUDIO_BUFFER_H
 
32
#define _AUDIO_BUFFER_H
 
33
 
 
34
#include <vector>
 
35
#include <cstddef> // for size_t
 
36
 
 
37
#include "sfl_types.h"
 
38
 
 
39
class AudioBuffer {
 
40
    public:
 
41
        /**
 
42
         * Default constructor.
 
43
         */
 
44
        AudioBuffer(size_t sample_num, unsigned channel_num, int sample_rate);
 
45
 
 
46
        /**
 
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);
 
49
 
 
50
        /**
 
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.
 
53
         */
 
54
        AudioBuffer(const AudioBuffer& other, bool copy_content = false);
 
55
        
 
56
        /**
 
57
         * Move contructor
 
58
         */
 
59
        AudioBuffer(AudioBuffer&& other) : sampleRate_(other.sampleRate_), samples_( std::move(other.samples_) ) {};
 
60
        
 
61
        /**
 
62
         * Copy operator
 
63
         */
 
64
        AudioBuffer& operator=(const AudioBuffer& other);
 
65
        
 
66
        /**
 
67
         * Move operator
 
68
         */
 
69
        AudioBuffer& operator=(AudioBuffer&& other);
 
70
 
 
71
        void reset() {
 
72
            for(auto& c : samples_)
 
73
                std::fill(c.begin(), c.end(), 0);
 
74
        }
 
75
 
 
76
        inline size_t size() {
 
77
            return frames() * channels() * sizeof(SFLAudioSample);
 
78
        }
 
79
 
 
80
        /**
 
81
         * Returns the sample rate (in samples/sec) associated to this buffer.
 
82
         */
 
83
        int getSampleRate() const;
 
84
 
 
85
        /**
 
86
         * Set the sample rate (in samples/sec) associated to this buffer.
 
87
         */
 
88
        void setSampleRate(int sr);
 
89
 
 
90
        /**
 
91
         * Returns the number of channels in this buffer.
 
92
         */
 
93
        inline unsigned channels() const {
 
94
            return samples_.size();
 
95
        }
 
96
 
 
97
        /**
 
98
         * Set the number of channels of this buffer.
 
99
         *
 
100
         * @param n: the new number of channels. If n < channels(), channels are removed from the buffer, otherwise the behavior depends on copy_first.
 
101
         *
 
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.
 
103
         */
 
104
        void setChannelNum(unsigned n, bool copy_first = false);
 
105
 
 
106
        /**
 
107
         * Returns the number of (multichannel) frames in this buffer.
 
108
         */
 
109
        inline size_t frames() const {
 
110
            if (not samples_.empty())
 
111
                return samples_[0].size();
 
112
            else
 
113
                return 0;
 
114
        }
 
115
 
 
116
        /**
 
117
         * Return the total number of single samples in the buffer
 
118
         */
 
119
        inline size_t capacity() const {
 
120
            return frames() * channels();
 
121
        }
 
122
 
 
123
        /**
 
124
         * Resize the buffer to make it able to hold sample_num multichannel samples.
 
125
         */
 
126
        void resize(size_t sample_num);
 
127
 
 
128
        /**
 
129
         * Set all samples in this buffer to 0. Buffer size is not changed.
 
130
         */
 
131
        void clear();
 
132
 
 
133
        /**
 
134
         * Resize the buffer to 0. All samples are lost but the number of channels and sample rate are kept.
 
135
         */
 
136
        void empty();
 
137
 
 
138
        /**
 
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.
 
141
         */
 
142
        std::vector<SFLAudioSample> *getChannel(unsigned chan);
 
143
 
 
144
        /**
 
145
         * Return a pointer to the raw data in this buffer.
 
146
         */
 
147
        inline std::vector<std::vector<SFLAudioSample> > &getData() {
 
148
            return samples_;
 
149
        }
 
150
 
 
151
        /**
 
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.
 
154
         *
 
155
         * @returns Number of samples writen.
 
156
         */
 
157
        size_t interleave(SFLAudioSample* out) const;
 
158
 
 
159
        /**
 
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.
 
162
         *
 
163
         * @returns Number of samples writen.
 
164
         */
 
165
        size_t interleaveFloat(float* out) const;
 
166
 
 
167
        /**
 
168
         * Import interleaved multichannel data. Internal buffer is resized as needed. Function will read sample_num*channel_num elements of the in buffer.
 
169
         */
 
170
        void deinterleave(const SFLAudioSample* in, size_t sample_num, unsigned channel_num = 1);
 
171
 
 
172
        /**
 
173
         * In-place gain transformation.
 
174
         *
 
175
         * @param gain: 0.0 -> 1.0 scale
 
176
         */
 
177
        void applyGain(double gain);
 
178
 
 
179
        /**
 
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.
 
184
         *
 
185
         * TODO: some kind of check for overflow/saturation.
 
186
         *
 
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.
 
190
         *
 
191
         * @returns Number of samples modified.
 
192
         */
 
193
        size_t mix(const AudioBuffer& other, bool upmix = true);
 
194
 
 
195
        /**
 
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.
 
198
         *
 
199
         * Buffer sample number is increased if required to hold the new requested samples.
 
200
         */
 
201
        size_t copy(AudioBuffer& in, int sample_num = -1, size_t pos_in = 0, size_t pos_out = 0, bool upmix = true);
 
202
 
 
203
        /**
 
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.
 
206
         *
 
207
         * Buffer sample number is increased if required to hold the new requested samples.
 
208
         */
 
209
        size_t copy(SFLAudioSample* in, size_t sample_num, size_t pos_out = 0);
 
210
 
 
211
    private:
 
212
        int sampleRate_;
 
213
 
 
214
        // main buffers holding data for each channels
 
215
        std::vector<std::vector<SFLAudioSample> > samples_;
 
216
};
 
217
 
 
218
#endif // _AUDIO_BUFFER_H