Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Files Functions Variables Typedefs Macros Groups
SampleStream.h
1 /***** SampleStream.h *****/
2 
3 // TODO: seek/rewind functions
4 
5 #ifndef SAMPLESTREAM_H_
6 #define SAMPLESTREAM_H_
7 
8 #include <SampleData.h>
9 #include <string>
10 #include <Bela.h>
11 
12 #include <sndfile.h> // to load audio files
13 #include <iostream>
14 #include <cstdlib>
15 
16 using namespace std;
17 
19 {
20 
21 public:
22 
23  SampleStream(const char* filename, int numChannels, int bufferLength);
24  ~SampleStream();
25  int openFile(const char* filename, int numChannels, int bufferLength);
26  void fillBuffer();
27  void processFrame();
28  float getSample(int channel);
29  int bufferNeedsFilled();
30  void togglePlayback();
31  void togglePlaybackWithFade(float fadeLengthInSeconds);
32  void togglePlayback(int toggle);
33  void togglePlaybackWithFade(int toggle, float fadeLengthInSeconds);
34  int isPlaying();
35 
36 private:
37 
38  // private libsndfile wrappers
39  int getSamples(const char* file, float *buf, int channel, int startFrame, int endFrame);
40  int getNumChannels(const char* file);
41  int getNumFrames(const char* file);
42 
43  // Two buffers for each channel:
44  // one of them loads the next chunk of audio while the other one is used for playback
45  SampleData *gSampleBuf[2];
46  // read pointer relative current buffer (range 0-BUFFER_LEN)
47  // initialise at BUFFER_LEN to pre-load second buffer (see render())
48  int gReadPtr;
49  // read pointer relative to file, increments by BUFFER_LEN (see fillBuffer())
50  int gBufferReadPtr;
51  // keeps track of which buffer is currently active (switches between 0 and 1)
52  int gActiveBuffer;
53  // this variable will let us know if the buffer doesn't manage to load in time
54  int gDoneLoadingBuffer;
55  int gBufferLength;
56  int gNumChannels;
57 
58  int gNumFramesInFile;
59  const char* gFilename;
60  int gBufferToBeFilled;
61  int gPlaying;
62 
63  float gFadeAmount;
64  float gFadeLengthInSeconds;
65  int gFadeDirection;
66 
67  int gBusy;
68 
69  SNDFILE *sndfile = NULL;
70  SF_INFO sfinfo ;
71 
72 };
73 
74 #endif // SAMPLESTREAM_H_
Definition: SampleStream.h:18
Definition: SampleData.h:12
Main Bela public API.