-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscenes.h
More file actions
760 lines (693 loc) · 26.3 KB
/
Copy pathscenes.h
File metadata and controls
760 lines (693 loc) · 26.3 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
#pragma once
#include <stdint.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <type_traits>
#include <utility>
#include "json_evented.h"
#include "src/dsp/mini_tb303.h"
#include "src/miniacid_config.h"
#if defined(MINIACID_SCENE_DEBUG) && defined(ARDUINO)
#include <Arduino.h>
#define SCENE_DEBUG_PRINTF(...) Serial.printf(__VA_ARGS__)
#define SCENE_DEBUG_PRINTLN(msg) Serial.println(msg)
#else
#define SCENE_DEBUG_PRINTF(...) do {} while (0)
#define SCENE_DEBUG_PRINTLN(msg) do {} while (0)
#endif
namespace scene_json_detail {
inline bool writeChunk(std::string& writer, const char* data, size_t len) {
writer.append(data, len);
return true;
}
template <typename Writer>
auto writeChunkImpl(Writer& writer, const char* data, size_t len, int)
-> decltype(writer.write(reinterpret_cast<const uint8_t*>(data), len), bool()) {
size_t written = writer.write(reinterpret_cast<const uint8_t*>(data), len);
return written == len;
}
template <typename Writer>
auto writeChunkImpl(Writer& writer, const char* data, size_t len, long)
-> decltype(writer.write(data, len), bool()) {
auto written = writer.write(data, len);
return static_cast<size_t>(written) == len;
}
template <typename Writer>
auto writeChunkImpl(Writer& writer, const char* data, size_t len, ...)
-> decltype(writer.write(static_cast<uint8_t>(0)), bool()) {
for (size_t i = 0; i < len; ++i) {
if (writer.write(static_cast<uint8_t>(data[i])) != 1) return false;
}
return true;
}
template <typename Writer>
bool writeChunk(Writer& writer, const char* data, size_t len) {
return writeChunkImpl(writer, data, len, 0);
}
} // namespace scene_json_detail
struct DrumStep {
bool hit;
};
static constexpr int kSequencerSteps = 16;
static constexpr int kSceneJsonVersion = 1;
struct DrumPattern {
static constexpr int kSteps = kSequencerSteps;
DrumStep steps[kSteps];
};
struct SynthStep {
int note;
bool slide;
bool accent;
};
static constexpr int kAutomationMaxX = kSequencerSteps + 1;
static constexpr int kAutomationMaxNodes = MINIACID_AUTOMATION_MAX_NODES;
static constexpr int kAutomationPoolNodes = MINIACID_AUTOMATION_POOL_NODES;
static constexpr int kAutomationMaxOptions = 8;
static constexpr int kAutomationOptionLabelMaxLen = 12;
struct AutomationNode {
uint8_t x;
uint8_t y;
};
static constexpr uint16_t kAutomationInvalidIndex = 0xFFFF;
struct AutomationLane {
// Nodes live in a shared pool to avoid storing large arrays in every pattern.
uint16_t start = kAutomationInvalidIndex;
uint16_t capacity = 0;
uint8_t nodeCount = 0;
bool enabled = false;
uint8_t optionCount = 0;
char* optionLabels = nullptr;
AutomationLane() = default;
AutomationLane(const AutomationLane& other);
AutomationLane& operator=(const AutomationLane& other);
~AutomationLane();
bool hasOptions() const;
bool hasNodes() const;
void clear();
uint8_t evaluate(float t) const;
AutomationNode* nodes();
const AutomationNode* nodes() const;
bool ensureCapacity(int needed);
void clearOptions();
void setOptions(const char* const* labels, int count);
void setOptionLabel(int index, const char* label);
const char* optionLabelAt(int index) const;
int clampOptionIndex(int index) const;
};
enum class DrumAutomationParamId : uint8_t {
DrumEngine = 0,
Count
};
struct DrumPatternSet {
static constexpr int kVoices = 8;
DrumPattern voices[kVoices];
bool accents[DrumPattern::kSteps]{};
AutomationLane automation[static_cast<int>(DrumAutomationParamId::Count)];
bool hasAutomationLane(DrumAutomationParamId id) const;
const AutomationLane* automationLane(DrumAutomationParamId id) const;
AutomationLane* editAutomationLane(DrumAutomationParamId id);
void clearAutomationLane(DrumAutomationParamId id);
};
struct SynthPattern {
static constexpr int kSteps = kSequencerSteps;
SynthStep steps[kSteps];
AutomationLane automation[static_cast<int>(TB303ParamId::Count)];
bool hasAutomationLane(TB303ParamId id) const;
const AutomationLane* automationLane(TB303ParamId id) const;
AutomationLane* editAutomationLane(TB303ParamId id);
void clearAutomationLane(TB303ParamId id);
};
struct SynthParameters {
float cutoff = 800.0f;
float resonance = 0.6f;
float envAmount = 400.0f;
float envDecay = 420.0f;
int oscType = 0;
};
enum class SongTrack : uint8_t {
SynthA = 0,
SynthB = 1,
Drums = 2,
};
struct SongPosition {
static constexpr int kTrackCount = 3;
int8_t patterns[kTrackCount] = {-1, -1, -1};
};
struct Song {
static constexpr int kMaxPositions = 128;
SongPosition positions[kMaxPositions];
int length = 1;
};
template <typename PatternType>
struct Bank {
static constexpr int kPatterns = 8;
PatternType patterns[kPatterns];
};
static constexpr int kBankCount = 4;
static constexpr int kSongPatternCount = kBankCount * Bank<SynthPattern>::kPatterns;
inline int clampSongPatternIndex(int idx) {
if (idx < -1) return -1;
int max = kSongPatternCount - 1;
if (idx > max) return max;
return idx;
}
inline int songPatternBank(int songPatternIdx) {
if (songPatternIdx < 0) return -1;
return songPatternIdx / Bank<SynthPattern>::kPatterns;
}
inline int songPatternIndexInBank(int songPatternIdx) {
if (songPatternIdx < 0) return -1;
return songPatternIdx % Bank<SynthPattern>::kPatterns;
}
inline int songPatternFromBank(int bankIndex, int patternIndex) {
if (bankIndex < 0 || patternIndex < 0) return -1;
if (bankIndex >= kBankCount) bankIndex = kBankCount - 1;
if (patternIndex >= Bank<SynthPattern>::kPatterns) {
patternIndex = Bank<SynthPattern>::kPatterns - 1;
}
return bankIndex * Bank<SynthPattern>::kPatterns + patternIndex;
}
struct Scene {
Bank<DrumPatternSet> drumBanks[kBankCount];
Bank<SynthPattern> synthABanks[kBankCount];
Bank<SynthPattern> synthBBanks[kBankCount];
Song song;
};
class SceneJsonObserver : public JsonObserver {
public:
explicit SceneJsonObserver(Scene& scene, float defaultBpm = 100.0f);
void onObjectStart() override;
void onObjectEnd() override;
void onArrayStart() override;
void onArrayEnd() override;
void onNumber(int value) override;
void onNumber(double value) override;
void onBool(bool value) override;
void onNull() override;
void onString(const std::string& value) override;
void onObjectKey(const std::string& key) override;
void onObjectValueStart() override;
void onObjectValueEnd() override;
bool hadError() const;
int drumPatternIndex() const;
int synthPatternIndex(int synthIdx) const;
int drumBankIndex() const;
int synthBankIndex(int synthIdx) const;
bool drumMute(int idx) const;
bool synthMute(int idx) const;
bool synthDistortionEnabled(int idx) const;
bool synthDelayEnabled(int idx) const;
const SynthParameters& synthParameters(int synthIdx) const;
float bpm() const;
const Song& song() const;
bool hasSong() const;
bool songMode() const;
int songPosition() const;
bool loopMode() const;
int loopStartRow() const;
int loopEndRow() const;
const std::string& drumEngineName() const;
private:
enum class Path {
Root,
DrumBanks,
DrumBank,
DrumPatternSet,
DrumVoice,
DrumHitArray,
DrumAccentArray,
DrumAutomation,
DrumAutomationLane,
DrumAutomationNodes,
DrumAutomationOptions,
DrumAutomationNode,
SynthABanks,
SynthABank,
SynthBBanks,
SynthBBank,
SynthPattern,
SynthSteps,
SynthStep,
SynthAutomation,
SynthAutomationLane,
SynthAutomationNodes,
SynthAutomationOptions,
SynthAutomationNode,
State,
SynthPatternIndex,
SynthBankIndex,
Mute,
MuteDrums,
MuteSynth,
SynthDistortion,
SynthDelay,
SynthParams,
SynthParam,
Song,
SongPositions,
SongPosition,
Unknown,
};
struct Context {
enum class Type { Object, Array };
Type type;
Path path;
int index;
};
Path deduceArrayPath(const Context& parent) const;
Path deduceObjectPath(const Context& parent) const;
int currentIndexFor(Path path) const;
bool inSynthBankA() const;
bool inSynthBankB() const;
static const char* pathName(Path path);
void pushContext(Context::Type type, Path path);
void popContext();
void handlePrimitiveNumber(double value, bool isInteger);
void handlePrimitiveBool(bool value);
static constexpr int kMaxStack = 16;
Context stack_[kMaxStack];
int stackSize_ = 0;
std::string lastKey_;
Scene& target_;
bool error_ = false;
int drumPatternIndex_ = 0;
int synthPatternIndex_[2] = {0, 0};
int drumBankIndex_ = 0;
int synthBankIndex_[2] = {0, 0};
bool drumMute_[DrumPatternSet::kVoices] = {false, false, false, false, false, false, false, false};
bool synthMute_[2] = {false, false};
bool synthDistortion_[2] = {false, false};
bool synthDelay_[2] = {false, false};
SynthParameters synthParameters_[2];
float bpm_ = 100.0f;
Song song_;
bool hasSong_ = false;
bool songMode_ = false;
int songPosition_ = 0;
bool loopMode_ = false;
int loopStartRow_ = 0;
int loopEndRow_ = 0;
std::string drumEngineName_ = "808";
int automationParam_ = -1;
bool automationEnabled_ = true;
bool automationNodeHasX_ = false;
bool automationNodeHasY_ = false;
int automationNodeX_ = 0;
int automationNodeY_ = 0;
int drumAutomationParam_ = -1;
bool drumAutomationEnabled_ = true;
bool drumAutomationNodeHasX_ = false;
bool drumAutomationNodeHasY_ = false;
int drumAutomationNodeX_ = 0;
int drumAutomationNodeY_ = 0;
};
class SceneManager {
public:
void loadDefaultScene();
Scene& currentScene();
const Scene& currentScene() const;
const DrumPatternSet& getCurrentDrumPattern() const;
DrumPatternSet& editCurrentDrumPattern();
const SynthPattern& getCurrentSynthPattern(int synthIndex) const;
SynthPattern& editCurrentSynthPattern(int synthIndex);
const SynthPattern& getSynthPattern(int synthIndex, int patternIndex) const;
SynthPattern& editSynthPattern(int synthIndex, int patternIndex);
const DrumPatternSet& getDrumPatternSet(int patternIndex) const;
DrumPatternSet& editDrumPatternSet(int patternIndex);
void setCurrentDrumPatternIndex(int idx);
void setCurrentSynthPatternIndex(int synthIdx, int idx);
int getCurrentDrumPatternIndex() const;
int getCurrentSynthPatternIndex(int synthIdx) const;
void setCurrentBankIndex(int instrumentId, int bankIdx);
int getCurrentBankIndex(int instrumentId) const;
void setDrumStep(int voiceIdx, int step, bool hit);
void setSynthStep(int synthIdx, int step, int note, bool slide, bool accent);
std::string dumpCurrentScene() const;
bool loadScene(const std::string& json);
void setDrumMute(int voiceIdx, bool mute);
bool getDrumMute(int voiceIdx) const;
void setSynthMute(int synthIdx, bool mute);
bool getSynthMute(int synthIdx) const;
void setSynthDistortionEnabled(int synthIdx, bool enabled);
bool getSynthDistortionEnabled(int synthIdx) const;
void setSynthDelayEnabled(int synthIdx, bool enabled);
bool getSynthDelayEnabled(int synthIdx) const;
void setSynthParameters(int synthIdx, const SynthParameters& params);
const SynthParameters& getSynthParameters(int synthIdx) const;
void setDrumEngineName(const std::string& name);
const std::string& getDrumEngineName() const;
void setBpm(float bpm);
float getBpm() const;
const Song& song() const;
Song& editSong();
void setSongPattern(int position, SongTrack track, int patternIndex);
void clearSongPattern(int position, SongTrack track);
int songPattern(int position, SongTrack track) const;
void setSongLength(int length);
int songLength() const;
void setSongPosition(int position);
int getSongPosition() const;
void setSongMode(bool enabled);
bool songMode() const;
void setLoopMode(bool enabled);
bool loopMode() const;
void setLoopRange(int startRow, int endRow);
int loopStartRow() const;
int loopEndRow() const;
template <typename TWriter>
bool writeSceneJson(TWriter&& writer) const;
template <typename TReader>
bool loadSceneEvented(TReader&& reader);
// static constexpr size_t sceneJsonCapacity();
private:
int clampPatternIndex(int idx) const;
int clampBankIndex(int idx) const;
int clampSynthIndex(int idx) const;
int clampSongPosition(int position) const;
int clampSongLength(int length) const;
int songTrackToIndex(SongTrack track) const;
void trimSongLength();
void clampLoopRange();
void clearSongData(Song& song) const;
bool loadSceneEventedWithReader(JsonVisitor::NextChar nextChar);
Scene scene_;
int drumPatternIndex_ = 0;
int synthPatternIndex_[2] = {0, 0};
int drumBankIndex_ = 0;
int synthBankIndex_[2] = {0, 0};
bool drumMute_[DrumPatternSet::kVoices] = {false, false, false, false, false, false, false, false};
bool synthMute_[2] = {false, false};
bool synthDistortion_[2] = {false, false};
bool synthDelay_[2] = {false, false};
SynthParameters synthParameters_[2];
float bpm_ = 100.0f;
bool songMode_ = false;
int songPosition_ = 0;
bool loopMode_ = false;
int loopStartRow_ = 0;
int loopEndRow_ = 0;
std::string drumEngineName_ = "808";
};
// inline constexpr size_t SceneManager::sceneJsonCapacity() {
// constexpr size_t drumPatternJsonSize = JSON_OBJECT_SIZE(2) + 2 * JSON_ARRAY_SIZE(DrumPattern::kSteps);
// constexpr size_t drumPatternSetJsonSize = JSON_ARRAY_SIZE(DrumPatternSet::kVoices) +
// DrumPatternSet::kVoices * drumPatternJsonSize;
// constexpr size_t drumBankJsonSize = JSON_ARRAY_SIZE(Bank<DrumPatternSet>::kPatterns) +
// Bank<DrumPatternSet>::kPatterns * drumPatternSetJsonSize;
// constexpr size_t synthStepJsonSize = JSON_OBJECT_SIZE(3);
// constexpr size_t synthPatternJsonSize = JSON_ARRAY_SIZE(SynthPattern::kSteps) +
// SynthPattern::kSteps * synthStepJsonSize;
// constexpr size_t synthBankJsonSize = JSON_ARRAY_SIZE(Bank<SynthPattern>::kPatterns) +
// Bank<SynthPattern>::kPatterns * synthPatternJsonSize;
// constexpr size_t stateJsonSize = JSON_OBJECT_SIZE(4) + JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(2);
// return JSON_OBJECT_SIZE(4) + drumBankJsonSize + synthBankJsonSize * 2 + stateJsonSize + 64; // small headroom
// }
template <typename TWriter>
bool SceneManager::writeSceneJson(TWriter&& writer) const {
using WriterType = typename std::remove_reference<TWriter>::type;
WriterType& out = writer;
auto writeChunk = [&](const char* data, size_t len) -> bool {
return scene_json_detail::writeChunk(out, data, len);
};
auto writeLiteral = [&](const char* literal) -> bool {
return writeChunk(literal, std::strlen(literal));
};
auto writeChar = [&](char c) -> bool {
return writeChunk(&c, 1);
};
auto writeBool = [&](bool value) -> bool {
return writeLiteral(value ? "true" : "false");
};
auto writeInt = [&](int value) -> bool {
char buffer[16];
int written = std::snprintf(buffer, sizeof(buffer), "%d", value);
if (written < 0 || written >= static_cast<int>(sizeof(buffer))) return false;
return writeChunk(buffer, static_cast<size_t>(written));
};
auto writeFloat = [&](float value) -> bool {
char buffer[24];
int written = std::snprintf(buffer, sizeof(buffer), "%.6g", static_cast<double>(value));
if (written < 0 || written >= static_cast<int>(sizeof(buffer))) return false;
return writeChunk(buffer, static_cast<size_t>(written));
};
auto writeBoolArray = [&](const bool* values, int count) -> bool {
for (int i = 0; i < count; ++i) {
if (i > 0 && !writeChar(',')) return false;
if (!writeBool(values[i])) return false;
}
return true;
};
auto writeString = [&](const std::string& value) -> bool {
if (!writeChar('"')) return false;
for (char ch : value) {
if (ch == '"' || ch == '\\') {
if (!writeChar('\\')) return false;
}
if (!writeChar(ch)) return false;
}
return writeChar('"');
};
auto writeCString = [&](const char* value) -> bool {
if (!value) value = "";
if (!writeChar('"')) return false;
for (const char* ptr = value; *ptr; ++ptr) {
char ch = *ptr;
if (ch == '"' || ch == '\\') {
if (!writeChar('\\')) return false;
}
if (!writeChar(ch)) return false;
}
return writeChar('"');
};
auto writeDrumPattern = [&](const DrumPattern& pattern) -> bool {
if (!writeLiteral("{\"hit\":[")) return false;
for (int i = 0; i < DrumPattern::kSteps; ++i) {
if (i > 0 && !writeChar(',')) return false;
if (!writeBool(pattern.steps[i].hit)) return false;
}
return writeLiteral("]}");
};
auto writeDrumBank = [&](const Bank<DrumPatternSet>& bank) -> bool {
if (!writeChar('[')) return false;
for (int p = 0; p < Bank<DrumPatternSet>::kPatterns; ++p) {
if (p > 0 && !writeChar(',')) return false;
if (!writeLiteral("{\"voices\":[")) return false;
for (int v = 0; v < DrumPatternSet::kVoices; ++v) {
if (v > 0 && !writeChar(',')) return false;
if (!writeDrumPattern(bank.patterns[p].voices[v])) return false;
}
if (!writeLiteral("],\"accent\":[")) return false;
for (int i = 0; i < DrumPattern::kSteps; ++i) {
if (i > 0 && !writeChar(',')) return false;
if (!writeBool(bank.patterns[p].accents[i])) return false;
}
if (!writeChar(']')) return false;
if (!writeLiteral(",\"automation\":[")) return false;
bool wroteLane = false;
for (int a = 0; a < static_cast<int>(DrumAutomationParamId::Count); ++a) {
const AutomationLane& lane = bank.patterns[p].automation[a];
if (!lane.enabled && lane.nodeCount == 0) continue;
if (wroteLane && !writeChar(',')) return false;
wroteLane = true;
if (!writeLiteral("{\"param\":")) return false;
if (!writeInt(a)) return false;
if (!writeLiteral(",\"enabled\":")) return false;
if (!writeBool(lane.enabled)) return false;
if (lane.optionCount > 0) {
if (!writeLiteral(",\"options\":[")) return false;
for (int i = 0; i < lane.optionCount; ++i) {
if (i > 0 && !writeChar(',')) return false;
const char* label = lane.optionLabelAt(i);
if (!writeCString(label)) return false;
}
if (!writeChar(']')) return false;
}
if (!writeLiteral(",\"nodes\":[")) return false;
const AutomationNode* laneNodes = lane.nodes();
for (int n = 0; n < lane.nodeCount; ++n) {
if (n > 0 && !writeChar(',')) return false;
if (!writeLiteral("{\"x\":")) return false;
if (!laneNodes || !writeInt(laneNodes[n].x)) return false;
if (!writeLiteral(",\"y\":")) return false;
if (!writeInt(laneNodes[n].y)) return false;
if (!writeChar('}')) return false;
}
if (!writeChar(']')) return false;
if (!writeChar('}')) return false;
}
if (!writeChar(']')) return false;
if (!writeChar('}')) return false;
}
return writeChar(']');
};
auto writeDrumBanks = [&](const Bank<DrumPatternSet>* banks) -> bool {
if (!writeChar('[')) return false;
for (int b = 0; b < kBankCount; ++b) {
if (b > 0 && !writeChar(',')) return false;
if (!writeDrumBank(banks[b])) return false;
}
return writeChar(']');
};
auto writeSynthPattern = [&](const SynthPattern& pattern) -> bool {
if (!writeLiteral("{\"steps\":[")) return false;
for (int i = 0; i < SynthPattern::kSteps; ++i) {
if (i > 0 && !writeChar(',')) return false;
if (!writeLiteral("{\"note\":")) return false;
if (!writeInt(pattern.steps[i].note)) return false;
if (!writeLiteral(",\"slide\":")) return false;
if (!writeBool(pattern.steps[i].slide)) return false;
if (!writeLiteral(",\"accent\":")) return false;
if (!writeBool(pattern.steps[i].accent)) return false;
if (!writeChar('}')) return false;
}
if (!writeLiteral("],\"automation\":[")) return false;
bool wroteLane = false;
for (int p = 0; p < static_cast<int>(TB303ParamId::Count); ++p) {
const AutomationLane& lane = pattern.automation[p];
if (!lane.enabled && lane.nodeCount == 0) continue;
if (wroteLane && !writeChar(',')) return false;
wroteLane = true;
if (!writeLiteral("{\"param\":")) return false;
if (!writeInt(p)) return false;
if (!writeLiteral(",\"enabled\":")) return false;
if (!writeBool(lane.enabled)) return false;
if (lane.optionCount > 0) {
if (!writeLiteral(",\"options\":[")) return false;
for (int i = 0; i < lane.optionCount; ++i) {
if (i > 0 && !writeChar(',')) return false;
const char* label = lane.optionLabelAt(i);
if (!writeCString(label)) return false;
}
if (!writeChar(']')) return false;
}
if (!writeLiteral(",\"nodes\":[")) return false;
const AutomationNode* laneNodes = lane.nodes();
for (int n = 0; n < lane.nodeCount; ++n) {
if (n > 0 && !writeChar(',')) return false;
if (!writeLiteral("{\"x\":")) return false;
if (!laneNodes || !writeInt(laneNodes[n].x)) return false;
if (!writeLiteral(",\"y\":")) return false;
if (!writeInt(laneNodes[n].y)) return false;
if (!writeChar('}')) return false;
}
if (!writeChar(']')) return false;
if (!writeChar('}')) return false;
}
if (!writeChar(']')) return false;
return writeChar('}');
};
auto writeSynthBank = [&](const Bank<SynthPattern>& bank) -> bool {
if (!writeChar('[')) return false;
for (int p = 0; p < Bank<SynthPattern>::kPatterns; ++p) {
if (p > 0 && !writeChar(',')) return false;
if (!writeSynthPattern(bank.patterns[p])) return false;
}
return writeChar(']');
};
auto writeSynthBanks = [&](const Bank<SynthPattern>* banks) -> bool {
if (!writeChar('[')) return false;
for (int b = 0; b < kBankCount; ++b) {
if (b > 0 && !writeChar(',')) return false;
if (!writeSynthBank(banks[b])) return false;
}
return writeChar(']');
};
if (!writeChar('{')) return false;
if (!writeLiteral("\"version\":")) return false;
if (!writeInt(kSceneJsonVersion)) return false;
if (!writeLiteral(",\"drumBanks\":")) return false;
if (!writeDrumBanks(scene_.drumBanks)) return false;
if (!writeLiteral(",\"synthABanks\":")) return false;
if (!writeSynthBanks(scene_.synthABanks)) return false;
if (!writeLiteral(",\"synthBBanks\":")) return false;
if (!writeSynthBanks(scene_.synthBBanks)) return false;
if (!writeLiteral(",\"song\":{")) return false;
int songLen = songLength();
if (!writeLiteral("\"length\":")) return false;
if (!writeInt(songLen)) return false;
if (!writeLiteral(",\"positions\":[")) return false;
for (int i = 0; i < songLen; ++i) {
if (i > 0 && !writeChar(',')) return false;
if (!writeChar('{')) return false;
if (!writeLiteral("\"a\":")) return false;
if (!writeInt(scene_.song.positions[i].patterns[0])) return false;
if (!writeLiteral(",\"b\":")) return false;
if (!writeInt(scene_.song.positions[i].patterns[1])) return false;
if (!writeLiteral(",\"drums\":")) return false;
if (!writeInt(scene_.song.positions[i].patterns[2])) return false;
if (!writeChar('}')) return false;
}
if (!writeChar(']')) return false;
if (!writeChar('}')) return false;
if (!writeLiteral(",\"state\":{")) return false;
if (!writeLiteral("\"drumPatternIndex\":")) return false;
if (!writeInt(drumPatternIndex_)) return false;
if (!writeLiteral(",\"bpm\":")) return false;
if (!writeFloat(bpm_)) return false;
if (!writeLiteral(",\"songMode\":")) return false;
if (!writeBool(songMode_)) return false;
if (!writeLiteral(",\"songPosition\":")) return false;
if (!writeInt(clampSongPosition(songPosition_))) return false;
if (!writeLiteral(",\"loopMode\":")) return false;
if (!writeBool(loopMode_)) return false;
if (!writeLiteral(",\"loopStart\":")) return false;
if (!writeInt(loopStartRow_)) return false;
if (!writeLiteral(",\"loopEnd\":")) return false;
if (!writeInt(loopEndRow_)) return false;
if (!writeLiteral(",\"synthPatternIndex\":[")) return false;
if (!writeInt(synthPatternIndex_[0])) return false;
if (!writeChar(',')) return false;
if (!writeInt(synthPatternIndex_[1])) return false;
if (!writeChar(']')) return false;
if (!writeLiteral(",\"drumBankIndex\":")) return false;
if (!writeInt(drumBankIndex_)) return false;
if (!writeLiteral(",\"drumEngine\":")) return false;
if (!writeString(drumEngineName_)) return false;
if (!writeLiteral(",\"synthBankIndex\":[")) return false;
if (!writeInt(synthBankIndex_[0])) return false;
if (!writeChar(',')) return false;
if (!writeInt(synthBankIndex_[1])) return false;
if (!writeChar(']')) return false;
if (!writeLiteral(",\"mute\":{")) return false;
if (!writeLiteral("\"drums\":[")) return false;
if (!writeBoolArray(drumMute_, DrumPatternSet::kVoices)) return false;
if (!writeLiteral("],\"synth\":[")) return false;
if (!writeBoolArray(synthMute_, 2)) return false;
if (!writeChar(']')) return false;
if (!writeChar('}')) return false;
if (!writeLiteral(",\"synthParams\":[")) return false;
for (int i = 0; i < 2; ++i) {
if (i > 0 && !writeChar(',')) return false;
if (!writeLiteral("{\"cutoff\":")) return false;
if (!writeFloat(synthParameters_[i].cutoff)) return false;
if (!writeLiteral(",\"resonance\":")) return false;
if (!writeFloat(synthParameters_[i].resonance)) return false;
if (!writeLiteral(",\"envAmount\":")) return false;
if (!writeFloat(synthParameters_[i].envAmount)) return false;
if (!writeLiteral(",\"envDecay\":")) return false;
if (!writeFloat(synthParameters_[i].envDecay)) return false;
if (!writeLiteral(",\"oscType\":")) return false;
if (!writeInt(synthParameters_[i].oscType)) return false;
if (!writeChar('}')) return false;
}
if (!writeChar(']')) return false;
if (!writeLiteral(",\"synthDistortion\":[")) return false;
if (!writeBool(synthDistortion_[0])) return false;
if (!writeChar(',')) return false;
if (!writeBool(synthDistortion_[1])) return false;
if (!writeChar(']')) return false;
if (!writeLiteral(",\"synthDelay\":[")) return false;
if (!writeBool(synthDelay_[0])) return false;
if (!writeChar(',')) return false;
if (!writeBool(synthDelay_[1])) return false;
if (!writeChar(']')) return false;
if (!writeChar('}')) return false;
if (!writeChar('}')) return false;
return true;
}
template <typename TReader>
bool SceneManager::loadSceneEvented(TReader&& reader) {
JsonVisitor::NextChar nextChar = [&reader]() -> int { return reader.read(); };
return loadSceneEventedWithReader(nextChar);
}