mirror of
https://github.com/TheBozos/BozoEngine.git
synced 2026-12-01 12:53:50 -10:00
Shinobido's Hideout music recorded from PCSX2 (its own media capture of the SPU2's samples) against the port's: the same to within 0.2 dB from 200 Hz to 4 kHz (coherence 1.000), but the port 1 dB brighter at 4-8 kHz, 2.6 dB at 8-12 kHz, 4 dB at 12-16 kHz and 6.4 dB above. The mixer resamples the voices by 4-point Catmull-Rom (nearly flat to the top); the SPU2 by its 4-point Gaussian table, as the PS1's SPU (psx-spx, "SPU ADPCM Pitch": 512 entries, the index the pitch counter's bits 4-11, each of the four products shifted right 15), which rolls the highs off. With it the port's music is within 0.17 dB of PCSX2's in every band from 200 Hz up. Mixer::setGaussianInterpolation(bool), for every mixer, off by default (the other ports' sound, and any recordings they compare against, unchanged); Mixer::gaussian(y0, y1, y2, y3, t) the interpolation itself. The table's 512 values checked as the page says: each set of four weights sums to 0x7F7F-0x7F81. Test (bozo-audio-tests): off by default; a constant 0x7000 at fraction 0 is 4206 + 20092 + 4262 - 1 = 28559; every index's weights sum within bounds; the fraction's low 4 bits ignored (y1 alone at 0: 0x59B3); the switch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
989 lines
44 KiB
C++
989 lines
44 KiB
C++
// SPDX-License-Identifier: MIT
|
|
// Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root).
|
|
//
|
|
// The native sound mixer (bozo/audio/mixer.h; behaviour: docs/specs/libraries.md section 15.14).
|
|
#include "bozo/audio/mixer.h"
|
|
|
|
#include "bozo/audio/port_stream.h"
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <iterator>
|
|
|
|
namespace bozo::audio
|
|
{
|
|
namespace
|
|
{
|
|
// PS2 ADPCM: a 16-byte block is a header (shift, filter, flags) and 28 4-bit samples; each filter predicts
|
|
// from the two samples before.
|
|
constexpr int32_t kFilterPositive[5] = {0, 60, 115, 98, 122};
|
|
constexpr int32_t kFilterNegative[5] = {0, 0, -52, -55, -60};
|
|
|
|
int32_t clamp16(int32_t value)
|
|
{
|
|
return std::clamp(value, -0x8000, 0x7FFF);
|
|
}
|
|
|
|
int32_t mul15(int32_t a, int32_t b)
|
|
{
|
|
return (a * b) >> 15;
|
|
}
|
|
|
|
// One step of an envelope or a volume sweep, once per 48 kHz sample: rate = (shift << 2) | step, 0..127.
|
|
// From psx-spx ("SPU ADSR"): the level moves by step = 7 - step (increasing) or -8 + step (decreasing),
|
|
// << max(0, 11 - shift), every 2^max(0, shift - 11) samples, clamped to 0..0x7FFF; exponential and decreasing,
|
|
// the move is step x level >> 15. Where psx-spx is ambiguous, the reference mixer's observed behaviour
|
|
// (libraries.md 15.14, settled black-box): the wait is a counter gaining 0x8000 >> (shift - 11) a sample (0x8000
|
|
// below shift 11), the level moving when it reaches 0x8000; shift >= 27 never moves; exponential and increasing
|
|
// at or above 0x6000 slows by 4: a quarter of the move below shift 10, half the move at half the counter's gain
|
|
// at shift 10, a quarter of the counter's gain from shift 11.
|
|
void envelopeStep(int32_t &level, uint32_t &counter, uint32_t rate, bool decreasing, bool exponential)
|
|
{
|
|
const int32_t shift = static_cast<int32_t>(rate >> 2u);
|
|
if (shift >= 27)
|
|
return;
|
|
const int32_t low = static_cast<int32_t>(rate & 3u);
|
|
int32_t move = (decreasing ? -8 + low : 7 - low) << std::max(0, 11 - shift);
|
|
uint32_t gain = shift >= 11 ? 0x8000u >> (shift - 11) : 0x8000u;
|
|
if (exponential && decreasing)
|
|
move = (move * level) >> 15;
|
|
else if (exponential && level >= 0x6000)
|
|
{
|
|
if (shift < 10)
|
|
move >>= 2;
|
|
else if (shift == 10)
|
|
{
|
|
move >>= 1;
|
|
gain >>= 1;
|
|
}
|
|
else
|
|
gain >>= 2;
|
|
}
|
|
counter += gain;
|
|
if (counter < 0x8000u)
|
|
return;
|
|
counter = 0;
|
|
level = std::clamp(level + move, 0, 0x7FFF);
|
|
}
|
|
|
|
// libsd's reverb presets (its table, mode 0 off .. 9 pipe): dAPF1, dAPF2, vIIR, vCOMB1..4, vWALL, vAPF1, vAPF2,
|
|
// mLSAME, mRSAME, mLCOMB1, mRCOMB1, mLCOMB2, mRCOMB2, dLSAME, dRSAME, mLDIFF, mRDIFF, mLCOMB3, mRCOMB3, mLCOMB4,
|
|
// mRCOMB4, dLDIFF, dRDIFF, mLAPF1, mRAPF1, mLAPF2, mRAPF2, vLIN, vRIN; offsets in units of 8 bytes. And each
|
|
// mode's effect area, in the same units.
|
|
constexpr uint16_t kPresets[10][32] = {
|
|
{0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
|
|
{0x007D, 0x005B, 0x6D80, 0x54B8, 0xBED0, 0x0000, 0x0000, 0xBA80, 0x5800, 0x5300, 0x04D6, 0x0333, 0x03F0, 0x0227, 0x0374, 0x01EF, 0x0336, 0x01B7, 0x0335, 0x01B6, 0x0334, 0x01B5, 0x0334, 0x01B5, 0x0334, 0x01B5, 0x01B4, 0x0136, 0x00B8, 0x005C, 0x8000, 0x8000},
|
|
{0x0033, 0x0025, 0x70F0, 0x4FA8, 0xBCE0, 0x4410, 0xC0F0, 0x9C00, 0x5280, 0x4EC0, 0x03E4, 0x031B, 0x03A4, 0x02AF, 0x0372, 0x0266, 0x031C, 0x025D, 0x025C, 0x018E, 0x022F, 0x0135, 0x01D2, 0x00B7, 0x018F, 0x00B5, 0x00B4, 0x0080, 0x004C, 0x0026, 0x8000, 0x8000},
|
|
{0x00B1, 0x007F, 0x70F0, 0x4FA8, 0xBCE0, 0x4510, 0xBEF0, 0xB4C0, 0x5280, 0x4EC0, 0x0904, 0x076B, 0x0824, 0x065F, 0x07A2, 0x0616, 0x076C, 0x05ED, 0x05EC, 0x042E, 0x050F, 0x0305, 0x0462, 0x02B7, 0x042F, 0x0265, 0x0264, 0x01B2, 0x0100, 0x0080, 0x8000, 0x8000},
|
|
{0x00E3, 0x00A9, 0x6F60, 0x4FA8, 0xBCE0, 0x4510, 0xBEF0, 0xA680, 0x5680, 0x52C0, 0x0DFB, 0x0B58, 0x0D09, 0x0A3C, 0x0BD9, 0x0973, 0x0B59, 0x08DA, 0x08D9, 0x05E9, 0x07EC, 0x04B0, 0x06EF, 0x03D2, 0x05EA, 0x031D, 0x031C, 0x0238, 0x0154, 0x00AA, 0x8000, 0x8000},
|
|
{0x01A5, 0x0139, 0x6000, 0x5000, 0x4C00, 0xB800, 0xBC00, 0xC000, 0x6000, 0x5C00, 0x15BA, 0x11BB, 0x14C2, 0x10BD, 0x11BC, 0x0DC1, 0x11C0, 0x0DC3, 0x0DC0, 0x09C1, 0x0BC4, 0x07C1, 0x0A00, 0x06CD, 0x09C2, 0x05C1, 0x05C0, 0x041A, 0x0274, 0x013A, 0x8000, 0x8000},
|
|
{0x033D, 0x0231, 0x7E00, 0x5000, 0xB400, 0xB000, 0x4C00, 0xB000, 0x6000, 0x5400, 0x1ED6, 0x1A31, 0x1D14, 0x183B, 0x1BC2, 0x16B2, 0x1A32, 0x15EF, 0x15EE, 0x1055, 0x1334, 0x0F2D, 0x11F6, 0x0C5D, 0x1056, 0x0AE1, 0x0AE0, 0x07A2, 0x0464, 0x0232, 0x8000, 0x8000},
|
|
{0x0003, 0x0003, 0x7FFF, 0x7FFF, 0x0000, 0x0000, 0x0000, 0x8100, 0x0000, 0x0000, 0x1FFD, 0x0FFD, 0x1009, 0x0009, 0x0000, 0x0000, 0x1009, 0x0009, 0x1FFF, 0x1FFF, 0x1FFE, 0x1FFE, 0x1FFE, 0x1FFE, 0x1FFE, 0x1FFE, 0x1008, 0x1004, 0x0008, 0x0004, 0x8000, 0x8000},
|
|
{0x0003, 0x0003, 0x7FFF, 0x7FFF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1FFD, 0x0FFD, 0x1009, 0x0009, 0x0000, 0x0000, 0x1009, 0x0009, 0x1FFF, 0x1FFF, 0x1FFE, 0x1FFE, 0x1FFE, 0x1FFE, 0x1FFE, 0x1FFE, 0x1008, 0x1004, 0x0008, 0x0004, 0x8000, 0x8000},
|
|
{0x0017, 0x0013, 0x70F0, 0x4FA8, 0xBCE0, 0x4510, 0xBEF0, 0x8500, 0x5F80, 0x54C0, 0x0371, 0x02AF, 0x02E5, 0x01DF, 0x02B0, 0x01D7, 0x0358, 0x026A, 0x01D6, 0x011E, 0x012D, 0x00B1, 0x011F, 0x0059, 0x01A0, 0x00E3, 0x0058, 0x0040, 0x0028, 0x0014, 0x8000, 0x8000},
|
|
};
|
|
constexpr uint32_t kAreaSizes[10] = {0x2, 0x4D8, 0x3E8, 0x908, 0xDFC, 0x15BC, 0x1ED8, 0x3008, 0x3008, 0x780};
|
|
|
|
// The SPU2's 4-point Gaussian interpolation (as the PS1's SPU: psx-spx, "SPU ADPCM Pitch", its 512-entry table):
|
|
// between y1 and y2, the table's index the fraction's top 8 bits, each product shifted right 15 on its own. Its
|
|
// four weights sum to 0x7F7F-0x7F81 (not quite 0x8000), and it rolls the highs off as the console does.
|
|
constexpr int16_t kGauss[512] = {
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3,
|
|
3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9, 10, 11, 12, 13, 14,
|
|
15, 16, 17, 18, 19, 21, 22, 24, 25, 27, 28, 30, 32, 33, 35, 37,
|
|
39, 41, 44, 46, 48, 51, 53, 56, 58, 61, 64, 67, 70, 73, 77, 80,
|
|
84, 87, 91, 95, 99, 103, 107, 111, 116, 120, 125, 130, 135, 140, 145, 150,
|
|
156, 161, 167, 173, 179, 186, 192, 199, 205, 212, 219, 227, 234, 242, 250, 257,
|
|
266, 274, 283, 291, 300, 309, 319, 328, 338, 348, 358, 369, 379, 390, 401, 412,
|
|
424, 436, 448, 460, 473, 485, 498, 512, 525, 539, 553, 567, 582, 597, 612, 627,
|
|
643, 659, 675, 692, 708, 726, 743, 761, 779, 797, 816, 835, 854, 874, 894, 914,
|
|
935, 956, 977, 999, 1020, 1043, 1066, 1089, 1112, 1136, 1160, 1184, 1209, 1234, 1260, 1286,
|
|
1312, 1339, 1366, 1394, 1422, 1450, 1479, 1508, 1537, 1567, 1598, 1628, 1660, 1691, 1723, 1756,
|
|
1789, 1822, 1856, 1890, 1924, 1959, 1995, 2031, 2067, 2104, 2141, 2179, 2217, 2256, 2295, 2334,
|
|
2374, 2415, 2456, 2497, 2539, 2582, 2624, 2668, 2712, 2756, 2801, 2846, 2892, 2938, 2985, 3032,
|
|
3079, 3128, 3176, 3225, 3275, 3325, 3376, 3427, 3479, 3531, 3584, 3637, 3691, 3745, 3799, 3855,
|
|
3910, 3967, 4023, 4081, 4138, 4197, 4255, 4315, 4374, 4435, 4495, 4557, 4619, 4681, 4744, 4807,
|
|
4871, 4935, 5000, 5065, 5131, 5197, 5264, 5332, 5399, 5468, 5536, 5606, 5676, 5746, 5817, 5888,
|
|
5959, 6032, 6104, 6177, 6251, 6325, 6400, 6475, 6550, 6626, 6702, 6779, 6856, 6934, 7012, 7091,
|
|
7170, 7249, 7329, 7409, 7490, 7571, 7653, 7735, 7817, 7900, 7983, 8066, 8150, 8234, 8319, 8404,
|
|
8489, 8575, 8661, 8748, 8834, 8922, 9009, 9097, 9185, 9273, 9362, 9451, 9541, 9630, 9720, 9811,
|
|
9901, 9992, 10083, 10174, 10266, 10358, 10450, 10542, 10635, 10727, 10820, 10913, 11007, 11100, 11194, 11288,
|
|
11382, 11476, 11571, 11665, 11760, 11855, 11950, 12045, 12140, 12236, 12331, 12427, 12522, 12618, 12714, 12809,
|
|
12905, 13001, 13097, 13193, 13289, 13385, 13481, 13577, 13673, 13769, 13865, 13961, 14056, 14152, 14248, 14343,
|
|
14439, 14534, 14630, 14725, 14820, 14915, 15010, 15104, 15199, 15293, 15387, 15481, 15575, 15669, 15762, 15855,
|
|
15948, 16041, 16133, 16226, 16317, 16409, 16500, 16592, 16682, 16773, 16863, 16953, 17042, 17131, 17220, 17308,
|
|
17396, 17484, 17571, 17658, 17744, 17830, 17916, 18001, 18086, 18170, 18254, 18337, 18420, 18502, 18584, 18665,
|
|
18746, 18826, 18905, 18985, 19063, 19141, 19219, 19295, 19372, 19447, 19522, 19597, 19671, 19744, 19816, 19888,
|
|
19959, 20030, 20100, 20169, 20238, 20306, 20373, 20439, 20505, 20570, 20634, 20698, 20760, 20822, 20884, 20944,
|
|
21004, 21063, 21121, 21178, 21235, 21290, 21345, 21399, 21452, 21505, 21556, 21607, 21657, 21706, 21754, 21801,
|
|
21848, 21893, 21938, 21982, 22025, 22066, 22107, 22148, 22187, 22225, 22262, 22299, 22334, 22369, 22402, 22435,
|
|
22467, 22498, 22527, 22556, 22584, 22611, 22637, 22662, 22686, 22709, 22731, 22752, 22772, 22791, 22809, 22826,
|
|
22842, 22857, 22872, 22885, 22897, 22908, 22918, 22927, 22935, 22942, 22948, 22953, 22957, 22960, 22962, 22963,
|
|
};
|
|
std::atomic<bool> g_gaussian{false};
|
|
|
|
// 4-point Catmull-Rom between y1 and y2; t a 12-bit fraction.
|
|
int32_t interpolate(int32_t y0, int32_t y1, int32_t y2, int32_t y3, uint32_t t)
|
|
{
|
|
const float f = static_cast<float>(t) * (1.0f / 4096.0f);
|
|
const float a = -0.5f * y0 + 1.5f * y1 - 1.5f * y2 + 0.5f * y3;
|
|
const float b = static_cast<float>(y0) - 2.5f * y1 + 2.0f * y2 - 0.5f * y3;
|
|
const float c = -0.5f * y0 + 0.5f * y2;
|
|
return clamp16(static_cast<int32_t>(((a * f + b) * f + c) * f + static_cast<float>(y1)));
|
|
}
|
|
}
|
|
|
|
void Mixer::setGaussianInterpolation(bool on) { g_gaussian.store(on, std::memory_order_relaxed); }
|
|
bool Mixer::gaussianInterpolation() { return g_gaussian.load(std::memory_order_relaxed); }
|
|
|
|
int32_t Mixer::gaussian(int32_t y0, int32_t y1, int32_t y2, int32_t y3, uint32_t t)
|
|
{
|
|
const uint32_t i = (t >> 4u) & 0xFFu;
|
|
return ((kGauss[0xFFu - i] * y0) >> 15) + ((kGauss[0x1FFu - i] * y1) >> 15) + ((kGauss[0x100u + i] * y2) >> 15) +
|
|
((kGauss[i] * y3) >> 15);
|
|
}
|
|
|
|
void Mixer::Volume::set(uint16_t value)
|
|
{
|
|
setting = value;
|
|
counter = 0u;
|
|
if ((value & 0x8000u) == 0u)
|
|
level = static_cast<int16_t>(static_cast<uint16_t>(value << 1u));
|
|
}
|
|
|
|
void Mixer::Volume::tick()
|
|
{
|
|
if ((setting & 0x8000u) == 0u)
|
|
return;
|
|
const bool negative = (setting & 0x1000u) != 0u;
|
|
int32_t magnitude = std::abs(level);
|
|
envelopeStep(magnitude, counter, setting & 0x7Fu, (setting & 0x2000u) != 0u, (setting & 0x4000u) != 0u);
|
|
level = negative ? -magnitude : magnitude;
|
|
}
|
|
|
|
void Mixer::reset()
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
for (Group &group : m_groups)
|
|
group = Group{};
|
|
m_groups[1].mix = 0xFFCu;
|
|
m_outputPosition = 0;
|
|
}
|
|
|
|
void Mixer::write(uint32_t address, const uint8_t *data, uint32_t bytes)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
for (uint32_t i = 0; i < bytes; ++i)
|
|
m_memory[(address + i) % kSoundMemory] = data[i];
|
|
m_stats.uploaded += bytes;
|
|
reachIrq(address, bytes);
|
|
}
|
|
|
|
void Mixer::read(uint32_t address, uint8_t *data, uint32_t bytes) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
for (uint32_t i = 0; i < bytes; ++i)
|
|
data[i] = m_memory[(address + i) % kSoundMemory];
|
|
}
|
|
|
|
void Mixer::setVolume(int group, int voice, int side, uint16_t value)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].voices[voice].volume[side].set(value);
|
|
}
|
|
void Mixer::setPitch(int group, int voice, uint16_t pitch)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].voices[voice].pitch = pitch;
|
|
}
|
|
void Mixer::setAdsr(int group, int voice, int word, uint16_t value)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].voices[voice].adsr[word] = value;
|
|
}
|
|
void Mixer::setStart(int group, int voice, uint32_t address)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].voices[voice].start = address & (kSoundMemory - 1u) & ~15u;
|
|
}
|
|
void Mixer::setLoop(int group, int voice, uint32_t address)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
Voice &v = m_groups[group].voices[voice];
|
|
v.loop = address & (kSoundMemory - 1u) & ~15u;
|
|
v.loopSet = true;
|
|
}
|
|
uint16_t Mixer::volume(int group, int voice, int side) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].voices[voice].volume[side].setting;
|
|
}
|
|
uint16_t Mixer::currentVolume(int group, int voice, int side) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return static_cast<uint16_t>(std::min<int32_t>(std::abs(m_groups[group].voices[voice].volume[side].level) >> 1, 0x7FFF));
|
|
}
|
|
uint16_t Mixer::pitch(int group, int voice) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].voices[voice].pitch;
|
|
}
|
|
uint16_t Mixer::adsr(int group, int voice, int word) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].voices[voice].adsr[word];
|
|
}
|
|
uint16_t Mixer::envelope(int group, int voice) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return static_cast<uint16_t>(m_groups[group].voices[voice].level);
|
|
}
|
|
uint32_t Mixer::start(int group, int voice) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].voices[voice].start;
|
|
}
|
|
uint32_t Mixer::loop(int group, int voice) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].voices[voice].loop;
|
|
}
|
|
uint32_t Mixer::position(int group, int voice) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].voices[voice].block;
|
|
}
|
|
|
|
void Mixer::keyOn(int group, uint32_t mask)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
Group &g = m_groups[group];
|
|
for (int index = 0; index < kVoices; ++index)
|
|
{
|
|
if ((mask & (1u << index)) == 0u)
|
|
continue;
|
|
Voice &voice = g.voices[index];
|
|
voice.phase = Voice::Phase::Attack;
|
|
voice.level = 0;
|
|
voice.envelopeCounter = 0u;
|
|
voice.counter = 0u;
|
|
std::fill(std::begin(voice.samples), std::end(voice.samples), static_cast<int16_t>(0));
|
|
voice.history[0] = voice.history[1] = 0;
|
|
voice.block = voice.start;
|
|
voice.loopSet = false;
|
|
g.ended &= ~(1u << index);
|
|
decode(voice);
|
|
reachIrq(voice.block, 16u);
|
|
++m_stats.keyOns;
|
|
}
|
|
}
|
|
|
|
void Mixer::keyOff(int group, uint32_t mask)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
for (int index = 0; index < kVoices; ++index)
|
|
{
|
|
Voice &voice = m_groups[group].voices[index];
|
|
if ((mask & (1u << index)) == 0u || voice.phase == Voice::Phase::Off)
|
|
continue;
|
|
voice.phase = Voice::Phase::Release;
|
|
voice.envelopeCounter = 0u;
|
|
}
|
|
}
|
|
|
|
uint32_t Mixer::ended(int group) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].ended;
|
|
}
|
|
void Mixer::clearEnded(int group, uint32_t mask)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].ended &= ~mask;
|
|
}
|
|
void Mixer::setDryMask(int group, int side, uint32_t mask)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].dry[side] = mask & 0xFFFFFFu;
|
|
}
|
|
void Mixer::setWetMask(int group, int side, uint32_t mask)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].wet[side] = mask & 0xFFFFFFu;
|
|
}
|
|
uint32_t Mixer::dryMask(int group, int side) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].dry[side];
|
|
}
|
|
uint32_t Mixer::wetMask(int group, int side) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].wet[side];
|
|
}
|
|
void Mixer::setMasterVolume(int group, int side, uint16_t value)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].master[side].set(value);
|
|
}
|
|
void Mixer::setEffectVolume(int group, int side, uint16_t value)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].effect[side].set(value);
|
|
}
|
|
uint16_t Mixer::masterVolume(int group, int side) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].master[side].setting;
|
|
}
|
|
uint16_t Mixer::effectVolume(int group, int side) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].effect[side].setting;
|
|
}
|
|
|
|
uint32_t Mixer::effectAreaBytes(int mode)
|
|
{
|
|
return kAreaSizes[std::clamp(mode, 0, 9)] * 8u;
|
|
}
|
|
|
|
void Mixer::setEffect(int group, int mode, uint32_t delay, uint32_t feedback, uint32_t end)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
Reverb &reverb = m_groups[group].reverb;
|
|
mode = std::clamp(mode, 0, 9);
|
|
reverb.mode = mode;
|
|
std::copy(std::begin(kPresets[mode]), std::end(kPresets[mode]), reverb.settings);
|
|
if (mode == 7 || mode == 8)
|
|
{
|
|
// Echo and delay: the delay time moves the same-side and all-pass lines (as libsd's sceSdSetEffectAttr
|
|
// does), the feedback is the wall coefficient.
|
|
const uint32_t t = std::min(delay, 127u) + 1u;
|
|
uint16_t *s = reverb.settings;
|
|
s[10] = static_cast<uint16_t>((t << 6u) - s[0]);
|
|
s[11] = static_cast<uint16_t>((t << 5u) - s[1]);
|
|
s[16] = static_cast<uint16_t>(s[17] + (t << 5u));
|
|
s[12] = static_cast<uint16_t>(s[13] + (t << 5u));
|
|
s[27] = static_cast<uint16_t>(s[29] + (t << 5u));
|
|
s[26] = static_cast<uint16_t>(s[28] + (t << 5u));
|
|
s[7] = static_cast<uint16_t>(std::min(feedback, 127u) * 258u);
|
|
}
|
|
// The area: the mode's size below end (its last byte), in 16-bit samples.
|
|
end = (end | 0x1FFFFu) & (kSoundMemory - 1u);
|
|
reverb.size = kAreaSizes[mode] * 4u;
|
|
reverb.start = ((end + 1u) / 2u - reverb.size) & (kSoundMemory / 2u - 1u);
|
|
reverb.position = 0u;
|
|
reverb.input[0] = reverb.input[1] = reverb.out[0] = reverb.out[1] = reverb.previous[0] = reverb.previous[1] = 0;
|
|
}
|
|
|
|
void Mixer::setEffectEnable(int group, bool enable)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].reverb.enabled = enable;
|
|
}
|
|
|
|
void Mixer::clearEffectArea(int mode, uint32_t end)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
end = (end | 0x1FFFFu) & (kSoundMemory - 1u);
|
|
const uint32_t bytes = effectAreaBytes(mode);
|
|
for (uint32_t i = 0; i < bytes; ++i)
|
|
m_memory[(end + 1u - bytes + i) & (kSoundMemory - 1u)] = 0u;
|
|
}
|
|
|
|
// One step of the console's reverb (24 kHz): the same-side and cross IIR lines, four combs, two all-pass filters,
|
|
// on the delay lines in sound memory, which advance a sample each step.
|
|
void Mixer::stepReverb(Reverb &reverb, const int32_t send[2], int32_t out[2])
|
|
{
|
|
const uint32_t size = reverb.size;
|
|
const auto at = [&](uint32_t offset, int32_t delta = 0)
|
|
{
|
|
const uint32_t relative = (reverb.position + offset + static_cast<uint32_t>(delta + static_cast<int32_t>(size))) % size;
|
|
return ((reverb.start + relative) & (kSoundMemory / 2u - 1u)) * 2u;
|
|
};
|
|
const auto load = [&](uint32_t offset, int32_t delta = 0)
|
|
{
|
|
const uint32_t a = at(offset, delta);
|
|
return static_cast<int32_t>(static_cast<int16_t>(m_memory[a] | (m_memory[a + 1u] << 8u)));
|
|
};
|
|
const auto store = [&](uint32_t offset, int32_t value)
|
|
{
|
|
const uint32_t a = at(offset);
|
|
const uint16_t v = static_cast<uint16_t>(clamp16(value));
|
|
m_memory[a] = static_cast<uint8_t>(v);
|
|
m_memory[a + 1u] = static_cast<uint8_t>(v >> 8u);
|
|
};
|
|
const uint16_t *s = reverb.settings;
|
|
const auto gain = [&](int index) { return static_cast<int32_t>(static_cast<int16_t>(s[index])); };
|
|
const auto line = [&](int index) { return static_cast<uint32_t>(s[index]) * 4u; };
|
|
const int32_t vIIR = gain(2), vCOMB1 = gain(3), vCOMB2 = gain(4), vCOMB3 = gain(5), vCOMB4 = gain(6),
|
|
vWALL = gain(7), vAPF1 = gain(8), vAPF2 = gain(9), vLIN = gain(30), vRIN = gain(31);
|
|
const uint32_t dAPF1 = line(0), dAPF2 = line(1), mLSAME = line(10), mRSAME = line(11), mLCOMB1 = line(12),
|
|
mRCOMB1 = line(13), mLCOMB2 = line(14), mRCOMB2 = line(15), dLSAME = line(16), dRSAME = line(17),
|
|
mLDIFF = line(18), mRDIFF = line(19), mLCOMB3 = line(20), mRCOMB3 = line(21), mLCOMB4 = line(22),
|
|
mRCOMB4 = line(23), dLDIFF = line(24), dRDIFF = line(25), mLAPF1 = line(26), mRAPF1 = line(27),
|
|
mLAPF2 = line(28), mRAPF2 = line(29);
|
|
|
|
const int32_t lin = mul15(clamp16(send[0]), vLIN);
|
|
const int32_t rin = mul15(clamp16(send[1]), vRIN);
|
|
store(mLSAME, mul15(lin + mul15(load(dLSAME), vWALL) - load(mLSAME, -1), vIIR) + load(mLSAME, -1));
|
|
store(mRSAME, mul15(rin + mul15(load(dRSAME), vWALL) - load(mRSAME, -1), vIIR) + load(mRSAME, -1));
|
|
store(mLDIFF, mul15(lin + mul15(load(dRDIFF), vWALL) - load(mLDIFF, -1), vIIR) + load(mLDIFF, -1));
|
|
store(mRDIFF, mul15(rin + mul15(load(dLDIFF), vWALL) - load(mRDIFF, -1), vIIR) + load(mRDIFF, -1));
|
|
|
|
int32_t lout = mul15(vCOMB1, load(mLCOMB1)) + mul15(vCOMB2, load(mLCOMB2)) + mul15(vCOMB3, load(mLCOMB3)) +
|
|
mul15(vCOMB4, load(mLCOMB4));
|
|
int32_t rout = mul15(vCOMB1, load(mRCOMB1)) + mul15(vCOMB2, load(mRCOMB2)) + mul15(vCOMB3, load(mRCOMB3)) +
|
|
mul15(vCOMB4, load(mRCOMB4));
|
|
const auto allPass = [&](int32_t value, uint32_t destination, uint32_t delay, int32_t coefficient)
|
|
{
|
|
const int32_t delayed = load(destination, -static_cast<int32_t>(delay));
|
|
value = clamp16(value - mul15(coefficient, delayed));
|
|
store(destination, value);
|
|
return mul15(value, coefficient) + delayed;
|
|
};
|
|
lout = allPass(lout, mLAPF1, dAPF1, vAPF1);
|
|
rout = allPass(rout, mRAPF1, dAPF1, vAPF1);
|
|
lout = allPass(lout, mLAPF2, dAPF2, vAPF2);
|
|
rout = allPass(rout, mRAPF2, dAPF2, vAPF2);
|
|
|
|
reverb.position = reverb.position + 1u >= size ? 0u : reverb.position + 1u;
|
|
out[0] = clamp16(lout);
|
|
out[1] = clamp16(rout);
|
|
}
|
|
|
|
void Mixer::setMix(int group, uint16_t mix)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].mix = mix & 0xFFFu;
|
|
}
|
|
uint16_t Mixer::mix(int group) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].mix;
|
|
}
|
|
void Mixer::setInputVolume(int group, int side, uint16_t value)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].input.volume[side] = static_cast<int16_t>(value);
|
|
}
|
|
uint16_t Mixer::inputVolume(int group, int side) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return static_cast<uint16_t>(m_groups[group].input.volume[side]);
|
|
}
|
|
|
|
namespace
|
|
{
|
|
// The input's layout: blocks of 1024 bytes, 256 left samples then 256 right.
|
|
uint32_t inputSampleAt(uint32_t offset)
|
|
{
|
|
return (offset / 1024u) * 256u + (offset % 512u) / 2u;
|
|
}
|
|
uint32_t inputOffsetOf(uint32_t sample)
|
|
{
|
|
return (sample / 256u) * 1024u + (sample % 256u) * 2u;
|
|
}
|
|
}
|
|
|
|
void Mixer::startInput(int group, const uint8_t *memory, uint32_t bytes, uint32_t offset, bool loop)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
Input &input = m_groups[group].input;
|
|
input.memory = memory;
|
|
input.samples = bytes / 1024u * 256u;
|
|
input.position = input.samples != 0u ? inputSampleAt(offset) % input.samples : 0u;
|
|
input.loop = loop;
|
|
input.playing = memory != nullptr && input.samples != 0u;
|
|
}
|
|
void Mixer::stopInput(int group)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].input.playing = false;
|
|
}
|
|
bool Mixer::inputPlaying(int group) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_groups[group].input.playing;
|
|
}
|
|
uint32_t Mixer::inputOffset(int group) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return inputOffsetOf(m_groups[group].input.position);
|
|
}
|
|
void Mixer::setMemoryInput(bool on)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_memoryInput = on;
|
|
}
|
|
void Mixer::setPortStream(PortStream *stream)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_portStream = stream;
|
|
}
|
|
|
|
uint64_t Mixer::nextInputHalfCycle() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
if (!m_clocked || !m_clockStarted)
|
|
return 0u;
|
|
uint64_t next = 0u;
|
|
for (const Group &group : m_groups)
|
|
{
|
|
const Input &in = group.input;
|
|
if (!in.playing || in.samples == 0u)
|
|
continue;
|
|
const uint32_t half = in.samples / 2u;
|
|
const uint32_t left = in.position < half ? half - in.position : in.samples - in.position;
|
|
const uint64_t at = (m_samplesMade + left) * kIopCyclesPerSample;
|
|
if (next == 0u || at < next)
|
|
next = at;
|
|
}
|
|
return next;
|
|
}
|
|
uint64_t Mixer::nextOutputIrqCycle() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
if (!m_clocked || !m_clockStarted)
|
|
return 0u;
|
|
uint64_t next = 0u;
|
|
for (const Group &group : m_groups)
|
|
{
|
|
const uint32_t halfword = (group.irqAddress >> 1u) & 0xFFFFFu;
|
|
if (!group.irqEnabled || halfword < 0x400u || halfword >= 0x2000u)
|
|
continue;
|
|
// Reached once the sample at that position is made (reachOutputIrq, after each sample).
|
|
const uint32_t left = ((halfword & 0x1FFu) - m_outputPosition) & 0x1FFu;
|
|
const uint64_t at = (m_samplesMade + left + 1u) * kIopCyclesPerSample;
|
|
if (next == 0u || at < next)
|
|
next = at;
|
|
}
|
|
return next;
|
|
}
|
|
void Mixer::setInputHalfCallback(std::function<void(int group, int half)> half)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_inputHalf = std::move(half);
|
|
}
|
|
|
|
void Mixer::setIrqAddress(int group, uint32_t address)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].irqAddress = address & (kSoundMemory - 1u);
|
|
}
|
|
|
|
void Mixer::setIrqEnabled(int group, bool enabled)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_groups[group].irqEnabled = enabled;
|
|
}
|
|
|
|
void Mixer::setIrqCallback(std::function<void(int group)> irq)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_irq = std::move(irq);
|
|
}
|
|
|
|
void Mixer::reachIrq(uint32_t address, uint32_t bytes)
|
|
{
|
|
address &= kSoundMemory - 1u;
|
|
for (int g = 0; g < 2; ++g)
|
|
{
|
|
Group &group = m_groups[g];
|
|
if (!group.irqEnabled)
|
|
continue;
|
|
const uint32_t offset = (group.irqAddress - address) & (kSoundMemory - 1u);
|
|
if (offset >= bytes)
|
|
continue;
|
|
group.irqEnabled = false;
|
|
if (m_irq)
|
|
m_irq(g);
|
|
}
|
|
}
|
|
|
|
void Mixer::decode(Voice &voice)
|
|
{
|
|
const uint8_t *block = &m_memory[voice.block & (kSoundMemory - 16u)];
|
|
uint32_t shift = block[0] & 0xFu, filter = (block[0] >> 4u) & 7u;
|
|
if (shift > 12u)
|
|
shift = 9u;
|
|
if (filter > 4u)
|
|
filter = 4u;
|
|
voice.flags = block[1];
|
|
if ((voice.flags & 4u) != 0u && !voice.loopSet)
|
|
voice.loop = voice.block;
|
|
voice.samples[0] = voice.samples[28];
|
|
voice.samples[1] = voice.samples[29];
|
|
voice.samples[2] = voice.samples[30];
|
|
int32_t h0 = voice.history[0], h1 = voice.history[1];
|
|
for (uint32_t i = 0; i < 28u; ++i)
|
|
{
|
|
const uint32_t nibble = (block[2u + (i >> 1u)] >> ((i & 1u) * 4u)) & 0xFu;
|
|
int32_t value = static_cast<int16_t>(static_cast<uint16_t>(nibble << 12u)) >> shift;
|
|
value = clamp16(value + ((h0 * kFilterPositive[filter] + h1 * kFilterNegative[filter] + 32) >> 6));
|
|
h1 = h0;
|
|
h0 = value;
|
|
voice.samples[3u + i] = static_cast<int16_t>(value);
|
|
}
|
|
voice.history[0] = h0;
|
|
voice.history[1] = h1;
|
|
}
|
|
|
|
void Mixer::stepEnvelope(Voice &voice)
|
|
{
|
|
const uint16_t a1 = voice.adsr[0], a2 = voice.adsr[1];
|
|
switch (voice.phase)
|
|
{
|
|
case Voice::Phase::Attack:
|
|
envelopeStep(voice.level, voice.envelopeCounter, (a1 >> 8u) & 0x7Fu, false, (a1 & 0x8000u) != 0u);
|
|
if (voice.level >= 0x7FFF)
|
|
{
|
|
voice.phase = Voice::Phase::Decay;
|
|
voice.envelopeCounter = 0u;
|
|
}
|
|
break;
|
|
case Voice::Phase::Decay:
|
|
envelopeStep(voice.level, voice.envelopeCounter, ((a1 >> 4u) & 0xFu) << 2u, true, true);
|
|
if (voice.level <= std::min<int32_t>(((a1 & 0xFu) + 1) * 0x800, 0x7FFF))
|
|
{
|
|
voice.phase = Voice::Phase::Sustain;
|
|
voice.envelopeCounter = 0u;
|
|
}
|
|
break;
|
|
case Voice::Phase::Sustain:
|
|
envelopeStep(voice.level, voice.envelopeCounter, (a2 >> 6u) & 0x7Fu, (a2 & 0x4000u) != 0u, (a2 & 0x8000u) != 0u);
|
|
break;
|
|
case Voice::Phase::Release:
|
|
envelopeStep(voice.level, voice.envelopeCounter, (a2 & 0x1Fu) << 2u, true, (a2 & 0x20u) != 0u);
|
|
if (voice.level <= 0)
|
|
{
|
|
voice.level = 0;
|
|
voice.phase = Voice::Phase::Off;
|
|
}
|
|
break;
|
|
case Voice::Phase::Off:
|
|
break;
|
|
}
|
|
}
|
|
|
|
int32_t Mixer::sample(Group &group, int index, Voice &voice)
|
|
{
|
|
const uint32_t at = voice.counter >> 12u;
|
|
const auto between = g_gaussian.load(std::memory_order_relaxed) ? &Mixer::gaussian : &interpolate;
|
|
int32_t value = between(voice.samples[at], voice.samples[at + 1u], voice.samples[at + 2u], voice.samples[at + 3u],
|
|
voice.counter & 0xFFFu);
|
|
stepEnvelope(voice);
|
|
value = mul15(value, voice.level);
|
|
voice.counter += std::min<uint32_t>(voice.pitch, 0x3FFFu);
|
|
while ((voice.counter >> 12u) >= 28u)
|
|
{
|
|
voice.counter -= 28u << 12u;
|
|
if ((voice.flags & 1u) != 0u)
|
|
{
|
|
// The block ends the sample: on to the loop point; without the repeat flag the voice stops.
|
|
group.ended |= 1u << index;
|
|
voice.block = voice.loop;
|
|
if ((voice.flags & 2u) == 0u)
|
|
{
|
|
voice.phase = Voice::Phase::Off;
|
|
voice.level = 0;
|
|
}
|
|
}
|
|
else
|
|
voice.block = (voice.block + 16u) & (kSoundMemory - 1u);
|
|
decode(voice);
|
|
reachIrq(voice.block, 16u);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
void Mixer::noteMixed(const int16_t *frames, size_t count)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_digestMutex);
|
|
const uint8_t *bytes = reinterpret_cast<const uint8_t *>(frames);
|
|
uint64_t hash = m_digestHash;
|
|
for (size_t i = 0; i < count * 4u; ++i)
|
|
hash = (hash ^ bytes[i]) * 0x100000001B3ull;
|
|
m_digestHash = hash;
|
|
m_digestCount += static_cast<uint32_t>(count);
|
|
}
|
|
|
|
void Mixer::takeMixed(uint64_t &hash, uint32_t &count)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_digestMutex);
|
|
hash = m_digestCount != 0u ? m_digestHash : 0u;
|
|
count = m_digestCount;
|
|
m_digestHash = 0xCBF29CE484222325ull;
|
|
m_digestCount = 0u;
|
|
}
|
|
|
|
Mixer::Stats Mixer::stats()
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
Stats stats = m_stats;
|
|
stats.voicesPlaying = 0;
|
|
for (const Group &group : m_groups)
|
|
for (const Voice &voice : group.voices)
|
|
stats.voicesPlaying += voice.phase != Voice::Phase::Off ? 1 : 0;
|
|
m_stats.peak = 0;
|
|
return stats;
|
|
}
|
|
|
|
void Mixer::setClocked(bool clocked)
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_clocked = clocked;
|
|
m_clockStarted = false;
|
|
std::lock_guard<std::mutex> queueLock(m_queueMutex);
|
|
m_queue.assign(kQueueFrames * 2u, 0);
|
|
m_queueRead = m_queueFill = 0u;
|
|
}
|
|
|
|
uint64_t Mixer::samplesMade() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
return m_samplesMade;
|
|
}
|
|
|
|
void Mixer::advanceTo(uint64_t iopCycle)
|
|
{
|
|
const uint64_t due = iopCycle / kIopCyclesPerSample;
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
if (!m_clocked)
|
|
return;
|
|
if (!m_clockStarted || due < m_samplesMade) // the first call, or the IOP's clock restarted (a reset)
|
|
{
|
|
m_clockStarted = true;
|
|
m_samplesMade = due;
|
|
return;
|
|
}
|
|
if (due == m_samplesMade)
|
|
return;
|
|
const size_t frames = static_cast<size_t>(std::min<uint64_t>(due - m_samplesMade, kQueueFrames));
|
|
m_samplesMade = due;
|
|
m_scratch.resize(frames * 2u);
|
|
m_stats.frames += frames;
|
|
mix(m_scratch.data(), frames);
|
|
noteMixed(m_scratch.data(), frames);
|
|
std::lock_guard<std::mutex> queueLock(m_queueMutex);
|
|
for (size_t i = 0; i < frames; ++i)
|
|
{
|
|
if (m_queueFill == kQueueFrames) // the device fell behind: the oldest goes
|
|
{
|
|
m_queueRead = (m_queueRead + 1u) % kQueueFrames;
|
|
--m_queueFill;
|
|
}
|
|
const size_t at = (m_queueRead + m_queueFill) % kQueueFrames;
|
|
m_queue[at * 2u] = m_scratch[i * 2u];
|
|
m_queue[at * 2u + 1u] = m_scratch[i * 2u + 1u];
|
|
++m_queueFill;
|
|
}
|
|
}
|
|
|
|
void Mixer::render(int16_t *out, size_t frames)
|
|
{
|
|
if (m_clocked)
|
|
{
|
|
// The device's thread: only the queue, never the mixer's state.
|
|
std::lock_guard<std::mutex> queueLock(m_queueMutex);
|
|
const size_t taken = std::min(frames, m_queueFill);
|
|
for (size_t i = 0; i < taken; ++i)
|
|
{
|
|
out[i * 2u] = m_queue[m_queueRead * 2u];
|
|
out[i * 2u + 1u] = m_queue[m_queueRead * 2u + 1u];
|
|
m_queueRead = (m_queueRead + 1u) % kQueueFrames;
|
|
}
|
|
m_queueFill -= taken;
|
|
std::fill(out + taken * 2u, out + frames * 2u, int16_t{0});
|
|
return;
|
|
}
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
m_stats.frames += frames;
|
|
mix(out, frames);
|
|
}
|
|
noteMixed(out, frames);
|
|
}
|
|
|
|
size_t Mixer::take(int16_t *out, size_t frames)
|
|
{
|
|
std::lock_guard<std::mutex> queueLock(m_queueMutex);
|
|
const size_t taken = std::min(frames, m_queueFill);
|
|
for (size_t i = 0; i < taken; ++i)
|
|
{
|
|
out[i * 2u] = m_queue[m_queueRead * 2u];
|
|
out[i * 2u + 1u] = m_queue[m_queueRead * 2u + 1u];
|
|
m_queueRead = (m_queueRead + 1u) % kQueueFrames;
|
|
}
|
|
m_queueFill -= taken;
|
|
return taken;
|
|
}
|
|
|
|
size_t Mixer::queuedFrames()
|
|
{
|
|
std::lock_guard<std::mutex> queueLock(m_queueMutex);
|
|
return m_queueFill;
|
|
}
|
|
|
|
void Mixer::mix(int16_t *out, size_t frames)
|
|
{
|
|
for (size_t frame = 0; frame < frames; ++frame)
|
|
{
|
|
// Group 0's output goes into group 1 (as its external input, through group 1's master volume).
|
|
int32_t mixed[2] = {0, 0};
|
|
for (int g = 0; g < kGroups; ++g)
|
|
{
|
|
Group &group = m_groups[g];
|
|
int32_t dry[2] = {0, 0}, wet[2] = {0, 0};
|
|
int32_t input[2] = {0, 0};
|
|
if (group.input.playing)
|
|
{
|
|
Input &in = group.input;
|
|
const uint32_t at = inputOffsetOf(in.position);
|
|
for (int side = 0; side < 2; ++side)
|
|
{
|
|
const uint8_t *p = in.memory + at + static_cast<uint32_t>(side) * 512u;
|
|
input[side] = mul15(static_cast<int16_t>(static_cast<uint16_t>(p[0] | (p[1] << 8u))), in.volume[side]);
|
|
}
|
|
++in.position;
|
|
const uint32_t half = in.samples / 2u;
|
|
if (in.position == half || in.position >= in.samples)
|
|
{
|
|
const int which = in.position == half ? 0 : 1;
|
|
if (in.position >= in.samples)
|
|
{
|
|
in.position = 0u;
|
|
in.playing = in.loop;
|
|
}
|
|
++m_stats.halves;
|
|
if (m_inputHalf)
|
|
m_inputHalf(g, which);
|
|
}
|
|
}
|
|
else if (m_memoryInput)
|
|
{
|
|
const uint32_t at = 0x4000u + static_cast<uint32_t>(g) * 0x800u + m_outputPosition * 2u;
|
|
for (int side = 0; side < 2; ++side)
|
|
{
|
|
const uint8_t *p = &m_memory[at + static_cast<uint32_t>(side) * 0x400u];
|
|
input[side] = mul15(static_cast<int16_t>(static_cast<uint16_t>(p[0] | (p[1] << 8u))),
|
|
group.input.volume[side]);
|
|
}
|
|
}
|
|
for (int index = 0; index < kVoices; ++index)
|
|
{
|
|
Voice &voice = group.voices[index];
|
|
voice.volume[0].tick();
|
|
voice.volume[1].tick();
|
|
if (voice.phase == Voice::Phase::Off)
|
|
continue;
|
|
const int32_t value = sample(group, index, voice);
|
|
const uint32_t bit = 1u << index;
|
|
if ((group.dry[0] & bit) != 0u)
|
|
dry[0] += mul15(value, voice.volume[0].level);
|
|
if ((group.dry[1] & bit) != 0u)
|
|
dry[1] += mul15(value, voice.volume[1].level);
|
|
if ((group.wet[0] & bit) != 0u)
|
|
wet[0] += mul15(value, voice.volume[0].level);
|
|
if ((group.wet[1] & bit) != 0u)
|
|
wet[1] += mul15(value, voice.volume[1].level);
|
|
}
|
|
// The reverb: the send averaged over two output samples, a step every other sample, its output
|
|
// eased between steps; at the group's effect volume.
|
|
int32_t returned[2] = {0, 0};
|
|
Reverb &reverb = group.reverb;
|
|
group.effect[0].tick();
|
|
group.effect[1].tick();
|
|
if (reverb.enabled && reverb.mode != 0)
|
|
{
|
|
int32_t send[2] = {clamp16(wet[0]), clamp16(wet[1])};
|
|
if ((group.mix & 0x200u) == 0u)
|
|
send[0] = 0;
|
|
if ((group.mix & 0x100u) == 0u)
|
|
send[1] = 0;
|
|
send[0] += (group.mix & 0x20u) != 0u ? input[0] : 0;
|
|
send[1] += (group.mix & 0x10u) != 0u ? input[1] : 0;
|
|
if (g == 1)
|
|
{
|
|
send[0] += (group.mix & 0x2u) != 0u ? mixed[0] : 0;
|
|
send[1] += (group.mix & 0x1u) != 0u ? mixed[1] : 0;
|
|
}
|
|
send[0] = clamp16(send[0]);
|
|
send[1] = clamp16(send[1]);
|
|
if (reverb.phase)
|
|
{
|
|
const int32_t average[2] = {(reverb.input[0] + send[0]) >> 1, (reverb.input[1] + send[1]) >> 1};
|
|
reverb.previous[0] = reverb.out[0];
|
|
reverb.previous[1] = reverb.out[1];
|
|
stepReverb(reverb, average, reverb.out);
|
|
}
|
|
else
|
|
{
|
|
reverb.input[0] = send[0];
|
|
reverb.input[1] = send[1];
|
|
}
|
|
for (int side = 0; side < 2; ++side)
|
|
{
|
|
const int32_t value = reverb.phase ? (reverb.previous[side] + reverb.out[side]) >> 1 : reverb.out[side];
|
|
returned[side] = mul15(value, group.effect[side].level);
|
|
}
|
|
reverb.phase = !reverb.phase;
|
|
}
|
|
group.master[0].tick();
|
|
group.master[1].tick();
|
|
for (int side = 0; side < 2; ++side)
|
|
{
|
|
// The dry switches: the voices (bits 11 / 10, left / right), the input (7 / 6), group 0 (3 / 2).
|
|
const uint32_t shift = side == 0 ? 1u : 0u;
|
|
int32_t level = returned[side];
|
|
if ((group.mix & (0x400u << shift)) != 0u)
|
|
level += clamp16(dry[side]);
|
|
if ((group.mix & (0x40u << shift)) != 0u)
|
|
level += input[side];
|
|
level = clamp16(level);
|
|
if (g == 0)
|
|
mixed[side] = mul15(level, group.master[side].level);
|
|
else
|
|
mixed[side] = clamp16(((group.mix & (0x4u << shift)) != 0u ? mixed[side] : 0) + level);
|
|
}
|
|
if (g == kGroups - 1)
|
|
for (int side = 0; side < 2; ++side)
|
|
mixed[side] = mul15(mixed[side], group.master[side].level);
|
|
}
|
|
if (int16_t stream[2]; m_portStream && m_portStream->pull(stream))
|
|
{
|
|
mixed[0] += stream[0];
|
|
mixed[1] += stream[1];
|
|
}
|
|
out[frame * 2u] = static_cast<int16_t>(clamp16(mixed[0]));
|
|
out[frame * 2u + 1u] = static_cast<int16_t>(clamp16(mixed[1]));
|
|
m_stats.peak = std::max({m_stats.peak, std::abs(clamp16(mixed[0])), std::abs(clamp16(mixed[1]))});
|
|
reachOutputIrq();
|
|
}
|
|
}
|
|
|
|
// The SPU2 writes each output sample back to its memory, at the same position (0-511) of 512-sample rings: the voice
|
|
// 1 / 3 outputs and the dry and wet mixes of both cores, halfwords 0x0400-0x1FFF. An IRQ address in those areas is
|
|
// reached when the position comes to it: a sample-clocked timer (Harmonix's softFX refills its stream buffers on it,
|
|
// its IRQ address alternating between two halves of a ring). Nothing is written there natively; the IRQ is.
|
|
void Mixer::reachOutputIrq()
|
|
{
|
|
for (int g = 0; g < kGroups; ++g)
|
|
{
|
|
Group &group = m_groups[g];
|
|
if (!group.irqEnabled)
|
|
continue;
|
|
const uint32_t halfword = (group.irqAddress >> 1u) & 0xFFFFFu;
|
|
if (halfword < 0x400u || halfword >= 0x2000u || (halfword & 0x1FFu) != m_outputPosition)
|
|
continue;
|
|
group.irqEnabled = false;
|
|
if (m_irq)
|
|
m_irq(g);
|
|
}
|
|
m_outputPosition = (m_outputPosition + 1u) & 0x1FFu;
|
|
}
|
|
}
|