Skip to content

Commit

Permalink
Audio: Properly close audio device on emulator destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
wheremyfoodat authored Nov 19, 2024
1 parent 452510b commit 37ee5c5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
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 @@ -249,7 +250,7 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
success = loadELF(path);
else if (extension == ".3ds" || extension == ".cci")
success = loadNCSD(path, ROMType::NCSD);
else if (extension == ".cxi" || extension == ".app" || extension == ".ncch")
else if (extension == ".cxi" || extension == ".app")
success = loadNCSD(path, ROMType::CXI);
else if (extension == ".3dsx")
success = load3DSX(path);
Expand Down

0 comments on commit 37ee5c5

Please sign in to comment.