8000 miniorg/miniorg.h at main · Strultz/miniorg · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1454 lines (1210 loc) · 49.8 KB

miniorg.h

File metadata and controls

1454 lines (1210 loc) · 49.8 KB
/* miniorg v1.1.0-dev
# 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_DEFAULT_VOLUME 200
#define MINIORG_DEFAULT_PANNING 6
#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
/* Only used if MINIORG_NO_INTERPOLATION and MINIORG_FAST_INTERPOLATION are not defined */
#define MINIORG_SOUND_BUFFER_SIZE 4
#define MINIORG_COMMENT_FIELD_LENGTH 0x20
#ifndef MINIORG_SCRATCH_LENGTH
#define MINIORG_SCRATCH_LENGTH 0x400 /* Results in 4 * 1024 = 4 KB */
#endif
/* 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 in a structure of arrays layout
* 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_uint8 *event_position;
miniorg_uint8 *event_pitch;
miniorg_uint8 *event_length;
miniorg_uint8 *event_volume;
miniorg_uint8 *event_panning;
}
miniorg_channel;
typedef struct miniorg_comments
{
char name[MINIORG_COMMENT_FIELD_LENGTH + 1];
char artist[MINIORG_COMMENT_FIELD_LENGTH + 1];
char version[MINIORG_COMMENT_FIELD_LENGTH + 1];
char *text;
miniorg_bool open_comments;
}
miniorg_comments;
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_comments comments;
}
miniorg_song;
typedef struct
{
const void *sample;
miniorg_uint32 sample_size;
miniorg_uint32 output_rate;
miniorg_uint32 frequency;
miniorg_uint16 volume;
miniorg_uint16 panning_left;
miniorg_uint16 panning_right;
miniorg_uint16 sample_shift;
miniorg_bool sample_signed;
/* Playback stuff */
miniorg_bool playing;
miniorg_bool looping;
miniorg_uint16 loop_count;
miniorg_uint16 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_bool has_looped;
#endif
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
miniorg_uint8 silence_timer;
miniorg_uint8 buffer_position;
miniorg_int16 buffer[4];
#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_bool muted;
miniorg_uint32 ticks;
miniorg_uint32 index;
}
miniorg_melody_channel;
typedef struct
{
miniorg_sound sound;
miniorg_uint8 pitch;
miniorg_uint8 volume;
miniorg_uint8 panning;
miniorg_bool muted;
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_song song;
miniorg_int32 play_position;
miniorg_uint32 sample_rate;
miniorg_uint32 tick_timer;
miniorg_uint8 org_volume;
miniorg_bool org_fadeout;
miniorg_bool paused;
}
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 8BD0 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);
/**
* Pauses the song playback.
*
* @param ctx Pointer to the miniorg_context structure
**/
MINIORG_API void miniorg_pause(miniorg_context *ctx);
/**
* Resumes the song playback.
*
* @param ctx Pointer to the miniorg_context structure
**/
MINIORG_API void miniorg_resume(miniorg_context *ctx);
/**
* Mutes a channel of the song.
*
* @param ctx Pointer to the miniorg_context structure
* @param channel Index of channel to mute
* @param mute If the channel should be muted
**/
MINIORG_API void miniorg_mute(miniorg_context *ctx, miniorg_uint8 channel, miniorg_bool mute);
/**
* Decreases the song's volume over time. This setting will reset when a new song is loaded.
*
* @param ctx Pointer to the miniorg_context structure
* @param fadeout Enabled or disabled
**/
MINIORG_API void miniorg_fadeout(miniorg_context *ctx, miniorg_bool fadeout);
/**
* Renders 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
#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))
#define MINIORG_LE32A(a, i) ((miniorg_int32) MINIORG_READ_32(&(a)[(i) * 4]))
#define MINIORG_MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#ifdef __GNUC__
#define MINIORG_HOT __attribute__((hot))
#else
#define MINIORG_HOT
#endif
MINIORG_HOT MINIORG_PRIVATE void miniorg_sound_render(miniorg_sound *sound, miniorg_int32 *output, miniorg_uint32 output_count)
{
miniorg_uint32 i;
for (i = 0; i < output_count; ++i)
{
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
miniorg_uint32 j;
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_int32 sample_last;
#endif
miniorg_uint32 wrapped_position;
miniorg_int32 sample;
#if defined(MINIORG_NO_INTERPOLATION) || defined(MINIORG_FAST_INTERPOLATION)
if (!sound->playing)
#else
if (!sound->playing && sound->silence_timer == 0)
#endif
{
break;
}
/* 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 += (miniorg_uint16) ((sound->target_volume_left - sound->volume_left) / (miniorg_int32) sound->volume_ticks);
sound->volume_right += (miniorg_uint16) ((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)
wrapped_position = sound->position << sound->sample_shift;
sample = (sound->sample_signed
? ((miniorg_int8 *) sound->sample)[wrapped_position]
: (miniorg_int8) (((miniorg_uint8 *) sound->sample)[wrapped_position] - 0x80)) << 8;
#elif defined(MINIORG_FAST_INTERPOLATION)
/* Linear interpolation is faster but lower quality */
if (sound->has_looped)
{
wrapped_position = (sound->position == 0 ? sound->sample_size - 1 : sound->position - 1) << sound->sample_shift;
sample_last = sound->sample_signed
? ((miniorg_int8 *) sound->sample)[wrapped_position]
: (miniorg_int8) (((miniorg_uint8 *) sound->sample)[wrapped_position] - 0x80);
}
else
{
wrapped_position = (sound->position - 1) << sound-> 10000 sample_shift;
sample_last = sound->position == 0 ? 0 : (sound->sample_signed
? ((miniorg_int8 *) sound->sample)[wrapped_position]
: (miniorg_int8) (((miniorg_uint8 *) sound->sample)[wrapped_position] - 0x80));
}
wrapped_position = sound->position << sound->sample_shift;
sample = sound->sample_signed
? ((miniorg_int8 *) sound->sample)[wrapped_position]
: (miniorg_int8) (((miniorg_uint8 *) sound->sample)[wrapped_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[i * 2 + 0] += (sample * sound->volume_left) >> 15;
output[i * 2 + 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 (j = 0; j < difference; ++j)
{
sound->buffer_position = (sound->buffer_position + 1) % MINIORG_SOUND_BUFFER_SIZE;
if (sound->playing)
{
wrapped_position = ((last_position + j) % sound->sample_size) << sound->sample_shift;
sound->buffer[sound->buffer_position] = (
sound->sample_signed
? ((miniorg_int8 *) sound->sample)[wrapped_position]
: (miniorg_int8)(((miniorg_uint8 *) sound->sample)[wrapped_position] - 0x80)
) << 8;
/* Check if it ended */
if (last_position + j >= sound->sample_size)
{
++sound->loops;
if (sound->loops >= sound->loop_count)
{
sound->loops = 0;
if (sound->looping)
{
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->position %= sound->sample_size;
}
}
}
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)
{
++sound->loops;
#if !defined(MINIORG_NO_INTERPOLATION) && defined(MINIORG_FAST_INTERPOLATION)
sound->has_looped = MINIORG_TRUE;
#endif
if (sound->loops >= sound->loop_count)
{
sound->loops = 0;
if (sound->looping)
{
sound->position %= sound->sample_size;
}
else
{
sound->playing = MINIORG_FALSE;
}
}
else
{
sound->position %= sound->sample_size;
}
}
#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_bool looping)
{
if (!sound->playing)
{
sound->position = 0;
sound->loops = 0;
#if !defined(MINIORG_NO_INTERPOLATION) && defined(MINIORG_FAST_INTERPOLATION)
sound->has_looped = MINIORG_FALSE;
#endif
#if !defined(MINIORG_NO_INTERPOLATION) && !defined(MINIORG_FAST_INTERPOLATION)
if (sound->silence_timer == 0)
{
sound->sub_position = 0;
}
#else
sound->sub_position = 0;
#endif
}
sound->playing = MINIORG_TRUE;
sound->looping = looping;
}
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_uint8 table, miniorg_uint8 volume)
{
static const miniorg_uint16 volume_table[256] = {
0x0C39, 0x0C56, 0x0C73, 0x0C90, 0x0CAE, 0x0CCC, 0x0CEB, 0x0D09, 0x0D28, 0x0D47, 0x0D67, 0x0D86, 0x0DA7, 0x0DC7, 0x0DE7, 0x0E08,
0x0E2A, 0x0E4B, 0x0E6D, 0x0E8F, 0x0EB2, 0x0ED5, 0x0EF8, 0x0F1B, 0x0F3F, 0x0F63, 0x0F88, 0x0FAC, 0x0FD1, 0x0FF7, 0x101D, 0x1043,
0x1069, 0x1090, 0x10B8, 0x10DF, 0x1107, 0x112F, 0x1158, 0x1181, 0x11AB, 0x11D5, 0x11FF, 0x1229, 0x1254, 0x1280, 0x12AC, 0x12D8,
0x1305, 0x1332, 0x135F, 0x138D, 0x13BB, 0x13EA, 0x1419, 0x1449, 0x1479, 0x14A9, 0x14DA, 0x150C, 0x153E, 0x1570, 0x15A3, 0x15D6,
0x160A, 0x163E, 0x1673, 0x16A8, 0x16DD, 0x1714, 0x174A, 0x1781, 0x17B9, 0x17F1, 0x182A, 0x1863, 0x189D, 0x18D7, 0x1912, 0x194E,
0x198A, 0x19C6, 0x1A03, 0x1A41, 0x1A7F, 0x1ABE, 0x1AFD, 0x1B3D, 0x1B7E, 0x1BBF, 0x1C00, 0x1C43, 0x1C86, 0x1CC9, 0x1D0D, 0x1D52,
0x1D98, 0x1DDE, 0x1E25, 0x1E6C, 0x1EB4, 0x1EFD, 0x1F46, 0x1F90, 0x1FDB, 0x2026, 0x2073, 0x20BF, 0x210D, 0x215B, 0x21AA, 0x21FA,
0x224B, 0x229C, 0x22EE, 0x2341, 0x2394, 0x23E8, 0x243D, 0x2493, 0x24EA, 0x2541, 0x259A, 0x25F3, 0x264D, 0x26A7, 0x2703, 0x275F,
0x27BD, 0x281B, 0x287A, 0x28DA, 0x293A, 0x299C, 0x29FF, 0x2A62, 0x2AC6, 0x2B2C, 0x2B92, 0x2BF9, 0x2C61, 0x2CCB, 0x2D35, 0x2DA0,
0x2E0C, 0x2E79, 0x2EE7, 0x2F56, 0x2FC6, 0x3037, 0x30AA, 0x311D, 0x3191, 0x3207, 0x327D, 0x32F5, 0x336D, 0x33E7, 0x3462, 0x34DE,
0x355B, 0x35DA, 0x3659, 0x36DA, 0x375C, 0x37DF, 0x3864, 0x38E9, 0x3970, 0x39F8, 0x3A81, 0x3B0C, 0x3B98, 0x3C25, 0x3CB3, 0x3D43,
0x3DD4, 0x3E67, 0x3EFB, 0x3F90, 0x4026, 0x40BE, 0x4158, 0x41F3, 0x428F, 0x432C, 0x43CC, 0x446C, 0x450E, 0x45B2, 0x4657, 0x46FD,
0x47A6, 0x484F, 0x48FB, 0x49A8, 0x4A56, 0x4B06, 0x4BB8, 0x4C6B, 0x4D20, 0x4DD7, 0x4E8F, 0x4F49, 0x5005, 0x50C3, 0x5182, 0x5243,
0x5306, 0x53CB, 0x5491, 0x5559, 0x5624, 0x56F0, 0x57BE, 0x588D, 0x595F, 0x5A33, 0x5B09, 0x5BE0, 0x5CBA, 0x5D95, 0x5E73, 0x5F53,
0x6035, 0x6119, 0x61FF, 0x62E7, 0x63D1, 0x64BD, 0x65AC, 0x669D, 0x6790, 0x6885, 0x697D, 0x6A77, 0x6B73, 0x6C71, 0x6D72, 0x6E76,
0x6F7B, 0x7083, 0x718E, 0x729B, 0x73AA, 0x74BC, 0x75D1, 0x76E8, 0x7802, 0x791E, 0x7A3D, 0x7B5E, 0x7C82, 0x7DA9, 0x7ED3, 0x8000
};
if (table > 50)
{
table = 50;
}
sound->volume = volume_table[volume * table * 2 / 0x7F];
#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,
miniorg_uint16 loop_count, miniorg_uint16 shift)
{
sound->sample = data;
sound->sample_signed = sample_signed;
sound->sample_size = size >> shift;
sound->sample_shift = shift;
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->looping = MINIORG_FALSE;
sound->loop_count = loop_count;
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;
sound->played_before = MINIORG_FALSE;
#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};
#ifdef MINIORG_EMULATE_CS
static const miniorg_int16 percussion_table[MINIORG_PERCUSSION_CHANNEL_COUNT] = {0, 2, 5, 6, 4, 8};
#endif
miniorg_uint32 i;
miniorg_uint32 channel;
miniorg_uint16 loops;
for (channel = 0; channel < MINIORG_MELODY_CHANNEL_COUNT; ++channel)
{
ctx->melody_channels[channel].pitch = MINIORG_PROPERTY_NOT_USED;
#ifndef MINIORG_EMULATE_CS
ctx->melody_channels[channel].volume = MINIORG_DEFAULT_VOLUME;
#endif
ctx->melody_channels[channel].panning = MINIORG_DEFAULT_PANNING;
ctx->melody_channels[channel].ticks = 0;
ctx->melody_channels[channel].alt_sound = 0;
for (i = 0; i < MINIORG_OCTAVE_COUNT; ++i)
{
loops = 1;
if (ctx->song.channels[channel].pizzicato)
{
loops = (miniorg_uint16) (4 + i * 4);
}
/* Init sound data */
miniorg_init_sound(&ctx->melody_channels[channel].sounds[i][0], &ctx->wave_table[ctx->song.channels[channel].instrument * MINIORG_WAVE_LENGTH],
MINIORG_WAVE_LENGTH, ctx->sample_rate, MINIORG_TRUE, loops, shift_table[i]);
miniorg_init_sound(&ctx->melody_channels[channel].sounds[i][1], &ctx->wave_table[ctx->song.channels[channel].instrument * MINIORG_WAVE_LENGTH],
MINIORG_WAVE_LENGTH, ctx->sample_rate, MINIORG_TRUE, loops, shift_table[i]);
}
}
for (channel = 0; channel < MINIORG_PERCUSSION_CHANNEL_COUNT; ++channel)
{
ctx->percussion_channels[channel].pitch = MINIORG_PROPERTY_NOT_USED;
#ifndef MINIORG_EMULATE_CS
ctx->percussion_channels[channel].volume = MINIORG_DEFAULT_VOLUME;
#endif
ctx->percussion_channels[channel].panning = MINIORG_DEFAULT_PANNING;
#ifdef MINIORG_EMULATE_CS
if (ctx->percussion_channels[channel].sound.sample)
{
continue;
}
i = percussion_table[channel];
#else
i = ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].instrument;
#endif
miniorg_init_sound(&ctx->percussion_channels[channel].sound, ctx->percussion_samples[i].sample, ctx->percussion_samples[i].sample_size,
ctx->sample_rate, MINIORG_FALSE, 1, 0);
}
}
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;
if (ctx->org_fadeout && ctx->org_volume > 0)
{
--ctx->org_volume;
}
for (channel = 0; channel < MINIORG_MELODY_CHANNEL_COUNT; ++channel)
{
index = ctx->melody_channels[channel].index;
if (index < ctx->song.channels[channel].event_count
#ifdef MINIORG_EMULATE_CS
&& !ctx->melody_channels[channel].muted
#endif
&& MINIORG_LE32A(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)
{
#ifndef MINIORG_EMULATE_CS
if (!ctx->song.channels[channel].pizzicato)
#endif
{
miniorg_play_sound(
&ctx->melody_channels[channel].sounds[ctx->melody_channels[channel].pitch / 12][ctx->melody_channels[channel].alt_sound], MINIORG_FALSE);
}
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],
#ifdef MINIORG_EMULATE_CS
MINIORG_TRUE);
#else
!ctx->song.channels[channel].pizzicato);
#endif
}
if (ctx->song.channels[channel].event_volume[index] != MINIORG_PROPERTY_NOT_USED)
{
ctx->melody_channels[channel].volume = ctx->song.channels[channel].event_volume[index];
#ifndef MINIORG_EMULATE_CS
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->org_volume, ctx->melody_channels[channel].volume);
}
#endif
}
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
#ifndef MINIORG_EMULATE_CS
&& !ctx->song.channels[channel].pizzicato
#endif
)
{
/* Stop old sound */
miniorg_play_sound(&ctx->melody_channels[channel].sounds[ctx->melody_channels[channel].pitch / 12][ctx->melody_channels[channel].alt_sound], MINIORG_FALSE);
ctx->melody_channels[channel].pitch = MINIORG_PROPERTY_NOT_USED;
}
}
else
{
--ctx->melody_channels[channel].ticks;
}
#ifdef MINIORG_EMULATE_CS
if (ctx->melody_channels[channel].index < ctx->song.channels[channel].event_count && 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->org_volume, ctx->melody_channels[channel].volume);
}
#endif
}
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
#ifdef MINIORG_EMULATE_CS
&& !ctx->percussion_channels[channel].muted
#endif
&& MINIORG_LE32A(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, MINIORG_FALSE);
}
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];
#ifndef MINIORG_EMULATE_CS
miniorg_set_sound_volume(&ctx->percussion_channels[channel].sound, ctx->org_volume, ctx->percussion_channels[channel].volume);
#endif
}
6526
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;
}
#ifdef MINIORG_EMULATE_CS
if (ctx->percussion_channels[channel].index < ctx->song.channels[MINIORG_MELODY_CHANNEL_COUNT + channel].event_count)
{
miniorg_set_sound_volume(&ctx->percussion_channels[channel].sound, ctx->org_volume, ctx->percussion_channels[channel].volume);
}
#endif
}
++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;
}
/* 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;
0