-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl2_audio_pump_demo.c
81 lines (63 loc) · 1.89 KB
/
sdl2_audio_pump_demo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2023 Sebastian Pipping <sebastian@pipping.org>
// Licensed under GPL v3 or later
#include <SDL2/SDL.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#define FREQUENCY 44100
#define CHANNELS 2
#define SAMPLE_CHANNEL_BITS 16
#define SAMPLE_FORMAT AUDIO_S16SYS
#define SAMPLES 1024
void input_callback(void *userdata, Uint8 *stream, int len /*in bytes*/) {
if (len < 1) {
return;
}
const SDL_AudioDeviceID output_device = (SDL_AudioDeviceID)(intptr_t)userdata;
assert(output_device != 0);
const int queued_ret = SDL_QueueAudio(output_device, stream, len);
assert(queued_ret == 0);
}
int main() {
const int init_ret = SDL_Init(SDL_INIT_AUDIO);
assert(init_ret == 0);
const SDL_AudioSpec output_spec_wanted = {
.freq = FREQUENCY,
.format = SAMPLE_FORMAT,
.channels = CHANNELS,
.samples = SAMPLES,
.callback = NULL,
};
SDL_AudioDeviceID output_device =
SDL_OpenAudioDevice(NULL, 0, &output_spec_wanted, NULL, 0);
assert(output_device != 0);
const SDL_AudioSpec input_spec_wanted = {
.freq = FREQUENCY,
.format = SAMPLE_FORMAT,
.channels = CHANNELS,
.samples = SAMPLES,
.callback = input_callback,
.userdata = (void *)(intptr_t)output_device,
};
SDL_AudioDeviceID input_device =
SDL_OpenAudioDevice(NULL, 1, &input_spec_wanted, NULL, 0);
assert(input_device != 0);
fprintf(stderr, "Pumping from default input device to default output device, "
"press Ctrl+C to exit...\n");
SDL_PauseAudioDevice(output_device, 0);
SDL_PauseAudioDevice(input_device, 0);
SDL_Event event;
bool running = true;
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type)
case SDL_QUIT:
running = false;
break;
}
SDL_Delay(1);
}
SDL_CloseAudioDevice(output_device);
SDL_CloseAudioDevice(input_device);
return 0;
}