mirror of
https://github.com/azahar-emu/azahar.git
synced 2026-11-16 03:00:02 +01:00
audio_core: Implement CSND support (#2680)
This commit is contained in:
1 parent
6daa07ab2a
commit
004fe74d07
7 files changed
+1421
-522
No files matched your search
@@ -2,6 +2,8 @@ add_library(audio_core STATIC EXCLUDE_FROM_ALL
|
||||
audio_types.h
|
||||
codec.cpp
|
||||
codec.h
|
||||
csnd.cpp
|
||||
csnd.h
|
||||
dsp_interface.cpp
|
||||
dsp_interface.h
|
||||
hle/aac_decoder.cpp
|
||||
|
||||
@@ -0,0 +1,354 @@
|
||||
// Copyright 2026 Citra Emulator Project / Azahar Emulator Project
|
||||
// Licensed under GPLv3 or any later version
|
||||
// Refer to the LICENSE.txt file included.
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/serialization/array.hpp>
|
||||
#include "audio_core/csnd.h"
|
||||
#include "common/archives.h"
|
||||
#include "core/memory.h"
|
||||
|
||||
namespace AudioCore {
|
||||
|
||||
namespace {
|
||||
|
||||
/// The channel timers tick at 67027964 Hz (the ARM11 base clock / 4) and the output runs at
|
||||
/// 67027964 / 2048 Hz (the native sample rate), so one output sample is 2048 timer ticks.
|
||||
constexpr u32 TimerTicksPerOutputSample = 2048;
|
||||
/// Shortest period the hardware supports, faster timer values hang it.
|
||||
constexpr u32 MinTimerPeriod = 0x42;
|
||||
|
||||
constexpr u16 MaxVolume = 0x8000;
|
||||
|
||||
constexpr std::array<s16, 89> ImaStepTable{
|
||||
7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23,
|
||||
25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80,
|
||||
88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
|
||||
307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
|
||||
1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
|
||||
3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
|
||||
12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767,
|
||||
};
|
||||
constexpr std::array<s8, 8> ImaIndexTable{-1, -1, -1, -1, 2, 4, 6, 8};
|
||||
|
||||
u16 ClampVolume(u16 volume) {
|
||||
return std::min(volume, MaxVolume);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Csnd::Csnd(Memory::MemorySystem& memory_) : memory(memory_) {}
|
||||
|
||||
Csnd::~Csnd() = default;
|
||||
|
||||
CsndCnt Csnd::GetCnt(u32 channel) const {
|
||||
return channels[channel].cnt;
|
||||
}
|
||||
|
||||
void Csnd::SetCnt(u32 channel, CsndCnt value) {
|
||||
Channel& ch = channels[channel];
|
||||
const bool was_enabled = True(ch.cnt & CsndCnt::Enable);
|
||||
ch.cnt = value;
|
||||
if (!was_enabled && True(value & CsndCnt::Enable)) {
|
||||
StartChannel(channel);
|
||||
} else if (was_enabled && False(value & CsndCnt::Enable)) {
|
||||
StopChannel(ch);
|
||||
ch.holding = false;
|
||||
}
|
||||
if (False(value & CsndCnt::Hold)) {
|
||||
ch.holding = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Csnd::SetTimer(u32 channel, u16 value) {
|
||||
channels[channel].timer = value;
|
||||
}
|
||||
|
||||
void Csnd::SetVolume(u32 channel, u16 left, u16 right) {
|
||||
channels[channel].volume_left = ClampVolume(left);
|
||||
channels[channel].volume_right = ClampVolume(right);
|
||||
}
|
||||
|
||||
void Csnd::SetCaptureVolume(u32 channel, u16 left, u16 right) {
|
||||
channels[channel].capture_volume_left = ClampVolume(left);
|
||||
channels[channel].capture_volume_right = ClampVolume(right);
|
||||
}
|
||||
|
||||
void Csnd::SetStartAddress(u32 channel, u32 address) {
|
||||
channels[channel].start_address = address;
|
||||
}
|
||||
|
||||
void Csnd::SetSize(u32 channel, u32 size) {
|
||||
channels[channel].size = size & 0x7FFFFFF;
|
||||
}
|
||||
|
||||
void Csnd::SetLoopAddress(u32 channel, u32 address) {
|
||||
channels[channel].loop_address = address;
|
||||
}
|
||||
|
||||
u32 Csnd::GetAdpcmState(u32 channel) const {
|
||||
const Channel& ch = channels[channel];
|
||||
if (True(ch.cnt & CsndCnt::Enable)) {
|
||||
return static_cast<u16>(ch.adpcm_predictor) | (static_cast<u32>(ch.adpcm_index) << 16);
|
||||
}
|
||||
return ch.adpcm_state;
|
||||
}
|
||||
|
||||
void Csnd::SetAdpcmState(u32 channel, u32 value) {
|
||||
channels[channel].adpcm_state = value;
|
||||
}
|
||||
|
||||
u32 Csnd::GetLoopAdpcmState(u32 channel) const {
|
||||
return channels[channel].loop_adpcm_state;
|
||||
}
|
||||
|
||||
void Csnd::SetLoopAdpcmState(u32 channel, u32 value) {
|
||||
channels[channel].loop_adpcm_state = value;
|
||||
}
|
||||
|
||||
CsndCaptureCnt Csnd::GetCaptureCnt(u32 unit) const {
|
||||
return capture_units[unit].cnt;
|
||||
}
|
||||
|
||||
void Csnd::SetCaptureCnt(u32 unit, CsndCaptureCnt value) {
|
||||
capture_units[unit].cnt = value;
|
||||
}
|
||||
|
||||
void Csnd::SetCaptureTimer(u32 unit, u16 value) {
|
||||
capture_units[unit].timer = value;
|
||||
}
|
||||
|
||||
void Csnd::SetCaptureAddress(u32 unit, u32 address, u32 size) {
|
||||
capture_units[unit].address = address;
|
||||
capture_units[unit].size = size;
|
||||
}
|
||||
|
||||
void Csnd::SetMasterVolume(u16 value) {
|
||||
master_volume = ClampVolume(value);
|
||||
}
|
||||
|
||||
u32 Csnd::GetChannelMaskRegister() const {
|
||||
return channel_mask_register;
|
||||
}
|
||||
|
||||
void Csnd::SetChannelMaskRegister(u32 value) {
|
||||
channel_mask_register = value;
|
||||
}
|
||||
|
||||
void Csnd::SetCaptureMaskRegister(u8 value) {
|
||||
capture_mask_register = value;
|
||||
}
|
||||
|
||||
bool Csnd::IsAnyChannelBusy() const {
|
||||
return std::any_of(channels.begin(), channels.end(),
|
||||
[](const Channel& ch) { return True(ch.cnt & CsndCnt::Enable); });
|
||||
}
|
||||
|
||||
void Csnd::StartChannel(u32 index) {
|
||||
Channel& ch = channels[index];
|
||||
ch.block_address = ch.start_address;
|
||||
ch.block_size = ch.size;
|
||||
ch.position = 0;
|
||||
ch.high_nibble = false;
|
||||
ch.holding = false;
|
||||
ch.counter = 0;
|
||||
ch.previous_sample = 0;
|
||||
ch.current_sample = 0;
|
||||
ch.adpcm_predictor = static_cast<s16>(ch.adpcm_state & 0xFFFF);
|
||||
ch.adpcm_index = std::min<u8>((ch.adpcm_state >> 16) & 0x7F, 88);
|
||||
}
|
||||
|
||||
void Csnd::StopChannel(Channel& ch) {
|
||||
// Keep the decoder state consistent after the channel stops.
|
||||
ch.adpcm_state =
|
||||
static_cast<u16>(ch.adpcm_predictor) | (static_cast<u32>(ch.adpcm_index) << 16);
|
||||
ch.cnt &= ~CsndCnt::Enable;
|
||||
}
|
||||
|
||||
bool Csnd::ReachedBlockEnd(const Channel& ch) const {
|
||||
if (GetFormat(ch.cnt) == Format::PsgNoise || GetLoopMode(ch.cnt) == LoopMode::Manual) {
|
||||
return false;
|
||||
}
|
||||
return ch.position >= ch.block_size;
|
||||
}
|
||||
|
||||
void Csnd::Step(Channel& ch) {
|
||||
if (ReachedBlockEnd(ch)) {
|
||||
const LoopMode loop_mode = GetLoopMode(ch.cnt);
|
||||
if (loop_mode == LoopMode::OneShot) {
|
||||
StopChannel(ch);
|
||||
ch.holding = True(ch.cnt & CsndCnt::Hold);
|
||||
return;
|
||||
}
|
||||
ch.block_address = ch.loop_address;
|
||||
if (loop_mode == LoopMode::Normal) {
|
||||
ch.block_size = ch.size;
|
||||
}
|
||||
ch.position = 0;
|
||||
ch.high_nibble = false;
|
||||
if (ch.loop_adpcm_state & LoopAdpcmReload) {
|
||||
ch.adpcm_predictor = static_cast<s16>(ch.loop_adpcm_state & 0xFFFF);
|
||||
ch.adpcm_index = std::min<u8>((ch.loop_adpcm_state >> 16) & 0x7F, 88);
|
||||
}
|
||||
if (ch.block_size == 0) {
|
||||
// An empty looped block would never advance.
|
||||
StopChannel(ch);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ch.previous_sample = ch.current_sample;
|
||||
ch.current_sample = FetchSample(ch);
|
||||
}
|
||||
|
||||
s16 Csnd::FetchSample(Channel& ch) {
|
||||
const u32 address = ch.block_address + ch.position;
|
||||
switch (GetFormat(ch.cnt)) {
|
||||
case Format::Pcm8: {
|
||||
const u8* data = memory.GetPhysicalPointer(address);
|
||||
ch.position++;
|
||||
return data ? static_cast<s16>(static_cast<s8>(*data) * 256) : 0;
|
||||
}
|
||||
case Format::Pcm16: {
|
||||
// At physical memory boundaries, the high byte may end outside of the
|
||||
// memory region, sacrifice that single OOB read edge case for speed.
|
||||
const u8* low = memory.GetPhysicalPointer(address);
|
||||
const u8* high = low + 1;
|
||||
ch.position += 2;
|
||||
if (!low) {
|
||||
return 0;
|
||||
}
|
||||
return static_cast<s16>(*low | (*high << 8));
|
||||
}
|
||||
case Format::ImaAdpcm: {
|
||||
const u8* data = memory.GetPhysicalPointer(address);
|
||||
const u8 byte = data ? *data : 0;
|
||||
const u8 nibble = ch.high_nibble ? (byte >> 4) : (byte & 0xF);
|
||||
if (ch.high_nibble) {
|
||||
ch.position++;
|
||||
}
|
||||
ch.high_nibble = !ch.high_nibble;
|
||||
|
||||
const s32 step = ImaStepTable[ch.adpcm_index];
|
||||
s32 diff = step >> 3;
|
||||
if (nibble & 1) {
|
||||
diff += step >> 2;
|
||||
}
|
||||
if (nibble & 2) {
|
||||
diff += step >> 1;
|
||||
}
|
||||
if (nibble & 4) {
|
||||
diff += step;
|
||||
}
|
||||
s32 predictor = ch.adpcm_predictor;
|
||||
if (nibble & 8) {
|
||||
predictor = std::max(predictor - diff, -0x7FFF);
|
||||
} else {
|
||||
predictor = std::min(predictor + diff, 0x7FFF);
|
||||
}
|
||||
ch.adpcm_predictor = static_cast<s16>(predictor);
|
||||
ch.adpcm_index =
|
||||
static_cast<u8>(std::clamp(ch.adpcm_index + ImaIndexTable[nibble & 7], 0, 88));
|
||||
return ch.adpcm_predictor;
|
||||
}
|
||||
case Format::PsgNoise:
|
||||
// TODO(PabloMK7): PSG square waves (channels 8-13) and noise (channels 14-15).
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
s16 Csnd::Interpolated(const Channel& ch) const {
|
||||
if (False(ch.cnt & CsndCnt::LinearInterpolation)) {
|
||||
return ch.current_sample;
|
||||
}
|
||||
const u32 period = std::max<u32>(0x10000 - ch.timer, MinTimerPeriod);
|
||||
const s32 delta = ch.current_sample - ch.previous_sample;
|
||||
return static_cast<s16>(ch.previous_sample +
|
||||
delta * static_cast<s32>(ch.counter) / static_cast<s32>(period));
|
||||
}
|
||||
|
||||
void Csnd::Render(std::span<std::array<s16, 2>> out) {
|
||||
for (auto& frame : out) {
|
||||
s32 left = 0;
|
||||
s32 right = 0;
|
||||
for (u32 i = 0; i < NumChannels; i++) {
|
||||
Channel& ch = channels[i];
|
||||
if (False(ch.cnt & CsndCnt::Enable)) {
|
||||
if (ch.holding) {
|
||||
left += ch.current_sample * ch.volume_left >> 15;
|
||||
right += ch.current_sample * ch.volume_right >> 15;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (True(ch.cnt & CsndCnt::Run)) {
|
||||
const u32 period = std::max<u32>(0x10000 - ch.timer, MinTimerPeriod);
|
||||
ch.counter += TimerTicksPerOutputSample;
|
||||
while (ch.counter >= period && True(ch.cnt & CsndCnt::Enable)) {
|
||||
ch.counter -= period;
|
||||
Step(ch);
|
||||
}
|
||||
if (False(ch.cnt & CsndCnt::Enable)) {
|
||||
if (ch.holding) {
|
||||
left += ch.current_sample * ch.volume_left >> 15;
|
||||
right += ch.current_sample * ch.volume_right >> 15;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const s32 sample = Interpolated(ch);
|
||||
left += sample * ch.volume_left >> 15;
|
||||
right += sample * ch.volume_right >> 15;
|
||||
}
|
||||
|
||||
const s64 mixed_left = static_cast<s64>(left) * master_volume >> 15;
|
||||
const s64 mixed_right = static_cast<s64>(right) * master_volume >> 15;
|
||||
frame[0] = static_cast<s16>(std::clamp<s64>(mixed_left, -0x8000, 0x7FFF));
|
||||
frame[1] = static_cast<s16>(std::clamp<s64>(mixed_right, -0x8000, 0x7FFF));
|
||||
}
|
||||
}
|
||||
|
||||
template <class Archive>
|
||||
void Csnd::Channel::serialize(Archive& ar, const unsigned int) {
|
||||
ar & cnt;
|
||||
ar & timer;
|
||||
ar & volume_left;
|
||||
ar & volume_right;
|
||||
ar & capture_volume_left;
|
||||
ar & capture_volume_right;
|
||||
ar & start_address;
|
||||
ar & size;
|
||||
ar & loop_address;
|
||||
ar & adpcm_state;
|
||||
ar & loop_adpcm_state;
|
||||
ar & block_address;
|
||||
ar & block_size;
|
||||
ar & position;
|
||||
ar & high_nibble;
|
||||
ar & holding;
|
||||
ar & counter;
|
||||
ar & previous_sample;
|
||||
ar & current_sample;
|
||||
ar & adpcm_predictor;
|
||||
ar & adpcm_index;
|
||||
}
|
||||
|
||||
template <class Archive>
|
||||
void Csnd::CaptureUnit::serialize(Archive& ar, const unsigned int) {
|
||||
ar & cnt;
|
||||
ar & timer;
|
||||
ar & address;
|
||||
ar & size;
|
||||
}
|
||||
|
||||
template <class Archive>
|
||||
void Csnd::serialize(Archive& ar, const unsigned int) {
|
||||
ar & channels;
|
||||
ar & capture_units;
|
||||
ar & master_volume;
|
||||
ar & channel_mask_register;
|
||||
ar & capture_mask_register;
|
||||
}
|
||||
SERIALIZE_IMPL(Csnd)
|
||||
|
||||
} // namespace AudioCore
|
||||
@@ -0,0 +1,201 @@
|
||||
// Copyright 2026 Citra Emulator Project / Azahar Emulator Project
|
||||
// Licensed under GPLv3 or any later version
|
||||
// Refer to the LICENSE.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <span>
|
||||
#include <boost/serialization/access.hpp>
|
||||
#include "common/common_funcs.h"
|
||||
#include "common/common_types.h"
|
||||
|
||||
namespace Memory {
|
||||
class MemorySystem;
|
||||
}
|
||||
|
||||
namespace AudioCore {
|
||||
|
||||
/// Bits of a CSND channel CNT register.
|
||||
enum class CsndCnt : u16 {
|
||||
None = 0,
|
||||
|
||||
LinearInterpolation = 1 << 6, // Linear interpolation on each two samples (0=Disable, 1=Enable)
|
||||
Hold = 1 << 7, // (0=Normal, 1=Hold last sample after one-shot sound)
|
||||
Run = 1 << 14, // Run DMA (0=Pause the channel, 1=Run the channel)
|
||||
Enable = 1 << 15, // Start/Stop control (0=Stop, 1=Start/Busy)
|
||||
};
|
||||
DECLARE_ENUM_FLAG_OPERATORS(CsndCnt)
|
||||
|
||||
/// Bits of a CSND capture unit CNT register.
|
||||
enum class CsndCaptureCnt : u16 {
|
||||
None = 0,
|
||||
|
||||
OneShot = 1 << 0,
|
||||
Pcm8 = 1 << 1,
|
||||
Unknown2 = 1 << 2,
|
||||
Enable = 1 << 15,
|
||||
};
|
||||
DECLARE_ENUM_FLAG_OPERATORS(CsndCaptureCnt)
|
||||
|
||||
/**
|
||||
* Register level model of the CSND sound hardware (0x10103000). It has 32 channels that play
|
||||
* PCM8, PCM16 or IMA-ADPCM samples from physical memory (or a PSG square wave / noise, not
|
||||
* emulated yet), and outputs the stereo mix at the I2S rate, the same rate as the DSP.
|
||||
* On hardware the mix goes to the codec chip on its own I2S line, where it is mixed with the DSP
|
||||
* output.
|
||||
*
|
||||
* Render() produces samples and advances the channels, the caller decides
|
||||
* when. Writes to the registers take effect from the next rendered sample.
|
||||
*/
|
||||
class Csnd {
|
||||
public:
|
||||
static constexpr u32 NumChannels = 32;
|
||||
static constexpr u32 NumCaptureUnits = 2;
|
||||
|
||||
static constexpr u32 CntLoopModeShift = 10;
|
||||
static constexpr u32 CntLoopModeMask = 0x3 << CntLoopModeShift;
|
||||
|
||||
static constexpr u32 CntFormatShift = 12;
|
||||
static constexpr u32 CntFormatMask = 0x3 << CntFormatShift;
|
||||
|
||||
/// Reloads the IMA-ADPCM state at the loop address.
|
||||
static constexpr u32 LoopAdpcmReload = 1u << 31;
|
||||
|
||||
enum class Format : u32 {
|
||||
Pcm8 = 0,
|
||||
Pcm16 = 1,
|
||||
ImaAdpcm = 2,
|
||||
PsgNoise = 3, ///< Not emulated yet
|
||||
};
|
||||
|
||||
enum class LoopMode : u32 {
|
||||
Manual = 0, ///< Plays ignoring the size
|
||||
Normal = 1, ///< At the end, restarts from the loop address, reloading the size
|
||||
OneShot = 2, ///< Stops at the end
|
||||
NormalNoReload = 3, ///< Like Normal, but latches the size at start
|
||||
};
|
||||
|
||||
explicit Csnd(Memory::MemorySystem& memory);
|
||||
~Csnd();
|
||||
|
||||
/// Get CNT bits of a format.
|
||||
static constexpr CsndCnt CntFormat(Format format) {
|
||||
return static_cast<CsndCnt>(static_cast<u16>(format) << CntFormatShift);
|
||||
}
|
||||
/// Get CNT bits of a loop mode.
|
||||
static constexpr CsndCnt CntLoopMode(LoopMode loop_mode) {
|
||||
return static_cast<CsndCnt>(static_cast<u16>(loop_mode) << CntLoopModeShift);
|
||||
}
|
||||
|
||||
/// Get format from CNT bits.
|
||||
static constexpr Format GetFormat(CsndCnt cnt) {
|
||||
return static_cast<Format>((static_cast<u32>(cnt) & CntFormatMask) >> CntFormatShift);
|
||||
}
|
||||
// Get loop mode from CNT bits.
|
||||
static constexpr LoopMode GetLoopMode(CsndCnt cnt) {
|
||||
return static_cast<LoopMode>((static_cast<u32>(cnt) & CntLoopModeMask) >> CntLoopModeShift);
|
||||
}
|
||||
|
||||
CsndCnt GetCnt(u32 channel) const;
|
||||
void SetCnt(u32 channel, CsndCnt value);
|
||||
|
||||
void SetTimer(u32 channel, u16 value);
|
||||
void SetVolume(u32 channel, u16 left, u16 right);
|
||||
void SetCaptureVolume(u32 channel, u16 left, u16 right);
|
||||
void SetStartAddress(u32 channel, u32 address);
|
||||
void SetSize(u32 channel, u32 size);
|
||||
void SetLoopAddress(u32 channel, u32 address);
|
||||
|
||||
/// IMA-ADPCM state register. PCM16 value in bits 0-15, table index in bits 16-22.
|
||||
/// While the channel plays it reads back the decoder's current state.
|
||||
u32 GetAdpcmState(u32 channel) const;
|
||||
void SetAdpcmState(u32 channel, u32 value);
|
||||
|
||||
/// Loop IMA-ADPCM state register. Bit 31 reloads it at the loop address.
|
||||
u32 GetLoopAdpcmState(u32 channel) const;
|
||||
void SetLoopAdpcmState(u32 channel, u32 value);
|
||||
|
||||
// Capture registers. Stored only as capture is not emulated.
|
||||
CsndCaptureCnt GetCaptureCnt(u32 unit) const;
|
||||
void SetCaptureCnt(u32 unit, CsndCaptureCnt value);
|
||||
void SetCaptureTimer(u32 unit, u16 value);
|
||||
void SetCaptureAddress(u32 unit, u32 address, u32 size);
|
||||
|
||||
// Global registers.
|
||||
void SetMasterVolume(u16 value);
|
||||
u32 GetChannelMaskRegister() const;
|
||||
void SetChannelMaskRegister(u32 value);
|
||||
void SetCaptureMaskRegister(u8 value);
|
||||
|
||||
/// True when at least one channel is enabled.
|
||||
bool IsAnyChannelBusy() const;
|
||||
|
||||
/// Renders samples into out, advancing every channel.
|
||||
void Render(std::span<std::array<s16, 2>> out);
|
||||
|
||||
private:
|
||||
struct Channel {
|
||||
// Registers
|
||||
CsndCnt cnt = CsndCnt::None;
|
||||
u16 timer = 0;
|
||||
u16 volume_left = 0;
|
||||
u16 volume_right = 0;
|
||||
u16 capture_volume_left = 0;
|
||||
u16 capture_volume_right = 0;
|
||||
u32 start_address = 0;
|
||||
u32 size = 0;
|
||||
u32 loop_address = 0;
|
||||
u32 adpcm_state = 0;
|
||||
u32 loop_adpcm_state = 0;
|
||||
|
||||
// Playback state
|
||||
u32 block_address = 0; ///< Start of the block being played
|
||||
u32 block_size = 0; ///< Size latched for the block being played
|
||||
u32 position = 0; ///< Byte offset in the block
|
||||
bool high_nibble = false;
|
||||
bool holding = false; ///< Ended a one-shot block with Hold set, outputs its last sample
|
||||
u32 counter = 0; ///< Timer, in units of 1/2048 output samples
|
||||
s16 previous_sample = 0;
|
||||
s16 current_sample = 0;
|
||||
s16 adpcm_predictor = 0;
|
||||
u8 adpcm_index = 0;
|
||||
|
||||
template <class Archive>
|
||||
void serialize(Archive& ar, const unsigned int);
|
||||
friend class boost::serialization::access;
|
||||
};
|
||||
|
||||
struct CaptureUnit {
|
||||
CsndCaptureCnt cnt = CsndCaptureCnt::None;
|
||||
u16 timer = 0;
|
||||
u32 address = 0;
|
||||
u32 size = 0;
|
||||
|
||||
template <class Archive>
|
||||
void serialize(Archive& ar, const unsigned int);
|
||||
friend class boost::serialization::access;
|
||||
};
|
||||
|
||||
void StartChannel(u32 index);
|
||||
void StopChannel(Channel& channel);
|
||||
void Step(Channel& channel);
|
||||
|
||||
s16 FetchSample(Channel& channel);
|
||||
s16 Interpolated(const Channel& channel) const;
|
||||
bool ReachedBlockEnd(const Channel& channel) const;
|
||||
|
||||
Memory::MemorySystem& memory;
|
||||
|
||||
std::array<Channel, NumChannels> channels{};
|
||||
std::array<CaptureUnit, NumCaptureUnits> capture_units{};
|
||||
u16 master_volume = 0x8000;
|
||||
u32 channel_mask_register = 0;
|
||||
u8 capture_mask_register = 0;
|
||||
|
||||
template <class Archive>
|
||||
void serialize(Archive& ar, const unsigned int);
|
||||
friend class boost::serialization::access;
|
||||
};
|
||||
|
||||
} // namespace AudioCore
|
||||
@@ -1,8 +1,10 @@
|
||||
// Copyright 2017-2026 Citra Emulator Project / Azahar Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the misc/licenses/gplv2.txt file included.
|
||||
// Licensed under GPLv3 or any later version
|
||||
// Refer to the LICENSE.txt file included.
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include "audio_core/dsp_interface.h"
|
||||
#include "audio_core/sink.h"
|
||||
#include "audio_core/sink_details.h"
|
||||
@@ -13,6 +15,14 @@
|
||||
|
||||
namespace AudioCore {
|
||||
|
||||
namespace {
|
||||
/// Amount of CSND frames to wait until they are outputted on their own
|
||||
/// because DSP is not producing any output.
|
||||
constexpr u32 CsndFramesBeforeDspIdle = 2;
|
||||
/// Max amount of CSND pending samples.
|
||||
constexpr std::size_t MaxCsndPendingSamples = samples_per_frame * 4;
|
||||
} // namespace
|
||||
|
||||
DspInterface::DspInterface(Core::System& system_) : system(system_) {}
|
||||
|
||||
DspInterface::~DspInterface() = default;
|
||||
@@ -37,36 +47,74 @@ void DspInterface::EnableStretching(bool enable) {
|
||||
}
|
||||
|
||||
void DspInterface::OutputFrame(StereoFrame16 frame) {
|
||||
if (!sink) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (sink->ImmediateSubmission()) {
|
||||
sink->PushSamples(frame.data(), frame.size());
|
||||
} else {
|
||||
fifo.Push(frame.data(), frame.size());
|
||||
}
|
||||
|
||||
auto video_dumper = system.GetVideoDumper();
|
||||
if (video_dumper && video_dumper->IsDumping()) {
|
||||
video_dumper->AddAudioFrame(std::move(frame));
|
||||
}
|
||||
std::scoped_lock lock{csnd_mutex};
|
||||
MixCsnd(frame);
|
||||
PushSamples(frame);
|
||||
}
|
||||
|
||||
void DspInterface::OutputSample(std::array<s16, 2> sample) {
|
||||
std::scoped_lock lock{csnd_mutex};
|
||||
MixCsnd({&sample, 1});
|
||||
PushSamples({&sample, 1});
|
||||
}
|
||||
|
||||
void DspInterface::OutputCsndFrame(const StereoFrame16& frame) {
|
||||
std::scoped_lock lock{csnd_mutex};
|
||||
|
||||
if (dsp_samples_since_csnd_frame == 0) {
|
||||
csnd_frames_without_dsp++;
|
||||
} else {
|
||||
csnd_frames_without_dsp = 0;
|
||||
}
|
||||
dsp_samples_since_csnd_frame = 0;
|
||||
|
||||
csnd_pending.insert(csnd_pending.end(), frame.begin(), frame.end());
|
||||
|
||||
if (csnd_frames_without_dsp >= CsndFramesBeforeDspIdle) {
|
||||
// The DSP is not running, CSND is the only output.
|
||||
const std::vector<std::array<s16, 2>> samples(csnd_pending.begin(), csnd_pending.end());
|
||||
csnd_pending.clear();
|
||||
PushSamples(samples);
|
||||
return;
|
||||
}
|
||||
|
||||
// The two clocks may drift a little, don't let CSND fall too far behind the DSP output.
|
||||
while (csnd_pending.size() > MaxCsndPendingSamples) {
|
||||
csnd_pending.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void DspInterface::MixCsnd(std::span<std::array<s16, 2>> samples) {
|
||||
// TODO(PabloMK7): Implement SIMD mixing here if possible.
|
||||
dsp_samples_since_csnd_frame += samples.size();
|
||||
for (auto& sample : samples) {
|
||||
if (csnd_pending.empty()) {
|
||||
break;
|
||||
}
|
||||
const auto& csnd = csnd_pending.front();
|
||||
for (std::size_t i = 0; i < 2; i++) {
|
||||
sample[i] = static_cast<s16>(std::clamp(sample[i] + csnd[i], -0x8000, 0x7FFF));
|
||||
}
|
||||
csnd_pending.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void DspInterface::PushSamples(std::span<const std::array<s16, 2>> samples) {
|
||||
if (!sink) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (sink->ImmediateSubmission()) {
|
||||
sink->PushSamples(&sample, 1);
|
||||
sink->PushSamples(samples.data(), samples.size());
|
||||
} else {
|
||||
fifo.Push(&sample, 1);
|
||||
fifo.Push(samples.data(), samples.size());
|
||||
}
|
||||
|
||||
auto video_dumper = system.GetVideoDumper();
|
||||
if (video_dumper && video_dumper->IsDumping()) {
|
||||
video_dumper->AddAudioSample(std::move(sample));
|
||||
for (const auto& sample : samples) {
|
||||
video_dumper->AddAudioSample(sample);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
// Copyright 2017-2025 Citra Emulator Project / Azahar Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the misc/licenses/gplv2.txt file included.
|
||||
// Copyright 2017-2026 Citra Emulator Project / Azahar Emulator Project
|
||||
// Licensed under GPLv3 or any later version
|
||||
// Refer to the LICENSE.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <span>
|
||||
#include <boost/serialization/access.hpp>
|
||||
#include "audio_core/audio_types.h"
|
||||
@@ -103,6 +104,13 @@ public:
|
||||
/// Enable/Disable audio stretching.
|
||||
void EnableStretching(bool enable);
|
||||
|
||||
/**
|
||||
* Adds one frame of CSND output. On hardware CSND and the DSP reach the codec chip on separate
|
||||
* I2S lines and the codec mixes them, here the frame is mixed to the DSP output.
|
||||
* Must be called once per audio frame period.
|
||||
*/
|
||||
void OutputCsndFrame(const StereoFrame16& frame);
|
||||
|
||||
protected:
|
||||
void OutputFrame(StereoFrame16 frame);
|
||||
void OutputSample(std::array<s16, 2> sample);
|
||||
@@ -110,6 +118,8 @@ protected:
|
||||
private:
|
||||
void FlushResidualStretcherAudio();
|
||||
void OutputCallback(s16* buffer, std::size_t num_frames);
|
||||
void MixCsnd(std::span<std::array<s16, 2>> samples);
|
||||
void PushSamples(std::span<const std::array<s16, 2>> samples);
|
||||
|
||||
Core::System& system;
|
||||
|
||||
@@ -121,6 +131,11 @@ private:
|
||||
TimeStretcher time_stretcher;
|
||||
std::unique_ptr<Sink> sink;
|
||||
|
||||
std::mutex csnd_mutex;
|
||||
StereoBuffer16 csnd_pending;
|
||||
std::size_t dsp_samples_since_csnd_frame = 0;
|
||||
u32 csnd_frames_without_dsp = 0;
|
||||
|
||||
template <class Archive>
|
||||
void serialize(Archive& ar, const unsigned int) {}
|
||||
friend class boost::serialization::access;
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,12 +1,17 @@
|
||||
// Copyright 2014-2025 Citra Emulator Project / Azahar Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the misc/licenses/gplv2.txt file included.
|
||||
// Copyright 2014-2026 Citra Emulator Project / Azahar Emulator Project
|
||||
// Licensed under GPLv3 or any later version
|
||||
// Refer to the LICENSE.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <bitset>
|
||||
#include <memory>
|
||||
#include <boost/serialization/array.hpp>
|
||||
#include <boost/serialization/bitset.hpp>
|
||||
#include <boost/serialization/shared_ptr.hpp>
|
||||
#include "audio_core/audio_types.h"
|
||||
#include "audio_core/csnd.h"
|
||||
#include "common/archives.h"
|
||||
#include "core/hle/kernel/mutex.h"
|
||||
#include "core/hle/kernel/shared_memory.h"
|
||||
@@ -14,83 +19,36 @@
|
||||
|
||||
namespace Core {
|
||||
class System;
|
||||
}
|
||||
struct TimingEventType;
|
||||
} // namespace Core
|
||||
|
||||
namespace Service::CSND {
|
||||
|
||||
enum class Encoding : u8 {
|
||||
Pcm8 = 0,
|
||||
Pcm16 = 1,
|
||||
Adpcm = 2,
|
||||
Psg = 3,
|
||||
};
|
||||
struct Type0Command;
|
||||
|
||||
enum class LoopMode : u8 {
|
||||
Manual = 0, // Play block 1 endlessly ignoring the size
|
||||
Normal = 1, // Play block 1 once, then repeat with block 2. Block size is reloaded every time a
|
||||
// new block is started
|
||||
OneShot = 2, // Play block 1 once and stop
|
||||
ConstantSize = 3, // Similar to Normal, but only load block size once at the beginning
|
||||
};
|
||||
|
||||
struct AdpcmState {
|
||||
s16 predictor = 0;
|
||||
u8 step_index = 0;
|
||||
/// State the CSND module keeps for each client session.
|
||||
struct ClientData : public Kernel::SessionRequestHandler::SessionDataBase {
|
||||
std::shared_ptr<Kernel::Mutex> mutex;
|
||||
std::shared_ptr<Kernel::SharedMemory> shared_memory;
|
||||
u32 master_state_offset = 0;
|
||||
u32 channel_state_offset = 0;
|
||||
u32 capture_state_offset = 0;
|
||||
u32 direct_sound_offset = 0;
|
||||
u32 channel_mask = 0;
|
||||
u8 capture_mask = 0;
|
||||
|
||||
private:
|
||||
template <class Archive>
|
||||
void serialize(Archive& ar, const unsigned int) {
|
||||
ar & predictor;
|
||||
ar & step_index;
|
||||
}
|
||||
void serialize(Archive& ar, const unsigned int);
|
||||
friend class boost::serialization::access;
|
||||
};
|
||||
|
||||
struct Channel {
|
||||
PAddr block1_address = 0;
|
||||
PAddr block2_address = 0;
|
||||
u32 block1_size = 0;
|
||||
u32 block2_size = 0;
|
||||
AdpcmState block1_adpcm_state;
|
||||
AdpcmState block2_adpcm_state;
|
||||
bool block2_adpcm_reload = false;
|
||||
u16 left_channel_volume = 0;
|
||||
u16 right_channel_volume = 0;
|
||||
u16 left_capture_volume = 0;
|
||||
u16 right_capture_volume = 0;
|
||||
u32 sample_rate = 0;
|
||||
bool linear_interpolation = false;
|
||||
LoopMode loop_mode = LoopMode::Manual;
|
||||
Encoding encoding = Encoding::Pcm8;
|
||||
u8 psg_duty = 0;
|
||||
|
||||
private:
|
||||
template <class Archive>
|
||||
void serialize(Archive& ar, const unsigned int) {
|
||||
ar & block1_address;
|
||||
ar & block2_address;
|
||||
ar & block1_size;
|
||||
ar & block2_size;
|
||||
ar & block1_adpcm_state;
|
||||
ar & block2_adpcm_state;
|
||||
ar & block2_adpcm_reload;
|
||||
ar & left_channel_volume;
|
||||
ar & right_channel_volume;
|
||||
ar & left_capture_volume;
|
||||
ar & right_capture_volume;
|
||||
ar & sample_rate;
|
||||
ar & linear_interpolation;
|
||||
ar & loop_mode;
|
||||
ar & encoding;
|
||||
ar & psg_duty;
|
||||
}
|
||||
friend class boost::serialization::access;
|
||||
};
|
||||
|
||||
class CSND_SND final : public ServiceFramework<CSND_SND> {
|
||||
class CSND_SND final : public ServiceFramework<CSND_SND, ClientData> {
|
||||
public:
|
||||
explicit CSND_SND(Core::System& system);
|
||||
~CSND_SND() = default;
|
||||
~CSND_SND();
|
||||
|
||||
void ClientDisconnected(std::shared_ptr<Kernel::ServerSession> server_session) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
@@ -101,7 +59,7 @@ private:
|
||||
* 2 : offset to master state located in the shared-memory, region size=8
|
||||
* 3 : offset to channel state located in the shared-memory, region size=12*num_channels
|
||||
* 4 : offset to capture state located in the shared-memory, region size=8*num_captures
|
||||
* 5 : offset to type 1 commands (?) located in the shared-memory.
|
||||
* 5 : offset to the direct sound structure located in the shared-memory.
|
||||
* Outputs:
|
||||
* 1 : Result of function, 0 on success, otherwise error code
|
||||
* 2 : Handle-list header
|
||||
@@ -129,17 +87,6 @@ private:
|
||||
*/
|
||||
void ExecuteCommands(Kernel::HLERequestContext& ctx);
|
||||
|
||||
/**
|
||||
* CSND_SND::ExecuteType1Commands service function
|
||||
* Inputs:
|
||||
* 0 : Header Code[0x00040080]
|
||||
* 1 : unknown
|
||||
* 2 : unknown
|
||||
* Outputs:
|
||||
* 1 : Result of function, 0 on success, otherwise error code
|
||||
*/
|
||||
void ExecuteType1Commands(Kernel::HLERequestContext& ctx);
|
||||
|
||||
/**
|
||||
* CSND_SND::AcquireSoundChannels service function
|
||||
* Inputs:
|
||||
@@ -238,38 +185,69 @@ private:
|
||||
*/
|
||||
void Reset(Kernel::HLERequestContext& ctx);
|
||||
|
||||
void CacheOperation(Kernel::HLERequestContext& ctx);
|
||||
|
||||
void ExecuteCommand(ClientData& client, const Type0Command& command);
|
||||
void UpdateState(ClientData& client);
|
||||
|
||||
/// Renders the pending samples up to the current emulated time, so that the registers read and
|
||||
/// written respresent the proper values at that time.
|
||||
void UpdateHardware();
|
||||
|
||||
bool IsRunning(u32 channel);
|
||||
void SetPlayStateRaw(u32 channel, bool play);
|
||||
void SetPlayState(u32 channel, bool play);
|
||||
void StopChannel(u32 channel);
|
||||
void SetRequestedVolume(u32 channel, u16 left, u16 right, bool start_fade);
|
||||
void SetTargetVolume(u32 channel, u16 left, u16 right, bool start_fade);
|
||||
void WriteCurrentVolume(u32 channel);
|
||||
void StartFade();
|
||||
void FadeCallback(std::uintptr_t step, s64 cycles_late);
|
||||
void StartTicking();
|
||||
void AudioTickCallback(s64 cycles_late);
|
||||
|
||||
Core::System& system;
|
||||
|
||||
std::shared_ptr<Kernel::Mutex> mutex = nullptr;
|
||||
std::shared_ptr<Kernel::SharedMemory> shared_memory = nullptr;
|
||||
AudioCore::Csnd csnd_hw;
|
||||
|
||||
static constexpr u32 MaxCaptureUnits = 2;
|
||||
std::array<bool, MaxCaptureUnits> capture_units = {false, false};
|
||||
struct ChannelVolume {
|
||||
std::array<u16, 2> requested{};
|
||||
std::array<u16, 2> target{};
|
||||
std::array<s32, 2> current{};
|
||||
std::array<s32, 2> step{};
|
||||
|
||||
static constexpr u32 ChannelCount = 32;
|
||||
std::array<Channel, ChannelCount> channels;
|
||||
private:
|
||||
template <class Archive>
|
||||
void serialize(Archive& ar, const unsigned int) {
|
||||
ar & requested;
|
||||
ar & target;
|
||||
ar & current;
|
||||
ar & step;
|
||||
}
|
||||
friend class boost::serialization::access;
|
||||
};
|
||||
std::array<ChannelVolume, AudioCore::Csnd::NumChannels> volumes{};
|
||||
using ChannelSet = std::bitset<AudioCore::Csnd::NumChannels>;
|
||||
ChannelSet started; ///< Channels started through command 1
|
||||
ChannelSet fading; ///< Channels fading towards their target
|
||||
ChannelSet stop_pending; ///< Channels to stop once faded out
|
||||
ChannelSet pause_pending; ///< Channels to pause once faded out
|
||||
bool fade_scheduled = false;
|
||||
|
||||
u32 master_state_offset = 0;
|
||||
u32 channel_state_offset = 0;
|
||||
u32 capture_state_offset = 0;
|
||||
u32 type1_command_offset = 0;
|
||||
std::array<bool, AudioCore::Csnd::NumCaptureUnits> capture_units_in_use{};
|
||||
|
||||
u32 acquired_channel_mask = 0;
|
||||
bool is_ticking = false;
|
||||
u32 initialized_clients = 0;
|
||||
AudioCore::StereoFrame16 frame{};
|
||||
u32 frame_samples = 0;
|
||||
s64 frame_start_ticks = 0;
|
||||
s64 frame_length_ticks = 0;
|
||||
|
||||
Core::TimingEventType* audio_tick_event = nullptr;
|
||||
Core::TimingEventType* fade_event = nullptr;
|
||||
|
||||
template <class Archive>
|
||||
void serialize(Archive& ar, const unsigned int) {
|
||||
DEBUG_SERIALIZATION_POINT;
|
||||
ar& boost::serialization::base_object<Kernel::SessionRequestHandler>(*this);
|
||||
ar & mutex;
|
||||
ar & shared_memory;
|
||||
ar & capture_units;
|
||||
ar & channels;
|
||||
ar & master_state_offset;
|
||||
ar & channel_state_offset;
|
||||
ar & capture_state_offset;
|
||||
ar & type1_command_offset;
|
||||
ar & acquired_channel_mask;
|
||||
}
|
||||
void serialize(Archive& ar, const unsigned int);
|
||||
friend class boost::serialization::access;
|
||||
};
|
||||
|
||||
@@ -279,4 +257,5 @@ void InstallInterfaces(Core::System& system);
|
||||
} // namespace Service::CSND
|
||||
|
||||
BOOST_CLASS_EXPORT_KEY(Service::CSND::CSND_SND)
|
||||
BOOST_CLASS_EXPORT_KEY(Service::CSND::ClientData)
|
||||
SERVICE_CONSTRUCT(Service::CSND::CSND_SND)
|
||||
Reference in new issue
Block a user