Repository navigation
Expand file tree
/
Copy pathSF2Parser.cpp
More file actions
757 lines (642 loc) · 26.7 KB
/
Copy pathSF2Parser.cpp
File metadata and controls
757 lines (642 loc) · 26.7 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
/*
* ----------------------------------------------------------------------------
* ESP32-S3 SF2 Synthesizer Firmware
*
* Description:
* Real-time SF2 (SoundFont) compatible wavetable synthesizer with USB MIDI, I2S audio,
* multi-layer voice allocation, per-channel filters, reverb, chorus and delay.
* GM/GS/XG support is partly implemented
*
* Hardware:
* - ESP32-S3 with PSRAM
* - I2S DAC output (44100Hz stereo, 16-bit PCM)
* - USB MIDI input
* - Optional SD card and/or LittleFS
*
* Author: Evgeny Aslovskiy AKA Copych
* License: MIT
* Repository: https://github.com/copych/ESP32-S3_SF2_Sampler_Synthesizer
*
* File: SF2Parser.cpp
* Purpose: Implementation of SF2 file parser.
* ----------------------------------------------------------------------------
*/
#include "SF2Parser.h"
#include "esp_log.h"
#include "operators.h"
static const char* TAG = "SF2Parser";
static float timecentsToSec(int tc) {
if (tc <= -32768) return 0.0f;
return powf(2.0f, tc * 8.3333333e-04f); // timecents → seconds ( 1 / 1200 )
}
static float centsToHz(int cents) {
return 8.176f * powf(2.0f, cents * 8.3333333e-04f); // cents → Hz ( 1 / 1200 )
}
inline float dB_to_linear(float dB) {
return exp2f(dB * 0.050025f); // ≈ dB / 20 * log2(10)
}
//struct PHDR { char name[20]; uint16_t preset, bank, bagIndex; uint32_t dummy[5]; };
struct __attribute__((packed)) PHDR {
char name[20];
uint16_t preset;
uint16_t bank;
uint16_t bagIndex;
uint32_t library;
uint32_t genre;
uint32_t morphology;
};
struct PBAG { uint16_t genIndex, modIndex; };
struct PGEN { uint16_t oper; int16_t amount; };
struct INST { char name[20]; uint16_t bagIndex; };
struct IBAG { uint16_t genIndex, modIndex; };
struct IGEN { uint16_t oper; int16_t amount; };
void decodeGeneratorAmount(Generator& gen, uint16_t raw) {
auto op = static_cast<GeneratorOperator>(gen.oper);
switch (op) {
case GeneratorOperator::Instrument:
case GeneratorOperator::SampleID:
case GeneratorOperator::SampleModes:
case GeneratorOperator::ExclusiveClass:
case GeneratorOperator::OverridingRootKey:
gen.amount.uAmount = raw;
break;
case GeneratorOperator::KeyRange:
case GeneratorOperator::VelRange:
gen.amount.range.lo = raw & 0xFF;
gen.amount.range.hi = (raw >> 8) & 0xFF;
break;
default:
gen.amount.sAmount = static_cast<int16_t>(raw);
break;
}
}
SF2Parser::SF2Parser(const char* path, fs::FS* fs) : filepath(path), filesystem(fs) {}
bool SF2Parser::parse() {
clear();
//file = LittleFS.open(filepath, "r");
file = filesystem->open(filepath, "r");
if (!file){
ESP_LOGE(TAG, "Error: File not found");
return false;
}
if (!parseHeaderChunks()) {
ESP_LOGE(TAG, "Error: Invalid SF2 format");
return false;
} else {
ESP_LOGI(TAG, "RIFF OK");
}
if (!parseSDTA()){
ESP_LOGE(TAG, "Error: Invalid SF2 sample data");
return false;
} else {
ESP_LOGI(TAG, "SDTA OK");
}
if (!parsePDTA()) {
ESP_LOGE(TAG, "PDTA parse failed");
return false;
} else {
ESP_LOGI(TAG, "PDTA OK");
}
if (!loadSampleDataToMemory()) {
ESP_LOGE(TAG, "Failed to load all sample data into memory, some samples may not play");
//optionally bind all absent samples to the first sample
//return false;
} else {
ESP_LOGI(TAG, "Memory load OK");
}
file.close();
return true;
}
void SF2Parser::seekTo(uint32_t offset) {
file.seek(offset, SeekSet);
}
bool SF2Parser::parseHeaderChunks() {
seekTo(0);
char riff[5] = {0}, sfbk[5] = {0};
file.readBytes(riff, 4);
uint32_t totalSize;
file.readBytes((char*)&totalSize, 4);
file.readBytes(sfbk, 4);
if (strncmp(riff, "RIFF", 4) != 0 || strncmp(sfbk, "sfbk", 4) != 0) {
ESP_LOGE(TAG, "Invalid SF2 file");
return false;
}
int steps = 0;
while (file.available() && steps++ < 100) {
char id[5] = {0};
file.readBytes(id, 4);
uint32_t size;
file.readBytes((char*)&size, 4);
if (strncmp(id, "LIST", 4) == 0) {
char listType[5] = {0};
file.readBytes(listType, 4);
ESP_LOGD(TAG, "LIST Type: %.*s", 4, listType);
if (strncmp(listType, "sdta", 4) == 0) {
sdtaOffset = file.position();
sdtaSize = size - 4;
file.seek(sdtaSize, SeekCur);
} else if (strncmp(listType, "pdta", 4) == 0) {
pdtaOffset = file.position();
pdtaSize = size - 4;
ESP_LOGI(TAG, "pdta found! offset: %lu size: %lu", pdtaOffset, pdtaSize);
return true;
} else {
if (size < 4) break;
file.seek(size - 4, SeekCur);
}
} else {
file.seek(size, SeekCur);
}
}
ESP_LOGE(TAG, "pdta not found");
return false;
}
bool SF2Parser::parseSDTA() {
seekTo(sdtaOffset);
char id[5] = {0};
file.readBytes(id, 4);
return (strncmp(id, "smpl", 4) == 0);
}
std::vector<SampleHeader>& SF2Parser::getSamples() {
return samples;
}
bool SF2Parser::parsePDTA() {
ESP_LOGI(TAG, "Parsing PDTA...");
seekTo(pdtaOffset);
uint32_t pdtaEnd = pdtaOffset + pdtaSize;
std::vector<PHDR> phdrs;
std::vector<PBAG> pbags;
std::vector<PGEN> pgens;
std::vector<INST> insts;
std::vector<IBAG> ibags;
std::vector<IGEN> igens;
while (file.position() + 8 <= pdtaEnd) {
uint32_t chunkStart = file.position();
char id[5] = {0};
file.readBytes(id, 4);
uint32_t size = 0;
file.readBytes((char*)&size, 4);
ESP_LOGD(TAG, "Raw chunk data: id=%.4s size=%08x", id, size);
if (strncmp(id, "phdr", 4) == 0) {
uint32_t count = size / sizeof(PHDR);
for (uint32_t i = 0; i < count; ++i) {
PHDR p;
file.readBytes((char*)&p, sizeof(PHDR));
phdrs.push_back(p);
ESP_LOGD(TAG, "PHDR[%u]: name='%s' preset=%u bank=%u bagIndex=%u",
i, p.name, p.preset, p.bank, p.bagIndex);
}
}
else if (strncmp(id, "pbag", 4) == 0) {
uint32_t count = size / sizeof(PBAG);
for (uint32_t i = 0; i < count; ++i) {
PBAG b;
file.readBytes((char*)&b, sizeof(PBAG));
pbags.push_back(b);
}
}
else if (strncmp(id, "pgen", 4) == 0) {
uint32_t count = size / sizeof(PGEN);
for (uint32_t i = 0; i < count; ++i) {
PGEN g;
file.readBytes((char*)&g, sizeof(PGEN));
pgens.push_back(g);
}
}
else if (strncmp(id, "inst", 4) == 0) {
uint32_t count = size / sizeof(INST);
for (uint32_t i = 0; i < count; ++i) {
INST n;
file.readBytes((char*)&n, sizeof(INST));
insts.push_back(n);
}
}
else if (strncmp(id, "ibag", 4) == 0) {
uint32_t count = size / sizeof(IBAG);
for (uint32_t i = 0; i < count; ++i) {
IBAG b;
file.readBytes((char*)&b, sizeof(IBAG));
ibags.push_back(b);
}
}
else if (strncmp(id, "igen", 4) == 0) {
uint32_t count = size / sizeof(IGEN);
for (uint32_t i = 0; i < count; ++i) {
IGEN g;
file.readBytes((char*)&g, sizeof(IGEN));
igens.push_back(g);
}
}
else if (strncmp(id, "shdr", 4) == 0) {
if (!readSampleHeaders(file.position(), size)) {
ESP_LOGE(TAG, "Failed to read sample headers");
return false;
}
}
else {
ESP_LOGW(TAG, "Unknown PDTA chunk id: %.4s, skipping", id);
file.seek(size, SeekCur);
}
// Padding for odd sizes
if (size % 2 != 0) {
file.seek(1, SeekCur);
}
ESP_LOGD(TAG, "Chunk %.4s processed. Current pos: %u", id, file.position());
if (file.position() > pdtaEnd) {
ESP_LOGW(TAG, "Stopped: reached end of PDTA section.");
break;
}
}
// Добавляем фиктивные окончания
phdrs.push_back(PHDR{.bagIndex = static_cast<uint16_t>(pbags.size())});
pbags.push_back(PBAG{.genIndex = static_cast<uint16_t>(pgens.size())});
insts.push_back(INST{.bagIndex = static_cast<uint16_t>(ibags.size())});
ibags.push_back(IBAG{.genIndex = static_cast<uint16_t>(igens.size())});
// Сохраняем структуры
this->presets.clear();
this->instruments.clear();
for (size_t i = 0; i + 1 < phdrs.size(); ++i) {
SF2Preset preset;
preset.name = String(phdrs[i].name);
preset.bank = phdrs[i].bank;
preset.program = phdrs[i].preset;
for (uint16_t b = phdrs[i].bagIndex; b < phdrs[i + 1].bagIndex; ++b) {
SF2Zone zone;
for (uint16_t g = pbags[b].genIndex; g < pbags[b + 1].genIndex; ++g) {
Generator gen;
gen.oper = pgens[g].oper;
gen.amount.sAmount = pgens[g].amount;
zone.generators.push_back(gen);
}
bool hasInstrument = std::any_of(zone.generators.begin(), zone.generators.end(), [](const Generator& g) {
return toGeneratorOperator(g.oper) == GeneratorOperator::Instrument;
});
if (hasInstrument) {
preset.zones.push_back(zone);
} else {
preset.globalGenerators = zone.generators; // <<< here
}
}
this->presets.push_back(preset);
}
for (size_t i = 0; i + 1 < insts.size(); ++i) {
SF2Instrument inst;
inst.name = String(insts[i].name);
for (uint16_t b = insts[i].bagIndex; b < insts[i + 1].bagIndex; ++b) {
SF2Zone zone;
for (uint16_t g = ibags[b].genIndex; g < ibags[b + 1].genIndex; ++g) {
Generator gen;
gen.oper = igens[g].oper;
decodeGeneratorAmount(gen, igens[g].amount);
zone.generators.push_back(gen);
}
bool hasSampleID = std::any_of(zone.generators.begin(), zone.generators.end(), [](const Generator& g) {
return toGeneratorOperator(g.oper) == GeneratorOperator::SampleID;
});
if (hasSampleID) {
inst.zones.push_back(zone);
} else {
inst.globalGenerators = zone.generators; // <<< inject this
}
}
this->instruments.push_back(inst);
}
ESP_LOGD(TAG, "PDTA parsed successfully: phdr=%zu pbags=%zu pgens=%zu instruments=%zu",
phdrs.size(), pbags.size(), pgens.size(), instruments.size());
return true;
}
bool SF2Parser::readSampleHeaders(uint32_t offset, uint32_t size) {
seekTo(offset);
size_t count = size / 46; // Каждая запись — 46 байт
samples.clear();
for (size_t i = 0; i < count; ++i) {
SampleHeader sample;
file.readBytes((char*)&sample, 46);
if (sample.start == 0 && sample.end == 0 && sample.sampleRate == 0) {
ESP_LOGW(TAG, "Invalid sample EOS: start=0 end=0 rate=0");
continue;
}
sampleMap[i] = &samples.emplace_back(sample);
ESP_LOGD(TAG, "Loaded sample %zu: %s (start=%u, end=%zu), orig=%d, sr=%u", i, sample.name, sample.start, sample.end, sample.originalPitch, sample.sampleRate);
}
file.seek(offset + size);
ESP_LOGD(TAG, "readSampleHeaders(): file.position() after read = %u, expected = %u",
file.position(), offset + size);
ESP_LOGD(TAG, "Total samples loaded: %zu", samples.size());
return true;
}
std::vector<Zone> SF2Parser::getZonesForNote(uint8_t note, uint8_t velocity, uint16_t bank, uint16_t program) {
std::vector<Zone> resultZones;
for (const auto& preset : presets) {
if (preset.bank != bank || preset.program != program) continue;
for (const auto& pzone : preset.zones) {
int instIndex = -1;
for (const auto& g : pzone.generators) {
if (static_cast<GeneratorOperator>(g.oper) == GeneratorOperator::Instrument) {
instIndex = g.amount.sAmount;
break;
}
}
if (instIndex < 0 || instIndex >= instruments.size()) continue;
const auto& inst = instruments[instIndex];
for (const auto& izone : inst.zones) {
int sampleIndex = -1;
uint8_t keyLo = 0, keyHi = 127;
uint8_t velLo = 0, velHi = 127;
for (const auto& g : izone.generators) {
auto oper = static_cast<GeneratorOperator>(g.oper);
if (oper == GeneratorOperator::KeyRange) {
keyLo = g.amount.range.lo;
keyHi = g.amount.range.hi;
} else if (oper == GeneratorOperator::VelRange) {
velLo = g.amount.range.lo;
velHi = g.amount.range.hi;
} else if (oper == GeneratorOperator::SampleID) {
sampleIndex = g.amount.sAmount;
}
}
if (sampleIndex >= 0 && sampleIndex < samples.size() &&
note >= keyLo && note <= keyHi &&
velocity >= velLo && velocity <= velHi) {
Zone z{};
z.sample = &samples[sampleIndex];
z.keyLo = keyLo;
z.keyHi = keyHi;
z.velLo = velLo;
z.velHi = velHi;
z.rootKey = z.sample->originalPitch;
// Apply generator hierarchy
applyGenerators(preset.globalGenerators, z);
applyGenerators(pzone.generators, z);
applyGenerators(inst.globalGenerators, z);
applyGenerators(izone.generators, z);
ESP_LOGD(TAG, "Mapped: note=%u velocity=%u -> sample=%s", note, velocity, z.sample->name);
resultZones.push_back(std::move(z));
}
}
}
}
return resultZones;
}
void SF2Parser::applyGenerators(const std::vector<Generator>& gens, Zone& zone) {
for (const auto& g : gens) {
auto op = static_cast<GeneratorOperator>(g.oper);
float val = g.amount.sAmount;
switch (op) {
case GeneratorOperator::SampleID:
zone.sample = resolveSample(g.amount.uAmount);
break;
case GeneratorOperator::KeyRange:
zone.keyLo = g.amount.range.lo;
zone.keyHi = g.amount.range.hi;
break;
case GeneratorOperator::VelRange:
zone.velLo = g.amount.range.lo;
zone.velHi = g.amount.range.hi;
break;
case GeneratorOperator::OverridingRootKey:
zone.rootKey = g.amount.sAmount;
break;
case GeneratorOperator::SampleModes:
zone.sampleModes = g.amount.uAmount;
break;
case GeneratorOperator::StartLoopAddrOffset:
zone.loopStartOffset = g.amount.sAmount;
break;
case GeneratorOperator::EndLoopAddrOffset:
zone.loopEndOffset = g.amount.sAmount;
break;
case GeneratorOperator::StartLoopAddrCoarseOffset:
zone.loopStartCoarseOffset = g.amount.sAmount;
break;
case GeneratorOperator::EndLoopAddrCoarseOffset:
zone.loopEndCoarseOffset = g.amount.sAmount;
break;
case GeneratorOperator::ExclusiveClass:
zone.exclusiveClass = g.amount.uAmount;
break;
case GeneratorOperator::FineTune:
zone.fineTune = val / 100.0f;
break;
case GeneratorOperator::CoarseTune:
zone.coarseTune = val;
break;
case GeneratorOperator::AttackVolEnv:
zone.attackTime = timecentsToSec(val);
break;
case GeneratorOperator::HoldVolEnv:
zone.holdTime = timecentsToSec(val);
break;
case GeneratorOperator::DecayVolEnv:
zone.decayTime = timecentsToSec(val);
break;
case GeneratorOperator::SustainVolEnv:
zone.sustainLevel = powf(10.0f, -val / 200.0f); // val is in centibels
break;
case GeneratorOperator::ReleaseVolEnv:
zone.releaseTime = timecentsToSec(val);
break;
case GeneratorOperator::AttackModEnv:
zone.modAttackTime = timecentsToSec(val);
break;
case GeneratorOperator::DecayModEnv:
zone.modDecayTime = timecentsToSec(val);
break;
case GeneratorOperator::ReleaseModEnv:
zone.modReleaseTime = timecentsToSec(val);
break;
case GeneratorOperator::ModEnvToPitch:
zone.modEnvToPitch = val;
break;
case GeneratorOperator::SustainModEnv:
// zone.modSustainLevel = powf(10.0f, -val / 200.0f); // val is in centibels
zone.modSustainLevel = val * 0.001f ; // map to 0..1
break;
case GeneratorOperator::Pan:
zone.pan = val * 0.01f;
break;
case GeneratorOperator::InitialFilterFc:
zone.filterFc = centsToHz(val);
break;
case GeneratorOperator::VibLfoToPitch:
zone.vibLfoToPitch = val; // in cents
break;
case GeneratorOperator::VibLfoDelay:
zone.vibLfoDelay = timecentsToSec(val); // val is in timecents
break;
case GeneratorOperator::VibLfoFreq:
zone.vibLfoFreq = centsToHz(val); // val is in cents, convert to Hz
break;
case GeneratorOperator::InitialFilterQ:
zone.filterQ = val * 0.1f;
break;
case GeneratorOperator::ReverbEffectsSend:
zone.reverbSend = val * 0.001f;
break;
case GeneratorOperator::ChorusEffectsSend:
zone.chorusSend = val * 0.001f;
break;
case GeneratorOperator::ModLfoToPitch:
zone.modLfoToPitch = val; // in cents
break;
case GeneratorOperator::ModLfoToFilterFc:
zone.modLfoToFilterFc = val; // in cents
break;
case GeneratorOperator::ModLfoToVolume:
zone.modLfoToVolume = powf(10.0f, -val / 200.0f); // in centibels
break;
case GeneratorOperator::ModLfoDelay:
zone.modLfoDelay = timecentsToSec(val);;
break;
case GeneratorOperator::ModLfoFreq:
zone.modLfoFreq = centsToHz(val);
break;
default:
break;
}
}
if (!zone.chorusSend) {zone.chorusSend = 1.0f; }
if (!zone.reverbSend) { zone.reverbSend = 0.1f; } // 기본 10% (원본 1.0f=100%는 과도)
}
SampleHeader* SF2Parser::resolveSample(uint32_t sampleID) {
if (sampleID < samples.size()) {
return &samples[sampleID];
}
ESP_LOGE(TAG, "Invalid sample ID: %u", sampleID);
return nullptr;
}
void SF2Parser::dumpPresetStructure() {
ESP_LOGI(TAG, "\n========== SF2 Preset Structure ==========");
for (size_t pi = 0; pi < presets.size(); ++pi) {
const auto& preset = presets[pi];
ESP_LOGI(TAG, "[Preset %zu] \"%s\" (Bank=%u, Program=%u, Zones=%zu)",
pi, preset.name.c_str(), preset.bank, preset.program, preset.zones.size());
for (size_t zi = 0; zi < preset.zones.size(); ++zi) {
const auto& zone = preset.zones[zi];
ESP_LOGI(TAG, " PZone[%zu]: %zu generators", zi, zone.generators.size());
int instIndex = -1;
for (const auto& gen : zone.generators) {
GeneratorOperator op = toGeneratorOperator(gen.oper);
ESP_LOGI(TAG, " Gen %s = %d", toString(op), gen.amount.sAmount);
if (op == GeneratorOperator::Instrument) {
instIndex = gen.amount.uAmount;
if (instIndex >= 0 && instIndex < instruments.size()) {
const auto& inst = instruments[instIndex];
ESP_LOGI(TAG, " → Instrument \"%s\" (Zones=%zu)",
inst.name.c_str(), inst.zones.size());
for (size_t iz = 0; iz < inst.zones.size(); ++iz) {
const auto& izone = inst.zones[iz];
ESP_LOGI(TAG, " IZone[%zu]:", iz);
SampleHeader* sample = nullptr;
uint8_t keyLo = 0, keyHi = 127;
uint8_t velLo = 0, velHi = 127;
for (const auto& g : izone.generators) {
GeneratorOperator iop = toGeneratorOperator(g.oper);
if (iop == GeneratorOperator::Instrument || iop == GeneratorOperator::SampleID
|| iop == GeneratorOperator::KeyRange || iop == GeneratorOperator::VelRange) {
ESP_LOGI(TAG, " Gen %s = %d", toString(iop), g.amount.sAmount);
}
if (iop == GeneratorOperator::SampleID) {
sample = resolveSample(g.amount.uAmount);
} else if (iop == GeneratorOperator::KeyRange) {
keyLo = g.amount.range.lo;
keyHi = g.amount.range.hi;
} else if (iop == GeneratorOperator::VelRange) {
velLo = g.amount.range.lo;
velHi = g.amount.range.hi;
}
}
if (sample) {
ESP_LOGI(TAG, " → Sample \"%s\" key[%u-%u] vel[%u-%u]", sample->name, keyLo, keyHi, velLo, velHi);
} else {
ESP_LOGW(TAG, " → No Sample");
}
}
} else {
ESP_LOGW(TAG, " → Invalid instrument index: %d", instIndex);
}
}
}
}
}
ESP_LOGI(TAG, "========== End of Preset Dump ==========\n");
}
bool SF2Parser::loadSampleDataToMemory() {
if (sdtaOffset == 0 || samples.empty()) {
ESP_LOGE(TAG, "Sample data not available or no sample headers found");
return false;
}
file.seek(sdtaOffset);
char id[5] = {0};
file.readBytes(id, 4);
if (strncmp(id, "smpl", 4) != 0) {
ESP_LOGE(TAG, "Expected 'smpl' chunk not found");
return false;
}
uint32_t smplSize = 0;
file.readBytes((char*)&smplSize, 4);
uint32_t smplStart = file.position();
ESP_LOGI(TAG, "Reading sample data: offset=%u size=%u", smplStart, smplSize);
SampleHeader* fallback = nullptr;
for (size_t i = 0; i < samples.size(); ++i) {
auto& s = samples[i];
uint32_t length = (s.end > s.start) ? (s.end - s.start) : 0;
if (length == 0) {
ESP_LOGW(TAG, "Sample %zu (%s) has zero length", i, s.name);
continue;
}
s.data = (uint8_t*)heap_caps_aligned_alloc(4, length * sizeof(int16_t), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
if (!s.data) {
ESP_LOGE(TAG, "PSRAM allocation failed for sample %zu (%s), size=%u", i, s.name, length * 2);
if (fallback) {
// Fallback: bind to first loaded sample
s.data = fallback->data;
s.dataSize = fallback->dataSize;
s.start = fallback->start;
s.end = fallback->end;
s.startLoop = fallback->startLoop;
s.endLoop = fallback->endLoop;
s.sampleRate = fallback->sampleRate;
s.originalPitch = fallback->originalPitch;
s.pitchCorrection = fallback->pitchCorrection;
s.sampleLink = fallback->sampleLink;
s.sampleType = fallback->sampleType;
ESP_LOGW(TAG, "Sample %zu (%s) will use fallback sample", i, s.name);
continue;
} else {
ESP_LOGE(TAG, "No fallback sample available — aborting");
return false;
}
}
file.seek(smplStart + s.start * 2); // 16-bit PCM
file.read((uint8_t*)s.data, length * 2);
s.dataSize = length * 2;
ESP_LOGD(TAG, "Loaded sample %zu: %s (offset=%u length=%u)", i, s.name, s.start, length);
if (!fallback)
fallback = &s;
}
return true;
}
void SF2Parser::clear() {
for (auto& sample : samples) {
if (sample.data) {
heap_caps_aligned_free(sample.data);
sample.data = nullptr;
sample.dataSize = 0;
}
}
samples.clear();
presets.clear();
instruments.clear();
zones.clear();
sampleMap.clear();
startPosMap.clear();
}
bool SF2Parser::hasPreset(uint16_t bank, uint16_t program) const {
for (const auto& preset : presets) {
if (preset.bank == bank && preset.program == program) {
return true;
}
}
return false;
}