-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhm_reader.h
More file actions
730 lines (641 loc) · 17.6 KB
/
hm_reader.h
File metadata and controls
730 lines (641 loc) · 17.6 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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "stb_vorbis.h"
#define HM_MODULE_NAME_LENGTH 32
#define HM_MAX_CHANNELS 32
#define FREQUENCY_MULTIPLIER 0.05946f
struct hm_sample {
uint8_t instrument_id;
uint8_t ogg;
uint32_t data_length;
uint32_t frame_count;
uint32_t sample_rate;
uint8_t sixteen_bit;
uint8_t channels;
uint8_t loop;
uint32_t loop_start;
float pan;
uint8_t relative_note;
uint8_t key_range_start;
uint8_t key_range_end;
uint8_t envelope;
uint32_t predelay;
uint32_t attack;
uint32_t hold;
uint32_t decay;
float sustain;
uint32_t fadeout;
uint64_t envelope_timer;
float *frames;
};
struct hm_ramp {
uint8_t enabled;
int32_t start;
int32_t end;
uint32_t frame_pos;
uint32_t frame_duration;
};
struct hm_trill {
uint8_t enabled;
int16_t depth;
uint8_t up;
int8_t result;
uint32_t frame_pos;
uint32_t frame_length;
};
struct hm_channel {
uint16_t sample_id;
uint8_t base_note; // Note being played
uint8_t key_off; // Note being played
uint8_t command_id;
uint8_t command_param;
int8_t coarse_detune;
int8_t fine_detune;
float pan;
float vol;
uint16_t predelay;
int64_t sample_frame;
double pos_between_samples; // For resampling and pitch shifting
uint32_t fadeout_timer;
struct hm_ramp ramps[4];
struct hm_trill trills[2];
};
struct hm_context {
uint8_t *data;
char name[HM_MODULE_NAME_LENGTH];
uint32_t rate;
uint16_t length; // Measured in ticks
uint16_t loop_position; // Measured in ticks
uint8_t num_channels;
uint16_t num_samples;
uint8_t bpm;
uint8_t subdivision; // Ticks in a beat
uint32_t tick_length; // in samples
int64_t tick_position;
uint32_t samples_left_in_tick;
struct hm_channel channels[HM_MAX_CHANNELS];
struct hm_sample *samples;
};
static inline uint16_t hm_read_16(const uint8_t *data, uint32_t *i) {
unsigned int a = data[(*i)++];
unsigned int b = data[(*i)++];
return a << 8 | b;
}
static inline uint32_t hm_read_32(const uint8_t *data, uint32_t *i) {
unsigned int a = data[(*i)++];
unsigned int b = data[(*i)++];
unsigned int c = data[(*i)++];
unsigned int d = data[(*i)++];
return a << 24 | b << 16 | c << 8 | d;
}
static void
hm_load_samples(struct hm_context *ctx, const uint8_t *data,
uint32_t data_length, uint32_t *index)
{
const float envelope_multiplier = (float) ctx->rate / 1000.0f;
int32_t temp_thirty_two;
int i;
uint32_t j;
int16_t *sixteen_pointer;
uint8_t *eight_pointer;
float temp_float, vol;
struct hm_sample *cur_sample;
stb_vorbis *ogg;
for (i = 0; i < ctx->num_samples; i++) {
cur_sample = ctx->samples + i;
cur_sample->instrument_id = data[(*index)++];
cur_sample->ogg = data[(*index)++];
cur_sample->data_length = hm_read_32(data, index);
cur_sample->frame_count = hm_read_32(data, index);
cur_sample->sample_rate = hm_read_32(data, index);
cur_sample->sixteen_bit = data[(*index)++];
cur_sample->channels = data[(*index)++];
cur_sample->loop = data[(*index)++];
cur_sample->loop_start = hm_read_32(data, index);
temp_thirty_two = (((int32_t) hm_read_16(data, index)) - 32767);
cur_sample->pan = ((float) temp_thirty_two) / 32767.0f;
vol = ((float) hm_read_16(data, index)) / 65535.0f;
cur_sample->relative_note = data[(*index)++];
cur_sample->key_range_start = data[(*index)++];
cur_sample->key_range_end = data[(*index)++];
cur_sample->envelope = data[(*index)++];
cur_sample->predelay = hm_read_16(data, index)
* envelope_multiplier;
cur_sample->attack = hm_read_16(data, index)
* envelope_multiplier;
cur_sample->attack += cur_sample->predelay;
cur_sample->hold = hm_read_16(data, index)
* envelope_multiplier;
cur_sample->hold += cur_sample->attack;
cur_sample->decay = hm_read_16(data, index)
* envelope_multiplier;
cur_sample->decay += cur_sample->hold;
cur_sample->sustain = ((float) hm_read_16(data, index)) / 65535.0f;
cur_sample->fadeout = hm_read_16(data, index)
* envelope_multiplier;
cur_sample->frames = malloc(cur_sample->frame_count
* cur_sample->channels * sizeof(float));
if (cur_sample->ogg) {
ogg = stb_vorbis_open_memory(data + *index,
cur_sample->data_length, NULL, NULL);
stb_vorbis_get_samples_float_interleaved(ogg,
cur_sample->channels, cur_sample->frames,
cur_sample->frame_count
* cur_sample->channels);
stb_vorbis_close(ogg);
if (cur_sample->channels == 2) {
for (j = 0; j < cur_sample->frame_count; j++) {
cur_sample->frames[j * 2] *= vol;
cur_sample->frames[j * 2 + 1] *= vol;
}
} else {
for (j = 0; j < cur_sample->frame_count; j++) {
cur_sample->frames[j] *= vol;
}
}
} else if (cur_sample->sixteen_bit) {
sixteen_pointer = (int16_t *) (data + *index);
if (cur_sample->channels == 2) {
for (j = 0; j < cur_sample->frame_count; j++) {
cur_sample->frames[j * 2]
= ((float)
*sixteen_pointer++)
/ 32767.0f;
cur_sample->frames[j * 2] *= vol;
cur_sample->frames[j * 2 + 1]
= ((float)
*sixteen_pointer++)
/ 32767.0f;
cur_sample->frames[j * 2 + 1] *= vol;
}
} else {
for (j = 0; j < cur_sample->frame_count; j++) {
temp_float = (float) *sixteen_pointer++;
cur_sample->frames[j] = temp_float
/ 32767.0f;
cur_sample->frames[j] *= vol;
}
}
} else {
eight_pointer = (uint8_t *) (data + *index);
if (cur_sample->channels == 2) {
for (j = 0; j < cur_sample->frame_count; j++) {
temp_thirty_two = *eight_pointer++;
temp_thirty_two -= 128;
cur_sample->frames[j * 2]
= ((float) temp_thirty_two)
/ 128.0f;
cur_sample->frames[j * 2] *= vol;
temp_thirty_two = *eight_pointer++;
temp_thirty_two -= 128;
cur_sample->frames[j * 2 + 1]
= ((float) temp_thirty_two)
/ 128.0f;
cur_sample->frames[j * 2 + 1] *= vol;
}
} else {
for (j = 0; j < cur_sample->frame_count; j++) {
temp_thirty_two = *eight_pointer++;
temp_thirty_two -= 128;
cur_sample->frames[j]
= ((float) temp_thirty_two)
/ 128.0f;
cur_sample->frames[j] *= vol;
}
}
}
*index += cur_sample->data_length;
}
}
int
hm_create_context(struct hm_context **ctxp, const void *data,
uint32_t data_length, uint32_t rate)
{
struct hm_context *ctx = NULL;
const uint8_t *info = (uint8_t *) data;
uint32_t i = 14;
int j;
uint8_t *mempool = calloc(1, sizeof(struct hm_context));
ctx = (*ctxp = (struct hm_context*) mempool);
ctx->rate = rate;
while (info[i]) {
ctx->name[i - 14] = info[i];
i++;
}
i++;
ctx->num_channels = info[i++];
ctx->num_samples = info[i++];
mempool = calloc(ctx->num_samples, sizeof(struct hm_sample));
ctx->samples = (struct hm_sample*) mempool;
ctx->bpm = info[i++];
ctx->subdivision = info[i++];
ctx->tick_length = ((ctx->rate * 60) / ctx->bpm) / ctx->subdivision;
ctx->tick_position = -1;
ctx->samples_left_in_tick = 0;
ctx->length = hm_read_16(info, &i);
ctx->loop_position = hm_read_16(info, &i);
hm_load_samples(ctx, info, data_length, &i);
mempool = malloc((data_length - i) * sizeof(uint8_t));
memcpy(mempool, info + i, (data_length - i));
ctx->data = mempool;
for (j = 0; j < ctx->num_channels; j++) {
ctx->channels[j].vol = 1.0f;
ctx->channels[j].sample_frame = -1;
}
return 0;
}
static void
hm_identify_sample(struct hm_context *ctx, struct hm_channel *channel,
uint8_t note, uint8_t instrument)
{
int i;
for (i = 0; i < ctx->num_samples; i++) {
if (ctx->samples[i].instrument_id == instrument
&& note >= ctx->samples[i].key_range_start
&& note <= ctx->samples[i].key_range_end) {
channel->sample_id = i;
break;
}
}
ctx->samples[channel->sample_id].envelope_timer = 0;
}
static void
hm_init_ramp(struct hm_ramp *ramp, uint32_t duration, int32_t start,
int32_t end)
{
ramp->enabled = 1;
ramp->frame_pos = 0;
ramp->frame_duration = duration;
ramp->start = start;
ramp->end = end;
}
static void
hm_process_command(struct hm_context *ctx, struct hm_channel *channel)
{
int i;
int32_t temp_32;
struct hm_ramp *cur_ramp;
switch (channel->command_id & 15) {
case 1:
if (channel->command_id >> 4) {
hm_init_ramp(channel->ramps + 0,
((channel->command_id >> 4) + 1)
* ctx->tick_length,
(int32_t) (channel->vol * 255.0f),
channel->command_param);
} else {
channel->vol = ((float) channel->command_param)
/ 255.0f;
channel->ramps[0].enabled = 0;
}
break;
case 2:
if (channel->command_id >> 4) {
hm_init_ramp(channel->ramps + 1,
((channel->command_id >> 4) + 1)
* ctx->tick_length,
(int32_t) (channel->pan * 127.0f),
((int32_t) channel->command_param) - 127);
} else {
temp_32 = ((int32_t) channel->command_param) - 127;
channel->pan = ((float) temp_32) / 127.0f;
channel->ramps[1].enabled = 0;
}
break;
case 3:
if (channel->command_id >> 4) {
hm_init_ramp(channel->ramps + 2,
((channel->command_id >> 4) + 1)
* ctx->tick_length,
channel->coarse_detune,
((int32_t) channel->command_param) - 127);
} else {
channel->coarse_detune = ((int32_t) channel->command_param) - 127;
channel->ramps[2].enabled = 0;
}
break;
case 4:
if (channel->command_id >> 4) {
hm_init_ramp(channel->ramps + 3,
((channel->command_id >> 4) + 1)
* ctx->tick_length,
channel->fine_detune,
((int32_t) channel->command_param) - 127);
} else {
channel->fine_detune = ((int32_t) channel->command_param) - 127;
channel->ramps[3].enabled = 0;
}
break;
case 5:
channel->predelay = channel->command_param;
break;
case 6:
channel->trills[0].enabled = channel->command_id >> 4 & 1;
channel->trills[0].depth = channel->command_param & 15;
channel->trills[0].frame_length = (ctx->rate / 100)
* (channel->command_param >> 4);
channel->trills[0].frame_pos = channel->trills[0].frame_length;
break;
case 7:
channel->trills[1].enabled = channel->command_id >> 4;
channel->trills[1].depth = channel->command_param & 15 + 10;
channel->trills[1].frame_length = (ctx->rate / 100)
* (channel->command_param >> 4);
channel->trills[1].frame_pos = channel->trills[1].frame_length;
break;
}
}
static void
hm_loop(struct hm_context *ctx)
{
int i, j;
ctx->tick_position = ctx->loop_position;
for (i = 0; i < ctx->num_channels; i++)
for (j = 0; j < 4; j++)
ctx->channels[i].ramps[j].enabled = 0;
}
static void
hm_load_new_tick(struct hm_context *ctx)
{
int i;
uint8_t instrument_id;
uint8_t requested_note;
uint32_t data_index;
struct hm_channel *channel;
ctx->tick_position++;
if (ctx->tick_position >= ctx->length)
hm_loop(ctx);
data_index = 4 * ctx->num_channels * ctx->tick_position;
for (i = 0; i < ctx->num_channels; i++) {
channel = ctx->channels + i;
if (ctx->data[data_index] >> 7) {
requested_note = ctx->data[data_index] & 127;
instrument_id = ctx->data[data_index + 1];
if (requested_note) {
requested_note--;
hm_identify_sample(ctx, channel,
requested_note, instrument_id);
channel->base_note = requested_note;
channel->key_off = 0;
channel->coarse_detune = 0;
channel->fine_detune = 0;
channel->predelay = 0;
channel->fadeout_timer = 0;
channel->sample_frame = 0;
channel->pos_between_samples = 0;
channel->trills[0].enabled = 0;
channel->trills[1].enabled = 0;
} else {
channel->key_off = 1;
}
}
data_index += 2;
if (ctx->data[data_index]) {
channel->command_id = ctx->data[data_index];
channel->command_param = ctx->data[data_index + 1];
hm_process_command(ctx, channel);
channel->predelay *= ((float) ctx->rate) / 1000.0f;
}
data_index += 2;
}
ctx->samples_left_in_tick = ctx->tick_length;
}
static void
hm_pan_frame(float *left, float *right, float pan)
{
if (pan < 0.0f) {
*right -= *right * (-1.0f * pan);
} else if (pan > 0.0f) {
*left -= *left * pan;
}
}
static void
hm_read_sample(struct hm_sample *sample, uint32_t number, float *left, float *right)
{
float panned_sample;
float envelope_multiplier = 1.0f;
if (sample->channels == 2) {
*left = sample->frames[number * 2];
*right = sample->frames[number * 2 + 1];
} else {
*left = sample->frames[number];
*right = *left;
}
hm_pan_frame(left, right, sample->pan);
if (sample->envelope) {
if (sample->envelope_timer < sample->predelay) {
sample->envelope_timer++;
envelope_multiplier = 0.0f;
} else if (sample->envelope_timer < sample->attack) {
envelope_multiplier = sample->envelope_timer
/ ((float) sample->attack);
sample->envelope_timer++;
} else if (sample->envelope_timer < sample->hold) {
envelope_multiplier = 1.0f;
sample->envelope_timer++;
} else if (sample->envelope_timer < sample->decay) {
envelope_multiplier -=
((float) sample->envelope_timer
/ (float) sample->decay)
* (1.0f - sample->sustain);
sample->envelope_timer++;
} else {
envelope_multiplier = sample->sustain;
}
*left *= envelope_multiplier;
*right *= envelope_multiplier;
}
}
static void
hm_update_ramps(struct hm_context *ctx, struct hm_channel *channel)
{
int i;
struct hm_ramp *cur_ramp;
int32_t ramp_val;
float ramp_pos;
for (i = 0; i < 4; i++) {
if (channel->ramps[i].enabled) {
cur_ramp = channel->ramps + i;
ramp_pos = (float) cur_ramp->frame_pos
/ (float) cur_ramp->frame_duration;
ramp_val = cur_ramp->start + ramp_pos
* (cur_ramp->end - cur_ramp->start);
switch (i) {
case 0:
channel->vol = ((float) ramp_val) / 255.0f;
break;
case 1:
channel->pan = (float) ramp_val / 127.0f;
break;
case 2:
channel->coarse_detune = ramp_val;
channel->fine_detune = 100
* (ramp_pos - (uint32_t) ramp_pos);
if (cur_ramp->end < 0)
channel->fine_detune *= -1;
break;
case 3:
channel->fine_detune = ramp_val;
break;
}
cur_ramp->frame_pos++;
if (cur_ramp->frame_pos >= cur_ramp->frame_duration)
cur_ramp->enabled = 0;
}
}
}
static void
hm_update_trills(struct hm_context *ctx, struct hm_channel *channel)
{
int i;
struct hm_trill *cur_trill;
int32_t trill_val;
for (i = 0; i < 4; i++) {
if (channel->trills[i].enabled) {
cur_trill = channel->trills + i;
cur_trill->frame_pos--;
if (!cur_trill->frame_pos) {
cur_trill->frame_pos = cur_trill->frame_length;
cur_trill->up = !cur_trill->up;
}
switch (i) {
case 0:
cur_trill->result = cur_trill->up
* cur_trill->depth;
break;
case 1:
cur_trill->result = cur_trill->depth
- (cur_trill->depth * 2
* !cur_trill->up);
break;
}
}
}
}
static void
hm_channel_generate_sample(struct hm_context *ctx, struct hm_channel *channel,
float *left, float *right)
{
int i, dist;
float l1 = 0, r1 = 0;
float l2 = 0, r2 = 0;
struct hm_sample *sample = &ctx->samples[channel->sample_id];
double step_size = 1.0f;
float panned_sample;
if (channel->sample_frame < 0)
return;
if (channel->predelay > 0) {
channel->predelay--;
return;
}
hm_update_ramps(ctx, channel);
hm_update_trills(ctx, channel);
dist = (sample->relative_note)
- (channel->base_note + channel->coarse_detune
+ (channel->trills[0].result * channel->trills[0].enabled));
if (dist < 0)
for (i = 0; i > dist; i--)
step_size *= 1 + FREQUENCY_MULTIPLIER;
else if (dist > 0)
for (i = 0; i < dist; i++)
step_size *= 1 - FREQUENCY_MULTIPLIER;
step_size *= 1 + ((channel->fine_detune
+ (channel->trills[1].result * channel->trills[1].enabled))
* (FREQUENCY_MULTIPLIER / 100.0f));
step_size *= ((double) sample->sample_rate / (double) ctx->rate);
step_size += channel->pos_between_samples;
channel->sample_frame += (uint32_t) step_size;
step_size -= (uint32_t) step_size;
channel->pos_between_samples = step_size;
if ((channel->sample_frame + 1) < sample->frame_count) {
hm_read_sample(sample, channel->sample_frame, &l1, &r1);
hm_read_sample(sample, channel->sample_frame + 1, &l2, &r2);
} else if (channel->sample_frame < sample->frame_count) {
if (sample->loop) {
hm_read_sample(sample, channel->sample_frame, &l1,
&r1);
hm_read_sample(sample, sample->loop_start, &l2, &r2);
} else {
hm_read_sample(sample, channel->sample_frame, &l1, &r1);
l2 = r2 = 0.0f;
}
} else {
if (sample->loop) {
channel->sample_frame = (channel->sample_frame
% sample->frame_count) + sample->loop_start;
hm_read_sample(sample, channel->sample_frame, &l1,
&r1);
hm_read_sample(sample, channel->sample_frame + 1, &l2,
&r2);
} else {
channel->sample_frame = -1;
return;
}
}
l1 += step_size * (l2 - l1);
r1 += step_size * (r2 - r1);
hm_pan_frame(&l1, &r1, channel->pan);
l1 *= channel->vol;
r1 *= channel->vol;
if (channel->key_off) {
channel->fadeout_timer++;
if (channel->fadeout_timer > sample->fadeout) {
channel->sample_frame = -1;
} else {
l1 += ((float) channel->fadeout_timer
/ (float) sample->fadeout) * (0.0f - l1);
r1 += ((float) channel->fadeout_timer
/ (float) sample->fadeout) * (0.0f - r1);
}
}
if (l1 > 1.0f)
l1 = 1.0f;
if (r1 > 1.0f)
r1 = 1.0f;
if (l1 < -1.0f)
l1 = -1.0f;
if (r1 < -1.0f)
r1 = -1.0f;
*left += l1;
*right += r1;
}
void
hm_mixdown(struct hm_context *ctx, float *left, float *right)
{
int i;
if (ctx->samples_left_in_tick <= 0)
hm_load_new_tick(ctx);
ctx->samples_left_in_tick--;
*left = *right = 0.0f;
for (i = 0; i < ctx->num_channels; i++)
hm_channel_generate_sample(ctx, ctx->channels + i, left, right);
if (*left > 1.0f)
*left = 1.0f;
if (*right > 1.0f)
*right = 1.0f;
if (*left < -1.0f)
*left = -1.0f;
if (*right < -1.0f)
*right = -1.0f;
}
void
hm_generate_samples(struct hm_context *ctx, float *buffer, uint64_t sample_count)
{
uint64_t i;
for (i = 0; i < sample_count; i++)
hm_mixdown(ctx, &buffer[i * 2], &buffer[i * 2 + 1]);
}
void
hm_free_context(struct hm_context *ctx)
{
int i;
if (!ctx)
return;
free(ctx->data);
for (i = 0; i < ctx->num_samples; i++)
if (ctx->samples[i].frames)
free(ctx->samples[i].frames);
free(ctx->samples);
free(ctx);
}