From: Tomas Mudrunka Date: Fri, 10 Jun 2022 16:50:22 +0000 (+0200) Subject: SDL2 audio record test X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=bcd9ab567f4a7eb457716ce0f4bfd697ec391645;p=mirrors%2FPrograms.git SDL2 audio record test --- diff --git a/c/SDL2_record_audio/main.c b/c/SDL2_record_audio/main.c new file mode 100644 index 0000000..b442782 --- /dev/null +++ b/c/SDL2_record_audio/main.c @@ -0,0 +1,44 @@ +//gcc -Wextra -pedantic-errors -o main.out main.c -lSDL2 + +#include +#include +#include + +#include + + +void SDL_AudioCallbacks(void* userdata, Uint8* stream, int len) { + printf("%d\n", (int)*((int *)stream) ); +} + +int main(void) { + SDL_AudioSpec want, have; + SDL_AudioDeviceID dev; + + SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */ + want.freq = 48000; + want.format = AUDIO_F32; + want.channels = 2; //1,2,4,6 + want.samples = 4096; + want.callback = SDL_AudioCallbacks; /* you wrote this function elsewhere -- see SDL_AudioSpec for details */ + //want.userdata = NULL; + + if((SDL_Init(SDL_INIT_AUDIO)==-1)) { + printf("Could not initialize SDL: %s.\n", SDL_GetError()); + exit(-1); + } + + dev = SDL_OpenAudioDevice(NULL, 1, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE); + if (dev == 0) { + SDL_Log("Failed to open audio: %s", SDL_GetError()); + } else { + if (have.format != want.format) { /* we let this one thing change. */ + SDL_Log("We didn't get Float32 audio format."); + } + SDL_PauseAudioDevice(dev, 0); /* start audio playing. */ + SDL_Delay(5000); /* let the audio callback play some sound for 5 seconds. */ + SDL_CloseAudioDevice(dev); + } + + return EXIT_SUCCESS; +}