-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathmulti.h
executable file
·474 lines (406 loc) · 12.9 KB
/
multi.h
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Copyright 2013 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
// -----------------------------------------------------------------------------
//
// Multi.
#ifndef YARNS_MULTI_H_
#define YARNS_MULTI_H_
#include "stmlib/stmlib.h"
#include "yarns/internal_clock.h"
#include "yarns/layout_configurator.h"
#include "yarns/part.h"
#include "yarns/voice.h"
namespace yarns {
const uint8_t kNumParts = 4;
const uint8_t kNumVoices = 4;
const uint8_t kMaxBarDuration = 32;
struct MultiSettings {
uint8_t layout;
uint8_t clock_tempo;
uint8_t clock_swing;
uint8_t clock_input_division;
uint8_t clock_output_division;
uint8_t clock_bar_duration;
uint8_t clock_override;
int8_t custom_pitch_table[12];
uint8_t remote_control_channel;
uint8_t nudge_first_tick;
uint8_t clock_manual_start;
uint8_t padding[10];
};
enum MultiSetting {
MULTI_LAYOUT,
MULTI_CLOCK_TEMPO,
MULTI_CLOCK_SWING,
MULTI_CLOCK_INPUT_DIVISION,
MULTI_CLOCK_OUTPUT_DIVISION,
MULTI_CLOCK_BAR_DURATION,
MULTI_CLOCK_OVERRIDE,
MULTI_PITCH_1,
MULTI_PITCH_2,
MULTI_PITCH_3,
MULTI_PITCH_4,
MULTI_PITCH_5,
MULTI_PITCH_6,
MULTI_PITCH_7,
MULTI_PITCH_8,
MULTI_PITCH_9,
MULTI_PITCH_10,
MULTI_PITCH_11,
MULTI_PITCH_12,
MULTI_REMOTE_CONTROL_CHANNEL,
MULTI_CLOCK_NUDGE_FIRST_TICK,
MULTI_CLOCK_MANUAL_START,
};
enum Layout {
LAYOUT_MONO,
LAYOUT_DUAL_MONO,
LAYOUT_QUAD_MONO,
LAYOUT_DUAL_POLY,
LAYOUT_QUAD_POLY,
LAYOUT_DUAL_POLYCHAINED,
LAYOUT_QUAD_POLYCHAINED,
LAYOUT_OCTAL_POLYCHAINED,
LAYOUT_QUAD_TRIGGERS,
LAYOUT_QUAD_VOLTAGES,
LAYOUT_THREE_ONE,
LAYOUT_LAST
};
class Multi {
public:
Multi() { }
~Multi() { }
void Init(bool reset_calibration);
inline uint8_t paques() const {
return settings_.clock_tempo == 49 && \
settings_.clock_swing == 49 && \
settings_.clock_output_division == 3 && \
settings_.clock_bar_duration == 9;
}
bool NoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
layout_configurator_.RegisterNote(channel, note);
bool thru = true;
bool received = false;
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (part_[i].accepts(channel, note, velocity)) {
received = true;
thru = part_[i].NoteOn(channel, note, velocity) && thru;
}
}
if (received &&
!running() &&
internal_clock() &&
!settings_.clock_manual_start) {
// Start the arpeggiators.
Start(true);
}
stop_count_down_ = 0;
return thru;
}
bool NoteOff(uint8_t channel, uint8_t note, uint8_t velocity) {
bool thru = true;
bool has_notes = false;
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (part_[i].accepts(channel, note)) {
thru = part_[i].NoteOff(channel, note) && thru;
}
has_notes = has_notes || part_[i].has_notes();
}
if (!has_notes && internal_clock() && started_by_keyboard_) {
stop_count_down_ = 12;
}
return thru;
}
bool ControlChange(uint8_t channel, uint8_t controller, uint8_t value);
bool PitchBend(uint8_t channel, uint16_t pitch_bend) {
bool thru = true;
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (part_[i].accepts(channel)) {
thru = part_[i].PitchBend(channel, pitch_bend) && thru;
}
}
return thru;
}
bool Aftertouch(uint8_t channel, uint8_t note, uint8_t velocity) {
bool thru = true;
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (part_[i].accepts(channel, note)) {
thru = part_[i].Aftertouch(channel, note, velocity) && thru;
}
}
return thru;
}
bool Aftertouch(uint8_t channel, uint8_t velocity) {
bool thru = true;
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (part_[i].accepts(channel)) {
thru = part_[i].Aftertouch(channel, velocity) && thru;
}
}
return thru;
}
void Reset() {
for (uint8_t i = 0; i < num_active_parts_; ++i) {
part_[i].Reset();
}
}
void Clock();
// A start initiated by a MIDI 0xfa event or the front panel start button will
// start the sequencers. A start initiated by the keyboard will not start
// the sequencers, and give priority to the arpeggiator. This allows the
// arpeggiator to be played without erasing a sequence.
void Start(bool started_by_keyboard);
void Stop();
void Continue() {
Start(false);
}
void StartRecording(uint8_t part) {
if (!recording_) {
// Do not record while the arpeggiator is running!
if (started_by_keyboard_ && running()) {
Stop();
}
part_[part].StartRecording();
uint8_t channel = part_[part].midi_settings().channel;
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (part_[i].midi_settings().channel == channel || \
channel == 0x10 || \
part_[i].midi_settings().channel == 0x10) {
part_[i].set_transposable(false);
}
}
recording_ = true;
}
}
void StopRecording(uint8_t part) {
if (recording_) {
part_[part].StopRecording();
for (uint8_t i = 0; i < num_active_parts_; ++i) {
part_[i].set_transposable(true);
}
recording_ = false;
}
}
inline void Latch() {
if (!latched_) {
for (uint8_t i = 0; i < num_active_parts_; ++i) {
part_[i].Latch();
}
latched_ = true;
}
}
inline void Unlatch() {
if (latched_) {
for (uint8_t i = 0; i < num_active_parts_; ++i) {
part_[i].Unlatch();
}
latched_ = false;
}
}
void PushItNoteOn(uint8_t note) {
uint8_t mask = recording_ ? 0x80 : 0;
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (settings_.layout == LAYOUT_QUAD_TRIGGERS) {
note = part_[i].midi_settings().min_note;
}
if (!recording_ || part_[i].recording()) {
part_[i].NoteOn(part_[i].tx_channel() | mask, note, 127);
}
}
if (!running() && internal_clock()) {
// Start the arpeggiators.
Start(true);
}
}
void PushItNoteOff(uint8_t note) {
uint8_t mask = recording_ ? 0x80 : 0;
bool has_notes = false;
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (settings_.layout == LAYOUT_QUAD_TRIGGERS) {
note = part_[i].midi_settings().min_note;
}
if (!recording_ || part_[i].recording()) {
part_[i].NoteOff(part_[i].tx_channel() | mask, note);
}
has_notes = has_notes || part_[i].has_notes();
}
if (!has_notes && internal_clock()) {
Stop();
}
}
void Touch();
void Refresh();
void RefreshInternalClock() {
if (running() && internal_clock() && internal_clock_.Process()) {
++internal_clock_ticks_;
}
}
void ProcessInternalClockEvents() {
while (internal_clock_ticks_) {
Clock();
--internal_clock_ticks_;
}
}
inline void RenderAudio() {
for (uint8_t i = 0; i < kNumVoices; ++i) {
voice_[i].RenderAudio();
}
}
void Set(uint8_t address, uint8_t value);
inline uint8_t Get(uint8_t address) const {
const uint8_t* bytes;
bytes = static_cast<const uint8_t*>(static_cast<const void*>(&settings_));
return bytes[address];
}
inline Layout layout() const { return static_cast<Layout>(settings_.layout); }
inline bool internal_clock() const { return settings_.clock_tempo >= 40; }
inline uint8_t tempo() const { return settings_.clock_tempo; }
inline bool running() const { return running_; }
inline bool latched() const { return latched_; }
inline bool recording() const { return recording_; }
inline bool clock() const {
return clock_pulse_counter_ > 0 && \
(!settings_.nudge_first_tick || \
settings_.clock_bar_duration == 0 || \
!reset());
}
inline bool reset() const {
return reset_pulse_counter_ > 0;
}
inline bool reset_or_playing_flag() const {
return reset() || ((settings_.clock_bar_duration == 0) && running_);
}
inline const Part& part(uint8_t index) const { return part_[index]; }
inline const Voice& voice(uint8_t index) const { return voice_[index]; }
inline const MultiSettings& settings() const { return settings_; }
inline uint8_t num_active_parts() const { return num_active_parts_; }
inline Voice* mutable_voice(uint8_t index) { return &voice_[index]; }
inline Part* mutable_part(uint8_t index) { return &part_[index]; }
inline MultiSettings* mutable_settings() { return &settings_; }
void set_custom_pitch(uint8_t pitch_class, int8_t correction) {
settings_.custom_pitch_table[pitch_class] = correction;
}
// Returns true when no part does anything fancy with the MIDI stream (such
// as producing arpeggiated notes, or suppressing messages). This means that
// the MIDI dispatcher can just copy to the MIDI out a MIDI data byte as soon
// as it is received. Otherwise, merging and message reformatting will be
// necessary and the output stream will be delayed :(
inline bool direct_thru() const {
for (uint8_t i = 0; i < num_active_parts_; ++i) {
if (!part_[i].direct_thru()) {
return false;
}
}
return true;
}
void GetCvGate(uint16_t* cv, bool* gate);
bool GetAudioSource(uint8_t* audio_source);
void GetLedsBrightness(uint8_t* brightness);
template<typename T>
void Serialize(T* stream_buffer) {
stream_buffer->Write(settings());
for (uint8_t i = 0; i < kNumParts; ++i) {
stream_buffer->Write(part_[i].midi_settings());
stream_buffer->Write(part_[i].voicing_settings());
stream_buffer->Write(part_[i].sequencer_settings());
}
};
template<typename T>
void Deserialize(T* stream_buffer) {
Stop();
stream_buffer->Read(mutable_settings());
for (uint8_t i = 0; i < kNumParts; ++i) {
stream_buffer->Read(part_[i].mutable_midi_settings());
stream_buffer->Read(part_[i].mutable_voicing_settings());
stream_buffer->Read(part_[i].mutable_sequencer_settings());
}
Touch();
};
template<typename T>
void SerializeCalibration(T* stream_buffer) {
for (uint8_t i = 0; i < kNumVoices; ++i) {
for (uint8_t j = 0; j < kNumOctaves; ++j) {
stream_buffer->Write(voice_[i].calibration_dac_code(j));
}
}
};
template<typename T>
void DeserializeCalibration(T* stream_buffer) {
for (uint8_t i = 0; i < kNumVoices; ++i) {
for (uint8_t j = 0; j < kNumOctaves; ++j) {
uint16_t v;
stream_buffer->Read(&v);
voice_[i].set_calibration_dac_code(j, v);
}
}
};
void StartLearning() {
layout_configurator_.StartLearning();
}
void StopLearning() {
layout_configurator_.StopLearning(this);
}
inline bool learning() const {
return layout_configurator_.learning();
}
void StartSong();
private:
void ChangeLayout(Layout old_layout, Layout new_layout);
void UpdateLayout();
void ClockSong();
void HandleRemoteControlCC(uint8_t controller, uint8_t value);
MultiSettings settings_;
bool running_;
bool started_by_keyboard_;
bool latched_;
bool recording_;
InternalClock internal_clock_;
uint8_t internal_clock_ticks_;
uint16_t midi_clock_tick_duration_;
int16_t swing_predelay_[12];
uint8_t swing_counter_;
uint8_t clock_input_prescaler_;
uint8_t clock_output_prescaler_;
uint16_t bar_position_;
uint8_t stop_count_down_;
uint16_t clock_pulse_counter_;
uint16_t reset_pulse_counter_;
uint16_t previous_output_division_;
bool needs_resync_;
// Indicates that a setting has been changed and that the multi should
// be saved in memory.
bool dirty_;
uint8_t num_active_parts_;
Part part_[kNumParts];
Voice voice_[kNumVoices];
LayoutConfigurator layout_configurator_;
const uint8_t* song_pointer_;
uint32_t song_clock_;
uint8_t song_delta_;
DISALLOW_COPY_AND_ASSIGN(Multi);
};
extern Multi multi;
} // namespace yarns
#endif // YARNS_MULTI_H_