-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.cpp
More file actions
245 lines (205 loc) · 7.47 KB
/
Copy pathcontroller.cpp
File metadata and controls
245 lines (205 loc) · 7.47 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <string>
#include <cmath>
#include <bitset>
#include <chrono>
#include <algorithm>
#include <fstream>
using namespace std;
#include "project.h"
#include "expr.h"
#include "pcm_wrapper.h"
#include "synth_api.h"
#include "util.h"
namespace controller {
project current_project;
instrument *instruments = current_project.instruments;
sequence *sequences = current_project.sequences;
expr<float> expressions[NUM_INSTRUMENTS];
expr_context ctx;
int current_step {-1};
uint8_t last_sample {0x80};
expr<float> default_instrument;
inline uint8_t f2u8(float value) {
return value > 1 ? 0xff : value < -1 ? 0 : 128 + value * 127;
}
void init() {
ctx.var("t", &synth::t);
ctx.var("f", &synth::f);
ctx.var("tq", &synth::tq);
ctx.var("w", &synth::w);
ctx.var("nl", &synth::nl);
ctx.var("nlq", &synth::nlq);
ctx.var("nt", &synth::nt);
ctx.var("ntq", &synth::ntq);
ctx.func("sin", synth::sin);
ctx.func("saw", synth::saw);
ctx.func("sqr", synth::sqr);
ctx.func("noise", synth::noise);
ctx.func("sin_t", synth::sin_t);
ctx.func("saw_t", synth::saw_t);
ctx.func("sqr_t", synth::sqr_t);
ctx.func("sin_tq", synth::sin_tq);
ctx.func("saw_tq", synth::saw_tq);
ctx.func("sqr_tq", synth::sqr_tq);
ctx.func("root", synth::root);
ctx.func("pow", synth::pow);
ctx.func("min", synth::min);
ctx.func("max", synth::max);
ctx.func("scale", synth::scale);
ctx.func("scale01", synth::scale01);
ctx.func("mix", synth::mix);
ctx.func("env", synth::env_tq);
ctx.func("env_t", synth::env_t);
pcm_open();
default_instrument = expr<float>(ctx, "mix(sin(1), sqr(2), 1.0 / (nl * 4 + 1))");
}
void setup_instrument(int id, const string &ex) {
expressions[id] = expr<float>(ctx, ex);
instruments[id].enabled = true;
}
void remove_instrument(int id) {
instruments[id] = instrument();
expressions[id] = expr<float>();
}
bool write_blank_frames(int n, int &ptr, bool (condition())) {
while (n > 0) {
int end = min(BUFFER_LEN, ptr + n);
int start = ptr;
while (ptr < end) {
audio_buffer[ptr++] = last_sample;
synth::incr_frame(current_project.bpm);
}
if (ptr == BUFFER_LEN) {
pcm_write();
ptr = 0;
}
n -= end - start;
if (!condition())
return false;
}
return true;
}
void play_note(const int instrument, const int note, bool (condition())) {
synth::reset_frame();
evt_note n;
n.start = 0;
n.length = 16 * 4 * 32;
n.start_note = n.end_note = note;
n.start_vel = n.end_vel = 100;
expr<float> &inst = instruments[instrument].enabled ?
expressions[instrument] : default_instrument;
float volume = ((float) current_project.volume / 100) *
((float) instruments[instrument].volume / 100);
while (condition()) {
for (int i = 0; i < BUFFER_LEN; ++i) {
if (synth::set_note(n) == 1) {
synth::reset_frame();
}
synth::incr_frame(60);
synth::set_note(n);
audio_buffer[i] = last_sample = f2u8(volume * inst.eval());
}
pcm_write();
}
int ptr;
write_blank_frames(BUFFER_LEN, ptr, condition);
}
void play_sequence(const int s_idx, bool(condition())) {
synth::reset_frame();
sequence &s = sequences[s_idx];
// iterate through the notes in sorted order
const int bpm = current_project.bpm;
synth::incr_frame(bpm);
float volume = (float) current_project.volume / 100;
int ptr = 0;
while (condition()) {
s.sort();
// range of notes being played
auto start = s.notes.begin(), end = start;
auto last = s.notes.end();
while (start < last) {
if (start >= end) {
// wait until we have a note
if (!write_blank_frames(synth::frames_until(*start), ptr, condition)) {
return;
}
end = start + 1;
}
// find all other notes playing at this time
int note_time = 0;
if (end != last) {
while (note_time == 0) {
note_time = synth::frames_until(*end);
if (note_time == 0) {
if (++end >= last)
break;
}
}
}
// sort the current range according to note end
sort(start, end, [](const evt_note &n1, const evt_note &n2) {
return n1.start + n1.length < n2.start + n2.length;
});
// we're playing the last note
if (note_time == 0) {
evt_note sample;
const evt_note latest = *(end-1);
sample.start = latest.start + latest.length;
sample.length = 0;
note_time = synth::frames_until(sample);
}
while (start < end) {
float val = 0;
for (auto itor = start; itor < end; ++itor) {
const evt_note n = *itor;
if (synth::set_note(n) == 0) {
val += ((float) instruments[n.instrument].volume / 100) *
expressions[n.instrument].eval();
} else {
start = itor + 1;
}
}
audio_buffer[ptr++] = last_sample = f2u8(volume * val);
if (ptr == BUFFER_LEN) {
if (!condition()) {
goto play_sequence_done;
}
pcm_write();
ptr = 0;
}
synth::incr_frame(current_project.bpm);
if (--note_time < 0)
break;
}
}
{
evt_note dummy;
dummy.start = s.length * s.ts;
if (!write_blank_frames(synth::frames_until(dummy), ptr, condition)) {
return;
}
}
}
play_sequence_done:
write_blank_frames(BUFFER_LEN-ptr, ptr, condition);
}
void eval_with_params(int id, int note, double seconds, int length, float *buffer) {
if (!instruments[id].enabled) {
return;
}
synth::reset_frame();
evt_note n;
n.start = 0;
n.length = 16;
n.start_note = n.end_note = note;
n.start_vel = n.end_vel = 100;
for (int i = 0; i < length; ++i) {
synth::incr_frame(current_project.bpm);
synth::set_note(n);
buffer[i] = expressions[id].eval();
}
}
void destroy() {
pcm_close();
}
}