Voss Connection Detection  1
Detect a connector click using realtime code on Bela hardware
render.cpp
Go to the documentation of this file.
1 
7 #include <Bela.h>
8 #include <cmath>
9 #include <iostream>
10 #include "config.h"
11 #include "conndetect.h"
12 
13 using std::cerr;
14 using std::endl;
15 #define NCHANNELS_INPUT_ANALOG 4
16 #define NCHANNELS_INPUT_AUDIO 2
17 
18 bool setup(BelaContext *context, void *ConnDetect_ptr)
19 {
20 
21 
22  ConnDetect* pud = nullptr;
23  try {
24  pud = new ConnDetect(context);
25  } catch(std::runtime_error& e) {
26  cerr << "Error initializing connection detection: " << e.what() << endl;
27  cerr << "Aborting..." << endl;
28  return false;
29  }
30 
31  if(NCHANNELS_INPUT_ANALOG != context->analogInChannels) {
32  cerr << "Unexpected number of analog in channels. Given: "
33  << context->analogInChannels << ". But required: "
34  << NCHANNELS_INPUT_ANALOG << endl;
35  return false;
36  }
37 
38  Bela_setUserData(static_cast<void*>(pud));
39 
40  // Sanity checks for sensors etc.
41  if(context->audioSampleRate != SAMPLING_FREQUENCY) {
42  cerr << "Invalid sampling frequency, should be " << SAMPLING_FREQUENCY << endl;
43  return false;
44  }
45 
46 
47  return true;
48 }
49 
50 // render() is called repeatedly by Bela for each audio block
51 void render(BelaContext *context, void *ConnDetect_opaque)
52 {
53 
54  ConnDetect& ud = *static_cast<ConnDetect*>(ConnDetect_opaque);
55 
56  // Loop over frames
57  /* for(us n=0; n <context->analogFrames; n++) { */
58 
59  /* const float* mic_samples = &(context->analogIn[NCHANNELS_INPUT_ANALOG*n]); */
60  /* const float* acc_samples = &(mic_samples[2]); */
61 
62  /* ud.addSamples(mic_samples, acc_samples); */
63 
64  /* } */
65  // We use channel 0,1,2,3 from the audio expander set, which map to channel
66  // 2,3,4,5 of the audio frames
67  for(us n=0; n <context->analogFrames; n++) {
68 
69  const float* mic_samples = &(context->analogIn[NCHANNELS_INPUT_ANALOG*n + 0]);
70  const float* acc_samples = &(mic_samples[2]);
71  ud.addSamples(context, mic_samples, acc_samples);
72 
73  }
74 
75 }
76 
77 // cleanup() runs once at the end of the program
78 void cleanup(BelaContext *context, void *ConnDetect_ptr)
79 {
80 
81  // Cleanup ConnDetect
82  ConnDetect* ud = static_cast<ConnDetect*>(ConnDetect_ptr);
83  delete ud;
84 }
SAMPLING_FREQUENCY
const float SAMPLING_FREQUENCY
This is the exact sampling frequency of the system. Do not change!
Definition: config.h:99
conndetect.h
render
void render(BelaContext *context, void *ConnDetect_opaque)
Definition: render.cpp:51
ConnDetect
The main connection detection class.
Definition: conndetect.h:83
setup
bool setup(BelaContext *context, void *ConnDetect_ptr)
Definition: render.cpp:18
NCHANNELS_INPUT_ANALOG
#define NCHANNELS_INPUT_ANALOG
Definition: render.cpp:15
cleanup
void cleanup(BelaContext *context, void *ConnDetect_ptr)
Definition: render.cpp:78
us
unsigned int us
Used to much to not abbreviate.
Definition: config.h:38
config.h
Configuration parameters for connection detection.