sounds now have start and end fields

master
bxwtf 2021-12-23 12:46:00 +00:00
parent 2680732685
commit 0bb207bbca
4 changed files with 62 additions and 17 deletions

View File

@ -67,7 +67,8 @@ int ampler_main(SDL_Window *w, SDL_Renderer *r, Ampler_state **u_data) {
state -> played_audio_last_frame = audio_frame(state); state -> played_audio_last_frame = audio_frame(state);
draw_frame(w, r, state); draw_frame(w, r, state);
// CLEAR UDP MESSAGES // CLEAR UDP MESSAGES, but only after we've played them
if state -> played_audio_last_frame do
foreach_ptr(Udp_msg, msg, state -> messages) foreach_ptr(Udp_msg, msg, state -> messages)
msg -> state = MSG_FREE; msg -> state = MSG_FREE;

View File

@ -40,9 +40,11 @@ for ; N##_i < arraylen(ARRAY); N##_i += 1, N = &((ARRAY)[N##_i]) do
typedef struct Sound_src Sound_src; typedef struct Sound_src Sound_src;
struct Sound_src { struct Sound_src {
s8 *tracks[CHANNELS]; s8 *tracks[CHANNELS];
s32 len; // in samples s32 track_len; // size of track buffers, in samples
f32 pos; // position in samples f32 pos; // position in samples
f32 speed; // in samples, per sample, can be negative f32 speed; // in samples, per sample, can be negative
s32 start; // play start in samples
s32 end; // play end in samples
enum { SND_FREE = 0, SND_STOPPED, SND_PLAYING, SND_LOOPING, } state; enum { SND_FREE = 0, SND_STOPPED, SND_PLAYING, SND_LOOPING, } state;
}; };

56
audio.c
View File

@ -3,17 +3,48 @@
// TODO: add something to make sure, we KNOW if we've dropped audio // TODO: add something to make sure, we KNOW if we've dropped audio
// eg. buffer has gone unfilled for a frame or more // eg. buffer has gone unfilled for a frame or more
void trigger_sounds(Ampler_state *state) {
// TODO: Check sound names
// TODO: func to decode orca numbers
// 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;
}
}
void sound_src_frame(Sound_src *s, s8 *mix[]) { void sound_src_frame(Sound_src *s, s8 *mix[]) {
if s -> state == SND_LOOPING do if s -> state == SND_LOOPING or s -> state == SND_PLAYING do
for int i = 0; i < FRAME_SAMPLES; i += 1 do { for int i = 0; i < FRAME_SAMPLES; i += 1 do {
for int c = 0; c < CHANNELS; c += 1 do for int c = 0; c < CHANNELS; c += 1 do
mix[c][i] += s -> tracks[c][(int) s -> pos]; mix[c][i] += s -> tracks[c][(int) s -> pos];
s -> pos += s -> speed; s -> pos += s -> speed;
while s -> pos < 0.0f do
s -> pos += s -> len; if s -> state == SND_LOOPING do {
while s -> pos >= s -> len do while s -> pos < s -> start do
s -> pos -= s -> len; 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;
}
} }
} }
@ -24,6 +55,8 @@ int audio_frame(Ampler_state *state) {
if queued > FRAME_SAMPLES * CHANNELS * 2 do return 0; if queued > FRAME_SAMPLES * CHANNELS * 2 do return 0;
// else puts("queued audio."); // else puts("queued audio.");
trigger_sounds(state);
// TODO: We should use CHANNELS here // TODO: We should use CHANNELS here
static s8 mix_l[FRAME_SAMPLES] = { 0 }, mix_r[FRAME_SAMPLES] = { 0 }; static s8 mix_l[FRAME_SAMPLES] = { 0 }, mix_r[FRAME_SAMPLES] = { 0 };
SDL_memset(mix_l, 0, sizeof mix_l); SDL_memset(mix_l, 0, sizeof mix_l);
@ -71,20 +104,25 @@ void load_track(Ampler_state *state) {
const int chans = spec.channels; const int chans = spec.channels;
const int length = (bytes / sizeof s16) / chans; const int length = (bytes / sizeof s16) / chans;
snd -> len = length; snd -> track_len = length;
snd -> speed = 1.0f; snd -> speed = 1.0f;
snd -> pos = 0.0f; snd -> pos = 0.0f;
// snd -> state = SND_LOOPING; snd -> start = 0;
snd -> end = snd->track_len;
snd -> state = SND_STOPPED; snd -> state = SND_STOPPED;
snd -> state = SND_LOOPING;
snd -> end /= 2;
snd -> start = snd -> end / 2;
for int i = 0; i < CHANNELS; i += 1 do for int i = 0; i < CHANNELS; i += 1 do
snd -> tracks[i] = malloc(snd -> len); snd -> tracks[i] = malloc(snd -> track_len);
for int i = 0; i < CHANNELS; i += 1 do for int i = 0; i < CHANNELS; i += 1 do
if !(snd -> tracks[i]) do if !(snd -> tracks[i]) do
puts("fffuuuck"); puts("fffuuuck");
for int i = 0; i < snd -> len; i += 1 do for int i = 0; i < snd -> track_len; i += 1 do
for int t = 0; t < chans; t += 1 do for int t = 0; t < chans; t += 1 do
snd -> tracks[t][i] = (s8) (buffer[i * chans + t] >> 8); snd -> tracks[t][i] = (s8) (buffer[i * chans + t] >> 8);

6
draw.c
View File

@ -5,6 +5,7 @@ void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
//SDL_SetRenderDrawColor(r, 10, 10, 10, 100); //SDL_SetRenderDrawColor(r, 10, 10, 10, 100);
SDL_RenderClear(r); SDL_RenderClear(r);
// TODO: DRAW WITH X SCALE FACTOR
// TODO: DRAW SMOLER // TODO: DRAW SMOLER
SDL_SetRenderDrawColor(r, 0xFF, 0x00, 0x4D, 255); SDL_SetRenderDrawColor(r, 0xFF, 0x00, 0x4D, 255);
const space = (44100 * 8) / 512; const space = (44100 * 8) / 512;
@ -20,9 +21,12 @@ void draw_frame(SDL_Window *w, SDL_Renderer *r, Ampler_state *state) {
} }
if state -> played_audio_last_frame do if state -> played_audio_last_frame do
foreach_ptr(Sound_src, snd, state -> sounds) { foreach_ptr(Sound_src, snd, state -> sounds)
if snd->state == SND_PLAYING or snd->state == SND_LOOPING do {
const int x = snd -> pos / space; const int x = snd -> pos / space;
SDL_RenderDrawLine(r, x, 0, x, 512); SDL_RenderDrawLine(r, x, 0, x, 512);
SDL_RenderDrawLine(r, snd->start / space, 0, snd->start / space, 512);
SDL_RenderDrawLine(r, snd->end / space, 0, snd->end / space, 512);
} }
SDL_RenderPresent(r); SDL_RenderPresent(r);