Skip to content

Commit

Permalink
AutoRelease better log and null handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone Coco committed May 20, 2024
1 parent 315a522 commit 7479ad4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*build*/
.idea/
roms/
saves/
*.bin
*.sh
.cache/
Expand Down
1 change: 0 additions & 1 deletion src/backend/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ struct Event;

namespace n64 {
struct Core {
~Core() { Stop(); }
Core(ParallelRDP&);
void Stop();
void LoadROM(const std::string&);
Expand Down
36 changes: 20 additions & 16 deletions src/backend/core/mmio/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ namespace n64 {

void audioCallback(void* user, Uint8* stream, int length) {
auto audioDevice = (AudioDevice*)user;
int gotten = 0;
audioDevice->LockMutex();
int available = SDL_AudioStreamAvailable(audioDevice->GetStream());
int gotten = 0, available = 0;
if (audioDevice) {
audioDevice->LockMutex();
}

if (audioDevice) {
available = SDL_AudioStreamAvailable(audioDevice->GetStream().get());
}

if (available > 0) {
gotten = SDL_AudioStreamGet(audioDevice->GetStream(), stream, length);
if (available > 0 && audioDevice) {
gotten = SDL_AudioStreamGet(audioDevice->GetStream().get(), stream, length);
}

if (audioDevice) {
audioDevice->UnlockMutex();
}
audioDevice->UnlockMutex();

int gottenSamples = (int)(gotten / sizeof(float));
auto* out = (float*)stream;
Expand All @@ -28,13 +36,11 @@ void audioCallback(void* user, Uint8* stream, int length) {
}
}

AudioDevice::AudioDevice() {
AudioDevice::AudioDevice() : audioStream(SDL_NewAudioStream, SDL_FreeAudioStream, "audioStream", SYSTEM_SAMPLE_FORMAT, 2, AUDIO_SAMPLE_RATE, SYSTEM_SAMPLE_FORMAT, 2, AUDIO_SAMPLE_RATE),
audioStreamMutex(SDL_CreateMutex, SDL_DestroyMutex, "audioStreamMutex") {
SDL_InitSubSystem(SDL_INIT_AUDIO);
audioStream = SDL_NewAudioStream(SYSTEM_SAMPLE_FORMAT, 2, AUDIO_SAMPLE_RATE, SYSTEM_SAMPLE_FORMAT, 2, AUDIO_SAMPLE_RATE);

audioStreamMutex = SDL_CreateMutex();

if(!audioStreamMutex) {
if(!audioStreamMutex.get()) {
Util::panic("Unable to initialize audio mutex: {}", SDL_GetError());
}

Expand All @@ -59,17 +65,15 @@ void AudioDevice::PushSample(float left, float volumeL, float right, float volum
float adjustedR = right * volumeR;
float samples[2]{ adjustedL, adjustedR };

auto availableBytes = (float)SDL_AudioStreamAvailable(audioStream);
auto availableBytes = (float)SDL_AudioStreamAvailable(audioStream.get());
if(availableBytes <= BYTES_PER_HALF_SECOND) {
SDL_AudioStreamPut(audioStream, samples, 2 * sizeof(float));
SDL_AudioStreamPut(audioStream.get(), samples, 2 * sizeof(float));
}
}

void AudioDevice::AdjustSampleRate(int sampleRate) {
LockMutex();
if(audioStream) SDL_FreeAudioStream(audioStream);

audioStream = SDL_NewAudioStream(SYSTEM_SAMPLE_FORMAT, 2, sampleRate, SYSTEM_SAMPLE_FORMAT, 2, AUDIO_SAMPLE_RATE);
audioStream.Construct(SDL_NewAudioStream, SYSTEM_SAMPLE_FORMAT, 2, sampleRate, SYSTEM_SAMPLE_FORMAT, 2, AUDIO_SAMPLE_RATE);
UnlockMutex();
}
}
22 changes: 10 additions & 12 deletions src/backend/core/mmio/Audio.hpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
#pragma once
#include <common.hpp>
#include <MemoryHelpers.hpp>
#include <SDL2/SDL.h>

namespace n64 {
struct AudioDevice {
AudioDevice();
~AudioDevice() {
SDL_FreeAudioStream(audioStream);
SDL_DestroyMutex(audioStreamMutex);
}

void PushSample(float, float, float, float);
void AdjustSampleRate(int);
void LockMutex() {
if(audioStreamMutex)
SDL_LockMutex(audioStreamMutex);
if(audioStreamMutex.get())
SDL_LockMutex(audioStreamMutex.get());
}
void UnlockMutex() {
if (audioStreamMutex)
SDL_UnlockMutex(audioStreamMutex);
if (audioStreamMutex.get())
SDL_UnlockMutex(audioStreamMutex.get());
}

SDL_AudioStream* GetStream() { return audioStream; }
Util::AutoRelease<SDL_AudioStream, const SDL_AudioFormat, const Uint8, const int, const SDL_AudioFormat,
const Uint8, const int>& GetStream() { return audioStream; }
private:
SDL_AudioStream* audioStream = nullptr;
SDL_mutex* audioStreamMutex = nullptr;
Util::AutoRelease<SDL_AudioStream, const SDL_AudioFormat, const Uint8, const int, const SDL_AudioFormat,
const Uint8, const int> audioStream;
Util::AutoRelease<SDL_mutex> audioStreamMutex;
SDL_AudioSpec audioSpec{};
SDL_AudioSpec request{};
SDL_AudioDeviceID handle{};
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/EmuThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
EmuThread::EmuThread(std::unique_ptr<QtInstanceFactory>&& instance_, std::unique_ptr<Vulkan::WSIPlatform>&& wsiPlatform_, std::unique_ptr<ParallelRDP::WindowInfo>&& windowInfo_, SettingsWindow& settings) noexcept
: instance(std::move(instance_)), wsiPlatform(std::move(wsiPlatform_)),
windowInfo(std::move(windowInfo_)),
controller(SDL_GameControllerClose),
controller(SDL_GameControllerClose, "GameController"),
core(parallel), settings(settings) {}

[[noreturn]] void EmuThread::run() noexcept {
Expand Down
12 changes: 9 additions & 3 deletions src/utils/MemoryHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
namespace Util {
template <class T, typename... Args>
struct AutoRelease {
AutoRelease(void (*dtor)(T*)) : dtor(dtor) { }
AutoRelease(void (*dtor)(T*), const char* name = "") : dtor(dtor), name(name) { }

AutoRelease(T* (*ctor)(Args...), void (*dtor)(T*), Args... args) : dtor(dtor) {
AutoRelease(T* (*ctor)(Args...), void (*dtor)(T*), const char* name = "", Args... args) : dtor(dtor), name(name) {
thing = ctor(args...);
}

T* get() { return thing; }
T* get() {
if (!thing) {
Util::panic("AutoRelease::{} is null!", name);
}
return thing;
}

void Construct(T* (*ctor)(Args...), Args... args) {
if(thing) {
Expand All @@ -36,6 +41,7 @@ struct AutoRelease {
}
}
private:
const char* name = "";
T* thing = nullptr;
void (*dtor)(T*) = nullptr;
};
Expand Down

0 comments on commit 7479ad4

Please sign in to comment.