1 //gcc -Wextra -pedantic-errors -o main.out main.c -lSDL2
10 void SDL_AudioCallbacks(void* userdata
, Uint8
* stream
, int len
) {
11 printf("%d\n", (int)*((int *)stream
) );
15 SDL_AudioSpec want
, have
;
16 SDL_AudioDeviceID dev
;
18 SDL_memset(&want
, 0, sizeof(want
)); /* or SDL_zero(want) */
20 want
.format
= AUDIO_F32
;
21 want
.channels
= 2; //1,2,4,6
23 want
.callback
= SDL_AudioCallbacks
; /* you wrote this function elsewhere -- see SDL_AudioSpec for details */
24 //want.userdata = NULL;
26 if((SDL_Init(SDL_INIT_AUDIO
)==-1)) {
27 printf("Could not initialize SDL: %s.\n", SDL_GetError());
31 int i
, count
= SDL_GetNumAudioDevices(0);
32 for (i
= 0; i
< count
; ++i
) {
33 printf("Audio device %d: %s\n", i
, SDL_GetAudioDeviceName(i
, 0));
36 dev
= SDL_OpenAudioDevice(NULL
, 1, &want
, &have
, SDL_AUDIO_ALLOW_FORMAT_CHANGE
);
38 SDL_Log("Failed to open audio: %s", SDL_GetError());
40 if (have
.format
!= want
.format
) { /* we let this one thing change. */
41 SDL_Log("We didn't get Float32 audio format.");
43 SDL_PauseAudioDevice(dev
, 0); /* start audio playing. */
44 SDL_Delay(5000); /* let the audio callback play some sound for 5 seconds. */
45 SDL_CloseAudioDevice(dev
);