2021-12-18 17:36:08 +00:00
|
|
|
// ampler.c
|
|
|
|
|
2021-12-19 22:44:48 +00:00
|
|
|
#include "ampler.h"
|
2021-12-18 20:58:21 +00:00
|
|
|
|
2022-02-10 19:26:20 +00:00
|
|
|
#include "random.c"
|
2021-12-19 22:44:48 +00:00
|
|
|
#include "audio.c"
|
|
|
|
#include "draw.c"
|
2021-12-18 20:58:21 +00:00
|
|
|
|
|
|
|
Ampler_state *init() {
|
2021-12-27 05:29:16 +00:00
|
|
|
Ampler_state *state = malloc(sizeof (Ampler_state));
|
|
|
|
SDL_memset(state, 0, sizeof (Ampler_state));
|
2021-12-18 20:58:21 +00:00
|
|
|
|
2021-12-27 05:29:16 +00:00
|
|
|
state -> size = sizeof (Ampler_state);
|
2021-12-18 20:58:21 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
);
|
2021-12-19 22:44:48 +00:00
|
|
|
if !(state -> playdev) do
|
2021-12-18 20:58:21 +00:00
|
|
|
puts(SDL_GetError());
|
|
|
|
|
2021-12-27 06:23:29 +00:00
|
|
|
state -> recdev = SDL_OpenAudioDevice(
|
|
|
|
NULL, // default device
|
|
|
|
1, // is capture
|
|
|
|
&want,
|
|
|
|
&have,
|
|
|
|
0 // no changes allowed
|
|
|
|
);
|
|
|
|
if !(state -> playdev) do
|
|
|
|
puts(SDL_GetError());
|
|
|
|
|
2021-12-20 00:03:14 +00:00
|
|
|
// TODO: iterate a directory to load samples
|
2022-02-10 21:02:29 +00:00
|
|
|
//load_sample(state, "./mix.wav");
|
2022-02-10 20:16:11 +00:00
|
|
|
//load_sample(state, "./haunted.wav");
|
2021-12-27 06:23:29 +00:00
|
|
|
//load_sample(state, "./loop2.wav");
|
2022-02-10 21:02:29 +00:00
|
|
|
load_sample(state, "./loop.wav");
|
2021-12-27 06:23:29 +00:00
|
|
|
//load_sample(state, "./chord.wav");
|
2021-12-20 00:03:14 +00:00
|
|
|
|
2021-12-18 20:58:21 +00:00
|
|
|
puts("init.");
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ampler_main(SDL_Window *w, SDL_Renderer *r, Ampler_state **u_data) {
|
2021-12-19 22:44:48 +00:00
|
|
|
if *u_data == NULL do *u_data = init();
|
2021-12-18 20:58:21 +00:00
|
|
|
|
2021-12-27 05:29:16 +00:00
|
|
|
u32 old_size = (*u_data) -> size, new_size = sizeof(Ampler_state);
|
2021-12-19 22:44:48 +00:00
|
|
|
if old_size < new_size do {
|
2021-12-18 20:58:21 +00:00
|
|
|
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;
|
|
|
|
|
2021-12-21 04:35:09 +00:00
|
|
|
// 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
|
2021-12-26 17:53:39 +00:00
|
|
|
SDL_SetWindowTitle(w, "ampler");
|
2021-12-18 20:58:21 +00:00
|
|
|
SDL_PauseAudioDevice(state -> playdev, 0); // unpause audio
|
2021-12-27 06:23:29 +00:00
|
|
|
SDL_PauseAudioDevice(state -> recdev, 0); // unpause audio
|
2021-12-21 04:35:09 +00:00
|
|
|
udp_init(state);
|
2021-12-19 22:44:48 +00:00
|
|
|
while 1 do {
|
2021-12-18 20:58:21 +00:00
|
|
|
SDL_Event e;
|
2021-12-19 22:44:48 +00:00
|
|
|
while SDL_PollEvent(&e) do
|
|
|
|
if e.type == SDL_QUIT do
|
2021-12-21 04:35:09 +00:00
|
|
|
goto quit;
|
2021-12-18 20:58:21 +00:00
|
|
|
|
2021-12-21 04:35:09 +00:00
|
|
|
udp_frame(state);
|
2021-12-20 02:06:02 +00:00
|
|
|
state -> played_audio_last_frame = audio_frame(state);
|
2021-12-19 22:44:48 +00:00
|
|
|
draw_frame(w, r, state);
|
2021-12-18 20:58:21 +00:00
|
|
|
|
2021-12-23 12:46:00 +00:00
|
|
|
// 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;
|
2021-12-22 23:20:51 +00:00
|
|
|
|
2021-12-18 17:36:08 +00:00
|
|
|
SDL_Delay(1);
|
|
|
|
|
2021-12-19 22:44:48 +00:00
|
|
|
if !remove("reload-trigger") do {
|
2021-12-18 20:58:21 +00:00
|
|
|
SDL_PauseAudioDevice(state -> playdev, 1); // pause audio
|
2021-12-27 06:23:29 +00:00
|
|
|
SDL_PauseAudioDevice(state -> recdev, 1); // pause audio
|
2021-12-21 04:35:09 +00:00
|
|
|
udp_quit(state);
|
2021-12-18 17:36:08 +00:00
|
|
|
puts("removed reload-trigger, reloading...");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2021-12-21 04:35:09 +00:00
|
|
|
|
|
|
|
quit:
|
|
|
|
udp_quit(state);
|
|
|
|
return 1;
|
2021-12-18 17:36:08 +00:00
|
|
|
}
|
|
|
|
|