-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleStream.cpp
278 lines (204 loc) · 7.42 KB
/
SampleStream.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***** SampleStream.cpp *****/
#include <SampleStream.h>
#include <string>
SampleStream::SampleStream(const char* filename, int numChannels, int bufferLength) {
gSampleBuf[0] = NULL;
gSampleBuf[1] = NULL;
openFile(filename,numChannels,bufferLength);
}
SampleStream::~SampleStream() {
sf_close(sndfile);
for(int ch=0;ch<gNumChannels;ch++) {
for(int i=0;i<2;i++) {
if(gSampleBuf[i][ch].samples != NULL)
delete[] gSampleBuf[i][ch].samples;
}
}
free(gSampleBuf[0]);
free(gSampleBuf[1]);
}
int SampleStream::openFile(const char* filename, int numChannels, int bufferLength) {
gBusy = 1;
sf_close(sndfile);
sfinfo.format = 0;
if (!(sndfile = sf_open (std::string(filename).c_str(), SFM_READ, &sfinfo))) {
std::cout << "Couldn't open file " << filename << ": " << sf_strerror(sndfile) << std::endl;
return 1;
}
gBufferLength = bufferLength;
gNumChannels = numChannels;
gFilename = filename;
for(int i=0;i<2;i++) {
if(gSampleBuf[i] != NULL) {
std::cout << "I AM NOT A NULL" << std::endl;
for(int ch=0;ch<numChannels;ch++) {
if(gSampleBuf[i][ch].samples != NULL)
delete[] gSampleBuf[i][ch].samples;
}
free(gSampleBuf[i]);
}
}
gSampleBuf[0] = (SampleData*)calloc(bufferLength*numChannels,sizeof(SampleData));
gSampleBuf[1] = (SampleData*)calloc(bufferLength*numChannels,sizeof(SampleData));
gReadPtr = bufferLength;
gBufferReadPtr = 0;
gActiveBuffer = 0;
gDoneLoadingBuffer = 1;
gBufferToBeFilled = 0;
gPlaying = 0;
gFadeAmount = 0;
gFadeLengthInSeconds = 0.1;
gFadeDirection = -1;
gNumFramesInFile = getNumFrames(gFilename);
if(gNumFramesInFile <= gBufferLength) {
printf("Sample needs to be longer than buffer size. This example is intended to work with long samples.");
return 1;
}
for(int ch=0;ch<gNumChannels;ch++) {
for(int i=0;i<2;i++) {
gSampleBuf[i][ch].sampleLen = gBufferLength;
gSampleBuf[i][ch].samples = new float[gBufferLength];
if(getSamples(gFilename,gSampleBuf[i][ch].samples,ch,0,gBufferLength)) {
printf("error getting samples\n");
return 1;
}
}
}
std::cout << "Loaded " << filename << std::endl;
gBusy = 0;
return 0;
}
void SampleStream::processFrame() {
if(gFadeAmount<1 && gFadeAmount>0) {
gFadeAmount += (gFadeDirection*((1.0/gFadeLengthInSeconds)/44100.0));
}
else if(gFadeAmount < 0)
gPlaying = 0;
if(gPlaying) {
// Increment read pointer and reset to 0 when end of file is reached
if(++gReadPtr >= gBufferLength) {
// if(!gDoneLoadingBuffer)
// rt_printf("Couldn't load buffer in time :( -- try increasing buffer size!");
gDoneLoadingBuffer = 0;
gReadPtr = 0;
gActiveBuffer = !gActiveBuffer;
gBufferToBeFilled = 1;
}
}
}
float SampleStream::getSample(int channel) {
if(gPlaying) {
// Wrap channel index in case there are more audio output channels than the file contains
float out = gSampleBuf[gActiveBuffer][channel%gNumChannels].samples[gReadPtr];
return out * gFadeAmount * gFadeAmount;
}
return 0;
}
void SampleStream::fillBuffer() {
if(!gBusy) {
// increment buffer read pointer by buffer length
gBufferReadPtr+=gBufferLength;
// reset buffer pointer if it exceeds the number of frames in the file
if(gBufferReadPtr>=gNumFramesInFile)
gBufferReadPtr=0;
int endFrame = gBufferReadPtr + gBufferLength;
int zeroPad = 0;
// if reaching the end of the file take note of the last frame index
// so we can zero-pad the rest later
if((gBufferReadPtr+gBufferLength)>=gNumFramesInFile-1) {
endFrame = gNumFramesInFile-1;
zeroPad = 1;
}
for(int ch=0;ch<gNumChannels;ch++) {
// fill (nonactive) buffer
getSamples(gFilename,gSampleBuf[!gActiveBuffer][ch].samples,ch
,gBufferReadPtr,endFrame);
// zero-pad if necessary
if(zeroPad) {
int numFramesToPad = gBufferLength - (endFrame-gBufferReadPtr);
for(int n=0;n<numFramesToPad;n++)
gSampleBuf[!gActiveBuffer][ch].samples[n+(gBufferLength-numFramesToPad)] = 0;
}
}
gDoneLoadingBuffer = 1;
gBufferToBeFilled = 0;
// printf("done loading buffer!\n");
}
}
int SampleStream::bufferNeedsFilled() {
return gBufferToBeFilled;
}
void SampleStream::togglePlayback() {
gPlaying = !gPlaying;
gFadeAmount = gPlaying;
}
void SampleStream::togglePlaybackWithFade(int toggle, float fadeLengthInSeconds) {
gFadeDirection = toggle;
if(fadeLengthInSeconds<=0)
fadeLengthInSeconds = 0.00001;
gFadeLengthInSeconds = fadeLengthInSeconds;
gFadeAmount += (gFadeDirection*((1.0/gFadeLengthInSeconds)/44100.0));
if(gFadeDirection)
gPlaying = 1;
}
void SampleStream::togglePlayback(int toggle) {
gPlaying = toggle;
gFadeAmount = gPlaying;
}
void SampleStream::togglePlaybackWithFade(float fadeLengthInSeconds) {
gFadeDirection = gFadeDirection*-1;
if(fadeLengthInSeconds<=0)
fadeLengthInSeconds = 0.00001;
gFadeLengthInSeconds = fadeLengthInSeconds;
gFadeAmount += (gFadeDirection*((1.0/gFadeLengthInSeconds)/44100.0));
if(gFadeDirection)
gPlaying = 1;
}
int SampleStream::isPlaying() {
return gPlaying;
}
// private libsndfile wrappers (previously in SampleLoader.h)
int SampleStream::getSamples(const char* file, float *buf, int channel, int startFrame, int endFrame)
{
int numChannelsInFile = sfinfo.channels;
if(numChannelsInFile < channel+1)
{
std::cout << "Error: " << file << " doesn't contain requested channel" << std::endl;
return 1;
}
int frameLen = endFrame-startFrame;
if(frameLen <= 0 || startFrame < 0 || endFrame <= 0 || endFrame > sfinfo.frames)
{
std::cout << "Error: " << file << " invalid frame range requested" << std::endl;
return 1;
}
sf_seek(sndfile,startFrame,SEEK_SET);
float* tempBuf = new float[frameLen*numChannelsInFile];
int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
int readcount = sf_read_float(sndfile, tempBuf, frameLen*numChannelsInFile); //FIXME
// Pad with zeros in case we couldn't read whole file
for(int k = readcount; k <frameLen*numChannelsInFile; k++)
tempBuf[k] = 0;
if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
double scale ;
int m ;
sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
if (scale < 1e-10)
scale = 1.0 ;
else
scale = 32700.0 / scale ;
std::cout << "File samples scale = " << scale << std::endl;
for (m = 0; m < frameLen; m++)
tempBuf[m] *= scale;
}
for(int n=0;n<frameLen;n++)
buf[n] = tempBuf[n*numChannelsInFile+channel];
delete[] tempBuf;
return 0;
}
int SampleStream::getNumChannels(const char* file) {
return sfinfo.channels;
}
int SampleStream::getNumFrames(const char* file) {
return sfinfo.frames;
}