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