From f2800aaf1e8fbbc015abbcb45ed4eae70db2cbed Mon Sep 17 00:00:00 2001 From: jaby Date: Mon, 23 Sep 2024 19:59:52 +0200 Subject: [PATCH] Support SFX --- .../PoolBox/application/src/asset_mgr.cpp | 4 ++- include/PSX/File/file_types.hpp | 8 ++++-- include/PSX/SPU/spu.hpp | 28 +++++++++++++------ .../System/IOPorts/IOValues/spu_io_values.hpp | 4 +++ .../src/File/Processor/vag_processor.cpp | 18 ++++++------ src/Library/src/SPU/spu.cpp | 6 ++-- 6 files changed, 44 insertions(+), 24 deletions(-) diff --git a/examples/PoolBox/application/src/asset_mgr.cpp b/examples/PoolBox/application/src/asset_mgr.cpp index 326e648d..b6153c4a 100644 --- a/examples/PoolBox/application/src/asset_mgr.cpp +++ b/examples/PoolBox/application/src/asset_mgr.cpp @@ -59,10 +59,12 @@ namespace Assets { } namespace Main { + using SPU::operator""_vol; + static const CDFile Files[] = { CDFileBuilder::simple_tim(LBA::PACO, PacoTIM), CDFileBuilder::simple_tim(LBA::DFISH, DoenerFishInfo.tim), - CDFileBuilder::sony_vag(LBA::APPLE_SFX, VAG::create(0)), + CDFileBuilder::sony_vag(LBA::APPLE_SFX, VAG::create(0, 1.0_vol)), CustomCDFileBuilder::jingle(32), }; diff --git a/include/PSX/File/file_types.hpp b/include/PSX/File/file_types.hpp index 00a247cf..542dbf99 100644 --- a/include/PSX/File/file_types.hpp +++ b/include/PSX/File/file_types.hpp @@ -2,6 +2,7 @@ #include "../Auxiliary/bits.hpp" #include "../GPU/gpu_types.hpp" #include "../jabyengine_defines.hpp" +#include "../SPU/spu.hpp" namespace JabyEngine { #pragma pack(push, 1) @@ -68,10 +69,11 @@ namespace JabyEngine { }; struct VAG { - uint8_t voice_number; + uint8_t voice_number; + SPU::SimpleVolume inital_stereo_vol; - static constexpr VAG create(uint8_t voice_num) { - return VAG{.voice_number = voice_num}; + static constexpr VAG create(uint8_t voice_num, SPU::SimpleVolume volume) { + return VAG{.voice_number = voice_num, .inital_stereo_vol = volume}; } }; diff --git a/include/PSX/SPU/spu.hpp b/include/PSX/SPU/spu.hpp index 84937d52..235bfb5f 100644 --- a/include/PSX/SPU/spu.hpp +++ b/include/PSX/SPU/spu.hpp @@ -3,31 +3,41 @@ namespace JabyEngine { namespace SPU { - using SRAMAdr = SPU_IO::SRAMAdr; + using SPU_IO::operator""_vol; + + using SRAMAdr = SPU_IO::SRAMAdr; + using SimpleVolume = SPU_IO::SimpleVolume; + using SweepVolume = SPU_IO::SweepVolume; // TODO: Rename to sample...? struct Voice { - size_t get_id() const { + size_t get_id() { return reinterpret_cast(this); } - SRAMAdr allocate(size_t size) const; - SRAMAdr allocate(SPU_IO::SampleRate frequency, size_t size) const; - void deallocate() const; + SRAMAdr allocate(size_t size); + SRAMAdr allocate(SPU_IO::SampleRate frequency, size_t size); + void deallocate(); - void set_sample_rate(SPU_IO::SampleRate frequency) const { + void set_sample_rate(SPU_IO::SampleRate frequency) { SPU_IO::Voice[Voice::get_id()].sampleRate.write(frequency); } - void play() const { + void set_volume(SimpleVolume left, SimpleVolume right) { + // TODO: Verify that assembly code is super simple + SPU_IO::Voice[Voice::get_id()].volumeLeft.write(SweepVolume::create(left)); + SPU_IO::Voice[Voice::get_id()].volumeRight.write(SweepVolume::create(right)); + } + + void play() { SPU_IO::Key::On.write(SPU_IO::KeyOn::for_specific(Voice::get_id())); } - void stop() const { + void stop() { SPU_IO::Key::Off.write(SPU_IO::KeyOff::for_specific(Voice::get_id())); } }; - extern const Voice voice[SPU_IO::VoiceCount]; + extern Voice voice[SPU_IO::VoiceCount]; } } \ No newline at end of file diff --git a/include/PSX/System/IOPorts/IOValues/spu_io_values.hpp b/include/PSX/System/IOPorts/IOValues/spu_io_values.hpp index 8a5ae53c..d2a9bcbe 100644 --- a/include/PSX/System/IOPorts/IOValues/spu_io_values.hpp +++ b/include/PSX/System/IOPorts/IOValues/spu_io_values.hpp @@ -219,6 +219,10 @@ namespace JabyEngine { static constexpr auto Step = BitRange::from_to(0, 1); }; + static constexpr SweepVolume create(SimpleVolume volume) { + return from(VolumeMode::Enable, VolumeMode::Volume.with(volume.raw >> 1)); + } + static constexpr SweepVolume mute() { return SweepVolume{0}; } diff --git a/src/Library/src/File/Processor/vag_processor.cpp b/src/Library/src/File/Processor/vag_processor.cpp index dec432df..ff429dd8 100644 --- a/src/Library/src/File/Processor/vag_processor.cpp +++ b/src/Library/src/File/Processor/vag_processor.cpp @@ -31,12 +31,12 @@ namespace JabyEngine { }; struct VAGState { - uint32_t voice_id; - size_t words_left; - SPU::SRAMAdr adr; + uint32_t voice_id; + size_t words_left; + SPU::SimpleVolume inital_vol; - static constexpr VAGState create(uint32_t voice_id) { - return VAGState{.voice_id = voice_id, .words_left = 0, .adr = 0}; + static constexpr VAGState create(uint32_t voice_id, SPU::SimpleVolume inital_vol) { + return VAGState{.voice_id = voice_id, .words_left = 0, .inital_vol = inital_vol}; } }; @@ -55,11 +55,13 @@ namespace JabyEngine { const auto bytes = header.get_sample_size(); state.words_left = bytes_to_words(bytes); - state.adr = SPU::voice[state.voice_id].allocate(SPU_IO::SampleRate::from_HZ(header.get_sample_frequency()), words_to_bytes(state.words_left)); + + auto sram_adr = SPU::voice[state.voice_id].allocate(SPU_IO::SampleRate::from_HZ(header.get_sample_frequency()), words_to_bytes(state.words_left)); + SPU::voice[state.voice_id].set_volume(state.inital_vol, state.inital_vol); config.processed(sizeof(VAGHeader)); SPU::internal::DMA::Receive::prepare(); - SPU::internal::DMA::Receive::set_dst(state.adr); + SPU::internal::DMA::Receive::set_dst(sram_adr); SPU::internal::DMA::Receive::set_src(reinterpret_cast(config.data_adr)); return Helper::exchange_and_execute_process_function(parse_sample, config, state); @@ -69,7 +71,7 @@ namespace JabyEngine { } State create(const uint32_t* data_adr, const VAG& file) { - return State::from(VAGState::create(file.voice_number), data_adr, parse_header); + return State::from(VAGState::create(file.voice_number, file.inital_stereo_vol), data_adr, parse_header); } } } diff --git a/src/Library/src/SPU/spu.cpp b/src/Library/src/SPU/spu.cpp index f541152f..46042ad6 100644 --- a/src/Library/src/SPU/spu.cpp +++ b/src/Library/src/SPU/spu.cpp @@ -8,7 +8,7 @@ namespace JabyEngine { namespace SPU { - SRAMAdr Voice :: allocate(size_t size) const { + SRAMAdr Voice :: allocate(size_t size) { Voice::stop(); const auto voice_id = Voice::get_id(); const auto adr = SRAMAdr{static_cast(reinterpret_cast(SPU_MMU::allocate(voice_id, size)))}; @@ -17,13 +17,13 @@ namespace JabyEngine { return adr; } - SRAMAdr Voice :: allocate(SPU_IO::SampleRate frequency, size_t size) const { + SRAMAdr Voice :: allocate(SPU_IO::SampleRate frequency, size_t size) { const auto result = Voice::allocate(size); Voice::set_sample_rate(frequency); return result; } - void Voice :: deallocate() const { + void Voice :: deallocate() { Voice::stop(); SPU_MMU::deallocate(Voice::get_id()); }