forked from leozimmerman/ofxAudioDecoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofxAudioDecoder.cpp
75 lines (61 loc) · 1.91 KB
/
ofxAudioDecoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "ofxAudioDecoder.h"
//----------
bool ofxAudioDecoder::load(ofSoundBuffer & buffer, string filename, size_t framesToRead) {
AudioDecoder audioDecoder(ofToDataPath(filename));
if (audioDecoder.open() != AUDIODECODER_OK) {
ofLogError() << "Failed to load " << filename;
return false;
}
auto numSamples = audioDecoder.numSamples();
auto numChannels = audioDecoder.channels();
auto sampleRate = audioDecoder.sampleRate();
buffer.setSampleRate(sampleRate);
buffer.allocate(numSamples / numChannels, numChannels);
//if user asked for 0 samples
if(framesToRead == 0) {
//we interpet that as wanting to read whole file
framesToRead = numSamples / numChannels;
}
int samplesToRead = framesToRead * numChannels;
int readBufferSize = sampleRate / numChannels;
int curSample = 0;
auto & rawSamples = buffer.getBuffer();
while(curSample < samplesToRead) {
int remainingSamples = MIN(readBufferSize, samplesToRead - curSample);
int samplesRead = audioDecoder.read(remainingSamples, &rawSamples[curSample]);
curSample += samplesRead;
if(samplesRead < readBufferSize) {
break;
}
}
ofLogVerbose() << "Read " << curSample << " of " << numSamples;
return true;
}
//----------
bool ofxAudioDecoder::load(string filename, size_t framesToRead) {
return ofxAudioDecoder::load(this->buffer, filename, framesToRead);
}
//----------
int ofxAudioDecoder::getNumChannels() const {
return this->buffer.getNumChannels();
}
//----------
int ofxAudioDecoder::getSampleRate() const {
return this->buffer.getSampleRate();
}
//----------
int ofxAudioDecoder::getNumSamples() const {
return this->buffer.size();
}
//----------
int ofxAudioDecoder::getNumFrames() const {
return this->buffer.getNumFrames();
}
//----------
const ofSoundBuffer & ofxAudioDecoder::getBuffer() const {
return this->buffer;
}
//----------
const vector<float> & ofxAudioDecoder::getRawSamples() const {
return this->buffer.getBuffer();
}