-
Notifications
You must be signed in to change notification settings - Fork 2
/
sf_to_sampler.cpp
481 lines (413 loc) · 13.4 KB
/
sf_to_sampler.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
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
/*
* Copyright (c) 2024 Marcel Licence
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
* veröffentlichten Version, weiter verteilen und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
* OHNE JEDE GEWÄHR,; sogar ohne die implizite
* Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Einzelheiten.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
*/
/**
* @file sf_to_sampler.cpp
* @author Marcel Licence
* @data 09.12.2023
*
* @brief Implementation of function helping to push samples from soundfont into the sampler
*/
#ifdef __CDT_PARSER__
#include <cdt.h>
#endif
/*
* includes
*/
#include <Arduino.h>
#include "config.h"
#include "sf_to_sampler.h"
#include "fs/fs_access.h"
#include <ml_types.h>
#include <ml_status.h>
#include <ml_soundfont.h>
#include <ml_sampler.h>
#define SF2_INFO_MESSAGES
/*
* data types
*/
union wavSampleS16
{
uint8_t data[256];
Q1_14 samples[128];
};
/*
* static function declarations
*/
static void TransferSampleData(uint32_t start, uint32_t end);
static void LoadAllSamples(void);
static void LoadSampleFromInfo(struct instrLoadInfo_s *info);
/*
* static function definitions
*/
static void TransferSampleData(uint32_t start, uint32_t end)
{
Sampler_StartTransfer();
uint32_t data_to_read = (end - start) * 2;
fileSeekTo((start) * 2 /* + sampleDataFileOffset */);
wavSampleS16 sampleData;
uint32_t bytesRead;
while (data_to_read > 0)
{
if (data_to_read >= 256)
{
bytesRead = readBytes(sampleData.data, 256);
}
else
{
bytesRead = readBytes(sampleData.data, data_to_read);
}
if (Sampler_AddSamples(sampleData.samples, bytesRead / 2))
{
//Serial.printf("added %d samples, %u left\n", bytesRead, data_to_read);
}
else
{
Serial.printf("Failed to add %" PRIu32 " samples, %" PRIu32 " left\n", bytesRead, data_to_read);
break;
}
data_to_read -= bytesRead;
}
Sampler_EndTransfer();
}
static void LoadAllSamples(void)
{
struct sf2_soundfont_info_s *offset = ML_SF2_GetSoundFontInfo();
TransferSampleData(offset->smpl / 2, offset->smpl / 2 + offset->smpl_cnt / 2);
for (uint32_t i = 0; i < offset->shdr_cnt - 1; i++)
{
struct instrLoadInfo_s info;
if (ML_SF2_LoadSamplesFromInfo(i, &info))
{
LoadSampleFromInfo(&info);
Sampler_InstrumentDone();
}
}
}
static void SF2ToSmpl_LoadAllInstrumentsMultiCB(struct instrLoadInfo_s *infoPtr)
{
Serial.printf("Instrument found: %s\n", infoPtr->name);
Serial.printf("sample modus: %" PRIu8 "\n", infoPtr->sampleModus);
Serial.printf("startLoop: %" PRIu32 "\n", infoPtr->startLoop);
Serial.printf("endLoop: %" PRIu32 "\n", infoPtr->endLoop);
Serial.printf("start: %" PRIu32 "\n", infoPtr->start);
Serial.printf("end: %" PRIu32 "\n", infoPtr->end);
LoadSampleFromInfo(infoPtr);
}
static void LoadSampleFromInfo(struct instrLoadInfo_s *info)
{
if ((info->start == 0) && (info->end == 0))
{
return;
}
if (Sampler_NewSample())
{
Sampler_NewSampleSetRange(info->start, info->end);
if ((info->sampleModus == 1) || (info->sampleModus == 3))
{
if ((info->startLoop != info->start) || (info->endLoop != info->end))
{
info->start += 1;
printf("loop: %" PRIu32 " - %" PRIu32 "\n", info->startLoop - info->start, info->endLoop - info->start);
Sampler_NewSampleSetLoop(info->startLoop, info->endLoop);
}
Sampler_SetLoopMode(info->sampleModus);
}
Sampler_SetExclusiveClass(info->exClass);
Sampler_SetPitch(info->rootKey, info->sampleRate, info->tune);
#ifdef MULTIPLE_SAMPLE_PER_INSTRUMENT
Sampler_SetKeyRange(info->keyRange.lowest, info->keyRange.highest);
Sampler_SetVelRange(info->velRange.lowest, info->velRange.highest);
#endif
{
float holdVolEnv_f = info->decayVolEnv;
holdVolEnv_f /= 1200;
holdVolEnv_f = pow(2, holdVolEnv_f);
/* time normalized by sample rate */
holdVolEnv_f *= ((float)SAMPLE_RATE);
/* multiplication count to min 16 bit value */
holdVolEnv_f = pow(1.0f / 32736.0f, 1.0 / holdVolEnv_f);
//pow(1.0/32736.0, 1/(holdVolEnv_f*SAMPLE_RATE));
/* normalize */
holdVolEnv_f *= 2147483648.0f;
Sampler_SetHold((uint32_t)holdVolEnv_f);
}
{
float releaseVolEnv_f = info->releaseVolEnv;
releaseVolEnv_f /= 1200;
releaseVolEnv_f = pow(2, releaseVolEnv_f);
/* time normalized by sample rate */
releaseVolEnv_f *= ((float)SAMPLE_RATE);
/* multiplication count to min 16 bit value */
releaseVolEnv_f = pow(1.0f / 32736.0f, 1.0 / releaseVolEnv_f);
//pow(1.0/32736.0, 1/(releaseVolEnv_f*SAMPLE_RATE));
/* normalize */
releaseVolEnv_f *= 2147483648.0f;
Sampler_SetRelease((uint32_t)releaseVolEnv_f);
}
Sampler_FinishSample();
}
else
{
Serial.printf("Could not add sample (LoadSampleFromInfo3)!\n");
}
}
/*
* extern function definitions
*/
void SF2ToSmpl_LoadAllInstrumentsMulti(void)
{
struct sf2_soundfont_info_s *offset = ML_SF2_GetSoundFontInfo();
TransferSampleData(offset->smpl / 2, offset->smpl / 2 + offset->smpl_cnt / 2);
for (uint32_t i = 0; i < offset->inst_cnt - 1; i++)
{
if (ML_SF2_GetInstrumentInfoMultiBag(i, SF2ToSmpl_LoadAllInstrumentsMultiCB))
{
Sampler_InstrumentDone();
}
else
{
Serial.printf("loadInstr failed!\n");
}
}
}
void SF2ToSmpl_LoadAllInstruments(void)
{
struct sf2_soundfont_info_s *offset = ML_SF2_GetSoundFontInfo();
TransferSampleData(offset->smpl / 2, offset->smpl / 2 + offset->smpl_cnt / 2);
for (uint32_t i = 0; i < offset->inst_cnt - 1; i++)
{
struct instrLoadInfo_s info;
if (ML_SF2_GetInstrumentInfo(i, &info))
{
LoadSampleFromInfo(&info);
Sampler_InstrumentDone();
}
else
{
Serial.printf("loadInstr failed!\n");
}
}
}
void SF2ToSmpl_LoadCompleteSoundFont(void)
{
struct sf2_soundfont_info_s *offset = ML_SF2_GetSoundFontInfo();
TransferSampleData(offset->smpl / 2, offset->smpl / 2 + offset->smpl_cnt / 2);
for (uint32_t i = 0; i < offset->phdr_cnt - 1; i++)
{
if (ML_SF2_LoadPresetMultiBag(i, LoadSampleFromInfo))
{
Sampler_InstrumentDone();
}
else
{
Serial.printf("loadPresetMultiBag failed!\n");
}
}
}
void SF2ToSmpl_LoadAllInstrumentsFromSF(fs_id_t fs_id, const char *filename)
{
if (FS_OpenFile(fs_id, filename))
{
SF2ToSmpl_LoadAllInstruments();
FS_CloseFile();
Status_ValueChangedStr("Instruments from Soundfont", "Loaded", filename);
}
else
{
Status_ValueChangedStr("Instruments from Soundfont", "Loading failed!", filename);
}
}
void SF2ToSmpl_LoadAllInstrumentsMultiFromSF(fs_id_t fs_id, const char *filename)
{
if (FS_OpenFile(fs_id, filename))
{
SF2ToSmpl_LoadAllInstrumentsMulti();
FS_CloseFile();
Status_ValueChangedStr("Instruments from Soundfont", "Loaded", filename);
}
else
{
Status_ValueChangedStr("Instruments from Soundfont", "Loading failed!", filename);
}
}
void SF2ToSmpl_LoadCompleteSoundFont(fs_id_t fs_id, const char *filename)
{
if (FS_OpenFile(fs_id, filename))
{
SF2ToSmpl_LoadCompleteSoundFont();
FS_CloseFile();
Status_ValueChangedStr("Complete Soundfont", "Loaded", filename);
}
else
{
Status_ValueChangedStr("Complete Soundfont", "Loading failed!", filename);
}
}
void SF2ToSmpl_LoadAllSamplesFromSF(fs_id_t fs_id, const char *filename)
{
if (FS_OpenFile(fs_id, filename))
{
LoadAllSamples();
FS_CloseFile();
Status_ValueChangedStr("Samples from Soundfont", "Loaded", filename);
}
else
{
Status_ValueChangedStr("Samples from Soundfont", "Loading failed!", filename);
}
}
/**
* @brief Handler for soundfont preset indications
*/
void sf2_preset_indication(union preset_hdr_s *preset, uint32_t idx)
{
#ifdef SF2_INFO_MESSAGES
Serial.printf("presetName[%" PRIu32 "]: %s\n", idx, preset->presetName);
Serial.printf(" preset: %d\n", preset->preset);
Serial.printf(" bank: %d\n", preset->bank);
Serial.printf(" presetBagIndex: %d\n", preset->presetBagIndex);
Serial.printf(" library: %" PRIu32 "\n", preset->library);
Serial.printf(" genre: %" PRIu32 "\n", preset->genre);
Serial.printf(" morphology: %" PRIu32 "\n", preset->morphology);
#else
(void)preset;
(void)idx;
#endif
}
/**
* @brief Handler for soundfont sample indications
*/
void sf2_sample_indication(union sf2_sample_hdr_s *sample, uint32_t idx)
{
#ifdef SF2_INFO_MESSAGES
char sampleName[21] = {0};
memcpy(sampleName, sample->sampleName, 20);
Serial.printf("sampleName[%" PRIu32 "]: %s\n", idx, sampleName);
Serial.printf(" start: %" PRIu32 "\n", sample->start);
Serial.printf(" end: %" PRIu32 "\n", sample->end);
Serial.printf(" startLoop: %" PRIu32 "\n", sample->startLoop);
Serial.printf(" endLoop: %" PRIu32 "\n", sample->endLoop);
Serial.printf(" sampleRate: %" PRIu32 "\n", sample->sampleRate);
Serial.printf(" originalPitch: %d\n", sample->originalPitch);
Serial.printf(" pitchCorrection: %d\n", sample->pitchCorrection);
Serial.printf(" sampleLink: %d\n", sample->sampleLink);
Serial.printf(" sampleType: %d\n", sample->sampleType);
#else
(void)sample;
(void)idx;
#endif
}
/**
* @brief Will be called when a soundfont will be parsed and a sample will be identified
*
* @param inst
* @param idx
*/
void sf2_instrument_indication(union SF2Instrument_u *inst, uint32_t idx)
{
#ifdef SF2_INFO_MESSAGES
char instName[21] = {0};
strncpy(instName, inst->name, 20);
Serial.printf("instrument[%" PRIu32 "]:\n", idx);
Serial.printf(" instName: %s\n", instName);
Serial.printf(" bagIndex: %u\n", inst->bagIndex);
#else
(void)inst;
(void)idx;
#endif
}
/**
* @brief Handler for soundfont sample tag indication
*/
void sf2_sdta_smpl_indication(uint32_t len)
{
#ifdef SF2_INFO_MESSAGES
Serial.printf("Sample in file at %" PRIu32 "\n", getStaticPos());
Serial.printf(" len %" PRIu32 "\n", len);
#else
(void)len;
#endif
}
void sf2_preset_bag_indication(union SF2PresetBag_u *pbag)
{
#ifdef SF2_INFO_MESSAGES
Serial.printf("preset bag:\n");
Serial.printf(" generatorIndex: %u\n", pbag->generatorIndex);
Serial.printf(" modulatorIndex: %u\n", pbag->modulatorIndex);
#else
(void)pbag;
#endif
}
void sf2_preset_modulator_indication(union SF2PresetModulator_u *pmod)
{
#ifdef SF2_INFO_MESSAGES
Serial.printf("preset modulator:\n");
Serial.printf(" sourceOperator: %u\n", pmod->sourceOperator);
Serial.printf(" destinationOperator: %u\n", pmod->destinationOperator);
Serial.printf(" amount: %d\n", pmod->amount);
Serial.printf(" amountSourceOperator: %u\n", pmod->amountSourceOperator);
Serial.printf(" transportOperator: %u\n", pmod->transportOperator);
#else
(void)pmod;
#endif
}
void sf2_preset_generator_indication(union SF2PresetGenerator_u *pgen, uint32_t idx)
{
#ifdef SF2_INFO_MESSAGES
Serial.printf("preset generator[%" PRIu32 "]:\n", idx);
Serial.printf(" generatorIndex: %u\n", pgen->generatorIndex);
Serial.printf(" amount: %d\n", pgen->amount);
#else
(void)pgen;
(void)idx;
#endif
}
void sf2_instrument_bag_indication(union SF2InstrumentBag_u *ibag, uint32_t idx)
{
#ifdef SF2_INFO_MESSAGES
Serial.printf("instrument bag[%" PRIu32 "]:\n", idx);
Serial.printf(" generatorIndex: %u\n", ibag->generatorIndex);
Serial.printf(" modulatorIndex: %u\n", ibag->modulatorIndex);
#else
(void)ibag;
(void)idx;
#endif
}
void sf2_instrument_generator_indication(union SF2InstrumentGenerator_u *igen, uint32_t idx)
{
#ifdef SF2_INFO_MESSAGES
Serial.printf("instrument generator[%" PRIu32 "]:\n", idx);
Serial.printf(" ioperator: %u\n", igen->ioperator);
Serial.printf(" amount: %u\n", igen->amount);
#else
(void)igen;
(void)idx;
#endif
}