reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofSoundBuffer.h
Go to the documentation of this file.
1/*
2 * ofSoundBuffer.h
3 *
4 * Created on: 25/07/2012
5 * Author: arturo
6 */
7
8#ifndef OFSOUNDBUFFER_H_
9#define OFSOUNDBUFFER_H_
10
11#include "ofConstants.h"
12
13
85public:
87 ofSoundBuffer(short * shortBuffer, std::size_t numFrames, std::size_t numChannels, unsigned int sampleRate);
88
93 static InterpolationAlgorithm defaultAlgorithm; //defaults to Linear for mobile, Hermite for desktop
94
95 void allocate(size_t numSamples, size_t numChannels);
96
98 unsigned int getSampleRate() const { return samplerate; }
99 void setSampleRate(unsigned int rate);
101 void resample(float speed, InterpolationAlgorithm algorithm=defaultAlgorithm);
103 std::size_t getNumChannels() const { return channels; }
105 void setNumChannels(int channels);
107 std::size_t getNumFrames() const { return size()/getNumChannels(); }
108
110 uint64_t getTickCount() const { return tickCount; }
111 void setTickCount(uint64_t tick){ tickCount = tick; }
112
114 uint64_t getDurationMS() const;
115 uint64_t getDurationMicros() const;
116 uint64_t getDurationNanos() const;
117
119 int getDeviceID() const { return soundStreamDeviceID; }
120 void setDeviceID(int id){ soundStreamDeviceID = id; }
121
127 float & operator[](std::size_t samplePos);
128 const float & operator[](std::size_t samplePos) const;
129
131 float & getSample(std::size_t frameIndex, std::size_t channel);
132 const float & getSample(std::size_t frameIndex, std::size_t channel) const;
133
135 ofSoundBuffer operator*(float value);
137 ofSoundBuffer & operator*=(float value);
138
140 void stereoPan(float left, float right);
141
143 void copyFrom(const short * shortBuffer, std::size_t numFrames, std::size_t numChannels, unsigned int sampleRate);
144
145 void copyFrom(const float * floatBuffer, std::size_t numFrames, std::size_t numChannels, unsigned int sampleRate);
146
147 void copyFrom(const std::vector<short> & shortBuffer, std::size_t numChannels, unsigned int sampleRate);
148
149 void copyFrom(const std::vector<float> & floatBuffer, std::size_t numChannels, unsigned int sampleRate);
150
151 void toShortPCM(std::vector<short> & dst) const;
152 void toShortPCM(short * dst) const;
153
158 void copyTo(ofSoundBuffer & outBuffer, std::size_t outNumFrames, std::size_t outNumChannels, std::size_t fromFrame, bool loop = false) const;
160 void addTo(ofSoundBuffer & outBuffer, std::size_t outNumFrames, std::size_t outNumChannels, std::size_t fromFrame, bool loop = false) const;
161
163 void copyTo(ofSoundBuffer & outBuffer, std::size_t frameFrame = 0, bool loop = false) const;
165 void addTo(ofSoundBuffer & outBuffer, std::size_t fromFrame = 0, bool loop = false) const;
166
167 void append(ofSoundBuffer & other);
168
173 void copyTo(float * outBuffer, std::size_t outNumFrames, std::size_t outNumChannels, std::size_t fromFrame, bool loop = false) const;
175 void addTo(float * outBuffer, std::size_t outNumFrames, std::size_t outNumChannels, std::size_t fromFrame, bool loop = false) const;
176
179 void resampleTo(ofSoundBuffer & outBuffer, std::size_t fromFrame, std::size_t numFrames, float speed, bool loop = false, InterpolationAlgorithm algorithm = defaultAlgorithm) const;
180
182 void getChannel(ofSoundBuffer & outBuffer, std::size_t sourceChannel) const;
184 void setChannel(const ofSoundBuffer & inBuffer, std::size_t channel);
185
186 float getRMSAmplitude() const;
187 float getRMSAmplitudeChannel(std::size_t channel) const;
188
189 void linearResampleTo(ofSoundBuffer & buffer, std::size_t fromFrame, std::size_t numFrames, float speed, bool loop) const;
190 void hermiteResampleTo(ofSoundBuffer & buffer, std::size_t fromFrame, std::size_t numFrames, float speed, bool loop) const;
191
193 void fillWithNoise(float amplitude = 1.0f);
194
196 float fillWithTone(float pitchHz = 440.0f, float phase = 0.0f);
197
199 void normalize(float level = 1);
200
202 bool trimSilence(float threshold = 0.0001f, bool trimStart = true, bool trimEnd = true);
203
205 std::size_t size() const { return buffer.size(); }
207 void resize(std::size_t numSamples, float val = float());
209 void clear();
211 void swap(ofSoundBuffer & otherBuffer);
213 void set(float value);
214
216 std::vector<float> & getBuffer();
217 const std::vector<float> & getBuffer() const;
218
219protected:
220
221 // checks that size() and number of channels are consistent, logs a warning if not. returns consistency check result.
222 bool checkSizeAndChannelsConsistency(const std::string& function="" );
223
224 std::vector<float> buffer;
225 std::size_t channels;
226 unsigned int samplerate;
227
228 uint64_t tickCount;
230};
231
232namespace std{
233 void swap(ofSoundBuffer & src, ofSoundBuffer & dst);
234}
235
236#endif /* OFSOUNDBUFFER_H_ */
Buffer for audio samples and associated metadata.
Definition ofSoundBuffer.h:84
void resampleTo(ofSoundBuffer &outBuffer, std::size_t fromFrame, std::size_t numFrames, float speed, bool loop=false, InterpolationAlgorithm algorithm=defaultAlgorithm) const
Definition ofSoundBuffer.cpp:481
void hermiteResampleTo(ofSoundBuffer &buffer, std::size_t fromFrame, std::size_t numFrames, float speed, bool loop) const
Definition ofSoundBuffer.cpp:397
float getRMSAmplitude() const
Definition ofSoundBuffer.cpp:536
void getChannel(ofSoundBuffer &outBuffer, std::size_t sourceChannel) const
copy the requested channel of our data to outBuffer. resize outBuffer to fit.
Definition ofSoundBuffer.cpp:498
void fillWithNoise(float amplitude=1.0f)
fills the buffer with random noise between -amplitude and amplitude. useful for debugging.
Definition ofSoundBuffer.cpp:602
bool trimSilence(float threshold=0.0001f, bool trimStart=true, bool trimEnd=true)
removes initial / ending silence from the buffer
Definition ofSoundBuffer.cpp:568
uint64_t getDurationMicros() const
Definition ofSoundBuffer.cpp:90
int soundStreamDeviceID
Definition ofSoundBuffer.h:229
uint64_t tickCount
Definition ofSoundBuffer.h:228
static InterpolationAlgorithm defaultAlgorithm
Definition ofSoundBuffer.h:93
void setSampleRate(unsigned int rate)
Definition ofSoundBuffer.cpp:103
std::size_t getNumFrames() const
the number of frames, ie the number of sets of (getNumChannels()) samples
Definition ofSoundBuffer.h:107
int getDeviceID() const
return the ID of the device which generated this buffer
Definition ofSoundBuffer.h:119
void setNumChannels(int channels)
set the number of channels. does not change the underlying data, ie causes getNumFrames() to return a...
Definition ofSoundBuffer.cpp:98
std::vector< float > buffer
Definition ofSoundBuffer.h:224
void append(ofSoundBuffer &other)
Definition ofSoundBuffer.cpp:308
void toShortPCM(std::vector< short > &dst) const
void setDeviceID(int id)
Definition ofSoundBuffer.h:120
void setTickCount(uint64_t tick)
Definition ofSoundBuffer.h:111
std::size_t channels
Definition ofSoundBuffer.h:225
unsigned int getSampleRate() const
sample rate of the audio in this buffer
Definition ofSoundBuffer.h:98
void linearResampleTo(ofSoundBuffer &buffer, std::size_t fromFrame, std::size_t numFrames, float speed, bool loop) const
Definition ofSoundBuffer.cpp:333
std::size_t size() const
return the total number of samples in this buffer (==getNumFrames()*getNumChannels())
Definition ofSoundBuffer.h:205
void allocate(size_t numSamples, size_t numChannels)
Definition ofSoundBuffer.cpp:107
uint64_t getTickCount() const
return the tickCount that was assigned by ofSoundStream (if this buffer originated from an ofSoundStr...
Definition ofSoundBuffer.h:110
InterpolationAlgorithm
Definition ofSoundBuffer.h:89
@ Linear
Definition ofSoundBuffer.h:90
@ Hermite
Definition ofSoundBuffer.h:91
void stereoPan(float left, float right)
assuming a 2-channel buffer, apply a stereo pan by multiplying channel 0 by left and channel 1 by rig...
Definition ofSoundBuffer.cpp:176
void copyFrom(const short *shortBuffer, std::size_t numFrames, std::size_t numChannels, unsigned int sampleRate)
copy length samples from shortBuffer and interpret as interleaved with the given number of channels a...
Definition ofSoundBuffer.cpp:40
void setChannel(const ofSoundBuffer &inBuffer, std::size_t channel)
copy data from inBuffer to the given channel. resize ourselves to match inBuffer's getNumFrames().
Definition ofSoundBuffer.cpp:522
float & operator[](std::size_t samplePos)
Definition ofSoundBuffer.cpp:139
std::vector< float > & getBuffer()
return the underlying buffer. careful!
Definition ofSoundBuffer.cpp:78
bool checkSizeAndChannelsConsistency(const std::string &function="")
Definition ofSoundBuffer.cpp:126
void copyTo(ofSoundBuffer &outBuffer, std::size_t outNumFrames, std::size_t outNumChannels, std::size_t fromFrame, bool loop=false) const
Definition ofSoundBuffer.cpp:188
float & getSample(std::size_t frameIndex, std::size_t channel)
access the sample at frameIndex on a soecific channel
Definition ofSoundBuffer.cpp:147
uint64_t getDurationMS() const
return the duration of audio in this buffer in milliseconds (==(getNumFrames()/getSampleRate())*1000)
Definition ofSoundBuffer.cpp:86
void resize(std::size_t numSamples, float val=float())
resize this buffer to exactly this many samples. it's up to you make sure samples matches the channel...
Definition ofSoundBuffer.cpp:112
void copyFrom(const std::vector< short > &shortBuffer, std::size_t numChannels, unsigned int sampleRate)
float fillWithTone(float pitchHz=440.0f, float phase=0.0f)
fills the buffer with a sine wave. useful for debugging.
Definition ofSoundBuffer.cpp:608
std::size_t getNumChannels() const
the number of channels per frame
Definition ofSoundBuffer.h:103
ofSoundBuffer()
Definition ofSoundBuffer.cpp:25
unsigned int samplerate
Definition ofSoundBuffer.h:226
uint64_t getDurationNanos() const
Definition ofSoundBuffer.cpp:94
float getRMSAmplitudeChannel(std::size_t channel) const
Definition ofSoundBuffer.cpp:544
void normalize(float level=1)
amplifies samples so that the maximum amplitude is equal to 'level'
Definition ofSoundBuffer.cpp:557
void clear()
remove all samples, preserving channel count and sample rate.
Definition ofSoundBuffer.cpp:117
void copyFrom(const std::vector< float > &floatBuffer, std::size_t numChannels, unsigned int sampleRate)
ofSoundBuffer & operator*=(float value)
multiply everything in this buffer by value, in-place.
Definition ofSoundBuffer.cpp:169
void resample(float speed, InterpolationAlgorithm algorithm=defaultAlgorithm)
resample by changing the playback speed, keeping the same sampleRate
Definition ofSoundBuffer.cpp:492
void addTo(ofSoundBuffer &outBuffer, std::size_t outNumFrames, std::size_t outNumChannels, std::size_t fromFrame, bool loop=false) const
as copyTo but mixes source audio with audio in outBuffer by adding samples together (+),...
Definition ofSoundBuffer.cpp:203
ofSoundBuffer operator*(float value)
return a new buffer containing the contents of this buffer multiplied by value.
Definition ofSoundBuffer.cpp:163
Definition ofPixels.h:1522
#define swap(a, i, j)