List soundcards
[mirrors/Programs.git] / c / SDL2_record_audio / main.c
CommitLineData
bcd9ab56
TM
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
10void SDL_AudioCallbacks(void* userdata, Uint8* stream, int len) {
11 printf("%d\n", (int)*((int *)stream) );
12}
13
14int 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
41163fe4
TM
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));
34 }
35
bcd9ab56
TM
36 dev = SDL_OpenAudioDevice(NULL, 1, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
37 if (dev == 0) {
38 SDL_Log("Failed to open audio: %s", SDL_GetError());
39 } else {
40 if (have.format != want.format) { /* we let this one thing change. */
41 SDL_Log("We didn't get Float32 audio format.");
42 }
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);
46 }
47
48 return EXIT_SUCCESS;
49}
This page took 0.251255 seconds and 4 git commands to generate.