Voss Connection Detection  1
Detect a connector click using realtime code on Bela hardware
conndetect_sndfile.cpp
Go to the documentation of this file.
1 #include "conndetect_sndfile.h"
2 #include <iostream>
3 #include <string>
4 #include <libraries/sndfile/sndfile.h>
5 
6 using namespace std;
7 
8 template<us numChannels>
9 void raw_sndfile_auxtask_callback(void* sndfile_ptr) {
10  SndFile<numChannels>* sndfile= (SndFile<numChannels>*) sndfile_ptr;
11  sndfile->write_task();
12 }
13 
14 template<us numChannels>
15 SndFile<numChannels>::SndFile(BelaContext* ctx,const char* filename) {
16  SF_INFO info;
17  info.frames = 0;
18  info.samplerate = ctx->analogSampleRate;
19  info.channels = numChannels;
20  /* info.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; */
21  info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_32;
22  info.sections = 0;
23  info.seekable = 0;
24 
25  string fn = string(filename) + ".wav";
26 
27  _file = sf_open(fn.c_str(), SFM_WRITE, &info);
28 
29  if(_file==nullptr) {
30  cerr << "SNDFILE error: " << sf_strerror(_file) << endl;
31  throw runtime_error("Unable to open sndfile!");
32  }
33  /* string title = "Bela raw data"; */
34  /* sf_set_string(_file, SF_STR_TITLE, title.c_str()); */
35 
36  _task = Bela_createAuxiliaryTask(raw_sndfile_auxtask_callback<numChannels>,
37  45, "Write_to_sndfile_task", this);
38 
39 }
40 template<us numChannels>
41 void SndFile<numChannels>::write(const std::array<float, numChannels>& samples) {
42  if(_buf0writeptr != BUFSIZE) {
43  for(us i=0;i<numChannels;i++) {
44  _buf0[_buf0writeptr*numChannels+i] = samples[i];
45  }
46  _buf0writeptr++;
47  }
48  else if(_buf1writeptr != BUFSIZE) {
49  for(us i=0;i<numChannels;i++) {
50  _buf1[_buf1writeptr*numChannels+i] = samples[i];
51  }
52  _buf1writeptr++;
53  }
54  else {
55  throw std::runtime_error("Both buffers full");
56  }
57  if(_buf0writeptr == BUFSIZE || _buf1writeptr == BUFSIZE) {
58  if(!_writing) {
59  Bela_scheduleAuxiliaryTask(_task);
60  }
61  }
62 }
63 
64 template<us numChannels>
66  _writing = true;
67  cerr << "Writing to file... " << endl;
68 
69  float* buf;
70  if(_buf0writeptr == BUFSIZE) {
71  buf = _buf0;
72  }
73  else if(_buf1writeptr == BUFSIZE) {
74  buf = _buf1;
75  }
76  else {
77  throw std::runtime_error("No buffers full");
78  }
79 
80 
81  if(Bela_stopRequested()) return;
82 
83  // Perform writing to sndfile
84  sf_count_t no = sf_writef_float(_file, buf, BUFSIZE);
85 
86  if(_buf0writeptr == BUFSIZE) {
87  _buf0writeptr = 0;
88  }
89  if(_buf1writeptr == BUFSIZE) {
90  _buf1writeptr = 0;
91  }
92 
93  _writing = false;
94 
95 }
96 template<us numChannels>
98  if(_writing) {
99  cerr << "Destructor called while writing to file" << endl;
100  }
101  sf_close(_file);
102 }
103 
104 template class SndFile<5>;
105 template class SndFile<6>;
106 template class SndFile<8>;
107 template class SndFile<10>;
SndFile::SndFile
SndFile(BelaContext *ctx, const char *filename)
Definition: conndetect_sndfile.cpp:15
raw_sndfile_auxtask_callback
void raw_sndfile_auxtask_callback(void *sndfile_ptr)
Definition: conndetect_sndfile.cpp:9
SndFile::write
void write(const std::array< float, numChannels > &samples)
Definition: conndetect_sndfile.cpp:41
SndFile
Class to write to a sound file.
Definition: conndetect_sndfile.h:16
SndFile::~SndFile
~SndFile()
Definition: conndetect_sndfile.cpp:97
us
unsigned int us
Used to much to not abbreviate.
Definition: config.h:38
BUFSIZE
const us BUFSIZE
Definition: conndetect_sndfile.h:9
conndetect_sndfile.h