Audio record and playback now works + removed comments

Fixed by moving SDL_AudioSpec definition to init() function.
This commit is contained in:
Sheldon Lee 2024-10-10 23:32:22 +08:00
parent ba14ddaf04
commit 2ef08b6b1b

92
main.c
View File

@ -1,13 +1,8 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h> #include <SDL2/SDL_ttf.h>
#include <stdio.h> #include <stdio.h>
#include "log.h" #include "log.h"
/*
* INFO: I will mark places to look at with INFO:
*/
const int SCREEN_WIDTH = 640; const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480; const int SCREEN_HEIGHT = 480;
@ -54,7 +49,7 @@ int text_texture_load(text_texture* text_texture, char* string);
int text_texture_render(text_texture* text_texture, int x, int y); int text_texture_render(text_texture* text_texture, int x, int y);
int text_texture_free(text_texture* text_texture); int text_texture_free(text_texture* text_texture);
/* /*
* Audio INFO: audio related declarations and callback here. * Audio
*/ */
int num_devices = 0; int num_devices = 0;
@ -119,14 +114,12 @@ int main(int argc, char* argv[])
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
// INFO: main state logic here.
handle_input(&event, &state); handle_input(&event, &state);
break; break;
} }
} }
// INFO: more state logic here. I check the buffer write position here
handle_task(&state); handle_task(&state);
sprintf(char_buffer, default_fmt, state.device_index); sprintf(char_buffer, default_fmt, state.device_index);
@ -157,6 +150,7 @@ int main(int argc, char* argv[])
} }
cleanup: cleanup:
printf("Cleanup");
SDL_CloseAudioDevice(state.recording_device_id); SDL_CloseAudioDevice(state.recording_device_id);
SDL_CloseAudioDevice(state.playback_device_id); SDL_CloseAudioDevice(state.playback_device_id);
text_texture_free(&display_text_texture); text_texture_free(&display_text_texture);
@ -172,48 +166,33 @@ void handle_input(SDL_Event* event, state* state)
{ {
SDL_Keycode keysym = event->key.keysym.sym; SDL_Keycode keysym = event->key.keysym.sym;
switch (state->application_state) { switch (state->application_state) {
// INFO: init of recording audio device here
case SELECTING_DEVICE: case SELECTING_DEVICE:
switch (keysym) { switch (keysym) {
case SDLK_y: case SDLK_y:
case SDLK_RETURN: case SDLK_RETURN:
// INFO: init audio device here and below if (state->recording_device_id)
// Here it initialises everytime you press enter or y, and
// unpauses device immediately. Seems to work mor consistently
SDL_CloseAudioDevice(state->recording_device_id); SDL_CloseAudioDevice(state->recording_device_id);
state->recording_device_id = state->recording_device_id = audio_recording_init(state->device_index, NULL);
audio_recording_init(state->device_index, NULL);
if (state->device_index == -1 || !state->recording_device_id) break; if (state->device_index == -1 || !state->recording_device_id) break;
recording_buffer_position = 0; recording_buffer_position = 0;
// INFO: calling this should unpause the device and start calling the callback
SDL_PauseAudioDevice(state->recording_device_id, 0); SDL_PauseAudioDevice(state->recording_device_id, 0);
/*SDL_UnlockAudioDevice(state->recording_device_id);*/
// INFO: state change looks like this here
state->application_state = RECORDING; state->application_state = RECORDING;
break; break;
case SDLK_ESCAPE: case SDLK_ESCAPE:
state->device_index = -1; /*state->device_index = -1;*/
SDL_CloseAudioDevice(state->recording_device_id); /*SDL_CloseAudioDevice(state->recording_device_id);*/
break; break;
} }
int device_index_new = select_device(&event->key, num_devices); int device_index_new = select_device(&event->key, num_devices);
if (device_index_new == -1) break; if (device_index_new == -1) break;
if (state->device_index != -1 && device_index_new == -1) break; if (state->device_index != -1 && device_index_new == -1) break;
state->device_index = device_index_new; state->device_index = device_index_new;
// INFO: inititialising here seems to be less consistent. When I
// spam enter or y after initialising here, Sometimes I see
// callback being called sometimes not.
/*SDL_CloseAudioDevice(state->recording_device_id);*/
/*state->recording_device_id = audio_recording_init(state->device_index, NULL);*/
break; break;
case RECORDING: case RECORDING:
switch (keysym) { switch (keysym) {
case SDLK_y:
SDL_PauseAudioDevice(state->recording_device_id, 0);
break;
case SDLK_q: case SDLK_q:
case SDLK_ESCAPE: case SDLK_ESCAPE:
SDL_PauseAudioDevice(state->recording_device_id, 1); SDL_PauseAudioDevice(state->recording_device_id, 1);
@ -222,16 +201,16 @@ void handle_input(SDL_Event* event, state* state)
break; break;
} }
break; break;
// INFO: init of playback audio device here. Can't seem to get it to play / call callback
case RECORDED: case RECORDED:
switch (keysym) { switch (keysym) {
case SDLK_y: case SDLK_y:
case SDLK_RETURN: case SDLK_RETURN:
if (state->playback_device_id)
SDL_CloseAudioDevice(state->playback_device_id); SDL_CloseAudioDevice(state->playback_device_id);
state->playback_device_id = audio_playback_init(NULL); state->playback_device_id = audio_playback_init(NULL);
recording_buffer_position = 0; recording_buffer_position = 0;
SDL_PauseAudioDevice(state->playback_device_id, 0); SDL_PauseAudioDevice(state->playback_device_id, 0);
/*SDL_UnlockAudioDevice(state->playback_device_id);*/
state->application_state = PLAYBACK; state->application_state = PLAYBACK;
break; break;
case SDLK_q: case SDLK_q:
@ -246,6 +225,7 @@ void handle_input(SDL_Event* event, state* state)
switch (keysym) { switch (keysym) {
case SDLK_q: case SDLK_q:
case SDLK_ESCAPE: case SDLK_ESCAPE:
SDL_PauseAudioDevice(state->playback_device_id, 1);
state->application_state = RECORDED; state->application_state = RECORDED;
break; break;
} }
@ -262,12 +242,9 @@ void handle_task(state* state)
break; break;
case RECORDING: case RECORDING:
// INFO: check buffer position here and stop if reaces the end
SDL_LockAudioDevice(state->recording_device_id); SDL_LockAudioDevice(state->recording_device_id);
/*printf("buf pos: %u\n", recording_buffer_position);*/
if (recording_buffer_position >= recording_buffer_position_max) { if (recording_buffer_position >= recording_buffer_position_max) {
SDL_UnlockAudioDevice(state->recording_device_id); SDL_UnlockAudioDevice(state->recording_device_id);
SDL_PauseAudioDevice(state->recording_device_id, 1);
SDL_CloseAudioDevice(state->recording_device_id); SDL_CloseAudioDevice(state->recording_device_id);
state->application_state = RECORDED; state->application_state = RECORDED;
break; break;
@ -279,11 +256,9 @@ void handle_task(state* state)
break; break;
case PLAYBACK: case PLAYBACK:
// INFO:
SDL_LockAudioDevice(state->playback_device_id); SDL_LockAudioDevice(state->playback_device_id);
if (recording_buffer_position >= recording_buffer_position_max) { if (recording_buffer_position >= recording_buffer_position_max) {
SDL_UnlockAudioDevice(state->playback_device_id); SDL_UnlockAudioDevice(state->playback_device_id);
SDL_PauseAudioDevice(state->playback_device_id, 1);
SDL_CloseAudioDevice(state->playback_device_id); SDL_CloseAudioDevice(state->playback_device_id);
state->application_state = RECORDED; state->application_state = RECORDED;
break; break;
@ -327,6 +302,20 @@ int init()
return 0; return 0;
} }
SDL_zero(recording_spec);
recording_spec.freq = 44100;
recording_spec.format = AUDIO_S16;
recording_spec.channels = 2;
recording_spec.samples = 4096;
recording_spec.callback = audio_recording_callback;
SDL_zero(playback_spec);
playback_spec.freq = 44100;
playback_spec.format = AUDIO_S16;
playback_spec.channels = 2;
playback_spec.samples = 4096;
playback_spec.callback = audio_playback_callback;
return 1; return 1;
} }
@ -368,10 +357,10 @@ void application_state_to_string(application_state state, char* string)
sprintf(string, "Selecting Device [0-9] -> [y/RET], "); sprintf(string, "Selecting Device [0-9] -> [y/RET], ");
break; break;
case RECORDING: case RECORDING:
sprintf(string, "Recording"); sprintf(string, "Recording [ESC] to cancel");
break; break;
case RECORDED: case RECORDED:
sprintf(string, "Recorded"); sprintf(string, "Recorded [y/RET] to play [ESC] to Select");
break; break;
case PLAYBACK: case PLAYBACK:
sprintf(string, "Playing"); sprintf(string, "Playing");
@ -442,19 +431,9 @@ int text_texture_free(text_texture* text_texture)
SDL_DestroyTexture(text_texture->texture); SDL_DestroyTexture(text_texture->texture);
return 1; return 1;
} }
// INFO: device init here
// Most of the audio stuff I copied from an example
SDL_AudioDeviceID audio_recording_init(int index, void* userdata) SDL_AudioDeviceID audio_recording_init(int index, void* userdata)
{ {
// INFO: audio spec and callback
SDL_zero(recording_spec);
recording_spec.freq = 44100;
recording_spec.format = AUDIO_F32;
recording_spec.channels = 2;
recording_spec.samples = 4096;
recording_spec.callback = audio_recording_callback;
recording_spec.userdata = userdata;
SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(index, 1), 1, &recording_spec, &received_recording_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE); SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(index, 1), 1, &recording_spec, &received_recording_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
printf("Recording Device: %s\n", SDL_GetAudioDeviceName(index, 1)); printf("Recording Device: %s\n", SDL_GetAudioDeviceName(index, 1));
if (!device_id) { if (!device_id) {
@ -464,7 +443,6 @@ SDL_AudioDeviceID audio_recording_init(int index, void* userdata)
unsigned int bytes_per_sample = received_recording_spec.channels * (SDL_AUDIO_BITSIZE(received_recording_spec.format)/8); unsigned int bytes_per_sample = received_recording_spec.channels * (SDL_AUDIO_BITSIZE(received_recording_spec.format)/8);
unsigned int bytes_per_second = received_recording_spec.freq * bytes_per_sample; unsigned int bytes_per_second = received_recording_spec.freq * bytes_per_sample;
// INFO: I just copied example where they allocated 1 second worth of buffer so it doesn't die
recording_buffer_size = RECORDING_BUFFER_SECONDS * bytes_per_second; recording_buffer_size = RECORDING_BUFFER_SECONDS * bytes_per_second;
recording_buffer_position_max = MAX_RECORDING_SECONDS * bytes_per_second; recording_buffer_position_max = MAX_RECORDING_SECONDS * bytes_per_second;
@ -473,21 +451,13 @@ SDL_AudioDeviceID audio_recording_init(int index, void* userdata)
return device_id; return device_id;
} }
// INFO:
SDL_AudioDeviceID audio_playback_init(void* userdata) SDL_AudioDeviceID audio_playback_init(void* userdata)
{ {
if (!recording_buffer) { if (!recording_buffer) {
printf("Audio buffer not initialized"); printf("Audio buffer not initialized");
return 0; return 0;
} }
// INFO: audio spec and callback
SDL_zero(playback_spec);
playback_spec.freq = 44100;
playback_spec.format = AUDIO_F32;
playback_spec.channels = 2;
playback_spec.samples = 4096;
playback_spec.callback = audio_playback_callback;
playback_spec.callback = userdata;
SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(NULL, 0, &playback_spec, &received_playback_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE); SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(NULL, 0, &playback_spec, &received_playback_spec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
if (!device_id) { if (!device_id) {
@ -512,23 +482,21 @@ void audio_buffer_destroy()
recording_buffer_size = 0; recording_buffer_size = 0;
recording_buffer_position = 0; recording_buffer_position = 0;
} }
// INFO: callback here
void audio_recording_callback(void* userdata, Uint8* stream, int len) void audio_recording_callback(void* userdata, Uint8* stream, int len)
{ {
memcpy(&recording_buffer[recording_buffer_position], stream, len); memcpy(&recording_buffer[recording_buffer_position], stream, len);
recording_buffer_position += len; recording_buffer_position += len;
char string[64]; char string[64];
sprintf(string, "Record pos: %u %u", recording_buffer_position, len); sprintf(string, "Record pos: %u %u", recording_buffer_position, len);
printf(string, "Record pos: %u %u\n", recording_buffer_position, len);
log_message(LOG_INFO, string); log_message(LOG_INFO, string);
} }
// INFO:
void audio_playback_callback(void* userdata, Uint8* stream, int len) void audio_playback_callback(void* userdata, Uint8* stream, int len)
{ {
memcpy(stream, &recording_buffer[recording_buffer_position], len); memcpy(stream, &recording_buffer[recording_buffer_position], len);
recording_buffer_position += len; recording_buffer_position += len;
char string[64]; char string[64];
sprintf(string, "Playback pos: %u %u", recording_buffer_position, len); sprintf(string, "Playback pos: %u %u", recording_buffer_position, len);
printf(string, "Playback pos: %u %u", recording_buffer_position, len);
log_message(LOG_INFO, string); log_message(LOG_INFO, string);
} }