-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
1650 lines (1368 loc) · 52.6 KB
/
main.c
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SC55 ROM to Soundfont converter
// Code by Kitrinx and NewRisingSun
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#ifdef __GNUC__
#define packed_struct struct __attribute__((__packed__))
#define PATH_DIV "/"
#else
#define packed_struct _Pragma("pack(1)") struct
#define PATH_DIV "\\"
#endif
#include "riff.h"
#include "sf2.h"
enum partial_bytes {
pp_panpot = 5,
pp_course_pitch = 6, // Shifts the instrument key
pp_fine_pitch = 7,
pp_random_pitch = 8,
pp_note_range = 9,
pp_vibrato_depth = 10,
pp_part_attenuation = 65,
pp_lfo_depth = 68,
pp_tva_p1_vol = 70,
pp_tva_p2_vol = 71,
pp_tva_p3_vol = 72,
pp_tva_p4_vol = 73,
pp_tva_p1_len = 74,
pp_tva_p2_len = 75,
pp_tva_p3_len = 76,
pp_tva_p4_len = 77,
pp_tva_p5_len = 78,
};
enum inst_header_bytes {
ih_attenuation = 0,
ih_note_flags = 1,
ih_reverb = 3,
ih_chorus = 4,
ih_panpot = 5,
ih_partial_en = 6,
};
#define BLOCK2 0x2A9A1
uint32_t banks_vsc[8] = {0x00034, 0x0BD34, 0x0DEF4, 0x10034, 0x1BD34, 0x1DEF4, 0x20034, 0x30000};
uint32_t banks_sc55[8] ={0x10000, 0x1BD00, 0x1DEC0, 0x20000, 0x2BD00, 0x2DEC0, 0x30000, 0x38080};
#define B1_SZ 0xD8
#define B2_SZ 0x3C
#define B3_SZ 0x10
#define B4_SZ 0x100
#define NAME_SZ 0xC
#define NUM_INST (224 * 2)
#define NUM_PARTS (144 * 2)
#define NUM_SAMPLES (532 * 2)
#define NUM_VARIATIONS 128
#define NUM_DRUMS 14
#define KIT_SIZE 0x48C
#define INT24_MAX 0x7FFFFF
#define INT24_MIN (0x7FFFFF * -1)
#define BOOST_AMOUNT 0.0
//#define MKII
#if defined(MKII)
#define CTRL_VER_ADDR 0x3D148
#else
#define CTRL_VER_ADDR 0xF380
#endif
#define MAG2DB(x) (-200.0 * log10(x))
#define SC552AMP(x) (0.1 * pow(2.0, (double)(x) / 36.7111) - 0.1)
#define PCT2VOL(x) ((x) == 127 ? 0 : ((x) > 0 ? MAG2DB(SC552AMP(x)) : 1440))
// 0x3CA48 - 0x70 len settings, 0x700 total
int32_t compress_sample(long double sample, long double value)
{
long double comp_f2 = value;
long double comp_a2 = value / 2.0;
long double comp_x2 = (((double)INT24_MAX * (comp_f2 - 1.0)) / ((comp_f2 * comp_a2) - 1.0)) + 1.0; // +1 to make sure it won't overflow
long double comp_b2 = comp_x2 * comp_a2;
long double v = (sample < 0) ? -1.0 * sample : sample;
long double v2 = (v < comp_x2) ? (v * comp_a2) : (((v - comp_x2) / comp_f2) + comp_b2);
long double compr = (sample < 0) ? -1.0 * v2 : v2;
return (int32_t) roundl(compr);
}
packed_struct ins_partial { // 92 bytes
uint8_t spacer1; // Always 0
uint8_t unknown1; // Has value, usually 0x40
uint16_t part_index; // Part table index, 0xFFFF for unused
uint8_t pp[88];// Unknown set of Part parameters
};
packed_struct instrument { // 204 bytes
char name[NAME_SZ];
uint8_t header[20]; // {7F, 10, 00, 3C, 4F, 01, 02?} // {FF?, FF?, FF, FF, FF, FF} // {00?, 00, 00, 00, 00, 00}
struct ins_partial parts[2];
};
packed_struct part { // 48 bytes
char name[NAME_SZ];
uint8_t breaks[16]; // Note breakpoints corresponding to sample addresses
uint16_t samples[16]; // Set of addresses to the sample table. 0 is default, and above corresponds to breakpoints
};
packed_struct sample { // 16 bytes
uint8_t volume; // Volume attenuation 7F to 0
uint8_t offset[3]; // Offset on vsc, bank + scrambled address on SC55. Bits above 20 are wave bank.
uint16_t attack_end; // boundry between attack and decay? Unconfirmed.
uint16_t sample_len; // Sample Size
uint16_t loop_len; // Loop point, used as sample_len - loop_len - 1
uint8_t loop_mode; // 2 if not a looping sound, 1 forward then back, 0 forward only.
uint8_t root_key; // Base pitch of the sample
uint16_t pitch; // Fine pitch adjustment, 2048 to 0. Positive increases pitch.
uint16_t fine_volume; // Always 0x400 on VSC, appears to be 1000ths of a decibel. Positive is higher volume.
};
packed_struct drum { //1164 bytes
uint16_t preset[128];
uint8_t volume[128];
uint8_t key[128];
uint8_t assignGroup[128];// AKA exclusive class
uint8_t panpot[128];
uint8_t reverb[128];
uint8_t chorus[128];
uint8_t flags[128];// 0x10 == responds to note on 0x01 responds to note_off
char name[NAME_SZ];
};
packed_struct variation {
uint16_t variation[128];
};
packed_struct synth {
struct drum drums[NUM_DRUMS];
struct variation variations[NUM_VARIATIONS];
struct instrument instruments[NUM_INST];
struct part parts[NUM_PARTS];
struct sample samples[NUM_SAMPLES];
uint8_t wave_data[0x300000];
uint8_t control_data[0x40000];
};
struct sf_instruments {
int32_t used_inst[NUM_INST * 10];
struct sfInst inst[NUM_INST * 100];
struct sfInstBag ibag[NUM_INST * 100 * 10];
struct sfModList imod[NUM_INST * 100 * 10];
struct sfInstGenList igen[NUM_INST * 100 * 10];
uint32_t inst_count;
uint32_t ibag_count;
uint32_t imod_count;
uint32_t igen_count;
};
struct pf_used_inst {
bool used;
uint32_t partial0;
uint32_t partial1;
};
struct sf_presets {
struct pf_used_inst used[NUM_INST * 10];
struct sfPresetHeader phdr[NUM_INST * 2];
struct sfPresetBag pbag[NUM_INST * 100];
struct sfModList pmod[NUM_INST * 100];
struct sfGenList pgen[NUM_INST * 100];
uint32_t phdr_count;
uint32_t pbag_count;
uint32_t pmod_count;
uint32_t pgen_count;
};
typedef struct sample_params {
double t1; // Times in seconds
double t2;
double t3;
double t4;
double t5;
double l1; // Levels in amplitude
double l2;
double l3;
double l4;
uint8_t s1; // Evelope Shapes. 0 = linear, 1 = Concave 2 = Convex
uint8_t s2;
uint8_t s3;
uint8_t s4;
uint8_t s5;
uint32_t terminal_phase; // The phase that reaches the terminal level
} sample_params;
packed_struct sample_record {
struct sample_params params;
uint32_t sample_number;
bool used;
};
#define MAX_SF_SAMPLES UINT16_MAX
struct sf_samples {
struct sfSample shdr[MAX_SF_SAMPLES];
int16_t *sample16;
uint8_t *sample8;
uint32_t num_samples;
size_t data_size;
struct sample_record used[NUM_SAMPLES][12];
};
packed_struct wav_params {
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bits_per_sample;
};
struct phase_data {
float durationInSeconds;
int endLevel;
};
typedef struct {
int nn;
int nd;
long double n[3];
long double d[3];
} biquad;
// Unknown tables in control rom:
// 0x3EF82 0xFA2 UINT16_T
// 0x3CA48 - 3D147 0x10 groups of 0x70 UINT8_T
#define TP1 (long double)(3180e-6)
#define TP2 (long double)(75e-6)
#define TZ1 (long double)(318e-6)
#define TZ2 (long double)(3.18e-6)
#define PZ(T) exp(-1.0/(fs*(T)))
biquad compute_riaa_irr(long double fs, long double dcgain, int extrazero)
{
static biquad bq;
long double p1, p2, z1, z2, gain;
int i;
p1 = PZ(TP1);
p2 = PZ(TP2);
z1 = PZ(TZ1);
z2 = PZ(TZ2);
bq.nd = 3;
bq.d[0] = 1.0;
bq.d[1] = -p1-p2;
bq.d[2] = p1*p2;
if(extrazero) {
bq.nn = 3;
bq.n[0] = 1.0;
bq.n[1] = -z1-z2;
bq.n[2] = z1*z2;
} else {
bq.nn = 2;
bq.n[0] = 1.0;
bq.n[1] = -z1;
bq.n[2] = 0.0;
}
gain = (bq.n[0]+bq.n[1]+bq.n[2])/(1.0+bq.d[1]+bq.d[2]);
long double gain_atten = (dcgain/gain);
for(i=0; i<3; i++) {
bq.n[i] *= gain_atten;
}
return bq;
}
void iir_filter(const long double *b, const long double *a, size_t filterLength, const long double *in, long double *out, size_t length)
{
const long double a0 = a[0];
const long double *a_end = &a[filterLength-1];
const long double *out_start = out;
a++;
out--;
size_t m;
for (m = 0; m < length; m++) {
const long double *b_macc = b;
const long double *in_macc = in;
const long double *a_macc = a;
const long double *out_macc = out;
long double b_acc = (*in_macc--) * (*b_macc++);
long double a_acc = 0;
while (a_macc <= a_end && out_macc >= out_start) {
b_acc += (*in_macc--) * (*b_macc++);
a_acc += (*out_macc--) * (*a_macc++);
}
*++out = (b_acc - a_acc) / a0;
in++;
}
}
uint16_t ntohs(uint16_t netshort) {
return ((netshort & 0xff) << 8) | (netshort >> 8);
}
uint32_t be_bytes_to_address(uint8_t *address_bytes)
{
uint32_t address = 0;
uint8_t *addr_ptr = (uint8_t *) &address;
for (int32_t x = 0; x < 3; x++) {
addr_ptr[x] = address_bytes[2 - x];
}
return address;
}
int32_t parse_control_rom(FILE *f, struct instrument *instruments, struct part *parts,
struct sample *samples, struct variation *variations, struct drum *drums,
uint32_t *banks, bool sc55)
{
int32_t part_index = 0;
int32_t instrument_index = 0;
int32_t sample_index = 0;
int32_t instrument_count = 0;
int32_t step = B1_SZ;
fseek(f, banks[0], SEEK_SET);
for (int32_t x = banks[0]; x < banks[6]; x += step) {
if ((x & 0xFFFF) == (banks[0] & 0xFFFF)) step = B1_SZ;
else if ((x & 0xFFFF) == (banks[1] & 0xFFFF)) step = B2_SZ;
else if ((x & 0xFFFF) == (banks[2] & 0xFFFF)) step = B3_SZ;
if (step == B3_SZ) {
fread(&samples[sample_index], 1, step, f);
if (sc55) {
samples[sample_index].loop_len= ntohs(samples[sample_index].loop_len);
samples[sample_index].sample_len = ntohs(samples[sample_index].sample_len);
samples[sample_index].attack_end = ntohs(samples[sample_index].attack_end);
samples[sample_index].pitch = ntohs(samples[sample_index].pitch);
samples[sample_index].fine_volume = ntohs(samples[sample_index].fine_volume);
}
// printf("Sample %3d: V:%3d U1:%5d SL:%5d LL:%5d LM:%3d RK:%3d FP:%+5d FV:%+5d\n",
// sample_index,
// samples[sample_index].volume,
// samples[sample_index].attack_end,
// samples[sample_index].sample_len,
// samples[sample_index].loop_len,
// samples[sample_index].loop_mode,
// samples[sample_index].root_key,
// samples[sample_index].pitch - 1024,
// samples[sample_index].fine_volume - 1024);
sample_index++;
} else if (step == B1_SZ) {
fread(&instruments[instrument_index], 1, step, f);
if (sc55) {
instruments[instrument_index].parts[0].part_index = ntohs(instruments[instrument_index].parts[0].part_index);
instruments[instrument_index].parts[1].part_index = ntohs(instruments[instrument_index].parts[1].part_index);
}
if (instruments[instrument_index].name[0])
instrument_count++;
instrument_index++;
} else {
fread(&parts[part_index], 1, step, f);
for (int32_t sflip = 0; sflip < 16; sflip++) {
if (sc55) {
parts[part_index].samples[sflip] = ntohs(parts[part_index].samples[sflip]);
}
}
part_index++;
}
}
fseek(f, banks[6], SEEK_SET);
for (int32_t x = 0; x < 128; x++) {
fread(variations[x].variation, B4_SZ, 1, f);
if (sc55) {
for (int32_t y = 0; y < 128; y++){
uint8_t *b = (uint8_t *) &variations[x].variation[y];
uint8_t top = b[0];
b[0] = b[1];
b[1] = top;
}
}
}
// Drums
if (sc55) {
uint32_t index = 0;
for (int32_t x = banks[7]; x < 0x3c028; x += KIT_SIZE) {
fseek(f, x, SEEK_SET);
fread(&drums[index], sizeof(struct drum), 1, f);
for (int32_t y = 0; y < 128; y++){
uint8_t *b = (uint8_t *) &drums[index].preset[y];
uint8_t top = b[0];
b[0] = b[1];
b[1] = top;
}
index++;
}
}
return instrument_count;
}
void *trim_name(char *name)
{
for (int32_t x = NAME_SZ - 1; x >= 0; x--) {
if (isspace(name[x])) {
name[x] = '\0';
} else {
break;
}
}
}
void clean_name(char *dirty_name, char *name)
{
// Not all names are NULL terminated, so this returns them safely
memset(name, 0, NAME_SZ + 1);
memcpy(name, dirty_name, NAME_SZ);
trim_name(name);
}
uint32_t unscramble_address(uint32_t address)
{
// Discovered and written by NewRisingSun
uint32_t new_addr = 0;
if (address >= 0x20) { // The first 32 bytes are not encrypted
static const int addressOrder [20] = {0x02, 0x00, 0x03, 0x04, 0x01, 0x09, 0x0D, 0x0A, 0x12,
0x11, 0x06, 0x0F, 0x0B, 0x10, 0x08, 0x05, 0x0C, 0x07, 0x0E, 0x13};
for (uint32_t bit = 0; bit < 20; bit++) {
new_addr |= ((address >> addressOrder[bit]) & 1) << bit;
}
} else {
new_addr = address;
}
return new_addr;
}
int8_t unscramble_byte(int8_t byte)
{
uint8_t byte_order[8] = {2, 0, 4, 5, 7, 6, 3, 1};
uint32_t new_byte = 0;
for (uint32_t bit = 0; bit < 8; bit++) {
new_byte |= ((byte >> byte_order[bit]) & 1) << bit;
}
return new_byte;
}
bool decode_wave_rom(uint8_t *dec_buf)
{
char *files_in[3] = {"roms"PATH_DIV"roland-gss.a_r15209276.ic28", "roms"PATH_DIV"roland-gss.b_r15209277.ic27", "roms"PATH_DIV"roland-gss.c_r15209281.ic26"};
uint8_t *enc_buf = calloc(1, 0x100000);
for (int32_t x = 0; x < 3; x++) {
FILE *f_in = fopen(files_in[x], "rb");
if (!f_in) {
printf("Unable to find wave roms. Results will be corrupt.\n");
return false;
}
fread(&enc_buf[0], 1, 0x100000, f_in);
fclose(f_in);
for (uint32_t y = 0; y < 0x100000; y++) {
dec_buf[unscramble_address(y) + (0x100000 * x)] = unscramble_byte(enc_buf[y]);
}
}
FILE *fo = fopen("wave_dec.rom", "wb");
fwrite(dec_buf, 0x300000, 1, fo);
fclose (fo);
free(enc_buf);
return true;
}
bool decode_wave_rom_scb(uint8_t *dec_buf)
{
uint8_t *enc_buf = calloc(1, 0x200000);
FILE *f_in = fopen("roms"PATH_DIV"R15209359_(samples1).BIN", "rb");
if (!f_in) {
printf("Unable to find wave roms. Results will be corrupt.\n");
return false;
}
fread(&enc_buf[0], 1, 0x200000, f_in);
fclose(f_in);
for (uint32_t y = 0; y < 0x100000; y++) {
dec_buf[unscramble_address(y) + (0x000000)] = unscramble_byte(enc_buf[y]);
}
for (uint32_t y = 0; y < 0x100000; y++) {
dec_buf[unscramble_address(y) + (0x100000)] = unscramble_byte(enc_buf[y + (0x100000)]);
}
FILE *fo = fopen("wave_dec_scb1.rom", "wb");
fwrite(dec_buf, 0x200000, 1, fo);
fclose (fo);
f_in = fopen("roms"PATH_DIV"R15279813_(samples2).BIN", "rb");
if (!f_in) {
printf("Unable to find wave roms. Results will be corrupt.\n");
return false;
}
fread(&enc_buf[0], 1, 0x100000, f_in);
fclose(f_in);
for (uint32_t y = 0; y < 0x100000; y++) {
dec_buf[unscramble_address(y) + (0x200000)] = unscramble_byte((enc_buf[y]));
}
fo = fopen("wave_dec_scb2.rom", "wb");
fwrite(dec_buf, 0x100000, 1, fo);
fclose (fo);
free(enc_buf);
return true;
}
//FIXME: Unused
struct instrument *get_instrument(struct synth *synth, uint16_t midi_number, uint16_t bank)
{
struct instrument *inst = NULL;
if (midi_number > 127 || bank > NUM_VARIATIONS - 1) {
printf ("rejecting %d %d\n", midi_number, bank);
return inst;
}
if (synth->variations[bank].variation[midi_number] < NUM_INST) {
if (synth->instruments[synth->variations[bank].variation[midi_number]].name[0]) {
inst = &synth->instruments[synth->variations[bank].variation[midi_number]];
} else {
printf("Nameless instrument at %04x\n", synth->variations[bank].variation[midi_number]);
}
} else if (synth->variations[bank].variation[midi_number] != 0xFFFF) {
printf("Unknown inst: %04x\n", synth->variations[bank].variation[midi_number]);
}
return inst;
}
//FIXME: Unused
struct part *get_part(struct synth *synth, struct instrument *inst, uint8_t part_num)
{
struct part *part = NULL;
if (inst->parts[part_num].part_index < NUM_PARTS &&
synth->parts[inst->parts[part_num].part_index].name[0])
part = &synth->parts[inst->parts[part_num].part_index];
else
{
printf("Nameless part at %04x\n", inst->parts[part_num].part_index);
}
return part;
}
//FIXME: Unused
struct sample *get_sample(struct synth *synth, struct part *part, uint8_t sample_index)
{
struct sample *sample = NULL;
if (sample_index > 15)
return sample;
if (part->samples[sample_index] < NUM_SAMPLES &&
synth->samples[part->samples[sample_index]].sample_len > 0) {
sample = &synth->samples[part->samples[sample_index]];
}
return sample;
}
//FIXME: Unused
void print_bits(uint32_t bits, size_t num_bits)
{
for (int32_t x = num_bits - 1; x >=0; x--) {
if (x % 4 == 3)
printf(" ");
if (bits & (1 << x))
printf("1");
else
printf("0");
}
}
void export_sample(int32_t *samples, size_t num_samples, char *file_name)
{
struct RIFF *samp = NULL;
riff_open(&samp, "WAVE", file_name);
struct wav_params p = {0};
p.audio_format = 1;
p.num_channels = 1;
p.sample_rate = 32000;
p.bits_per_sample = 24;
p.byte_rate = p.sample_rate * p.num_channels * p.bits_per_sample / 8;
p.block_align = p.num_channels * p.bits_per_sample / 8;
riff_write_chunk(samp, "fmt ", sizeof(struct wav_params), (uint8_t *)&p);
uint8_t *sample_24 = calloc(1, num_samples * 3 * sizeof(uint8_t));
for (int32_t x = 0; x < num_samples; x++) {
memcpy(&sample_24[x * 3], &samples[x], 3);
}
riff_write_chunk(samp, "data", num_samples * 3, sample_24);
riff_close(&samp);
free(sample_24);
}
uint32_t apply_riaa_filter(biquad bq, int32_t *sample, size_t len, double vol, uint32_t sample_num)
{
long double attenuate = vol / (SC552AMP(127));
long double *conv_buffer = calloc(len + 1000, sizeof(long double));
long double *conv_buffer2 = calloc(len + 1000, sizeof(long double));
for(int32_t x = 0; x < len; x++) {
conv_buffer[x] = sample[x];
}
biquad hpf;
biquad lpf;
// hpf.d[0] = 1.000000000000000L;
// hpf.d[1] = -0.999018733894812L;
// hpf.n[0] = 0.999509366947406L;
// hpf.n[1] = -0.999509366947406l;
// hpf.nd = 2;
// hpf.nn = 2;
hpf.d[0] = 1.000000000000000L;
hpf.d[1] = -0.998038429728411L;
hpf.n[0] = 0.999019214864206L;
hpf.n[1] = -0.999019214864206l;
hpf.nd = 2;
hpf.nn = 2;
// Samples use heavy pre-emphasis. RIAA curve x2 appears to make them sound normal.
iir_filter(&bq.n[0], &bq.d[0], bq.nd, conv_buffer, conv_buffer2, len);
iir_filter(&bq.n[0], &bq.d[0], bq.nd, conv_buffer2, conv_buffer, len);
// Apply a HPF to remove bias from the audio
iir_filter(&hpf.n[0], &hpf.d[0], hpf.nd, conv_buffer, conv_buffer2, len);
size_t clipped = 0;
for (int32_t x = 0; x < len; x++) {
// FIXME: Unfortunately after filtering twice the volume is extremely low, and the range
// much too high so I do a raw shift of * 8 and then use a compressor to further increase
// the percieved volume.
int32_t t_val = compress_sample((conv_buffer2[x] * 4.0L) * attenuate, 16);
if (t_val > INT24_MAX) {
t_val = INT24_MAX;
clipped++;
} else if (t_val < INT24_MIN) {
t_val = INT24_MIN;
clipped++;
}
sample[x] = round(t_val);
}
if (clipped)
printf("%ld samples clipped in sample %d\n", clipped, sample_num);
free(conv_buffer);
free(conv_buffer2);
return len;
}
int32_t make_sample(uint8_t *decoded_rom, uint32_t address)
{
int8_t data_byte = decoded_rom[address];
uint8_t shift_byte = decoded_rom[((address & 0xFFFFF) >> 5) | (address & 0xF00000)];
uint8_t shift_nibble = (address & 0x10) ? (shift_byte >> 4 ) : (shift_byte & 0x0F);
int32_t final = ((data_byte << shift_nibble) << 14); // Shift nibbles thus far never exceed 10, thus 18 bit samples
final = final >> 8; // To maintain sign for 24 bit sample
return final;
}
uint32_t write_sample_data(uint32_t address, uint32_t loop_start, uint32_t loop_mode, uint32_t loop_end, int32_t length,
int32_t *delta, int32_t *samples, size_t *data_size, uint8_t *decoded_rom, uint32_t sample_end)
{
if (length <= 0) {
printf("Invalid Length %d written\n", length);
return address;
}
// Delta is used for loop mode 1 forward-then-back looping. Set 0 for mode 0.
switch (loop_mode) {
case 0:
while (length--) {
samples[*data_size] = make_sample(decoded_rom, address++);
*data_size = *data_size + 1;
if (address == loop_end) address = loop_start;
}
break;
case 1:
while (length--) {
samples[*data_size] = make_sample(decoded_rom, address);
*data_size = *data_size + 1;
address += *delta;
if (address > loop_end) {
*delta = -1;
address--;
} else if (address == loop_start && *delta < 0) {
*delta = 1;
address++;
}
}
break;
case 2:
while (length--) {
if (address > sample_end) {
printf("Sample writing exceeded end of sample in non-looping sample by %d bytes.\n", length);
return address;
}
samples[*data_size] = make_sample(decoded_rom, address++);
*data_size = *data_size + 1;
}
break;
}
return address;
}
void apply_envelope(int32_t *samples, uint32_t length, double *initial_amplitude, double target_amplitude, uint8_t shape)
{
if (length <= 0) return;
double current_amp = *initial_amplitude;
int32_t total_length = length;
double max_amp = SC552AMP(0X7F);
double level_difference = (target_amplitude - *initial_amplitude);
uint32_t samp_index = 0;
while (1) {
samp_index++;
if (samp_index > length) break;
switch (shape) {
case 0: // Linear
current_amp = *initial_amplitude + level_difference * ((double)samp_index / (double)length);
break;
case 1: // Concave
case 2: // "Convex"
current_amp = *initial_amplitude + level_difference * (log(10.0 *samp_index / length + 1) / log(10.0 + 1));
break;
case 4: // Level only
current_amp = target_amplitude;
break;
}
samples[samp_index] = round((double)samples[samp_index] * (current_amp / max_amp));
}
*initial_amplitude = current_amp;
}
#define MIN_SAMPLE_PAD 512
#define TIME2SAMP(x) ((int32_t)(round(32000.0 * (double)(x))))
uint32_t fill_single_sample(struct sf_samples *s, struct sample *sc55_samples, uint8_t *dec,
uint32_t source, struct sample_params *params)
{
// Also, to allow a variety of hardware platforms to be able to reproduce the data, the samples
// have a minimum length of 48 data points, a minimum loop size of 32 data points and a minimum
// of 8 valid points prior to dwStartloop and after dwEndloop. Thus dwStart must be less than
// dwStartloop-7, dwStartloop must be less than dwEndloop-31, and dwEndloop must be less than
// dwEnd-7. If these constraints are not met, the sound may optionally not be played if the
// hardware cannot support artifact-free playback for the parameters given.
// Additionally, each sample must be followed by at least 46 zero-valued data points
biquad bq = compute_riaa_irr(32000.0, 9.89808, 1);
uint32_t sample_address = be_bytes_to_address(sc55_samples[source].offset);
uint32_t bank = 0;
switch ((sample_address & 0x700000) >> 20) {
case 0: bank = 0x000000; break;
case 1: bank = 0x100000; break; // Used in SCB/MKII
case 2: bank = 0x100000; break;
case 4: bank = 0x200000; break;
default: printf("Encountered unknown bank ID: %d\n", (sample_address & 0x700000));
}
uint32_t address = (sample_address & 0xFFFFF) | bank;
uint32_t total_length = sc55_samples[source].sample_len + 1;
uint32_t loop_length = sc55_samples[source].loop_len;
uint32_t loop_start_offset = total_length - 2 - loop_length;
uint32_t loop_end_offset = total_length - 1;
int32_t loop_start = address + loop_start_offset;
int32_t loop_end = address + loop_end_offset;
char sample_name[40] = {0};
snprintf(s->shdr[s->num_samples].achSampleName, 20, "SC-55 - %d", source);
s->shdr[s->num_samples].byOriginalPitch = sc55_samples[source].root_key;
s->shdr[s->num_samples].dwSampleRate = 32000;
s->shdr[s->num_samples].sfSampleType = 1;
s->shdr[s->num_samples].wSampleLink = 0;
s->shdr[s->num_samples].chPitchCorrection = round(((double)sc55_samples[source].pitch - 1024.0) / 10.0);
s->shdr[s->num_samples].dwStart = s->data_size;
s->shdr[s->num_samples].dwStartloop = s->shdr[s->num_samples].dwStart + loop_start_offset + 2;
s->shdr[s->num_samples].dwEndloop = s->shdr[s->num_samples].dwStart + loop_end_offset;
// 30 second buffer size
int32_t *sbuf_full = calloc(120 * 32000, sizeof(int32_t));
int32_t *sbuf = &sbuf_full[2 * 32000];
size_t sbuf_size = 0;
uint32_t sample_end = address + total_length;
int32_t delta = 1;
uint32_t addr_ptr = address;
uint32_t real_end = 0;
// Phase 1, Phase 2, and Phase 5 always exist. The other two are optional. However, because the
// terminal phase has the potential to be very long, for the purposes of space, it would be best
// to only account for the phases prior to it. In some situations, like Seashore, the sample
// may end up being extremely long regardless.
uint32_t env_index = 0;
size_t starts[5] = {0};
double levels[5] = {0};
uint8_t shapes[5] = {0};
uint32_t env_start = 0;
double initial_amplitude = 0.000001;
// Phase 1 (Attack)
addr_ptr = write_sample_data(addr_ptr, loop_start, sc55_samples[source].loop_mode, loop_end,
TIME2SAMP(params->t1), &delta, sbuf, &sbuf_size, dec, sample_end);
levels[env_index] = params->l1;
shapes[env_index] = params->s1;
starts[env_index++] = sbuf_size;
// Phase 2
if (params->terminal_phase > 2) {
addr_ptr = write_sample_data(addr_ptr, loop_start, sc55_samples[source].loop_mode, loop_end,
TIME2SAMP(params->t2), &delta, sbuf, &sbuf_size, dec, sample_end);
levels[env_index] = params->l2;
shapes[env_index] = params->s2;
starts[env_index++] = sbuf_size;
}
// Phase 3
if (params->terminal_phase > 3) {
addr_ptr = write_sample_data(addr_ptr, loop_start, sc55_samples[source].loop_mode, loop_end,
TIME2SAMP(params->t3), &delta, sbuf, &sbuf_size, dec, sample_end);
levels[env_index] = params->l3;
shapes[env_index] = params->s3;
starts[env_index++] = sbuf_size;
}
double final_level = 0;
switch (params->terminal_phase) {
case 2: final_level = params->l1; break;
case 3: final_level = params->l2; break;
case 4: final_level = params->l3; break;
}
if (sc55_samples[source].loop_mode != 2) {
// Square off sample to the nearest loop
addr_ptr = write_sample_data(addr_ptr, loop_start, sc55_samples[source].loop_mode, loop_end,
delta < 0 ? loop_length + (loop_end - addr_ptr) : (loop_end - addr_ptr), &delta, sbuf, &sbuf_size, dec, sample_end);
s->shdr[s->num_samples].dwStartloop = s->data_size + sbuf_size; //FIXME: - 1?
addr_ptr = write_sample_data(addr_ptr, loop_start, sc55_samples[source].loop_mode, loop_end,
(loop_end - loop_start) * (sc55_samples[source].loop_mode == 1 ? 2 : 1), &delta, sbuf, &sbuf_size, dec, sample_end);
s->shdr[s->num_samples].dwEndloop = s->data_size + sbuf_size;
real_end = s->data_size + 8;
// Write a large tail so that the RIAA filter doesn't mis-align the amplitude of loop-ends
addr_ptr = write_sample_data(addr_ptr, loop_start, sc55_samples[source].loop_mode, loop_end,
MIN_SAMPLE_PAD, &delta, sbuf, &sbuf_size, dec, sample_end);
levels[env_index] = final_level;
shapes[env_index] = 4;
starts[env_index++] = sbuf_size;
} else {
// Write any remaining data for non-looping samples
addr_ptr = write_sample_data(addr_ptr, loop_start, sc55_samples[source].loop_mode, loop_end,
sample_end - addr_ptr, &delta, sbuf, &sbuf_size, dec, sample_end);
levels[env_index] = final_level;
shapes[env_index] = 4;
starts[env_index++] = sbuf_size;
real_end = sbuf_size;
}
s->shdr[s->num_samples].dwEnd = s->data_size + sbuf_size;
uint32_t len = s->shdr[s->num_samples].dwEnd - s->shdr[s->num_samples].dwStart;
double volume = SC552AMP((double)sc55_samples[source].volume) + (((double)(sc55_samples[source].fine_volume - 1024) / 1000.0));
uint32_t last_value = 0;
for (int32_t x = 0; x < 5; x++) {
if (starts[x]) {
apply_envelope(&sbuf[last_value], starts[x] - last_value, &initial_amplitude, levels[x], shapes[x]);
last_value = starts[x];
}
}
// Add data to prevent filter nonsense
for (int32_t x = 0; x < (2 * 32000); x++) {
sbuf_full[(2 * 32000) - x] = sbuf_full[(2 * 32000) + x];
}
apply_riaa_filter(bq, sbuf_full, len + (2 * 32000), volume, source);
for (int32_t x = 0; x < sbuf_size; x++) {
s->sample16[s->data_size + x] = sbuf[x] >> 8;
s->sample8[s->data_size + x] = (sbuf[x] & 0xFF);
}
free(sbuf_full);
s->data_size += sbuf_size + 46;
if (s->shdr[s->num_samples].dwStartloop - s->shdr[s->num_samples].dwStart < 8) {
printf("Short pre-loop sample %d\n", source);
}
if (s->shdr[s->num_samples].dwEnd - s->shdr[s->num_samples].dwStart < 48) {
printf("Short sample %d\n", source);
}
return s->num_samples++;
}
uint32_t find_or_make_sample(struct sf_samples *s, struct sample *sc55_samples, uint8_t *dec,
uint32_t source, struct sample_params *params)
{
uint32_t first_unused = 0;
for (int32_t x = 0; x < 12; x++) {
if (s->used[source][x].used) {
if (!memcmp(&s->used[source][x].params, params, sizeof(struct sample_params)))
return s->used[source][x].sample_number;
} else {
first_unused = x;
break;
}
}
uint32_t new_sample = fill_single_sample(s, sc55_samples, dec, source, params);
s->used[source][first_unused].used = true;
s->used[source][first_unused].sample_number = new_sample;
memcpy(&s->used[source][first_unused].params, params, sizeof(struct sample_params));
return new_sample;
}
#define SEC2SF(x) ((x) ? 1200.0 * log2(x) : INT16_MIN)
#define CONV_VALUE(x) ((pow(2.0, (double)(x & 0x7F) / 18.0) / 5.45 - 0.183))
void add_igen_short(struct sf_instruments *i, uint16_t operator, int32_t value)
{
int16_t clean_value = value < INT16_MIN ? INT16_MIN : value > INT16_MAX ? INT16_MAX : value;
i->igen[i->igen_count].sfGenOper = operator;
i->igen[i->igen_count++].genAmount.shAmount = clean_value;
}
void add_igen_word(struct sf_instruments *i, uint16_t operator, int32_t value)
{
uint16_t clean_value = value < 0 ? 0 : value > UINT16_MAX ? UINT16_MAX : value;
i->igen[i->igen_count].sfGenOper = operator;
i->igen[i->igen_count++].genAmount.wAmount = value;
}
void add_igen_split(struct sf_instruments *i, uint16_t operator, uint8_t max, uint8_t min)
{
i->igen[i->igen_count].sfGenOper = operator;
i->igen[i->igen_count].genAmount.ranges.byLo = min;
i->igen[i->igen_count++].genAmount.ranges.byHi = max;
}
void add_imod(struct sf_instruments *i, uint16_t operator, uint16_t trans_operator,
uint16_t dest_operator, int16_t amount, uint16_t amount_source)
{
i->imod[i->imod_count].sfModSrcOper = operator;
i->imod[i->imod_count].sfModDestOper = dest_operator;
i->imod[i->imod_count].modAmount = amount;
i->imod[i->imod_count].sfModAmtSrcOper = amount_source;
i->imod[i->imod_count++].sfModTransOper = trans_operator;
}