Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio: Properly close audio device on emulator destruction #637

Merged
merged 2 commits into from
Nov 19, 2024
Merged
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: 2 additions & 0 deletions include/audio/miniaudio_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ class MiniAudioDevice {
bool running = false;

std::vector<std::string> audioDevices;

public:
MiniAudioDevice();
// If safe is on, we create a null audio device
void init(Samples& samples, bool safe = false);
void close();

void start();
void stop();
Expand Down
23 changes: 17 additions & 6 deletions src/core/audio/miniaudio_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void MiniAudioDevice::init(Samples& samples, bool safe) {

// TODO: Make backend selectable here
found = true;
//count = 1;
//backends[0] = backend;
// count = 1;
// backends[0] = backend;
}

if (!found) {
Expand Down Expand Up @@ -81,8 +81,8 @@ void MiniAudioDevice::init(Samples& samples, bool safe) {
deviceConfig.playback.format = ma_format_s16;
deviceConfig.playback.channels = channelCount;
deviceConfig.sampleRate = sampleRate;
//deviceConfig.periodSizeInFrames = 64;
//deviceConfig.periods = 16;
// deviceConfig.periodSizeInFrames = 64;
// deviceConfig.periods = 16;
deviceConfig.pUserData = this;
deviceConfig.aaudio.usage = ma_aaudio_usage_game;
deviceConfig.wasapi.noAutoConvertSRC = true;
Expand Down Expand Up @@ -130,7 +130,7 @@ void MiniAudioDevice::start() {

void MiniAudioDevice::stop() {
if (!initialized) {
Helpers::warn("MiniAudio device not initialized, can't start");
Helpers::warn("MiniAudio device not initialized, can't stop");
return;
}

Expand All @@ -139,6 +139,17 @@ void MiniAudioDevice::stop() {

if (ma_device_stop(&device) != MA_SUCCESS) {
Helpers::warn("Failed to stop audio device");
}
}
}
}

void MiniAudioDevice::close() {
stop();

if (initialized) {
initialized = false;

ma_device_uninit(&device);
ma_context_uninit(&context);
}
}
3 changes: 2 additions & 1 deletion src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Emulator::Emulator()
Emulator::~Emulator() {
config.save();
lua.close();
audioDevice.close();

#ifdef PANDA3DS_ENABLE_DISCORD_RPC
discordRpc.stop();
Expand Down Expand Up @@ -442,4 +443,4 @@ void Emulator::loadRenderdoc() {
std::string capturePath = (std::filesystem::current_path() / "RenderdocCaptures").generic_string();
Renderdoc::loadRenderdoc();
Renderdoc::setOutputDir(capturePath, "");
}
}
Loading