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
AudioFile.h
1 #pragma once
2 #include <string>
3 #include <vector>
4 
11 namespace AudioFileUtilities {
22  int getSamples(const std::string& file, float *buf, unsigned int channel, unsigned int startFrame, unsigned int endFrame);
29  int getNumChannels(const std::string& file);
36  int getNumFrames(const std::string& file);
46  int write(const std::string& filename, float *buf, unsigned int channels, unsigned int frames, unsigned int samplerate);
55  int write(const std::string& filename, const std::vector<std::vector<float> >& dataIn, unsigned int sampleRate);
69  std::vector<std::vector<float> > load(const std::string& filename, int maxCount = -1, unsigned int start = 0);
76  std::vector<float> loadMono(const std::string& file);
77 };
78 
79 #include <libraries/sndfile/sndfile.h>
80 #include <thread>
81 #include <array>
82 
83 class AudioFile
84 {
85 protected:
86  typedef enum {
87  kRead,
88  kWrite,
89  } Mode;
90  enum { kNumBufs = 2};
91 protected:
92  int setup(const std::string& path, size_t bufferSize, Mode mode, size_t channels = 0, unsigned int sampleRate = 0);
93 public:
94  size_t getLength() const { return sfinfo.frames; };
95  size_t getChannels() const { return sfinfo.channels; };
96  int getSampleRate() const { return sfinfo.samplerate; };
97  virtual ~AudioFile();
98 private:
99  void cleanup();
100 protected:
101  volatile size_t ioBuffer;
102  size_t ioBufferOld;
103 protected:
104  void scheduleIo();
105  std::vector<float>& getRtBuffer();
106  std::array<std::vector<float>,kNumBufs> internalBuffers;
107  void threadLoop();
108  virtual void io(std::vector<float>& buffer) = 0;
109  std::thread diskIo;
110  size_t size;
111  volatile bool stop;
112  bool ramOnly;
113  size_t rtIdx;
114  SNDFILE* sndfile = NULL;
115  SF_INFO sfinfo = { 0 };
116 };
117 
119 {
120 public:
129  int setup(const std::string& path, size_t bufferSize);void getSamples(std::vector<float>& buffer);
143  void getSamples(float* dst, size_t samplesCount);
144  int setLoop(bool loop);
145  int setLoop(size_t start, size_t end);
146  size_t getIdx();
147 private:
148  void io(std::vector<float>& buffer) override;
149  bool loop;
150  size_t loopStart;
151  size_t loopStop;
152  size_t idx;
153 };
154 
156 {
157 public:
165  int setup(const std::string& path, size_t bufferSize, size_t channels, unsigned int sampleRate);
172  void setSamples(std::vector<float>& buffer);
173  void setSamples(float const * src, size_t samplesCount);
174 private:
175  void io(std::vector<float>& buffer) override;
176 };
int getNumFrames(const std::string &file)
Definition: AudioFileUtilities.cpp:66
std::vector< std::vector< float > > load(const std::string &filename, int maxCount=-1, unsigned int start=0)
Definition: AudioFileUtilities.cpp:135
int getSamples(const std::string &file, float *buf, unsigned int channel, unsigned int startFrame, unsigned int endFrame)
Definition: AudioFileUtilities.cpp:9
Definition: AudioFile.h:118
int setup(const std::string &path, size_t bufferSize, size_t channels, unsigned int sampleRate)
Definition: AudioFile.cpp:209
int setup(const std::string &path, size_t bufferSize)
Definition: AudioFile.cpp:97
int getNumChannels(const std::string &file)
Definition: AudioFileUtilities.cpp:53
int write(const std::string &filename, float *buf, unsigned int channels, unsigned int frames, unsigned int samplerate)
Definition: AudioFileUtilities.cpp:79
Definition: AudioFile.h:83
std::vector< float > loadMono(const std::string &file)
Definition: AudioFileUtilities.cpp:157
void getSamples(std::vector< float > &buffer)
Definition: AudioFile.cpp:167
void setSamples(std::vector< float > &buffer)
Definition: AudioFile.cpp:214
Definition: AudioFile.h:155