Formatting

This commit is contained in:
neauoire 2023-10-20 11:54:25 -04:00
parent c4ec4a6340
commit 87d6798593
3 changed files with 242 additions and 229 deletions

View File

@ -55,6 +55,8 @@ typedef struct AudioChannel {
AudioChannel channel[POLYPHONY];
/* clang-format off */
const float tuning[109] = {
0.00058853f, 0.00062352f, 0.00066060f, 0.00069988f, 0.00074150f,
0.00078559f, 0.00083230f, 0.00088179f, 0.00093423f, 0.00098978f,
@ -80,34 +82,38 @@ const float tuning[109] = {
0.25338348f, 0.26845044f, 0.28441334f, 0.30132544f,
};
/* clang-format on */
void
env_on(Envelope *env) {
env_on(Envelope *env)
{
env->stage = ENV_ATTACK;
env->vol = 0.0f;
if (env->a > 0) {
if(env->a > 0) {
env->a = (SOUND_TIMER / AUDIO_BUFSIZE) / env->a;
} else if (env->stage == ENV_ATTACK) {
} else if(env->stage == ENV_ATTACK) {
env->stage = ENV_DECAY;
env->vol = 1.0f;
}
if (env->d < 10.0f) {
if(env->d < 10.0f) {
env->d = 10.0f;
}
env->d = (SOUND_TIMER / AUDIO_BUFSIZE) / env->d;
if (env->r < 10.0f) {
if(env->r < 10.0f) {
env->r = 10.0f;
}
env->r = (SOUND_TIMER / AUDIO_BUFSIZE) / env->r;
}
void
env_off(Envelope *env) {
env_off(Envelope *env)
{
env->stage = ENV_RELEASE;
}
void
note_on(AudioChannel *channel, Uint16 duration, Uint8 *data, Uint16 len, Uint8 vol,
Uint8 attack, Uint8 decay, Uint8 sustain, Uint8 release, Uint8 pitch, bool loop) {
note_on(AudioChannel *channel, Uint16 duration, Uint8 *data, Uint16 len, Uint8 vol, Uint8 attack, Uint8 decay, Uint8 sustain, Uint8 release, Uint8 pitch, bool loop)
{
channel->duration = duration > 0 ? duration : len / 44.1f;
channel->vol_l = (vol >> 4) / 15.0f;
channel->vol_r = (vol & 0xf) / 15.0f;
@ -120,45 +126,47 @@ note_on(AudioChannel *channel, Uint16 duration, Uint8 *data, Uint16 len, Uint8 v
sample.env.d = decay * 64.0f;
sample.env.s = sustain / 16.0f;
sample.env.r = release * 64.0f;
if (loop) {
if(loop) {
sample.loop = len;
} else {
sample.loop = 0;
}
env_on(&sample.env);
if (pitch < 20) {
if(pitch < 20) {
pitch = 20;
}
float sample_rate = 44100 / 261.60;
if (len <= 256) {
if(len <= 256) {
sample_rate = len;
}
const float *inc = &tuning[pitch - 20];
sample.inc = *(inc) * sample_rate;
sample.inc = *(inc)*sample_rate;
channel->next_sample = sample;
channel->xfade = true;
}
void
note_off(AudioChannel *channel, Uint16 duration) {
note_off(AudioChannel *channel, Uint16 duration)
{
channel->duration = duration;
env_off(&channel->sample.env);
}
void
env_advance(Envelope *env) {
switch (env->stage) {
env_advance(Envelope *env)
{
switch(env->stage) {
case ENV_ATTACK: {
env->vol += env->a;
if (env->vol >= 1.0f) {
if(env->vol >= 1.0f) {
env->stage = ENV_DECAY;
env->vol = 1.0f;
}
} break;
case ENV_DECAY: {
env->vol -= env->d;
if (env->vol <= env->s || env->d <= 0) {
if(env->vol <= env->s || env->d <= 0) {
env->stage = ENV_SUSTAIN;
env->vol = env->s;
}
@ -167,7 +175,7 @@ env_advance(Envelope *env) {
env->vol = env->s;
} break;
case ENV_RELEASE: {
if (env->vol <= 0 || env->r <= 0) {
if(env->vol <= 0 || env->r <= 0) {
env->vol = 0;
} else {
env->vol -= env->r;
@ -177,7 +185,8 @@ env_advance(Envelope *env) {
}
float
interpolate_sample(Uint8 *data, Uint16 len, float pos) {
interpolate_sample(Uint8 *data, Uint16 len, float pos)
{
#if INTERPOL_METHOD == 0
return data[(int)pos];
@ -211,13 +220,14 @@ interpolate_sample(Uint8 *data, Uint16 len, float pos) {
}
Sint16
next_sample(Sample *sample) {
if (sample->pos >= sample->len) {
if (sample->loop == 0) {
next_sample(Sample *sample)
{
if(sample->pos >= sample->len) {
if(sample->loop == 0) {
sample->data = 0;
return 0;
}
while (sample->pos >= sample->len) {
while(sample->pos >= sample->len) {
sample->pos -= sample->loop;
}
}
@ -232,29 +242,30 @@ next_sample(Sample *sample) {
}
void
audio_handler(void *ctx, Uint8 *out_stream, int len) {
audio_handler(void *ctx, Uint8 *out_stream, int len)
{
Sint16 *stream = (Sint16 *)out_stream;
memset(stream, 0x00, len);
int n;
for (n = 0; n < POLYPHONY; n++) {
for(n = 0; n < POLYPHONY; n++) {
Uint8 device = (3 + n) << 4;
Uxn *u = (Uxn *)ctx;
Uint8 *addr = &u->dev[device];
if (channel[n].duration <= 0 && PEEK2(addr)) {
if(channel[n].duration <= 0 && PEEK2(addr)) {
uxn_eval(u, PEEK2(addr));
}
channel[n].duration -= SOUND_TIMER;
int x = 0;
if (channel[n].xfade) {
if(channel[n].xfade) {
float delta = 1.0f / (XFADE_SAMPLES * 2);
while (x < XFADE_SAMPLES * 2) {
while(x < XFADE_SAMPLES * 2) {
float alpha = x * delta;
float beta = 1.0f - alpha;
Sint16 next_a = next_sample(&channel[n].next_sample);
Sint16 next_b = 0;
if (channel[n].sample.data != 0) {
if(channel[n].sample.data != 0) {
next_b = next_sample(&channel[n].sample);
}
Sint16 next = alpha * next_a + beta * next_b;
@ -265,8 +276,8 @@ audio_handler(void *ctx, Uint8 *out_stream, int len) {
channel[n].xfade = false;
}
Sample *sample = &channel[n].sample;
while (x < len / 2) {
if (sample->data == 0) {
while(x < len / 2) {
if(sample->data == 0) {
break;
}
Sint16 next = next_sample(sample);
@ -275,7 +286,7 @@ audio_handler(void *ctx, Uint8 *out_stream, int len) {
}
}
int i;
for (i = 0; i < len / 2; i++) {
for(i = 0; i < len / 2; i++) {
stream[i] <<= 6;
}
}
@ -286,7 +297,7 @@ audio_start(int idx, Uint8 *d, Uxn *u)
Uint16 duration = PEEK2(d + 0x5);
Uint8 off = d[0xf] == 0x00;
if (!off) {
if(!off) {
Uint16 addr = PEEK2(d + 0xc);
Uint8 *data = &u->ram[addr];
Uint16 len = PEEK2(d + 0xa);
@ -305,11 +316,13 @@ audio_start(int idx, Uint8 *d, Uxn *u)
}
Uint8
audio_get_vu(int instance) {
audio_get_vu(int instance)
{
return channel[instance].sample.env.vol * 255.0f;
}
Uint16
audio_get_position(int instance) {
audio_get_position(int instance)
{
return channel[instance].sample.pos;
}