Adding audio interface part 1

This commit is contained in:
wheremyfoodat 2025-02-09 03:53:15 +02:00
parent be071ffb78
commit 486e2ea5cb
7 changed files with 91 additions and 51 deletions

View file

@ -0,0 +1,31 @@
#pragma once
#include <array>
#include "config.hpp"
#include "helpers.hpp"
#include "ring_buffer.hpp"
class AudioDeviceInterface {
protected:
using Samples = Common::RingBuffer<s16, 0x2000 * 2>;
Samples* samples = nullptr;
const AudioDeviceConfig& audioSettings;
public:
AudioDeviceInterface(Samples* samples, const AudioDeviceConfig& audioSettings) : samples(samples), audioSettings(audioSettings) {}
// Store the last stereo sample we output. We play this when underruning to avoid pops.
// TODO: Make this protected again before merging!!!
std::array<s16, 2> lastStereoSample{};
bool running = false;
Samples* getSamples() { return samples; }
// If safe is on, we create a null audio device
virtual void init(Samples& samples, bool safe = false) = 0;
virtual void close() = 0;
virtual void start() = 0;
virtual void stop() = 0;
};

View file

@ -0,0 +1,33 @@
#pragma once
#include <atomic>
#include <string>
#include <vector>
#include "audio/audio_device.hpp"
class LibretroAudioDevice : public AudioDeviceInterface {
bool initialized = false;
public:
LibretroAudioDevice(const AudioDeviceConfig& audioSettings) : AudioDeviceInterface(nullptr, audioSettings), initialized(false) {
running = false;
}
void init(Samples& samples, bool safe = false) override {
this->samples = &samples;
initialized = true;
running = false;
}
void close() override {
initialized = false;
running = false;
};
void start() override { running = true; }
void stop() override { running = false; };
bool isInitialized() const { return initialized; }
bool isRunning() const { return running; }
};

View file

@ -3,40 +3,31 @@
#include <string>
#include <vector>
#include "config.hpp"
#include "helpers.hpp"
#include "audio/audio_device.hpp"
#include "miniaudio.h"
#include "ring_buffer.hpp"
class MiniAudioDevice {
public:
using Samples = Common::RingBuffer<ma_int16, 0x2000 * 2>;
class MiniAudioDevice : public AudioDeviceInterface {
static constexpr ma_uint32 sampleRate = 32768; // 3DS sample rate
static constexpr ma_uint32 channelCount = 2; // Audio output is stereo
bool initialized = false;
ma_device device;
ma_context context;
ma_device_config deviceConfig;
Samples* samples = nullptr;
const AudioDeviceConfig& audioSettings;
bool initialized = false;
bool running = false;
// Store the last stereo sample we output. We play this when underruning to avoid pops.
std::array<s16, 2> lastStereoSample;
std::vector<std::string> audioDevices;
public:
MiniAudioDevice(const AudioDeviceConfig& audioSettings);
// If safe is on, we create a null audio device
void init(Samples& samples, bool safe = false);
void close();
void init(Samples& samples, bool safe = false) override;
void close() override;
void start();
void stop();
void start() override;
void stop() override;
bool isInitialized() const { return initialized; }
};

View file

@ -8,6 +8,7 @@
#include "PICA/gpu.hpp"
#include "audio/dsp_core.hpp"
#include "audio/libretro_audio_device.hpp"
#include "audio/miniaudio_device.hpp"
#include "cheats.hpp"
#include "config.hpp"
@ -48,7 +49,11 @@ class Emulator {
Scheduler scheduler;
Crypto::AESEngine aesEngine;
#ifndef __LIBRETRO__
MiniAudioDevice audioDevice;
#else
LibretroAudioDevice audioDevice;
#endif
Cheats cheats;
public:
@ -126,6 +131,7 @@ class Emulator {
LuaManager& getLua() { return lua; }
Scheduler& getScheduler() { return scheduler; }
Memory& getMemory() { return memory; }
AudioDeviceInterface& getAudioDevice() { return audioDevice; }
RendererType getRendererType() const { return config.rendererType; }
Renderer* getRenderer() { return gpu.getRenderer(); }