-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNBO.cpp
165 lines (137 loc) · 3.6 KB
/
RNBO.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "daisy_pod.h"
#include "tlsf.h"
#define RNBO_USE_FLOAT32
#define RNBO_NOTHROW
#define RNBO_FIXEDLISTSIZE 64
#define RNBO_USECUSTOMALLOCATOR
#include "export/rnbo_source.h"
// implement our custom allocation methods, which are just re-directing all calls to
// a simple pool based allocator
tlsf_t myPool;
// use all the available 64 MB in the SDRAM for our pool
#define POOLSIZE (64 * 1024 * 1024)
namespace RNBO
{
namespace Platform
{
void *malloc(size_t size)
{
return tlsf_malloc(myPool, size);
}
void free(void *ptr)
{
tlsf_free(myPool, ptr);
}
void *realloc(void *ptr, size_t size)
{
return tlsf_realloc(myPool, ptr, size);
}
void *calloc(size_t count, size_t size)
{
auto mem = malloc(count * size);
memset(mem, 0, count * size);
return mem;
}
}
}
using namespace daisy;
static const size_t vectorsize = 64;
DaisyPod hw;
MidiUsbHandler midi;
RNBO::rnbomatic<> rnbo;
// just an axample of how to control a named parameter
long Param1Index = -1; // this we will find out later
const char *Param1Name = "level"; // just an arbitrary name for the sake of this demo
// Typical Switch case for Message Type.
void HandleMidiMessage(MidiEvent m)
{
if (m.type < MidiMessageType::SystemCommon)
{
uint8_t midiData[3];
switch (m.type)
{
case NoteOff:
midiData[0] = 0x80 + m.channel;
break;
case NoteOn:
midiData[0] = 0x90 + m.channel;
break;
case PolyphonicKeyPressure:
midiData[0] = 0xA0 + m.channel;
break;
case ControlChange:
midiData[0] = 0xB0 + m.channel;
break;
case ProgramChange:
midiData[0] = 0xC0 + m.channel;
break;
case ChannelPressure:
midiData[0] = 0xD0 + m.channel;
break;
case PitchBend:
midiData[0] = 0xE0 + m.channel;
break;
default:
break;
}
midiData[1] = m.data[0];
midiData[2] = m.data[1];
rnbo.processMidiEvent(RNBO::RNBOTimeNow, 0, midiData, 3);
}
}
void AudioCallback(AudioHandle::InputBuffer in, AudioHandle::OutputBuffer out, size_t size)
{
hw.ProcessAllControls();
while (midi.HasEvents())
{
HandleMidiMessage(midi.PopEvent());
}
if (Param1Index >= 0)
{
auto inc = hw.encoder.Increment();
if (inc != 0)
{
auto val = rnbo.getParameterValue(Param1Index);
val += inc == 1 ? 0.1 : -0.1;
if (val > 1)
val = 1;
else if (val < 0)
val = 0;
rnbo.setParameterValueNormalized(Param1Index, val);
}
}
rnbo.process(in, 2, out, 2, vectorsize);
}
int main(void)
{
hw.Init();
hw.SetAudioBlockSize(vectorsize);
hw.SetAudioSampleRate(SaiHandle::Config::SampleRate::SAI_48KHZ);
// directly use the base address of the SDRAM as a pool, see:
// https://electro-smith.github.io/libDaisy/md_doc_2md_2__a6___getting-_started-_external-_s_d_r_a_m.html
myPool = tlsf_create_with_pool((void *)0xC0000000, POOLSIZE);
// initialize the Daisy for receiving MIDI over USB
MidiUsbHandler::Config midi_cfg;
midi_cfg.transport_config.periph = MidiUsbTransport::Config::INTERNAL;
midi.Init(midi_cfg);
// initialize RNBO, here for example audio samples are allocated in the SDRAM (through our allocator)
rnbo.initialize();
// if you do not want this to allocate (and a slightly better performance) consider exporting your code
// with a fixed audio vector size matching the one you are using (vectorsize)
rnbo.prepareToProcess(48000, vectorsize, true);
// find out the index of our named parameter to be able to interact with it later
for (int i = 0; i < rnbo.getNumParameters(); i++)
{
auto name = rnbo.getParameterName(i);
if (strcmp(name, Param1Name) == 0)
{
Param1Index = i;
}
}
hw.StartAdc();
hw.StartAudio(AudioCallback);
for (;;)
{
midi.Listen();
}
}