2024-10-22 00:52:51 +00:00
|
|
|
#include <audio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int audioInit(AudioContext *aud) {
|
|
|
|
SDL_AudioSpec spec;
|
|
|
|
spec.freq = 41700;
|
2024-10-22 03:31:55 +00:00
|
|
|
spec.format = SDL_AUDIO_S16;
|
2024-10-22 00:52:51 +00:00
|
|
|
spec.channels = 2;
|
|
|
|
|
2024-10-22 03:31:55 +00:00
|
|
|
aud->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, NULL, NULL);
|
2024-10-22 00:52:51 +00:00
|
|
|
aud->paused = true;
|
2024-10-22 03:31:55 +00:00
|
|
|
if (!aud->stream) {
|
|
|
|
fprintf(stderr, "could not open audio device stream: %s\n", SDL_GetError());
|
2024-10-22 00:52:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int audioUpdate(AudioContext *aud, void *data, uint32_t bytes) {
|
2024-10-22 03:31:55 +00:00
|
|
|
if (!aud->stream) return -1;
|
2024-10-22 00:52:51 +00:00
|
|
|
|
2024-10-22 03:31:55 +00:00
|
|
|
if (!SDL_PutAudioStreamData(aud->stream, data, bytes)) {
|
2024-10-22 00:52:51 +00:00
|
|
|
fprintf(stderr, "could not write audio: %s\n", SDL_GetError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aud->paused) {
|
2024-10-22 03:31:55 +00:00
|
|
|
SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(aud->stream));
|
2024-10-22 00:52:51 +00:00
|
|
|
aud->paused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|