2021-12-18 20:58:21 +00:00
|
|
|
// audio.c
|
|
|
|
|
2021-12-22 23:20:51 +00:00
|
|
|
// TODO: add something to make sure, we KNOW if we've dropped audio
|
|
|
|
// eg. buffer has gone unfilled for a frame or more
|
|
|
|
|
2021-12-23 12:46:00 +00:00
|
|
|
void trigger_sounds(Ampler_state *state) {
|
|
|
|
// TODO: Check sound names
|
|
|
|
// TODO: func to decode orca numbers
|
|
|
|
|
2021-12-24 07:37:18 +00:00
|
|
|
foreach_ptr(Sound_src, s, state -> sounds)
|
|
|
|
if s->state == SND_LOOPING do {
|
|
|
|
// THIS IS ALL JUST So I PLAY WITH THIS LOOP
|
|
|
|
// s->speed = 1.65f;
|
|
|
|
// s->note_speed = 0.84f;
|
|
|
|
// f32 sp = 0.9f;
|
|
|
|
// s->start = (s->track_len / 10) * sp;
|
|
|
|
// s->end = (s->track_len / 10) * (sp + 0.2f);
|
|
|
|
}
|
|
|
|
|
2021-12-23 12:46:00 +00:00
|
|
|
// TODO: NOTE SPEED
|
|
|
|
foreach_ptr(Udp_msg, m, state -> messages)
|
|
|
|
if m -> state == MSG_TRIGGER do
|
|
|
|
foreach_ptr(Sound_src, s, state -> sounds)
|
|
|
|
if s -> state == SND_STOPPED do {
|
|
|
|
s -> state = SND_PLAYING;
|
|
|
|
s -> speed = 7.01f;
|
|
|
|
s -> pos = 0.0f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if s -> state == SND_PLAYING do {
|
|
|
|
s -> speed = -1.51f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 01:37:24 +00:00
|
|
|
void sound_src_frame(Sound_src *s, s8 *mix[]) {
|
2021-12-24 05:21:23 +00:00
|
|
|
if s->state != SND_LOOPING and s->state != SND_PLAYING do
|
|
|
|
return;
|
|
|
|
|
2021-12-20 02:06:02 +00:00
|
|
|
for int i = 0; i < FRAME_SAMPLES; i += 1 do {
|
|
|
|
for int c = 0; c < CHANNELS; c += 1 do
|
2021-12-21 01:37:24 +00:00
|
|
|
mix[c][i] += s -> tracks[c][(int) s -> pos];
|
2021-12-20 02:06:02 +00:00
|
|
|
|
2021-12-24 05:21:23 +00:00
|
|
|
s -> pos += s->speed * s->note_speed;
|
2021-12-23 12:46:00 +00:00
|
|
|
|
|
|
|
if s -> state == SND_LOOPING do {
|
|
|
|
while s -> pos < s -> start do
|
|
|
|
s -> pos += (s->end - s->start);
|
|
|
|
while s -> pos >= s -> end do
|
|
|
|
s -> pos -= (s->end - s->start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: THIS WILL BREAK REVERSE SOUNDS
|
|
|
|
// FIXME: CHECK SIGN OF SPEED
|
|
|
|
if s -> state == SND_PLAYING do
|
|
|
|
if s->pos < s->start or s->pos >= s->end do {
|
|
|
|
s -> pos = s->start;
|
|
|
|
s -> state = SND_STOPPED;
|
|
|
|
}
|
2021-12-20 02:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 20:58:21 +00:00
|
|
|
int audio_frame(Ampler_state *state) {
|
|
|
|
int queued = SDL_GetQueuedAudioSize(state -> playdev);
|
|
|
|
queued /= sizeof s16; // queued is in bytes
|
|
|
|
|
2021-12-19 22:44:48 +00:00
|
|
|
if queued > FRAME_SAMPLES * CHANNELS * 2 do return 0;
|
2021-12-18 20:58:21 +00:00
|
|
|
// else puts("queued audio.");
|
|
|
|
|
2021-12-23 12:46:00 +00:00
|
|
|
trigger_sounds(state);
|
|
|
|
|
2021-12-21 01:37:24 +00:00
|
|
|
// TODO: We should use CHANNELS here
|
2021-12-24 05:21:23 +00:00
|
|
|
// static s8 mix_l[FRAME_SAMPLES] = { 0 }, mix_r[FRAME_SAMPLES] = { 0 };
|
|
|
|
s8 *mix_l = state->frame_mix[0], *mix_r = state->frame_mix[1];
|
|
|
|
SDL_memset(mix_l, 0, FRAME_SAMPLES / sizeof s8);
|
|
|
|
SDL_memset(mix_r, 0, FRAME_SAMPLES / sizeof s8);
|
2021-12-20 00:03:14 +00:00
|
|
|
|
2021-12-20 02:06:02 +00:00
|
|
|
s8 *mix[] = { mix_l, mix_r };
|
2021-12-24 05:21:23 +00:00
|
|
|
foreach_ptr(Sound_src, snd, state -> sounds)
|
|
|
|
sound_src_frame(snd, mix);
|
2021-12-20 02:06:02 +00:00
|
|
|
|
|
|
|
const int vol = 100;
|
|
|
|
static s16 frame[FRAME_SAMPLES * CHANNELS] = { 0 };
|
|
|
|
for int i = 0; i < FRAME_SAMPLES; i += 1 do
|
2021-12-20 00:03:14 +00:00
|
|
|
for int t = 0; t < CHANNELS; t += 1 do
|
2021-12-20 02:06:02 +00:00
|
|
|
frame[i * CHANNELS + t] = ((s16) (mix[t][i])) * vol;
|
2021-12-20 00:03:14 +00:00
|
|
|
|
2021-12-19 22:44:48 +00:00
|
|
|
if SDL_QueueAudio(state -> playdev, frame, sizeof frame) do
|
2021-12-18 20:58:21 +00:00
|
|
|
puts(SDL_GetError());
|
|
|
|
|
|
|
|
return 1; // audio was qued
|
|
|
|
}
|
|
|
|
|
2021-12-24 05:21:23 +00:00
|
|
|
void load_sample(Ampler_state *state, const char *file_name) {
|
2021-12-22 23:20:51 +00:00
|
|
|
Sound_src *snd = NULL;
|
|
|
|
foreach_ptr(Sound_src, s, state -> sounds)
|
|
|
|
if s -> state == SND_FREE do {
|
|
|
|
snd = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if snd == NULL do {
|
|
|
|
puts("error: all sound sources in use");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-19 22:44:48 +00:00
|
|
|
SDL_AudioSpec spec;
|
|
|
|
s16 *buffer = NULL;
|
|
|
|
u32 bytes = 0;
|
|
|
|
|
2021-12-24 05:21:23 +00:00
|
|
|
SDL_LoadWAV(file_name, &spec, &buffer, &bytes);
|
2021-12-19 22:44:48 +00:00
|
|
|
|
|
|
|
if spec.format != AUDIO_S16 do {
|
|
|
|
puts("error: sample.wav is not s16");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-20 00:03:14 +00:00
|
|
|
const int chans = spec.channels;
|
2021-12-21 01:37:24 +00:00
|
|
|
const int length = (bytes / sizeof s16) / chans;
|
2021-12-20 00:03:14 +00:00
|
|
|
|
2021-12-23 12:46:00 +00:00
|
|
|
snd -> track_len = length;
|
2021-12-21 01:37:24 +00:00
|
|
|
snd -> speed = 1.0f;
|
|
|
|
snd -> pos = 0.0f;
|
2021-12-23 12:46:00 +00:00
|
|
|
snd -> start = 0;
|
|
|
|
snd -> end = snd->track_len;
|
2021-12-22 23:20:51 +00:00
|
|
|
snd -> state = SND_STOPPED;
|
2021-12-24 05:21:23 +00:00
|
|
|
snd -> note_speed = 1.0f; // TODO: Look up a note ?
|
|
|
|
SDL_memset(snd->name, 0, sizeof snd->name);
|
|
|
|
for int i = 0; i < arraylen(snd->name) - 1; i += 1 do
|
|
|
|
if file_name[i] == '\0' or file_name[i] == '.' do break;
|
|
|
|
else snd->name[i] = file_name[i];
|
2021-12-19 22:44:48 +00:00
|
|
|
|
2021-12-26 17:53:39 +00:00
|
|
|
// NOTE: This is just here for gur sake of testing audio works
|
2021-12-24 05:21:23 +00:00
|
|
|
if snd == &(state->sounds[0]) do
|
|
|
|
snd -> state = SND_LOOPING;
|
2021-12-23 12:46:00 +00:00
|
|
|
|
2021-12-21 01:37:24 +00:00
|
|
|
for int i = 0; i < CHANNELS; i += 1 do
|
2021-12-23 12:46:00 +00:00
|
|
|
snd -> tracks[i] = malloc(snd -> track_len);
|
2021-12-19 22:44:48 +00:00
|
|
|
|
2021-12-24 05:21:23 +00:00
|
|
|
// TODO: Change to a malloc wrapper that does gur error
|
|
|
|
// checking for us and also adds ptrs to gur half gc
|
2021-12-21 01:37:24 +00:00
|
|
|
for int i = 0; i < CHANNELS; i += 1 do
|
2021-12-22 23:20:51 +00:00
|
|
|
if !(snd -> tracks[i]) do
|
2021-12-21 01:37:24 +00:00
|
|
|
puts("fffuuuck");
|
2021-12-19 22:44:48 +00:00
|
|
|
|
2021-12-23 12:46:00 +00:00
|
|
|
for int i = 0; i < snd -> track_len; i += 1 do
|
2021-12-21 01:37:24 +00:00
|
|
|
for int t = 0; t < chans; t += 1 do
|
|
|
|
snd -> tracks[t][i] = (s8) (buffer[i * chans + t] >> 8);
|
2021-12-20 02:06:02 +00:00
|
|
|
|
2021-12-19 22:44:48 +00:00
|
|
|
SDL_FreeWAV((void *) buffer);
|
2021-12-21 01:37:24 +00:00
|
|
|
|
|
|
|
printf("loaded sample.wav %f seconds.\n",
|
|
|
|
((f32) length) / ((f32) SAMPLE_RATE));
|
2021-12-19 22:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void load_track_f32() { // idk if we're ever gonna be loading floats
|
|
|
|
/*
|
|
|
|
f32 max = 0.0f;
|
|
|
|
for int i = 0; i < len; i++ do
|
|
|
|
if buffer[i] > max do
|
|
|
|
max = buffer[i];
|
|
|
|
else if buffer[i] < -max do
|
|
|
|
max = -buffer[i];
|
|
|
|
printf("%f max\n", max);
|
|
|
|
for int i = 0; i < len; i += 2 do {
|
|
|
|
buffer[i] /= max;
|
|
|
|
buffer[i + 1] /= max;
|
|
|
|
state -> track_l[i] = ((s8)(buffer[i] * 127.0f));
|
|
|
|
state -> track_r[i] = ((s8)(buffer[i + 1] * 127.0f));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|