2023-02-19 10:46:12 +01:00
|
|
|
#include "../ib_sound.h"
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2023-02-19 12:38:25 +01:00
|
|
|
#include <dp_common.h>
|
|
|
|
|
|
|
|
#include "SDL.h"
|
|
|
|
|
|
|
|
/* The function that actually produces the output audio */
|
|
|
|
static void (*audio_callback)(short* output_buffer, size_t frames_to_do, void *user_data);
|
|
|
|
|
|
|
|
static void dp_sdl_error(const char *const msg)
|
|
|
|
{
|
|
|
|
log_error("%s: %s", msg, SDL_GetError());
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_AudioDeviceID audio_device;
|
|
|
|
|
|
|
|
static void Callback(void *user_data, Uint8 *output_buffer, int bytes_to_do)
|
|
|
|
{
|
|
|
|
const int frames_to_do = bytes_to_do / 2 / sizeof(short);
|
|
|
|
|
|
|
|
audio_callback((short*)output_buffer, (size_t)frames_to_do, user_data);
|
|
|
|
}
|
2023-02-19 10:46:12 +01:00
|
|
|
|
|
|
|
int IB_StartupSound(void (*initial_callback)(unsigned int output_sample_rate, void *user_data), void (*_audio_callback)(short* output_buffer, size_t frames_to_do, void *user_data), void *user_data)
|
|
|
|
{
|
2023-02-19 12:38:25 +01:00
|
|
|
#if 0
|
2023-02-19 10:46:12 +01:00
|
|
|
unsigned int freq = 48000;
|
|
|
|
initial_callback(freq, user_data);
|
|
|
|
return 1;
|
2023-02-19 12:38:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_AudioSpec desired_audio_specification;
|
|
|
|
SDL_AudioSpec obtained_audio_specification;
|
|
|
|
|
|
|
|
audio_callback = _audio_callback;
|
|
|
|
|
|
|
|
if (SDL_Init(SDL_INIT_AUDIO))
|
|
|
|
{
|
|
|
|
dp_sdl_error("IB_StartupSound: SDL_INIT_AUDIO failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
desired_audio_specification.freq = 48000;
|
|
|
|
desired_audio_specification.format = AUDIO_S16;
|
|
|
|
desired_audio_specification.channels = 2;
|
|
|
|
desired_audio_specification.samples = 0x200; /* About 10ms at 48000Hz. */
|
|
|
|
desired_audio_specification.callback = Callback;
|
|
|
|
desired_audio_specification.userdata = user_data;
|
|
|
|
|
|
|
|
audio_device = SDL_OpenAudioDevice(NULL,
|
|
|
|
0, &desired_audio_specification, &obtained_audio_specification,
|
|
|
|
SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE);
|
|
|
|
|
|
|
|
if (audio_device)
|
|
|
|
{
|
|
|
|
initial_callback(obtained_audio_specification.freq, user_data);
|
|
|
|
SDL_PauseAudioDevice(audio_device, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dp_sdl_error("IB_StartupSound: SDL_INIT_AUDIO failed");
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
return 0;
|
2023-02-19 10:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IB_ShutdownSound(void)
|
|
|
|
{
|
2023-02-19 12:38:25 +01:00
|
|
|
SDL_CloseAudioDevice(audio_device);
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
2023-02-19 10:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IB_LockSound(void)
|
|
|
|
{
|
2023-02-19 12:38:25 +01:00
|
|
|
SDL_LockAudioDevice(audio_device);
|
2023-02-19 10:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void IB_UnlockSound(void)
|
|
|
|
{
|
2023-02-19 12:38:25 +01:00
|
|
|
SDL_UnlockAudioDevice(audio_device);
|
2023-02-19 10:46:12 +01:00
|
|
|
}
|