-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiorg.h
More file actions
1096 lines (897 loc) · 39.5 KB
/
miniorg.h
File metadata and controls
1096 lines (897 loc) · 39.5 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
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
/* miniorg v1.0.0
# miniorg
**miniorg** is a tiny ANSI C library for reading and decoding Organya music (.org files).
Organya is a sequenced music format created in 1999 by [Studio Pixel](https://studiopixel.jp/) and used in games such as
[Cave Story](https://www.cavestory.org/game-info/about-cave-story.php),
[Azarashi (2001 version)](https://www.cavestory.org/pixels-works/azarashi.php),
[STARGAZER](http://www5b.biglobe.ne.jp/~kiss-me/aji/star/), and more.
The library does not use floating point arithmetic, or allocate any memory. In fact, it does not use libc at all,
and can be cleanly compiled with `-std=c89 -Wall -Wextra -Wpedantic -Werror -nostdlib`.
## Usage
To use this library, just `#include "miniorg.h"` in your project. Define `MINIORG_IMPLEMENTATION` before including the
header in one .c file to create the implementation.
### Example
```c
#define MINIORG_IMPLEMENTATION
#include "miniorg.h"
int main(void)
{
// Create the context. soundbank_data must not be freed as it will be used as long as the context is
miniorg_context ctx;
if (miniorg_init(&ctx, soundbank_data, sizeof(soundbank_data), 44100) != MINIORG_RESULT_SUCCESS)
{
// Handle the error here
return 1;
}
// song_data must not be freed as it will be used as long as the context is, or until another song is loaded
if (miniorg_read(&ctx, song_data, sizeof(song_data)) != MINIORG_RESULT_SUCCESS)
{
// Handle the error here
return 1;
}
// Generate samples (which would then be output to an audio player, or a .wav file, or etc.)
// The size of output_buffer should be a multiple of 4 (sizeof(int16) * 2).
miniorg_render(&ctx, (miniorg_int16 *)output_buffer, sizeof(output_buffer));
// You do not have to deinitialize the context, however...
// If you allocated soundbank_data or song_data dynamically, you should free those once you are done using the context.
return 0;
}
```
There are some extra preprocessor definitions you can define to change the functionality of the library:
- Define `MINIORG_USE_STDINT` to use `stdint.h` for the integer types. This is recommended if your compiler/environment supports it.
- Define `MINIORG_FAST_INTERPOLATION` to use a faster interpolation algorithm (faster, but results in worse sound quality).
- Define `MINIORG_NO_INTERPOLATION` to disable interpolation entirely (faster, but results in very poor sound quality).
- Define `MINIORG_NO_VOLUME_RAMP` to disable ramping for volume changes (faster, but results in a popping noise when volume changes).
You must make sure the definitions are consistent each time the library is `#include`d.
It is recommended to define them in your compiler flags (as in `-DMINIORG_...`).
See bottom of file for license information.
*/
#ifndef MINIORG_H_
#define MINIORG_H_
#ifdef __cplusplus
extern "C" {
#endif
#ifndef MINIORG_API
#define MINIORG_API extern
#endif
#ifndef MINIORG_PRIVATE
#define MINIORG_PRIVATE static
#endif
/* Types */
#ifdef MINIORG_USE_STDINT
#include <stdint.h>
typedef int8_t miniorg_int8;
typedef uint8_t miniorg_uint8;
typedef int16_t miniorg_int16;
typedef uint16_t miniorg_uint16;
typedef int32_t miniorg_int32;
typedef uint32_t miniorg_uint32;
#else
typedef signed char miniorg_int8;
typedef unsigned char miniorg_uint8;
typedef signed short miniorg_int16;
typedef unsigned short miniorg_uint16;
typedef signed long miniorg_int32;
typedef unsigned long miniorg_uint32;
#endif
typedef miniorg_uint8 miniorg_bool;
#define MINIORG_TRUE 1
#define MINIORG_FALSE 0
typedef char miniorg_char;
/* Constant stuff */
#define MINIORG_MELODY_CHANNEL_COUNT 8
#define MINIORG_PERCUSSION_CHANNEL_COUNT 8
#define MINIORG_CHANNEL_COUNT (MINIORG_MELODY_CHANNEL_COUNT + MINIORG_PERCUSSION_CHANNEL_COUNT)
#define MINIORG_WAVE_TABLE_COUNT 100
#define MINIORG_WAVE_LENGTH 0x100
#define MINIORG_WAVE_TABLE_SIZE (MINIORG_WAVE_TABLE_COUNT * MINIORG_WAVE_LENGTH)
#define MINIORG_PERCUSSION_COUNT 42
#define MINIORG_OCTAVE_COUNT 8
#define MINIORG_PROPERTY_NOT_USED 0xFF
#define MINIORG_MIN_SONG_SIZE (6 + 12 + (MINIORG_CHANNEL_COUNT * 6))
#define MINIORG_MIN_SOUNDBANK_SIZE (MINIORG_WAVE_TABLE_SIZE + (MINIORG_PERCUSSION_COUNT * 4))
#define MINIORG_EVENT_SIZE 8
/* This MUST MATCH the values in miniorg_shift_table */
#define MINIORG_GEN_WAVEFORMS_SIZE (256 + 128 + 64 + 32 + 16 + 8)
/* Only used if MINIORG_NO_INTERPOLATION and MINIORG_FAST_INTERPOLATION are not defined */
#define MINIORG_SOUND_BUFFER_SIZE 4
/* Structs */
typedef enum
{
MINIORG_RESULT_SUCCESS = 0,
MINIORG_RESULT_ERROR,
MINIORG_RESULT_INVALID_ARGS,
MINIORG_RESULT_INVALID_DATA,
MINIORG_RESULT_MEMORY_ERROR
}
miniorg_result;
typedef struct
{
miniorg_uint8 instrument;
miniorg_uint16 finetune;
miniorg_bool pizzicato;
miniorg_uint16 event_count;
/* For whatever reason, it's saved like arrays for each of the values individually
* As in: Every position (in order from left to right), then every pitch, then every length, etc...
* It's not stored this way in memory, so I am unsure why it's saved like this */
miniorg_int32 *event_position;
miniorg_uint8 *event_pitch;
miniorg_uint8 *event_length;
miniorg_uint8 *event_volume;
miniorg_uint8 *event_panning;
}
miniorg_channel;
typedef struct
{
miniorg_uint16 tempo_ms;
miniorg_uint8 beats;
miniorg_uint8 steps;
miniorg_int32 repeat_start;
miniorg_int32 repeat_end;
miniorg_channel channels[MINIORG_CHANNEL_COUNT];
}
miniorg_song;
typedef struct
{
const void *sample;
miniorg_bool sample_signed;
miniorg_uint32 sample_size;
miniorg_uint32 output_rate;
miniorg_uint32 frequency;
miniorg_uint16 volume;
miniorg_uint16 panning_left;
miniorg_uint16 panning_right;
/* Playback stuff */
miniorg_bool playing;
miniorg_int16 loops;
miniorg_uint32 position;
miniorg_uint32 sub_position;
miniorg_uint32 position_increment;
miniorg_uint16 volume_left;
miniorg_uint16 volume_right;
#ifndef MINIORG_NO_VOLUME_RAMP
miniorg_uint16 target_volume_left;
miniorg_uint16 target_volume_right;
miniorg_uint32 volume_ticks;
miniorg_bool played_before;
#endif
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
miniorg_int16 buffer[4];
miniorg_uint8 silence_timer;
miniorg_uint8 buffer_position;
#endif
}
miniorg_sound;
typedef struct
{
miniorg_sound sounds[MINIORG_OCTAVE_COUNT][2];
miniorg_bool alt_sound;
miniorg_uint8 pitch;
miniorg_uint8 volume;
miniorg_uint8 panning;
miniorg_uint32 ticks;
miniorg_uint32 index;
}
miniorg_melody_channel;
typedef struct
{
miniorg_sound sound;
miniorg_uint8 pitch;
miniorg_uint8 volume;
miniorg_uint8 panning;
miniorg_uint32 index;
}
miniorg_percussion_channel;
typedef struct
{
miniorg_uint32 sample_size;
const miniorg_uint8 *sample;
}
miniorg_percussion_sample;
typedef struct
{
miniorg_melody_channel melody_channels[MINIORG_MELODY_CHANNEL_COUNT];
miniorg_percussion_channel percussion_channels[MINIORG_PERCUSSION_CHANNEL_COUNT];
/* 100 * 256 bytes */
const miniorg_int8 *wave_table;
miniorg_percussion_sample percussion_samples[MINIORG_PERCUSSION_COUNT];
miniorg_int8 gen_wave_data[MINIORG_GEN_WAVEFORMS_SIZE * MINIORG_MELODY_CHANNEL_COUNT];
miniorg_song song;
miniorg_int32 play_position;
miniorg_uint32 sample_rate;
miniorg_uint32 tick_timer;
}
miniorg_context;
/**
* Initializes a context.
*
* @param ctx Pointer to the miniorg_context structure
* @param soundbank_data Pointer to the soundbank data. You must not free this until you are done using the context!
* @param soundbank_size Size of soundbank_data buffer
* @param rate Sample rate this context will output
*
* @returns Success/failure status; see miniorg_result enum for possible values
**/
MINIORG_API miniorg_result miniorg_init(miniorg_context *ctx, const miniorg_uint8 *soundbank_data, miniorg_uint32 soundbank_size, miniorg_uint32 rate);
/**
* Reads Organya music data.
*
* @param ctx Pointer to the miniorg_context structure
* @param song_data Pointer to the soundbank data. You must not free this until you are done using the context, or you have loaded another song!
* @param song_size Size of song_data buffer
*
* @returns Success/failure status; see miniorg_result enum for possible values
**/
MINIORG_API miniorg_result miniorg_read(miniorg_context *ctx, const miniorg_uint8 *song_data, miniorg_uint32 song_size);
/**
* Sets the current song position.
*
* @param ctx Pointer to the miniorg_context structure
* @param position New position to seek to
**/
MINIORG_API void miniorg_seek(miniorg_context *ctx, miniorg_int32 position);
/**
* Render samples of music playback.
*
* @param ctx Pointer to the miniorg_context structure
* @param output Output buffer, format is signed 16 bit PCM with interleaved stereo
* @param size Size of output buffer, this should be a multiple of 4 (sizeof(int16) * 2)
*
* @returns Success/failure status; see miniorg_result enum for possible values
**/
MINIORG_API miniorg_result miniorg_render(miniorg_context *ctx, miniorg_int16 *output, miniorg_uint32 size);
#ifdef __cplusplus
}
#endif
#endif /* MINIORG_H_ */
#ifdef MINIORG_IMPLEMENTATION
MINIORG_PRIVATE void miniorg_sound_render(miniorg_sound *sound, miniorg_int32 *output)
{
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
miniorg_uint32 i;
miniorg_uint32 last_position;
miniorg_uint32 difference;
miniorg_int32 margin;
miniorg_int32 sample_a, sample_b, sample_c, sample_d;
miniorg_int32 c0, c1, c2, c3;
miniorg_uint32 t0, t1, t2;
#elif !defined(MINIORG_NO_INTERPOLATION) && defined(MINIORG_FAST_INTERPOLATION)
miniorg_uint32 wrapped_position;
miniorg_int32 sample_last;
#endif
miniorg_int32 sample;
#if defined(MINIORG_NO_INTERPOLATION) || defined(MINIORG_FAST_INTERPOLATION)
if (sound->playing)
#else
if (sound->playing || sound->silence_timer > 0)
#endif
{
/* Volume ramp prevents a click when it changes */
#ifndef MINIORG_NO_VOLUME_RAMP
sound->played_before = MINIORG_TRUE;
if (sound->volume_ticks > 0)
{
sound->volume_left += (sound->target_volume_left - sound->volume_left) / (miniorg_int32) sound->volume_ticks;
sound->volume_right += (sound->target_volume_right - sound->volume_right) / (miniorg_int32) sound->volume_ticks;
--sound->volume_ticks;
}
#endif
/* No interpolation is fastest but it has a crunchy sound */
#if defined(MINIORG_NO_INTERPOLATION)
sample = (sound->sample_signed
? ((miniorg_int8 *) sound->sample)[sound->position]
: (miniorg_int8) (((miniorg_uint8 *) sound->sample)[sound->position] - 0x80)) << 8;
#elif defined(MINIORG_FAST_INTERPOLATION)
/* Linear interpolation is faster but lower quality */
if (sound->loops == -1 || sound->loops > 0)
{
wrapped_position = sound->position == 0 ? sound->sample_size - 1 : sound->position - 1;
sample_last = sound->sample_signed
? ((miniorg_int8 *) sound->sample)[wrapped_position]
: (miniorg_int8) (((miniorg_uint8 *) sound->sample)[wrapped_position] - 0x80);
}
else
{
sample_last = sound->position == 0 ? 0 : (sound->sample_signed
? ((miniorg_int8 *) sound->sample)[sound->position - 1]
: (miniorg_int8) (((miniorg_uint8 *) sound->sample)[sound->position - 1] - 0x80));
}
sample = sound->sample_signed
? ((miniorg_int8 *) sound->sample)[sound->position]
: (miniorg_int8) (((miniorg_uint8 *) sound->sample)[sound->position] - 0x80);
sample = ((sample_last * (0x10000 - (miniorg_int32) sound->sub_position)) >> 8) + ((sample * (miniorg_int32) sound->sub_position) >> 8);
#else
margin = sound->buffer_position - 2;
#define MINIORG_MOD_CLAMP(a, x, y) ((a) >= (x) ? (a) - (x) : ((a) < (y) ? ((a) - (y)) + (x) : (a)))
sample_a = sound->buffer[MINIORG_MOD_CLAMP(margin - 1, MINIORG_SOUND_BUFFER_SIZE, 0)];
sample_b = sound->buffer[MINIORG_MOD_CLAMP(margin, MINIORG_SOUND_BUFFER_SIZE, 0)];
sample_c = sound->buffer[MINIORG_MOD_CLAMP(margin + 1, MINIORG_SOUND_BUFFER_SIZE, 0)];
sample_d = sound->buffer[MINIORG_MOD_CLAMP(margin + 2, MINIORG_SOUND_BUFFER_SIZE, 0)];
#undef MINIORG_MOD_CLAMP
c0 = sample_b;
c1 = ((miniorg_int32) sample_c * 65536 >> 16) - ((miniorg_int32) sample_a * 21845 >> 16)
- ((miniorg_int32) sample_b * 32768 >> 16) - ((miniorg_int32) sample_d * 10923 >> 16);
c2 = (((miniorg_int32) (sample_a + sample_c) * 32768) >> 16) - (((miniorg_int32) sample_b * 65536) >> 16);
c3 = (((miniorg_int32) (sample_d - sample_a) * 10923) >> 16) + (((miniorg_int32) (sample_b - sample_c) * 32768) >> 16);
t0 = sound->sub_position;
t1 = (t0 * t0) >> 16;
t2 = (t1 * t0) >> 16;
#define MINIORG_MUL(c, t) ((((c) >> 16) * (t)) + (((c) & 0xFFFF) * (t) >> 16))
sample = MINIORG_MUL(c3, t2) + MINIORG_MUL(c2, t1) + MINIORG_MUL(c1, t0) + c0;
#undef MINIORG_MUL
#endif
/* Mix and write out */
output[0] += (sample * sound->volume_left) >> 15;
output[1] += (sample * sound->volume_right) >> 15;
/* Increment sample position */
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
last_position = sound->position;
#endif
sound->sub_position += sound->position_increment;
sound->position += sound->sub_position >> 16;
sound->sub_position &= 0xFFFF;
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
/* Update the buffer with new sample data.
* Doing it this way prevents clicking when starting/stopping the sample */
if (sound->position > last_position)
{
difference = sound->position - last_position;
for (i = 0; i < difference; ++i)
{
sound->buffer_position = (sound->buffer_position + 1) % MINIORG_SOUND_BUFFER_SIZE;
if (sound->playing)
{
sound->buffer[sound->buffer_position] = (
sound->sample_signed
? ((miniorg_int8 *) sound->sample)[(last_position + i) % sound->sample_size]
: (miniorg_int8)(((miniorg_uint8 *) sound->sample)[(last_position + i) % sound->sample_size] - 0x80)
) << 8;
/* Check if it ended */
if (last_position + i >= sound->sample_size)
{
if (sound->loops > 0)
{
--sound->loops;
}
if (sound->loops == -1 || sound->loops > 0)
{
sound->position %= sound->sample_size;
}
else
{
sound->position = 0;
sound->playing = MINIORG_FALSE;
sound->buffer[sound->buffer_position] = 0;
sound->silence_timer = MINIORG_SOUND_BUFFER_SIZE - 1;
}
}
}
else
{
sound->buffer[sound->buffer_position] = 0;
--sound->silence_timer;
}
}
}
#else
/* We don't need to do most of that if there is no interpolation */
if (sound->position >= sound->sample_size)
{
if (sound->loops > 0)
{
--sound->loops;
}
if (sound->loops == -1 || sound->loops > 0)
{
sound->position %= sound->sample_size;
}
else
{
sound->playing = MINIORG_FALSE;
}
}
#endif
}
}
MINIORG_PRIVATE void miniorg_stop_sound(miniorg_sound *sound)
{
sound->playing = MINIORG_FALSE;
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
sound->silence_timer = MINIORG_SOUND_BUFFER_SIZE;
#endif
}
MINIORG_PRIVATE void miniorg_play_sound(miniorg_sound *sound, miniorg_int16 loops)
{
if (!sound->playing)
{
sound->position = 0;
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
if (sound->silence_timer == 0)
{
sound->sub_position = 0;
}
#else
sound->sub_position = 0;
#endif
}
if (!sound->playing || (loops >= 0 && sound->loops == -1) || (loops == -1 && sound->loops >= 0))
{
sound->loops = loops;
}
sound->playing = MINIORG_TRUE;
}
MINIORG_PRIVATE void miniorg_set_sound_frequency(miniorg_sound *sound, miniorg_uint32 frequency)
{
miniorg_uint32 f = frequency << 15;
miniorg_uint32 d = f / sound->output_rate;
miniorg_uint32 r = f % sound->output_rate;
sound->frequency = frequency;
sound->position_increment = (d << 1) + ((r << 1) >= sound->output_rate);
}
MINIORG_PRIVATE void miniorg_set_sound_volume(miniorg_sound *sound, miniorg_int16 volume)
{
static const miniorg_uint16 volume_table[] = {
0x0C39, 0x0C39, 0x0C56, 0x0C73, 0x0C90, 0x0C90, 0x0CAE, 0x0CCC, 0x0CEB, 0x0D09, 0x0D09, 0x0D28, 0x0D47, 0x0D67, 0x0D86, 0x0D86,
0x0DA7, 0x0DC7, 0x0DE7, 0x0DE7, 0x0E08, 0x0E2A, 0x0E4B, 0x0E6D, 0x0E6D, 0x0E8F, 0x0EB2, 0x0ED5, 0x0EF8, 0x0EF8, 0x0F1B, 0x0F3F,
0x0F63, 0x0F63, 0x0F88, 0x0FAC, 0x0FD1, 0x0FF7, 0x0FF7, 0x101D, 0x1043, 0x1069, 0x1090, 0x1090, 0x10B8, 0x10DF, 0x1107, 0x112F,
0x112F, 0x1158, 0x1181, 0x11AB, 0x11AB, 0x11D5, 0x11FF, 0x1229, 0x1254, 0x1254, 0x1280, 0x12AC, 0x12D8, 0x1305, 0x1305, 0x1332,
0x135F, 0x138D, 0x138D, 0x13BB, 0x13EA, 0x1419, 0x1449, 0x1449, 0x1479, 0x14A9, 0x14DA, 0x150C, 0x150C, 0x153E, 0x1570, 0x15A3,
0x15A3, 0x15D6, 0x160A, 0x163E, 0x1673, 0x1673, 0x16A8, 0x16DD, 0x1714, 0x174A, 0x174A, 0x1781, 0x17B9, 0x17F1, 0x182A, 0x182A,
0x1863, 0x189D, 0x18D7, 0x18D7, 0x1912, 0x194E, 0x198A, 0x19C6, 0x19C6, 0x1A03, 0x1A41, 0x1A7F, 0x1ABE, 0x1ABE, 0x1AFD, 0x1B3D,
0x1B7E, 0x1B7E, 0x1BBF, 0x1C00, 0x1C43, 0x1C86, 0x1C86, 0x1CC9, 0x1D0D, 0x1D52, 0x1D98, 0x1D98, 0x1DDE, 0x1E25, 0x1E6C, 0x1EB4,
0x1EB4, 0x1EFD, 0x1F46, 0x1F90, 0x1F90, 0x1FDB, 0x2026, 0x2073, 0x20BF, 0x20BF, 0x210D, 0x215B, 0x21AA, 0x21FA, 0x21FA, 0x224B,
0x229C, 0x22EE, 0x22EE, 0x2341, 0x2394, 0x23E8, 0x243D, 0x243D, 0x2493, 0x24EA, 0x2541, 0x259A, 0x259A, 0x25F3, 0x264D, 0x26A7,
0x26A7, 0x2703, 0x275F, 0x27BD, 0x281B, 0x281B, 0x287A, 0x28DA, 0x293A, 0x299C, 0x299C, 0x29FF, 0x2A62, 0x2AC6, 0x2B2C, 0x2B2C,
0x2B92, 0x2BF9, 0x2C61, 0x2C61, 0x2CCB, 0x2D35, 0x2DA0, 0x2E0C, 0x2E0C, 0x2E79, 0x2EE7, 0x2F56, 0x2FC6, 0x2FC6, 0x3037, 0x30AA,
0x311D, 0x311D, 0x3191, 0x3207, 0x327D, 0x32F5, 0x32F5, 0x336D, 0x33E7, 0x3462, 0x34DE, 0x34DE, 0x355B, 0x35DA, 0x3659, 0x3659,
0x36DA, 0x375C, 0x37DF, 0x3864, 0x3864, 0x38E9, 0x3970, 0x39F8, 0x3A81, 0x3A81, 0x3B0C, 0x3B98, 0x3C25, 0x3CB3, 0x3CB3, 0x3D43,
0x3DD4, 0x3E67, 0x3E67, 0x3EFB, 0x3F90, 0x4026, 0x40BE, 0x40BE, 0x4158, 0x41F3, 0x428F, 0x432C, 0x432C, 0x43CC, 0x446C, 0x450E,
0x450E, 0x45B2, 0x4657, 0x46FD, 0x47A6, 0x47A6, 0x484F, 0x48FB, 0x49A8, 0x4A56, 0x4A56, 0x4B06, 0x4BB8, 0x4C6B, 0x4D20, 0xFFFF
};
sound->volume = volume_table[volume];
#ifndef MINIORG_NO_VOLUME_RAMP
sound->target_volume_left = (sound->volume * sound->panning_left) >> 15;
sound->target_volume_right = (sound->volume * sound->panning_right) >> 15;
if (!sound->playing || !sound->played_before)
{
sound->volume_left = sound->target_volume_left;
sound->volume_right = sound->target_volume_right;
sound->volume_ticks = 0;
}
else
{
sound->volume_ticks = 4 * sound->output_rate / 1000;
}
#else
sound->volume_left = (sound->volume * sound->panning_left) >> 15;
sound->volume_right = (sound->volume * sound->panning_right) >> 15;
#endif
}
MINIORG_PRIVATE void miniorg_set_sound_panning(miniorg_sound *sound, miniorg_int16 panning)
{
static const miniorg_uint32 panning_table[] = {
0x06B78000, 0x0B058000, 0x12148000, 0x1DA98000, 0x30AA8000, 0x4FD68000, 0x80008000,
0x80004FD6, 0x800030AA, 0x80001DA9, 0x80001214, 0x80000B05, 0x800006B7
};
if (panning > 13) panning = 0;
sound->panning_left = panning_table[panning] & 0xFFFF;
sound->panning_right = (panning_table[panning] >> 16) & 0xFFFF;
#ifndef MINIORG_NO_VOLUME_RAMP
sound->target_volume_left = (sound->volume * sound->panning_left) >> 15;
sound->target_volume_right = (sound->volume * sound->panning_right) >> 15;
if (!sound->playing || !sound->played_before)
{
sound->volume_left = sound->target_volume_left;
sound->volume_right = sound->target_volume_right;
sound->volume_ticks = 0;
}
else
{
sound->volume_ticks = 4 * sound->output_rate / 1000;
}
#else
sound->volume_left = (sound->volume * sound->panning_left) >> 15;
sound->volume_right = (sound->volume * sound->panning_right) >> 15;
#endif
}
MINIORG_PRIVATE void miniorg_init_sound(miniorg_sound *sound, const void *data, miniorg_uint32 size, miniorg_uint32 rate, miniorg_bool sample_signed)
{
sound->sample = data;
sound->sample_signed = sample_signed;
sound->sample_size = size;
sound->output_rate = rate;
miniorg_set_sound_frequency(sound, 22050);
sound->volume = 1 << 15;
sound->panning_left = 1 << 15;
sound->panning_right = 1 << 15;
sound->playing = MINIORG_FALSE;
sound->loops = 0;
sound->position = 0;
sound->sub_position = 0;
sound->volume_left = (sound->volume * sound->panning_left) >> 15;
sound->volume_right = (sound->volume * sound->panning_right) >> 15;
#ifndef MINIORG_NO_VOLUME_RAMP
sound->target_volume_left = sound->volume_left;
sound->target_volume_right = sound->volume_right;
sound->volume_ticks = 0;
#endif
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
for (sound->buffer_position = 0; sound->buffer_position < MINIORG_SOUND_BUFFER_SIZE; ++sound->buffer_position)
{
sound->buffer[sound->buffer_position] = 0;
}
sound->silence_timer = 0;
sound->buffer_position = 0;
#endif
}
MINIORG_PRIVATE void miniorg_init_channels(miniorg_context *ctx)
{
static const miniorg_int16 shift_table[MINIORG_OCTAVE_COUNT] = {0, 0, 1, 1, 2, 3, 4, 5};
miniorg_uint32 i, j;
miniorg_uint32 channel;
miniorg_uint32 offset;
miniorg_uint16 wave_size;
miniorg_uint16 shift;
for (channel = 0; channel < MINIORG_MELODY_CHANNEL_COUNT; ++channel)
{
ctx->melody_channels[channel].pitch = MINIORG_PROPERTY_NOT_USED;
ctx->melody_channels[channel].volume = MINIORG_PROPERTY_NOT_USED;
ctx->melody_channels[channel].panning = MINIORG_PROPERTY_NOT_USED;
ctx->melody_channels[channel].ticks = 0;
ctx->melody_channels[channel].alt_sound = 0;
offset = channel * MINIORG_GEN_WAVEFORMS_SIZE;
for (i = 0; i < MINIORG_OCTAVE_COUNT; ++i)
{
shift = shift_table[i];
wave_size = MINIORG_WAVE_LENGTH >> shift;
/* Init sound data */
miniorg_init_sound(&ctx->melody_channels[channel].sounds[i][0], &ctx->gen_wave_data[offset], wave_size, ctx->sample_rate, MINIORG_TRUE);
miniorg_init_sound(&ctx->melody_channels[channel].sounds[i][1], &ctx->gen_wave_data[offset], wave_size, ctx->sample_rate, MINIORG_TRUE);
/* Make the waveform
* Since 0/1 and 2/3 are the same waveform, we skip 0 and 2 for this */
if (i != 0 && i != 2)
{
for (j = 0; j < wave_size; ++j)
{
ctx->gen_wave_data[offset + j] = ctx->wave_table[(ctx->song.channels[channel].instrument * MINIORG_WAVE_LENGTH) + (j << shift)];
}
offset += wave_size;
}
}
}
for (channel = 0; channel < MINIORG_PERCUSSION_CHANNEL_COUNT; ++channel)
{
ctx->percussion_channels[channel].pitch = MINIORG_PROPERTY_NOT_USED;
ctx->percussion_channels[channel].volume = MINIORG_PROPERTY_NOT_USED;
ctx->percussion_channels[channel].panning = MINIORG_PROPERTY_NOT_USED;
i = ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].instrument;
miniorg_init_sound(&ctx->percussion_channels[channel].sound, ctx->percussion_samples[i].sample, ctx->percussion_samples[i].sample_size,
ctx->sample_rate, MINIORG_FALSE);
}
}
MINIORG_PRIVATE void miniorg_tick_player(miniorg_context *ctx)
{
static const miniorg_int16 frequency_table[12] = {262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494};
miniorg_uint32 i, j;
miniorg_uint32 channel;
miniorg_uint32 index;
for (channel = 0; channel < MINIORG_MELODY_CHANNEL_COUNT; ++channel)
{
index = ctx->melody_channels[channel].index;
if (index < ctx->song.channels[channel].event_count
&& ctx->song.channels[channel].event_position[index] == ctx->play_position)
{
if (ctx->song.channels[channel].event_pitch[index] != MINIORG_PROPERTY_NOT_USED)
{
if (ctx->melody_channels[channel].pitch != MINIORG_PROPERTY_NOT_USED)
{
if (!ctx->song.channels[channel].pizzicato)
{
miniorg_play_sound(
&ctx->melody_channels[channel].sounds[ctx->melody_channels[channel].pitch / 12][ctx->melody_channels[channel].alt_sound], 0);
}
ctx->melody_channels[channel].alt_sound ^= 1;
}
ctx->melody_channels[channel].pitch = ctx->song.channels[channel].event_pitch[index];
ctx->melody_channels[channel].ticks = ctx->song.channels[channel].event_length[index];
/* Set frequency */
for (i = 0; i < 8; ++i)
{
for (j = 0; j < 2; ++j)
{
miniorg_set_sound_frequency(&ctx->melody_channels[channel].sounds[i][j],
(ctx->melody_channels[channel].sounds[i][j].sample_size * frequency_table[ctx->melody_channels[channel].pitch % 12] * (1 << i))
/ 8 + (ctx->song.channels[channel].finetune - 1000));
}
}
/* Play sound */
miniorg_play_sound(&ctx->melody_channels[channel].sounds[ctx->melody_channels[channel].pitch / 12][ctx->melody_channels[channel].alt_sound],
ctx->song.channels[channel].pizzicato ? (4 + (ctx->melody_channels[channel].pitch / 12) * 4) : -1);
}
if (ctx->song.channels[channel].event_volume[index] != MINIORG_PROPERTY_NOT_USED)
{
ctx->melody_channels[channel].volume = ctx->song.channels[channel].event_volume[index];
if (ctx->melody_channels[channel].pitch != MINIORG_PROPERTY_NOT_USED)
{
miniorg_set_sound_volume(
&ctx->melody_channels[channel].sounds[ctx->melody_channels[channel].pitch / 12][ctx->melody_channels[channel].alt_sound],
ctx->melody_channels[channel].volume);
}
}
if (ctx->song.channels[channel].event_panning[index] != MINIORG_PROPERTY_NOT_USED)
{
ctx->melody_channels[channel].panning = ctx->song.channels[channel].event_panning[index];
if (ctx->melody_channels[channel].pitch != MINIORG_PROPERTY_NOT_USED)
{
miniorg_set_sound_panning(
&ctx->melody_channels[channel].sounds[ctx->melody_channels[channel].pitch / 12][ctx->melody_channels[channel].alt_sound],
ctx->melody_channels[channel].panning);
}
}
++ctx->melody_channels[channel].index;
}
/* Tick note length */
if (ctx->melody_channels[channel].ticks == 0)
{
if (ctx->melody_channels[channel].pitch != MINIORG_PROPERTY_NOT_USED && !ctx->song.channels[channel].pizzicato)
{
/* Stop old sound */
miniorg_play_sound(&ctx->melody_channels[channel].sounds[ctx->melody_channels[channel].pitch / 12][ctx->melody_channels[channel].alt_sound], 0);
ctx->melody_channels[channel].pitch = MINIORG_PROPERTY_NOT_USED;
}
}
else
{
--ctx->melody_channels[channel].ticks;
}
}
for (channel = 0; channel < MINIORG_PERCUSSION_CHANNEL_COUNT; ++channel)
{
index = ctx->percussion_channels[channel].index;
if (index < ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_count
&& ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_position[index] == ctx->play_position)
{
if (ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_pitch[index] != MINIORG_PROPERTY_NOT_USED)
{
ctx->percussion_channels[channel].pitch = ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_pitch[index];
miniorg_stop_sound(&ctx->percussion_channels[channel].sound);
miniorg_set_sound_frequency(&ctx->percussion_channels[channel].sound, ctx->percussion_channels[channel].pitch * 800 + 100);
miniorg_play_sound(&ctx->percussion_channels[channel].sound, 0);
}
if (ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_volume[index] != MINIORG_PROPERTY_NOT_USED)
{
ctx->percussion_channels[channel].volume = ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_volume[index];
miniorg_set_sound_volume(&ctx->percussion_channels[channel].sound, ctx->percussion_channels[channel].volume);
}
if (ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_panning[index] != MINIORG_PROPERTY_NOT_USED)
{
ctx->percussion_channels[channel].panning = ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_panning[index];
miniorg_set_sound_panning(&ctx->percussion_channels[channel].sound, ctx->percussion_channels[channel].panning);
}
++ctx->percussion_channels[channel].index;
}
}
++ctx->play_position;
/* Check for repeat */
if (ctx->play_position >= ctx->song.repeat_end)
{
miniorg_seek(ctx, ctx->song.repeat_start);
}
ctx->tick_timer += ctx->sample_rate * ctx->song.tempo_ms / 1000;
}
#define MINIORG_READ_16(p) ((p)[0] | ((p)[1] << 8))
#define MINIORG_READ_32(p) ((p)[0] | ((p)[1] << 8) | ((p)[2] << 16) | ((p)[3] << 24))
/* Api */
MINIORG_API miniorg_result miniorg_init(miniorg_context *ctx, const miniorg_uint8 *soundbank_data, miniorg_uint32 soundbank_size, miniorg_uint32 rate)
{
miniorg_uint32 i;
miniorg_uint32 offset;
if (!ctx || !soundbank_data || !rate)
{
return MINIORG_RESULT_INVALID_ARGS;
}
/* Valid files can't be smaller than this */
if (soundbank_size < MINIORG_MIN_SOUNDBANK_SIZE)
{
return MINIORG_RESULT_INVALID_DATA;
}
/* Soundbank files are formatted like this:
* 0x100 * 100 bytes, for every melody waveform
* For percussion instruments it's just a uint32 for the sample length
* and then the sample data, repeat that 42 times for each sample */
ctx->wave_table = (miniorg_int8 *) &soundbank_data[0];
offset = MINIORG_WAVE_TABLE_SIZE;
for (i = 0; i < MINIORG_PERCUSSION_COUNT; ++i)
{
if (offset + 4 > soundbank_size)
{
/* EOF happened, not good */
return MINIORG_RESULT_INVALID_DATA;
}
ctx->percussion_samples[i].sample_size = (miniorg_uint32) MINIORG_READ_32(&soundbank_data[offset]);
offset += 4;
/* We also need to check if this is too long */
if (offset + ctx->percussion_samples[i].sample_size > soundbank_size)
{
/* Nope */
return MINIORG_RESULT_INVALID_DATA;
}
ctx->percussion_samples[i].sample = (miniorg_uint8 *) &soundbank_data[offset];
offset += ctx->percussion_samples[i].sample_size;
}
ctx->play_position = 0;
ctx->sample_rate = rate;
ctx->tick_timer = 0;
/* Note, user BETTER NOT FREE song_data!!!! We still read from it until you call uninit (or load another file)... */
return MINIORG_RESULT_SUCCESS;
}
MINIORG_API miniorg_result miniorg_read(miniorg_context *ctx, const miniorg_uint8 *song_data, miniorg_uint32 song_size)
{
miniorg_uint32 i;
miniorg_uint32 offset;
miniorg_uint16 version;
if (!ctx || !song_data)
{
return MINIORG_RESULT_INVALID_ARGS;
}
/* Valid files can't be smaller than this */
if (song_size < MINIORG_MIN_SONG_SIZE)
{
return MINIORG_RESULT_INVALID_DATA;
}
/* Note, user BETTER NOT FREE song_data!!!! We still read from it until you call unload (or load another file)...
* Also, we can be sure about reading up until we start reading events (because of the size check above) */
offset = 0;
if ((miniorg_uint32) MINIORG_READ_32(&song_data[offset]) != 0x2D67724F) /* This should always be "Org-" */
{
/* Yeah, this is not an org, get out of here */
return MINIORG_RESULT_INVALID_DATA;
}
offset += 4;
version = (song_data[offset] - 48) * 10 + (song_data[offset + 1] - 48);
offset += 2;
if (version < 1 || version > 3) /* The only valid versions are "Org-01", "Org-02", and "Org-03" */
{
/* This is a fake org */
return MINIORG_RESULT_INVALID_DATA;
}
/* This is a (seemingly) normal file, let's start reading */
ctx->song.tempo_ms = (miniorg_uint16) MINIORG_READ_16(&song_data[offset]); offset += 2;
ctx->song.beats = song_data[offset]; ++offset;
ctx->song.steps = song_data[offset]; ++offset;
ctx->song.repeat_start = (miniorg_int32) MINIORG_READ_32(&song_data[offset]); offset += 4;
ctx->song.repeat_end = (miniorg_int32) MINIORG_READ_32(&song_data[offset]); offset += 4;
/* Okay, now we read the channel headers. There should always be 16 channels (8 melody, 8 percussion) */
for (i = 0; i < MINIORG_CHANNEL_COUNT; ++i)
{
ctx->song.channels[i].finetune = (miniorg_uint16) MINIORG_READ_16(&song_data[offset]); offset += 2; /* This value is weird */
ctx->song.channels[i].instrument = song_data[offset]; ++offset;
/* Org-01 doesn't have this option, but there's still a byte here in the file structure */
ctx->song.channels[i].pizzicato = (version > 1 ? song_data[offset] : 0); ++offset;
ctx->song.channels[i].event_count = (miniorg_uint16) MINIORG_READ_16(&song_data[offset]); offset += 2;
}
/* Okay, event time */
for (i = 0; i < MINIORG_CHANNEL_COUNT; ++i)
{
if (offset + (ctx->song.channels[i].event_count * MINIORG_EVENT_SIZE) > song_size)
{
/* This is a broken org */
return MINIORG_RESULT_INVALID_DATA;
}
ctx->song.channels[i].event_position = (miniorg_int32 *) &song_data[offset];
offset += 4 * ctx->song.channels[i].event_count;
ctx->song.channels[i].event_pitch = (miniorg_uint8 *) &song_data[offset];
offset += ctx->song.channels[i].event_count;
ctx->song.channels[i].event_length = (miniorg_uint8 *) &song_data[offset];
offset += ctx->song.channels[i].event_count;
ctx->song.channels[i].event_volume = (miniorg_uint8 *) &song_data[offset];
offset += ctx->song.channels[i].event_count;
ctx->song.channels[i].event_panning = (miniorg_uint8 *) &song_data[offset];
offset += ctx->song.channels[i].event_count;
}
/* We're done loading, initialize the channels now */
miniorg_init_channels(ctx);
/* Reset playback */
miniorg_seek(ctx, 0);
ctx->tick_timer = 1;
return MINIORG_RESULT_SUCCESS;
}
#undef MINIORG_READ_16
#undef MINIORG_READ_32
MINIORG_API void miniorg_seek(miniorg_context *ctx, miniorg_int32 position)
{
miniorg_uint32 i, j;
if (!ctx)
{
return;
}
ctx->play_position = position;
/* Get melody indexes */
for (i = 0; i < MINIORG_MELODY_CHANNEL_COUNT; ++i)
{
ctx->melody_channels[i].index = 0;
for (j = 0; j < ctx->song.channels[i].event_count; ++j)
{
if (position <= ctx->song.channels[i].event_position[j])
{
ctx->melody_channels[i].index = j;
break;
}
}