-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleStream.h
71 lines (55 loc) · 1.94 KB
/
SampleStream.h
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
/***** SampleStream.h *****/
// TODO: seek/rewind functions
#ifndef SAMPLESTREAM_H_
#define SAMPLESTREAM_H_
#include <SampleData.h>
#include <Bela.h>
#include <libraries/sndfile/sndfile.h> // to load audio files
#include <iostream>
#include <cstdlib>
class SampleStream
{
public:
SampleStream(const char* filename, int numChannels, int bufferLength);
~SampleStream();
int openFile(const char* filename, int numChannels, int bufferLength);
void fillBuffer();
void processFrame();
float getSample(int channel);
int bufferNeedsFilled();
void togglePlayback();
void togglePlaybackWithFade(float fadeLengthInSeconds);
void togglePlayback(int toggle);
void togglePlaybackWithFade(int toggle, float fadeLengthInSeconds);
int isPlaying();
private:
// private libsndfile wrappers
int getSamples(const char* file, float *buf, int channel, int startFrame, int endFrame);
int getNumChannels(const char* file);
int getNumFrames(const char* file);
// Two buffers for each channel:
// one of them loads the next chunk of audio while the other one is used for playback
SampleData *gSampleBuf[2];
// read pointer relative current buffer (range 0-BUFFER_LEN)
// initialise at BUFFER_LEN to pre-load second buffer (see render())
int gReadPtr;
// read pointer relative to file, increments by BUFFER_LEN (see fillBuffer())
int gBufferReadPtr;
// keeps track of which buffer is currently active (switches between 0 and 1)
int gActiveBuffer;
// this variable will let us know if the buffer doesn't manage to load in time
int gDoneLoadingBuffer;
int gBufferLength;
int gNumChannels;
int gNumFramesInFile;
const char* gFilename;
int gBufferToBeFilled;
int gPlaying;
float gFadeAmount;
float gFadeLengthInSeconds;
int gFadeDirection;
int gBusy;
SNDFILE *sndfile = NULL;
SF_INFO sfinfo ;
};
#endif // SAMPLESTREAM_H_