Compare commits
No commits in common. "2ef08b6b1b309761335dc3b639fbfa42fc4fcb32" and "0b8f5a562048c4b040a1799b47e70b5a96eb6c85" have entirely different histories.
2ef08b6b1b
...
0b8f5a5620
@ -1,584 +0,0 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_audio.h>
|
||||
#include <stdio.h>
|
||||
/*
|
||||
* INFO: I will mark places to look at with INFO:
|
||||
*/
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
|
||||
const int MAX_RECORDING_DEVICES = 10;
|
||||
const int MAX_RECORDING_SECONDS = 1;
|
||||
const int RECORDING_BUFFER_SECONDS = MAX_RECORDING_SECONDS + 1;
|
||||
/*
|
||||
* Application
|
||||
*/
|
||||
int init();
|
||||
void destroy();
|
||||
int select_device(SDL_KeyboardEvent* key_event, int max_index);
|
||||
|
||||
typedef enum {
|
||||
SELECTING_DEVICE,
|
||||
RECORDING,
|
||||
RECORDED,
|
||||
PLAYBACK,
|
||||
} application_state;
|
||||
|
||||
typedef struct {
|
||||
application_state application_state;
|
||||
int device_index;
|
||||
int recording_device_id;
|
||||
int playback_device_id;
|
||||
} state;
|
||||
|
||||
void application_state_to_string(application_state state, char* string);
|
||||
/*
|
||||
* Visual
|
||||
*/
|
||||
SDL_Window* window = NULL;
|
||||
SDL_Renderer* renderer = NULL;
|
||||
//TTF_Font* global_font = NULL;
|
||||
|
||||
typedef struct {
|
||||
SDL_Texture* texture;
|
||||
int width;
|
||||
int height;
|
||||
} text_texture;
|
||||
|
||||
int text_texture_init(text_texture* text_texture);
|
||||
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_free(text_texture* text_texture);
|
||||
/*
|
||||
* Audio INFO: audio related declarations and callback here.
|
||||
*/
|
||||
int num_devices = 0;
|
||||
int num_recdevices = 0;
|
||||
int num_playdevices = 0;
|
||||
|
||||
SDL_AudioSpec recording_spec;
|
||||
SDL_AudioSpec playback_spec;
|
||||
SDL_AudioSpec received_recording_spec;
|
||||
SDL_AudioSpec received_playback_spec;
|
||||
Uint8* recording_buffer = NULL;
|
||||
unsigned int recording_buffer_size = 0;
|
||||
unsigned int recording_buffer_position = 0;
|
||||
unsigned int recording_buffer_position_max = 0;
|
||||
|
||||
SDL_AudioDeviceID audio_recording_init(int index, void* userdata);
|
||||
SDL_AudioDeviceID audio_playback_init(void* userdata);
|
||||
void audio_buffer_destroy();
|
||||
void audio_recording_callback( void* userdata, Uint8* stream, int len );
|
||||
void audio_playback_callback( void* userdata, Uint8* stream, int len );
|
||||
|
||||
void handle_input(SDL_Event* event, state* state);
|
||||
void handle_task(state* state);
|
||||
|
||||
//Recording data buffer
|
||||
Uint8* gRecordingBuffer = NULL;
|
||||
|
||||
//Size of data buffer
|
||||
Uint32 gBufferByteSize = 0;
|
||||
|
||||
//Position in data buffer
|
||||
Uint32 gBufferBytePosition = 0;
|
||||
|
||||
void audioRecordingCallback(void* userdata, Uint8* stream, int len )
|
||||
{
|
||||
memcpy(&gRecordingBuffer[ gBufferBytePosition ], stream, len);
|
||||
gBufferBytePosition += len;
|
||||
printf("recording\n");
|
||||
}
|
||||
|
||||
void audioPlaybackCallback(void* userdata, Uint8* stream, int len )
|
||||
{
|
||||
memcpy(stream, &gRecordingBuffer[ gBufferBytePosition ], len);
|
||||
gBufferBytePosition += len;
|
||||
printf("playback\n");
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
SDL_AudioDeviceID recordingDeviceId = 0;
|
||||
SDL_AudioDeviceID playbackDeviceId = 0;
|
||||
|
||||
SDL_AudioSpec gReceivedRecordingSpec;
|
||||
SDL_AudioSpec gReceivedPlaybackSpec;
|
||||
|
||||
SDL_AudioSpec desiredRecordingSpec;
|
||||
|
||||
SDL_zero(desiredRecordingSpec);
|
||||
desiredRecordingSpec.freq = 44100;
|
||||
desiredRecordingSpec.format = AUDIO_S16;
|
||||
desiredRecordingSpec.channels = 2;
|
||||
desiredRecordingSpec.samples = 4096;
|
||||
desiredRecordingSpec.callback = audioRecordingCallback;
|
||||
|
||||
SDL_AudioSpec desiredPlaybackSpec;
|
||||
SDL_zero(desiredPlaybackSpec);
|
||||
desiredPlaybackSpec.freq = 44100;
|
||||
desiredPlaybackSpec.format = AUDIO_S16; // AUDIO_F32;
|
||||
desiredPlaybackSpec.channels = 2;
|
||||
desiredPlaybackSpec.samples = 4096;
|
||||
desiredPlaybackSpec.callback = audioPlaybackCallback;
|
||||
|
||||
//Maximum position in data buffer for recording
|
||||
Uint32 gBufferByteMaxPosition = 0;
|
||||
|
||||
if (!init()) return 1;
|
||||
|
||||
printf("Audio Driver: %s\n", SDL_GetCurrentAudioDriver());
|
||||
|
||||
num_recdevices = SDL_GetNumAudioDevices(1);
|
||||
if(num_recdevices < 1) {
|
||||
printf( "Unable to get audio capture device! SDL Error: %s\n", SDL_GetError() );
|
||||
return 0;
|
||||
}
|
||||
printf("Num audio recording devices: %i\n", num_recdevices);
|
||||
for(int i = 0; i < num_recdevices; ++i) {
|
||||
const char* deviceName = SDL_GetAudioDeviceName(i, 1);
|
||||
printf("REC: %d - %s\n", i, deviceName);
|
||||
}
|
||||
|
||||
num_playdevices = SDL_GetNumAudioDevices(0);
|
||||
if(num_playdevices < 1) {
|
||||
printf( "Unable to get audio playback device! SDL Error: %s\n", SDL_GetError() );
|
||||
return 0;
|
||||
}
|
||||
printf("Num audio playback devices: %i\n", num_playdevices);
|
||||
for(int i = 0; i < num_recdevices; ++i) {
|
||||
const char* deviceName = SDL_GetAudioDeviceName(i, 0);
|
||||
printf("PBK: %d - %s\n", i, deviceName);
|
||||
}
|
||||
|
||||
recordingDeviceId = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(2, SDL_TRUE), SDL_TRUE, &desiredRecordingSpec, &gReceivedRecordingSpec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
|
||||
if(recordingDeviceId == 0) {
|
||||
printf("Failed to open recording device! SDL Error: %s", SDL_GetError() );
|
||||
return 1;
|
||||
}
|
||||
int bytesPerSample = gReceivedRecordingSpec.channels * (SDL_AUDIO_BITSIZE(gReceivedRecordingSpec.format) / 8);
|
||||
int bytesPerSecond = gReceivedRecordingSpec.freq * bytesPerSample;
|
||||
gBufferByteSize = RECORDING_BUFFER_SECONDS * bytesPerSecond;
|
||||
gBufferByteMaxPosition = MAX_RECORDING_SECONDS * bytesPerSecond;
|
||||
printf("bytesPerSample %d, bytesPerSecond %d\n", bytesPerSample,bytesPerSecond);
|
||||
printf("gBufferByteSize %d, gBufferByteMaxPosition %d\n", gBufferByteSize,gBufferByteMaxPosition);
|
||||
|
||||
playbackDeviceId = SDL_OpenAudioDevice( NULL, 0, &desiredPlaybackSpec, &gReceivedPlaybackSpec, SDL_AUDIO_ALLOW_FORMAT_CHANGE );
|
||||
if(playbackDeviceId == 0) {
|
||||
printf("Failed to open playback device! SDL Error: %s", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
gRecordingBuffer = malloc(gBufferByteSize);
|
||||
memset(gRecordingBuffer, 0, gBufferByteSize);
|
||||
gBufferBytePosition = 0;
|
||||
|
||||
SDL_Delay(100);
|
||||
SDL_PauseAudioDevice( recordingDeviceId, SDL_FALSE );
|
||||
|
||||
while(1) {
|
||||
SDL_LockAudioDevice( recordingDeviceId );
|
||||
if(gBufferBytePosition > gBufferByteMaxPosition) {
|
||||
printf("end\n");
|
||||
SDL_UnlockAudioDevice( recordingDeviceId );
|
||||
SDL_PauseAudioDevice( recordingDeviceId, SDL_TRUE );
|
||||
break;
|
||||
//} else {
|
||||
// printf("bufferPos %d\n",gBufferBytePosition);
|
||||
}
|
||||
SDL_UnlockAudioDevice( recordingDeviceId );
|
||||
}
|
||||
|
||||
printf("playback\n");
|
||||
gBufferBytePosition = 0;
|
||||
SDL_PauseAudioDevice(playbackDeviceId, 0);
|
||||
while(1) {
|
||||
SDL_LockAudioDevice(playbackDeviceId);
|
||||
if(gBufferBytePosition > gBufferByteMaxPosition) {
|
||||
printf("end\n");
|
||||
SDL_UnlockAudioDevice(playbackDeviceId);
|
||||
SDL_PauseAudioDevice(playbackDeviceId, SDL_TRUE);
|
||||
break;
|
||||
}
|
||||
SDL_UnlockAudioDevice(playbackDeviceId);
|
||||
}
|
||||
|
||||
cleanup:
|
||||
printf("1\n");
|
||||
free(gRecordingBuffer);
|
||||
printf("2\n");
|
||||
SDL_CloseAudioDevice(recordingDeviceId);
|
||||
printf("3\n");
|
||||
destroy();
|
||||
printf("4\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void handle_input(SDL_Event* event, state* state)
|
||||
{
|
||||
SDL_Keycode keysym = event->key.keysym.sym;
|
||||
switch (state->application_state) {
|
||||
// INFO: init of recording audio device here
|
||||
case SELECTING_DEVICE:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
case SDLK_RETURN:
|
||||
// INFO: init audio device here and below
|
||||
// Here it initialises everytime you press enter or y, and
|
||||
// unpauses device immediately. Seems to work mor consistently
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
state->recording_device_id =
|
||||
audio_recording_init(state->device_index, NULL);
|
||||
|
||||
if (state->device_index == -1 || !state->recording_device_id) break;
|
||||
recording_buffer_position = 0;
|
||||
// INFO: calling this should unpause the device and start calling the callback
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 0);
|
||||
/*SDL_UnlockAudioDevice(state->recording_device_id);*/
|
||||
// INFO: state change looks like this here
|
||||
state->application_state = RECORDING;
|
||||
break;
|
||||
case SDLK_ESCAPE:
|
||||
state->device_index = -1;
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
break;
|
||||
}
|
||||
int device_index_new = select_device(&event->key, num_devices);
|
||||
if (device_index_new == -1) break;
|
||||
if (state->device_index != -1 && device_index_new == -1) break;
|
||||
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;
|
||||
|
||||
case RECORDING:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 0);
|
||||
break;
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 1);
|
||||
audio_buffer_destroy();
|
||||
state->application_state = SELECTING_DEVICE;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
// INFO: init of playback audio device here. Can't seem to get it to play / call callback
|
||||
case RECORDED:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
case SDLK_RETURN:
|
||||
SDL_CloseAudioDevice(state->playback_device_id);
|
||||
state->playback_device_id = audio_playback_init(NULL);
|
||||
recording_buffer_position = 0;
|
||||
SDL_PauseAudioDevice(state->playback_device_id, 0);
|
||||
/*SDL_UnlockAudioDevice(state->playback_device_id);*/
|
||||
state->application_state = PLAYBACK;
|
||||
break;
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
audio_buffer_destroy();
|
||||
state->application_state = SELECTING_DEVICE;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case PLAYBACK:
|
||||
switch (keysym) {
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void handle_task(state* state)
|
||||
{
|
||||
switch (state->application_state) {
|
||||
|
||||
case SELECTING_DEVICE:
|
||||
break;
|
||||
|
||||
case RECORDING:
|
||||
// INFO: check buffer position here and stop if reaces the end
|
||||
SDL_LockAudioDevice(state->recording_device_id);
|
||||
/*printf("buf pos: %u\n", recording_buffer_position);*/
|
||||
if (recording_buffer_position >= recording_buffer_position_max) {
|
||||
SDL_UnlockAudioDevice(state->recording_device_id);
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 1);
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
}
|
||||
SDL_UnlockAudioDevice(state->recording_device_id);
|
||||
break;
|
||||
|
||||
case RECORDED:
|
||||
break;
|
||||
|
||||
case PLAYBACK:
|
||||
// INFO:
|
||||
SDL_LockAudioDevice(state->playback_device_id);
|
||||
if (recording_buffer_position >= recording_buffer_position_max) {
|
||||
SDL_UnlockAudioDevice(state->playback_device_id);
|
||||
SDL_PauseAudioDevice(state->playback_device_id, 1);
|
||||
SDL_CloseAudioDevice(state->playback_device_id);
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
}
|
||||
SDL_UnlockAudioDevice(state->playback_device_id);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int init()
|
||||
{
|
||||
//if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
|
||||
int nRet=0;
|
||||
nRet = SDL_Init(SDL_INIT_AUDIO);
|
||||
if (nRet < 0) {
|
||||
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("SDL initialized: %d %s\n",nRet, SDL_GetError());
|
||||
|
||||
// if (TTF_Init() == -1) {
|
||||
// printf("TTF could not initialize! TTF Error: %s\n", TTF_GetError());
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// window = SDL_CreateWindow("SDL record", SDL_WINDOWPOS_UNDEFINED,
|
||||
// SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
|
||||
// SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
|
||||
// if (!window) {
|
||||
// printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
// if (!renderer) {
|
||||
// printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// global_font = TTF_OpenFont("FiraCode-Regular.ttf", 24);
|
||||
// if (!global_font) {
|
||||
// printf("Failed to load font! TTF_Error: %s\n", TTF_GetError());
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void destroy()
|
||||
{
|
||||
free(recording_buffer);
|
||||
|
||||
//TTF_CloseFont(global_font);
|
||||
//SDL_DestroyRenderer(renderer);
|
||||
//SDL_DestroyWindow(window);
|
||||
//TTF_Quit();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
int select_device(SDL_KeyboardEvent* key_event, int max_index)
|
||||
{
|
||||
SDL_Keycode keycode = key_event->keysym.sym;
|
||||
SDL_Keycode mod = key_event->keysym.mod;
|
||||
|
||||
int keycode_is_num = (keycode >= SDLK_0 && keycode <= SDLK_9);
|
||||
int no_mod = (mod == 0);
|
||||
if (!keycode_is_num || !no_mod) {
|
||||
printf("Not number: %c <%u>\n", keycode, keycode);
|
||||
return -1;
|
||||
} else {
|
||||
printf("Is number: %c <%u>\n", keycode, keycode);
|
||||
}
|
||||
|
||||
int index = keycode - SDLK_0;
|
||||
if (index >= max_index) return -1;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
void application_state_to_string(application_state state, char* string)
|
||||
{
|
||||
switch (state) {
|
||||
case SELECTING_DEVICE:
|
||||
sprintf(string, "Selecting Device [0-9] -> [y/RET], ");
|
||||
break;
|
||||
case RECORDING:
|
||||
sprintf(string, "Recording");
|
||||
break;
|
||||
case RECORDED:
|
||||
sprintf(string, "Recorded");
|
||||
break;
|
||||
case PLAYBACK:
|
||||
sprintf(string, "Playing");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int text_texture_init(text_texture* text_texture)
|
||||
{
|
||||
text_texture->texture = NULL;
|
||||
text_texture->width = 0;
|
||||
text_texture->height = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// int text_texture_load(text_texture* text_texture, char* string)
|
||||
// {
|
||||
// if (text_texture->texture) {
|
||||
// SDL_DestroyTexture(text_texture->texture);
|
||||
// text_texture->texture = NULL;
|
||||
// }
|
||||
|
||||
// SDL_Color text_color = { 255, 255, 255 };
|
||||
// SDL_Surface* text_surface = TTF_RenderText_Solid(global_font, string, text_color);
|
||||
// if (!text_surface) {
|
||||
// printf("Unable to render text surface! TTF_Error: %s\n", TTF_GetError());
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, text_surface);
|
||||
// if (!texture) {
|
||||
// printf("Unable to create texture from rendered text! SDL_Error: %s\n", SDL_GetError());
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// text_texture->texture = SDL_CreateTextureFromSurface(renderer, text_surface);
|
||||
// text_texture->width = text_surface->w;
|
||||
// text_texture->height = text_surface->h;
|
||||
|
||||
// SDL_FreeSurface(text_surface);
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
int text_texture_render(text_texture* text_texture, int x, int y)
|
||||
{
|
||||
if (!text_texture) {
|
||||
printf("text_texture uninitialized");
|
||||
return 0;
|
||||
}
|
||||
if (!text_texture->texture) {
|
||||
printf("text_texture->texture uninitialized");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int quad_width = text_texture->width;
|
||||
int quad_height = text_texture->height;
|
||||
SDL_Rect render_quad = { x, y, quad_width, quad_height };
|
||||
SDL_RenderCopy(renderer, text_texture->texture, NULL, &render_quad);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int text_texture_free(text_texture* text_texture)
|
||||
{
|
||||
if (!text_texture->texture) {
|
||||
return 0;
|
||||
}
|
||||
SDL_DestroyTexture(text_texture->texture);
|
||||
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)
|
||||
{
|
||||
// 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);
|
||||
printf("Recording Device: %s\n", SDL_GetAudioDeviceName(index, 1));
|
||||
if (!device_id) {
|
||||
printf("Failed to open recording device! SDL Error: %s", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
// 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_position_max = MAX_RECORDING_SECONDS * bytes_per_second;
|
||||
|
||||
recording_buffer = malloc(recording_buffer_size);
|
||||
memset(recording_buffer, 0, recording_buffer_size);
|
||||
|
||||
return device_id;
|
||||
}
|
||||
// INFO:
|
||||
SDL_AudioDeviceID audio_playback_init(void* userdata)
|
||||
{
|
||||
if (!recording_buffer) {
|
||||
printf("Audio buffer not initialized");
|
||||
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);
|
||||
if (!device_id) {
|
||||
printf("Failed to open playback device! SDL Error: %s", SDL_GetError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
return device_id;
|
||||
}
|
||||
|
||||
int audio_device_stop(int device_id)
|
||||
{
|
||||
SDL_PauseAudioDevice(device_id, 1);
|
||||
SDL_CloseAudioDevice(device_id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void audio_buffer_destroy()
|
||||
{
|
||||
free(recording_buffer);
|
||||
recording_buffer = NULL;
|
||||
recording_buffer_size = 0;
|
||||
recording_buffer_position = 0;
|
||||
}
|
||||
// INFO: callback here
|
||||
void audio_recording_callback(void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
memcpy(&recording_buffer[recording_buffer_position], stream, len);
|
||||
recording_buffer_position += len;
|
||||
char string[64];
|
||||
sprintf(string, "Record pos: %u %u", recording_buffer_position, len);
|
||||
printf(string, "Record pos: %u %u\n", recording_buffer_position, len);
|
||||
}
|
||||
// INFO:
|
||||
void audio_playback_callback(void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
memcpy(stream, &recording_buffer[recording_buffer_position], len);
|
||||
recording_buffer_position += len;
|
||||
char string[64];
|
||||
sprintf(string, "Playback pos: %u %u", recording_buffer_position, len);
|
||||
printf(string, "Playback pos: %u %u", recording_buffer_position, len);
|
||||
}
|
92
main.c
92
main.c
@ -1,8 +1,13 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_audio.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "log.h"
|
||||
/*
|
||||
* INFO: I will mark places to look at with INFO:
|
||||
*/
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
|
||||
@ -49,7 +54,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_free(text_texture* text_texture);
|
||||
/*
|
||||
* Audio
|
||||
* Audio INFO: audio related declarations and callback here.
|
||||
*/
|
||||
int num_devices = 0;
|
||||
|
||||
@ -114,12 +119,14 @@ int main(int argc, char* argv[])
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
// INFO: main state logic here.
|
||||
handle_input(&event, &state);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// INFO: more state logic here. I check the buffer write position here
|
||||
handle_task(&state);
|
||||
|
||||
sprintf(char_buffer, default_fmt, state.device_index);
|
||||
@ -150,7 +157,6 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
|
||||
cleanup:
|
||||
printf("Cleanup");
|
||||
SDL_CloseAudioDevice(state.recording_device_id);
|
||||
SDL_CloseAudioDevice(state.playback_device_id);
|
||||
text_texture_free(&display_text_texture);
|
||||
@ -166,33 +172,48 @@ void handle_input(SDL_Event* event, state* state)
|
||||
{
|
||||
SDL_Keycode keysym = event->key.keysym.sym;
|
||||
switch (state->application_state) {
|
||||
|
||||
// INFO: init of recording audio device here
|
||||
case SELECTING_DEVICE:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
case SDLK_RETURN:
|
||||
if (state->recording_device_id)
|
||||
// INFO: init audio device here and below
|
||||
// Here it initialises everytime you press enter or y, and
|
||||
// unpauses device immediately. Seems to work mor consistently
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
state->recording_device_id = audio_recording_init(state->device_index, NULL);
|
||||
state->recording_device_id =
|
||||
audio_recording_init(state->device_index, NULL);
|
||||
|
||||
if (state->device_index == -1 || !state->recording_device_id) break;
|
||||
recording_buffer_position = 0;
|
||||
// INFO: calling this should unpause the device and start calling the callback
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 0);
|
||||
/*SDL_UnlockAudioDevice(state->recording_device_id);*/
|
||||
// INFO: state change looks like this here
|
||||
state->application_state = RECORDING;
|
||||
break;
|
||||
case SDLK_ESCAPE:
|
||||
/*state->device_index = -1;*/
|
||||
/*SDL_CloseAudioDevice(state->recording_device_id);*/
|
||||
state->device_index = -1;
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
break;
|
||||
}
|
||||
int device_index_new = select_device(&event->key, num_devices);
|
||||
if (device_index_new == -1) break;
|
||||
if (state->device_index != -1 && device_index_new == -1) break;
|
||||
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;
|
||||
|
||||
case RECORDING:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 0);
|
||||
break;
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 1);
|
||||
@ -201,16 +222,16 @@ void handle_input(SDL_Event* event, state* state)
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
// INFO: init of playback audio device here. Can't seem to get it to play / call callback
|
||||
case RECORDED:
|
||||
switch (keysym) {
|
||||
case SDLK_y:
|
||||
case SDLK_RETURN:
|
||||
if (state->playback_device_id)
|
||||
SDL_CloseAudioDevice(state->playback_device_id);
|
||||
state->playback_device_id = audio_playback_init(NULL);
|
||||
recording_buffer_position = 0;
|
||||
SDL_PauseAudioDevice(state->playback_device_id, 0);
|
||||
/*SDL_UnlockAudioDevice(state->playback_device_id);*/
|
||||
state->application_state = PLAYBACK;
|
||||
break;
|
||||
case SDLK_q:
|
||||
@ -225,7 +246,6 @@ void handle_input(SDL_Event* event, state* state)
|
||||
switch (keysym) {
|
||||
case SDLK_q:
|
||||
case SDLK_ESCAPE:
|
||||
SDL_PauseAudioDevice(state->playback_device_id, 1);
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
}
|
||||
@ -242,9 +262,12 @@ void handle_task(state* state)
|
||||
break;
|
||||
|
||||
case RECORDING:
|
||||
// INFO: check buffer position here and stop if reaces the end
|
||||
SDL_LockAudioDevice(state->recording_device_id);
|
||||
/*printf("buf pos: %u\n", recording_buffer_position);*/
|
||||
if (recording_buffer_position >= recording_buffer_position_max) {
|
||||
SDL_UnlockAudioDevice(state->recording_device_id);
|
||||
SDL_PauseAudioDevice(state->recording_device_id, 1);
|
||||
SDL_CloseAudioDevice(state->recording_device_id);
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
@ -256,9 +279,11 @@ void handle_task(state* state)
|
||||
break;
|
||||
|
||||
case PLAYBACK:
|
||||
// INFO:
|
||||
SDL_LockAudioDevice(state->playback_device_id);
|
||||
if (recording_buffer_position >= recording_buffer_position_max) {
|
||||
SDL_UnlockAudioDevice(state->playback_device_id);
|
||||
SDL_PauseAudioDevice(state->playback_device_id, 1);
|
||||
SDL_CloseAudioDevice(state->playback_device_id);
|
||||
state->application_state = RECORDED;
|
||||
break;
|
||||
@ -302,20 +327,6 @@ int init()
|
||||
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;
|
||||
}
|
||||
|
||||
@ -357,10 +368,10 @@ void application_state_to_string(application_state state, char* string)
|
||||
sprintf(string, "Selecting Device [0-9] -> [y/RET], ");
|
||||
break;
|
||||
case RECORDING:
|
||||
sprintf(string, "Recording [ESC] to cancel");
|
||||
sprintf(string, "Recording");
|
||||
break;
|
||||
case RECORDED:
|
||||
sprintf(string, "Recorded [y/RET] to play [ESC] to Select");
|
||||
sprintf(string, "Recorded");
|
||||
break;
|
||||
case PLAYBACK:
|
||||
sprintf(string, "Playing");
|
||||
@ -431,9 +442,19 @@ int text_texture_free(text_texture* text_texture)
|
||||
SDL_DestroyTexture(text_texture->texture);
|
||||
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)
|
||||
{
|
||||
// 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);
|
||||
printf("Recording Device: %s\n", SDL_GetAudioDeviceName(index, 1));
|
||||
if (!device_id) {
|
||||
@ -443,6 +464,7 @@ 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_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_position_max = MAX_RECORDING_SECONDS * bytes_per_second;
|
||||
|
||||
@ -451,13 +473,21 @@ SDL_AudioDeviceID audio_recording_init(int index, void* userdata)
|
||||
|
||||
return device_id;
|
||||
}
|
||||
|
||||
// INFO:
|
||||
SDL_AudioDeviceID audio_playback_init(void* userdata)
|
||||
{
|
||||
if (!recording_buffer) {
|
||||
printf("Audio buffer not initialized");
|
||||
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);
|
||||
if (!device_id) {
|
||||
@ -482,21 +512,23 @@ void audio_buffer_destroy()
|
||||
recording_buffer_size = 0;
|
||||
recording_buffer_position = 0;
|
||||
}
|
||||
|
||||
// INFO: callback here
|
||||
void audio_recording_callback(void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
memcpy(&recording_buffer[recording_buffer_position], stream, len);
|
||||
recording_buffer_position += len;
|
||||
char string[64];
|
||||
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);
|
||||
}
|
||||
|
||||
// INFO:
|
||||
void audio_playback_callback(void* userdata, Uint8* stream, int len)
|
||||
{
|
||||
memcpy(stream, &recording_buffer[recording_buffer_position], len);
|
||||
recording_buffer_position += len;
|
||||
char string[64];
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user