| 1 | //gcc -Wextra -pedantic-errors -o main.out main.c -lSDL2 |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | #include <SDL2/SDL.h> |
| 8 | |
| 9 | |
| 10 | void SDL_AudioCallbacks(void* userdata, Uint8* stream, int len) { |
| 11 | printf("%d\n", (int)*((int *)stream) ); |
| 12 | } |
| 13 | |
| 14 | int main(void) { |
| 15 | SDL_AudioSpec want, have; |
| 16 | SDL_AudioDeviceID dev; |
| 17 | |
| 18 | SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */ |
| 19 | want.freq = 48000; |
| 20 | want.format = AUDIO_F32; |
| 21 | want.channels = 2; //1,2,4,6 |
| 22 | want.samples = 4096; |
| 23 | want.callback = SDL_AudioCallbacks; /* you wrote this function elsewhere -- see SDL_AudioSpec for details */ |
| 24 | //want.userdata = NULL; |
| 25 | |
| 26 | if((SDL_Init(SDL_INIT_AUDIO)==-1)) { |
| 27 | printf("Could not initialize SDL: %s.\n", SDL_GetError()); |
| 28 | exit(-1); |
| 29 | } |
| 30 | |
| 31 | dev = SDL_OpenAudioDevice(NULL, 1, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE); |
| 32 | if (dev == 0) { |
| 33 | SDL_Log("Failed to open audio: %s", SDL_GetError()); |
| 34 | } else { |
| 35 | if (have.format != want.format) { /* we let this one thing change. */ |
| 36 | SDL_Log("We didn't get Float32 audio format."); |
| 37 | } |
| 38 | SDL_PauseAudioDevice(dev, 0); /* start audio playing. */ |
| 39 | SDL_Delay(5000); /* let the audio callback play some sound for 5 seconds. */ |
| 40 | SDL_CloseAudioDevice(dev); |
| 41 | } |
| 42 | |
| 43 | return EXIT_SUCCESS; |
| 44 | } |