mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-08 23:25:40 +12:00
Libretro: Add audio support
This commit is contained in:
parent
54a78902bc
commit
be071ffb78
3 changed files with 57 additions and 0 deletions
|
@ -9,6 +9,7 @@
|
|||
#include "ring_buffer.hpp"
|
||||
|
||||
class MiniAudioDevice {
|
||||
public:
|
||||
using Samples = Common::RingBuffer<ma_int16, 0x2000 * 2>;
|
||||
static constexpr ma_uint32 sampleRate = 32768; // 3DS sample rate
|
||||
static constexpr ma_uint32 channelCount = 2; // Audio output is stereo
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
MiniAudioDevice::MiniAudioDevice(const AudioDeviceConfig& audioSettings)
|
||||
: initialized(false), running(false), samples(nullptr), audioSettings(audioSettings) {}
|
||||
|
||||
#ifndef __LIBRETRO__
|
||||
void MiniAudioDevice::init(Samples& samples, bool safe) {
|
||||
this->samples = &samples;
|
||||
running = false;
|
||||
|
@ -213,3 +214,4 @@ void MiniAudioDevice::close() {
|
|||
ma_context_uninit(&context);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -22,6 +22,7 @@ static bool usingGLES = false;
|
|||
|
||||
std::unique_ptr<Emulator> emulator;
|
||||
RendererGL* renderer;
|
||||
MiniAudioDevice* audioDevice;
|
||||
|
||||
std::filesystem::path Emulator::getConfigPath() {
|
||||
return std::filesystem::path(savePath / "config.toml");
|
||||
|
@ -31,6 +32,27 @@ std::filesystem::path Emulator::getAppDataRoot() {
|
|||
return std::filesystem::path(savePath / "Emulator Files");
|
||||
}
|
||||
|
||||
void MiniAudioDevice::init(Samples& samples, bool safe) {
|
||||
this->samples = &samples;
|
||||
initialized = true;
|
||||
running = false;
|
||||
|
||||
audioDevice = this;
|
||||
}
|
||||
|
||||
void MiniAudioDevice::start() {
|
||||
running = true;
|
||||
}
|
||||
|
||||
void MiniAudioDevice::stop() {
|
||||
running = false;
|
||||
}
|
||||
|
||||
void MiniAudioDevice::close() {
|
||||
running = false;
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
static void* getGLProcAddress(const char* name) {
|
||||
return (void*)hwRender.get_proc_address(name);
|
||||
}
|
||||
|
@ -389,6 +411,38 @@ void retro_run() {
|
|||
emulator->runFrame();
|
||||
|
||||
videoCallback(RETRO_HW_FRAME_BUFFER_VALID, emulator->width, emulator->height, 0);
|
||||
|
||||
if (audioDevice->running) {
|
||||
static constexpr int frameCount = 547;
|
||||
static constexpr int channelCount = 2;
|
||||
static int16_t audioBuffer[frameCount * channelCount];
|
||||
|
||||
usize samplesWritten = 0;
|
||||
samplesWritten += audioDevice->samples->pop(audioBuffer, frameCount * channelCount);
|
||||
|
||||
// Get the last sample for underrun handling
|
||||
if (samplesWritten != 0) {
|
||||
std::memcpy(
|
||||
&audioDevice->lastStereoSample[0],
|
||||
&audioBuffer[(samplesWritten - 1) * 2],
|
||||
sizeof(audioDevice->lastStereoSample)
|
||||
);
|
||||
}
|
||||
|
||||
// If underruning, copy the last output sample
|
||||
{
|
||||
s16* pointer = &audioBuffer[samplesWritten * 2];
|
||||
s16 l = audioDevice->lastStereoSample[0];
|
||||
s16 r = audioDevice->lastStereoSample[1];
|
||||
|
||||
for (usize i = samplesWritten; i < frameCount; i++) {
|
||||
*pointer++ = l;
|
||||
*pointer++ = r;
|
||||
}
|
||||
}
|
||||
|
||||
audioBatchCallback(audioBuffer, sizeof(audioBuffer) / (2 * sizeof(int16_t)));
|
||||
}
|
||||
}
|
||||
|
||||
void retro_set_controller_port_device(uint port, uint device) {}
|
||||
|
|
Loading…
Add table
Reference in a new issue