Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/ship/audio/SDLAudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ class SDLAudioPlayer final : public AudioPlayer {

private:
SDL_AudioDeviceID mDevice = 0; ///< Handle to the opened SDL audio device.
int32_t mNumChannels = 2; ///< Number of output channels (2 for stereo, 6 for 5.1).
uint8_t mNumChannels = 2; ///< Number of output channels (2 for stereo, 6 for 5.1).
};
} // namespace Ship
37 changes: 22 additions & 15 deletions src/ship/audio/SDLAudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,47 @@ void SDLAudioPlayer::DoClose() {
if (mDevice != 0) {
// Pause playback first
SDL_PauseAudioDevice(mDevice, 1);
// Clear any queued audio to prevent glitches when reopening

// Prevent stale audio when reopening
SDL_ClearQueuedAudio(mDevice);
SDL_CloseAudioDevice(mDevice);
mDevice = 0;
}
}

bool SDLAudioPlayer::DoInit() {
if (SDL_Init(SDL_INIT_AUDIO) != 0) {
if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0) {
Comment thread
briaguya0 marked this conversation as resolved.
SPDLOG_ERROR("SDL init error: {}", SDL_GetError());
return false;
}

// Always open with the correct number of output channels
mNumChannels = this->GetNumOutputChannels();

SDL_AudioSpec want, have;
SDL_zero(want);

want.freq = this->GetSampleRate();
want.format = AUDIO_S16SYS;
want.channels = mNumChannels;
want.samples = this->GetSampleLength();
want.callback = NULL;
want.channels = this->GetNumOutputChannels();

// Increase backend/device buffering slightly
// to reduce rare underruns/glitches.
//
// IMPORTANT:
// This is NOT part of the emulator buffering logic.
// It only affects SDL/backend latency.
want.samples = GetSampleLength() * 2;
Comment thread
bassdr marked this conversation as resolved.

want.callback = nullptr;

mDevice = SDL_OpenAudioDevice(nullptr, 0, &want, &have, 0);

mDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (mDevice == 0) {
SPDLOG_ERROR("SDL_OpenAudio error: {}", SDL_GetError());
SPDLOG_ERROR("SDL_OpenAudioDevice error: {}", SDL_GetError());
return false;
}

SPDLOG_INFO("SDL Audio initialized: {} channels, {} Hz", mNumChannels, this->GetSampleRate());
mNumChannels = have.channels;

SPDLOG_INFO("SDL audio initialized: {} Hz, {} channels, {} samples", have.freq, have.channels, have.samples);

SDL_PauseAudioDevice(mDevice, 0);
return true;
Expand All @@ -54,9 +64,6 @@ int SDLAudioPlayer::Buffered() {
}

void SDLAudioPlayer::DoPlay(const uint8_t* buf, size_t len) {
if (Buffered() < 6000) {
Comment thread
bassdr marked this conversation as resolved.
// Don't fill the audio buffer too much in case this happens
SDL_QueueAudio(mDevice, buf, len);
}
SDL_QueueAudio(mDevice, buf, len);
}
} // namespace Ship
Loading