-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jaffx.hpp
88 lines (73 loc) · 2.88 KB
/
Jaffx.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "libDaisy/src/daisy_seed.h"
using namespace daisy;
namespace Jaffx {
struct Program {
// declare an instance of the hardware
static DaisySeed hardware;
static Program* instance; // Static pointer to the current instance of Program
// It's handy to have these numbers on tap
const int samplerate = 48000;
const int buffersize = 128;
// loadMeter for debugging, bool for toggle
CpuLoadMeter loadMeter;
bool debug = false;
// overridable init function
inline virtual void init() {}
// debug init function
inline void initDebug() {
if (debug) {
hardware.StartLog();
loadMeter.Init(hardware.AudioSampleRate(), hardware.AudioBlockSize());
}
}
// overridable per-sample operation
inline virtual float processAudio(float in) {return in;}
// overridable loop operation
inline virtual void loop() {}
// debug loop
inline void debugLoop() {
if (debug) {
// as seen in https://electro-smith.github.io/libDaisy/md_doc_2md_2__a3___getting-_started-_audio.html
const float avgLoad = loadMeter.GetAvgCpuLoad();
const float maxLoad = loadMeter.GetMaxCpuLoad();
const float minLoad = loadMeter.GetMinCpuLoad();
// print it to the serial connection (as percentages)
hardware.PrintLine("Processing Load:");
hardware.PrintLine("Max: " FLT_FMT3 "%%", FLT_VAR3(maxLoad * 100.0f));
hardware.PrintLine("Avg: " FLT_FMT3 "%%", FLT_VAR3(avgLoad * 100.0f));
hardware.PrintLine("Min: " FLT_FMT3 "%%", FLT_VAR3(minLoad * 100.0f));
System::Delay(1000); // Don't spam the serial!
}
}
// overridable block start/end operation
inline virtual void blockStart() {}
inline virtual void blockEnd() {}
// basic mono->dual-mono callback
static void AudioCallback(AudioHandle::InputBuffer in, AudioHandle::OutputBuffer out, size_t size) {
if (instance->debug) {instance->loadMeter.OnBlockStart();}
instance->blockStart();
for (size_t i = 0; i < size; i++) {
out[0][i] = instance->processAudio(in[0][i]); // format is in/out[channel][sample]
out[1][i] = out[0][i];
}
instance->blockEnd();
if (instance->debug) {instance->loadMeter.OnBlockEnd();}
}
void start() {
// initialize hardware
hardware.Init();
hardware.SetAudioBlockSize(buffersize); // number of samples handled per callback (buffer size)
hardware.SetAudioSampleRate(SaiHandle::Config::SampleRate::SAI_48KHZ); // sample rate
// init instance and start callback
instance = this;
this->init();
this->initDebug();
hardware.StartAudio(AudioCallback);
// loop indefinitely
while(1) {this->loop(); this->debugLoop();}
}
};
// Global instancing of static members
DaisySeed Program::hardware;
Program* Program::instance = nullptr;
} // namespace Jaffx