Basic audio system start

This commit is contained in:
PJB3005
2026-03-12 11:49:03 +01:00
parent d77bceb821
commit 9dbade65d4
6 changed files with 121 additions and 1 deletions
+5 -1
View File
@@ -83,7 +83,11 @@ source_group("dusk" FILES ${DUSK_FILES})
# game_debug is for game code files that we know work when compiled with DEBUG=1
# Of course, if building a release build, this distinction is irrelevant
add_library(game_debug STATIC ${JSYSTEM_DEBUG_FILES} ${SSYSTEM_FILES})
add_library(game_debug STATIC ${JSYSTEM_DEBUG_FILES} ${SSYSTEM_FILES}
src/dusk/audio/DuskAudioSystem.cpp
src/dusk/audio/JASCriticalSection.cpp
src/dusk/audio/DuskDsp.hpp
src/dusk/audio/DuskDsp.cpp)
target_compile_definitions(game_debug PRIVATE TARGET_PC AVOID_UB=1 VERSION=0 $<$<CONFIG:Debug>:DEBUG=1>)
# Make these properties PUBLIC so that the regular game target also sees them.
+3
View File
@@ -0,0 +1,3 @@
#pragma once
void DuskAudioInitialize();
@@ -15,6 +15,8 @@
#include "JSystem/JKernel/JKRSolidHeap.h"
#include "JSystem/JKernel/JKRThread.h"
#include "dusk/audio/DuskAudioSystem.h"
JAU_JASInitializer::JAU_JASInitializer() {
audioMemory_ = 0;
audioMemSize_ = 0;
@@ -60,7 +62,11 @@ void JAU_JASInitializer::initJASystem(JKRSolidHeap* heap) {
}
JASDvd::createThread(dvdThreadPriority_, 0x80, 0x1000);
#if TARGET_PC
DuskAudioInitialize();
#else
JASAudioThread::create(audioThreadPriority_);
#endif
JKRThreadSwitch* threadSwitch = JKRThreadSwitch::getManager();
if (threadSwitch) {
if (dvdThreadId_ >= 0) {
+82
View File
@@ -0,0 +1,82 @@
#include "dusk/audio/DuskAudioSystem.h"
#include <SDL3/SDL_init.h>
#include <array>
#include "JSystem/JAudio2/JASAiCtrl.h"
#include "JSystem/JAudio2/JASChannel.h"
#include "JSystem/JAudio2/JASCriticalSection.h"
#include "JSystem/JAudio2/JASDSPChannel.h"
#include "JSystem/JAudio2/JASHeapCtrl.h"
#include "DuskDsp.hpp"
static DspSubframe AllSubframeBuffers[DSP_OUTPUT_CHANNELS];
static SDL_AudioStream* PlaybackStream;
static void SDLCALL GetNewAudio(
void *userdata,
SDL_AudioStream *stream,
int additional_amount,
int total_amount);
static int RenderNewAudioFrame();
static void RenderAudioSubframe();
static void InitSDL3Output() {
SDL_Init(SDL_INIT_AUDIO);
constexpr SDL_AudioSpec spec = {
SDL_AUDIO_S16,
1,
32000,
};
PlaybackStream = SDL_OpenAudioDeviceStream(
SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK,
&spec,
&GetNewAudio,
nullptr);
}
void DuskAudioInitialize() {
InitSDL3Output();
JASDsp::initBuffer();
JASDSPChannel::initAll();
JASPoolAllocObject_MultiThreaded<JASChannel>::newMemPool(0x48);
SDL_ResumeAudioStreamDevice(PlaybackStream);
}
void SDLCALL GetNewAudio(
void*,
SDL_AudioStream*,
int,
int total_amount) {
while (total_amount > 0) {
const int rendered = RenderNewAudioFrame();
total_amount -= rendered;
}
}
int RenderNewAudioFrame() {
JASCriticalSection section;
const u32 countSubframes = JASDriver::getSubFrames();
for (u32 i = 0; i < countSubframes; i++) {
RenderAudioSubframe();
}
return static_cast<u16>(countSubframes) * DSP_SUBFRAME_SIZE;
}
void RenderAudioSubframe() {
DspSubframe& subFrame = AllSubframeBuffers[0];
JASDriver::updateDSP();
DuskDspRender(subFrame);
SDL_PutAudioStreamData(PlaybackStream, &subFrame, sizeof(subFrame));
}
+16
View File
@@ -0,0 +1,16 @@
#include "DuskDsp.hpp"
#include <limits>
static float SinePos;
void DuskDspRender(DspSubframe& subframe) {
subframe.fill(0);
for (auto& elem : subframe) {
elem = static_cast<s16>(sinf(SinePos) * std::numeric_limits<s16>::max() * 0.2);
SinePos += 0.05f;
}
auto& channels = *reinterpret_cast<std::array<JASDsp::TChannel, DSP_CHANNELS>*>(JASDsp::CH_BUF);
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "JSystem/JAudio2/JASDSPInterface.h"
#include <array>
using DspSubframe = std::array<s16, DSP_SUBFRAME_SIZE>;
void DuskDspRender(DspSubframe& subframe);