-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsequencer.cpp
275 lines (243 loc) · 8.84 KB
/
sequencer.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <sound/sequencer.h>
#include <os_config.h>
#include <service/prefs.h>
#include <algorithm>
static char LOG_TAG[] = "MELODY";
#define BIT_END_PLAY 0b00000001
#if HAS(SERIAL_MIDI)
#include <MIDI.h>
static const uint16_t sNotePitches[] = {
31, 33, 35, 37, 39, 41, 44, 46,
49, 52, 55, 58, 62, 65, 69, 73,
78, 82, 87, 93, 98, 104, 110, 117,
123, 131, 139, 147, 156, 165, 175, 185,
196, 208, 220, 233, 247, 262, 277, 294,
311, 330, 349, 370, 392, 415, 440, 466,
494, 523, 554, 587, 622, 659, 698, 740,
784, 831, 880, 932, 988, 1047, 1109, 1175,
1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865,
1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960,
3136, 3322, 3520, 3729, 3951, 4186, 4435, 4699, 4978,
};
struct MySettings : public midi::DefaultSettings
{
static const unsigned SysExMaxSize = 1024; // Accept SysEx messages up to 1024 bytes long.
static const unsigned BaudRate = 115200;
};
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, midiIo, MySettings);
static void serMidiTask(void * pvParameters) {
NewSequencer * seq = static_cast<NewSequencer*>(pvParameters);
midiIo.begin();
midiIo.setInputChannel(MIDI_CHANNEL_OMNI);
midiIo.turnThruOff();
while(1) {
if(midiIo.read()) {
ESP_LOGV(LOG_TAG, "MIDI recv");
seq->midi_task();
}
taskYIELD();
}
}
void NewSequencer::midi_task() {
switch(midiIo.getType()) {
case midi::NoteOn:
{
int ch = midiIo.getChannel() - 1;
if(ch < 0 || ch >= NewSequencer::CHANNELS) {
ESP_LOGV(LOG_TAG, "Invalid channel %i", ch);
return;
}
int noteNo = midiIo.getData1();
lastNote[ch] = noteNo;
int velo = midiIo.getData2();
ESP_LOGV(LOG_TAG, "Midi Note On ch=%i, noteNo=%i, velo=%i, freq=%i", ch, noteNo, velo, sNotePitches[noteNo]/2);
voices[ch]->set_parameter(ToneGenerator::Parameter::PARAMETER_FREQUENCY, velo > 0 ? sNotePitches[noteNo]/2 : 0);
}
break;
case midi::NoteOff:
{
int ch = midiIo.getChannel() - 1;
if(ch < 0 || ch >= NewSequencer::CHANNELS) {
ESP_LOGV(LOG_TAG, "Invalid channel %i", ch);
return;
}
if(lastNote[ch] == midiIo.getData1()) {
ESP_LOGV(LOG_TAG, "Midi Note Off ch=%i", ch);
voices[ch]->set_parameter(ToneGenerator::Parameter::PARAMETER_ACTIVE, false);
}
}
break;
case midi::ControlChange:
{
int ch = midiIo.getChannel() - 1;
if(ch < 0 || ch >= NewSequencer::CHANNELS) {
ESP_LOGV(LOG_TAG, "Invalid channel %i", ch);
return;
}
int controlNo = midiIo.getData1();
int controlVal = midiIo.getData2();
switch(controlNo) {
case 2:
voices[ch]->set_parameter(ToneGenerator::Parameter::PARAMETER_DUTY, controlVal);
break;
default:
ESP_LOGV(LOG_TAG, "Invalid control %i", controlNo);
break;
}
}
break;
}
}
#endif
static const uint8_t kick_rle_data[] = {0, 7, 4, 2, 1, 24, 23, 30, 33, 26, 38, 38, 30, 41, 41, 52, 62, 50, 58, 64, 61, 70, 99, 92, 80, 119, 102, 119, 119, 142, 146, 117, 160, 119, 154, 5};
const rle_sample_t kick_sample = { .sample_rate = 8000, .root_frequency = 524 /* C5 */, .length = 36, .mode = MIX_MODE_XOR, .rle_data = kick_rle_data };
NewSequencer::NewSequencer() {
num_rows = 0;
rows = nullptr;
// Ch 0, 1, 2, 3: tone
for(int i = 0; i < TONE_CHANNELS; i++) voices[i] = new SquareGenerator();
voices[4] = new NoiseGenerator(); // Ch4: Noise
voices[5] = new Sampler(&kick_sample); // Ch5: RLE PWM
wait_end_group = xEventGroupCreate();
#if HAS(SERIAL_MIDI)
if(prefs_get_bool(PREFS_KEY_SERIAL_MIDI)) {
xTaskCreate(
serMidiTask,
"MIDI",
2048,
this,
pisosTASK_PRIORITY_SERIAL_MIDI,
&hMidiTask
);
}
#endif
}
NewSequencer::~NewSequencer() {
for(int i = 0; i < CHANNELS; i++) delete voices[i];
}
bool NewSequencer::is_sequencing() {
return is_running;
}
void NewSequencer::stop_sequence() {
is_running = false;
for(int i = 0; i < CHANNELS; i++) voices[i]->set_parameter(ToneGenerator::PARAMETER_ACTIVE, false);
voices[5]->set_parameter(ToneGenerator::PARAMETER_SAMPLE_ADDR, (int) &kick_sample);
sequence->unload();
num_rows = 0;
rows = nullptr;
xEventGroupSetBits(wait_end_group, BIT_END_PLAY);
}
void NewSequencer::wait_end_play() {
xEventGroupWaitBits(wait_end_group, BIT_END_PLAY, pdFALSE, pdTRUE, portMAX_DELAY);
}
void NewSequencer::play_sequence(MelodySequence * s, sequence_playback_flags_t f, int repeat) {
if(!s->load()) {
ESP_LOGE(LOG_TAG, "Failed to load sequence");
return;
}
repetitions = repeat;
sequence = s;
rows = s->get_array();
num_rows = s->get_num_rows();
flags = f;
pointer = 0;
loop_point = 0;
for(int i = 0; i < CHANNELS; i++) {
voices[i]->set_parameter(ToneGenerator::PARAMETER_FREQUENCY, 0);
voices[i]->set_parameter(ToneGenerator::PARAMETER_DUTY, 0);
}
voices[5]->set_parameter(ToneGenerator::PARAMETER_SAMPLE_ADDR, (int) &kick_sample);
if((f & SEQUENCER_PLAY_HOOK_ONLY) != 0) {
find_hook();
pointer = loop_point;
}
remaining_delay_samples = 0;
xEventGroupClearBits(wait_end_group, BIT_END_PLAY);
process_steps_until_delay();
is_running = true;
}
bool NewSequencer::end_of_song() {
// End of Song condition
if((flags & SEQUENCER_REPEAT_INDEFINITELY) != 0 || repetitions > 0) {
if(repetitions > 0) repetitions--;
pointer = loop_point;
process_steps_until_delay();
return true;
} else if(repetitions == 0) {
stop_sequence();
return true;
}
return false;
}
bool NewSequencer::process_step(const melody_item_t * cur_line) {
switch(cur_line->command) {
case FREQ_SET:
voices[cur_line->channel]->set_parameter(ToneGenerator::PARAMETER_FREQUENCY, cur_line->argument);
break;
case DUTY_SET:
voices[cur_line->channel]->set_parameter(ToneGenerator::PARAMETER_DUTY, cur_line->argument);
break;
case LOOP_POINT_SET:
if(cur_line->argument == LOOP_POINT_TYPE_HOOK_END && (flags & SEQUENCER_PLAY_HOOK_ONLY) != 0) {
if(end_of_song()) return true;
}
else if((cur_line->argument == LOOP_POINT_TYPE_HOOK_START && (flags & SEQUENCER_PLAY_HOOK_ONLY) != 0) || (cur_line->argument == LOOP_POINT_TYPE_LOOP) ){
loop_point = pointer + 1;
}
break;
case DELAY:
remaining_delay_samples = cur_line->argument * WaveOut::BAUD_RATE / 1000;
break;
case SAMPLE_LOAD:
voices[cur_line->channel]->set_parameter(ToneGenerator::PARAMETER_SAMPLE_ADDR, cur_line->argument);
break;
default:
break;
}
return false;
}
void NewSequencer::find_hook() {
while(pointer < num_rows) {
const melody_item_t * cur_line = &rows[pointer];
if(cur_line->command == LOOP_POINT_SET) {
if(cur_line->argument == LOOP_POINT_TYPE_HOOK_START) {
loop_point = pointer + 1;
ESP_LOGI(LOG_TAG, "Hook found, starts at %i", loop_point);
break;
} else {
pointer++;
}
} else if(cur_line->command == DELAY || cur_line->command == LOOP_POINT_SET || cur_line->command == FREQ_SET) {
pointer++;
} else {
process_step(cur_line);
pointer++;
}
}
}
void NewSequencer::process_steps_until_delay() {
if(num_rows == 0) return;
if(pointer >= num_rows) {
if(end_of_song()) return;
}
const melody_item_t * cur_line = &rows[pointer];
if(process_step(cur_line)) return;
pointer++;
if(cur_line->command != DELAY) {
process_steps_until_delay();
}
}
size_t NewSequencer::fill_buffer(void* buffer, size_t length) {
#if !HAS(SERIAL_MIDI)
if(!is_running) return 0;
#endif
if(is_running && remaining_delay_samples == 0) process_steps_until_delay();
size_t generated = 0;
uint32_t want_samples = std::min(length * 8, (size_t) remaining_delay_samples);
for(int i = 0; i < CHANNELS; i++) {
size_t cur = voices[i]->generate_samples(buffer, length, want_samples);
if(cur > generated) generated = cur;
}
remaining_delay_samples -= want_samples;
return generated;
}