106 lines
2.5 KiB
C
106 lines
2.5 KiB
C
// ampler.c
|
|
|
|
#include "ampler.h"
|
|
|
|
#include "audio.c"
|
|
#include "draw.c"
|
|
|
|
Ampler_state *init() {
|
|
Ampler_state *state = malloc(sizeof (Ampler_state));
|
|
SDL_memset(state, 0, sizeof (Ampler_state));
|
|
|
|
state -> size = sizeof (Ampler_state);
|
|
|
|
SDL_AudioSpec want = { 0 }, have = { 0 };
|
|
want.freq = SAMPLE_RATE;
|
|
want.format = AUDIO_S16;
|
|
want.channels = CHANNELS;
|
|
want.samples = 512;
|
|
want.callback = NULL; // use que funcs
|
|
want.userdata = NULL;
|
|
|
|
state -> playdev = SDL_OpenAudioDevice(
|
|
NULL, // default device
|
|
0, // not capture
|
|
&want,
|
|
&have,
|
|
0 // no changes allowed
|
|
);
|
|
if !(state -> playdev) do
|
|
puts(SDL_GetError());
|
|
|
|
state -> recdev = SDL_OpenAudioDevice(
|
|
NULL, // default device
|
|
1, // is capture
|
|
&want,
|
|
&have,
|
|
0 // no changes allowed
|
|
);
|
|
if !(state -> playdev) do
|
|
puts(SDL_GetError());
|
|
|
|
// TODO: iterate a directory to load samples
|
|
//load_sample(state, "./mix.wav");
|
|
//load_sample(state, "./loop2.wav");
|
|
//load_sample(state, "./loop.wav");
|
|
//load_sample(state, "./chord.wav");
|
|
|
|
puts("init.");
|
|
return state;
|
|
}
|
|
|
|
int ampler_main(SDL_Window *w, SDL_Renderer *r, Ampler_state **u_data) {
|
|
if *u_data == NULL do *u_data = init();
|
|
|
|
u32 old_size = (*u_data) -> size, new_size = sizeof(Ampler_state);
|
|
if old_size < new_size do {
|
|
printf("increasing size. %i to %i\n", old_size, new_size);
|
|
Ampler_state *nu = malloc(new_size);
|
|
SDL_memset(nu, 0, new_size);
|
|
SDL_memcpy(nu, *u_data, old_size);
|
|
nu -> size = new_size;
|
|
free(*u_data);
|
|
*u_data = nu;
|
|
}
|
|
|
|
Ampler_state *state = *u_data;
|
|
|
|
// TODO:
|
|
// Main loop perf timing diff bits, with SDL_GetPerformanceCounter
|
|
// put results in state for last frame's perf, so that we can draw it
|
|
SDL_SetWindowTitle(w, "ampler");
|
|
SDL_PauseAudioDevice(state -> playdev, 0); // unpause audio
|
|
SDL_PauseAudioDevice(state -> recdev, 0); // unpause audio
|
|
udp_init(state);
|
|
while 1 do {
|
|
SDL_Event e;
|
|
while SDL_PollEvent(&e) do
|
|
if e.type == SDL_QUIT do
|
|
goto quit;
|
|
|
|
udp_frame(state);
|
|
state -> played_audio_last_frame = audio_frame(state);
|
|
draw_frame(w, r, state);
|
|
|
|
// CLEAR UDP MESSAGES, but only after we've played them
|
|
if state -> played_audio_last_frame do
|
|
foreach_ptr(Udp_msg, msg, state -> messages)
|
|
msg -> state = MSG_FREE;
|
|
|
|
SDL_Delay(1);
|
|
|
|
if !remove("reload-trigger") do {
|
|
SDL_PauseAudioDevice(state -> playdev, 1); // pause audio
|
|
SDL_PauseAudioDevice(state -> recdev, 1); // pause audio
|
|
udp_quit(state);
|
|
puts("removed reload-trigger, reloading...");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
quit:
|
|
udp_quit(state);
|
|
return 1;
|
|
}
|
|
|