36 lines
836 B
C
36 lines
836 B
C
|
#include <audio.h>
|
||
|
#include <stdio.h>
|
||
|
|
||
|
int audioInit(AudioContext *aud) {
|
||
|
SDL_AudioSpec spec;
|
||
|
spec.freq = 41700;
|
||
|
spec.format = AUDIO_S16;
|
||
|
spec.channels = 2;
|
||
|
spec.samples = 1024;
|
||
|
spec.callback = NULL;
|
||
|
spec.userdata = NULL;
|
||
|
|
||
|
aud->id = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0);
|
||
|
aud->paused = true;
|
||
|
if (!aud->id) {
|
||
|
fprintf(stderr, "could not open audio device: %s\n", SDL_GetError());
|
||
|
return -1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int audioUpdate(AudioContext *aud, void *data, uint32_t bytes) {
|
||
|
if (!aud->id) return -1;
|
||
|
|
||
|
if (SDL_QueueAudio(aud->id, data, bytes)) {
|
||
|
fprintf(stderr, "could not write audio: %s\n", SDL_GetError());
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (aud->paused) {
|
||
|
SDL_PauseAudioDevice(aud->id, false);
|
||
|
aud->paused = false;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|