mirror of
https://github.com/Dingo-Shenanigans/ReSkate.git
synced 2026-10-17 17:27:19 +02:00
Sources - Local Files: add a folder of music from the setup page (MP3, M4A, AAC, Ogg, FLAC, WAV; up to 200 songs a folder, 12 folders). Names and covers come from the files' tags. Each folder is a tile in its own row. - Online Radio: find stations by name (radio-browser.info) or add a stream's address (MP3, AAC, .pls, .m3u; no HLS). Each station is a tile in its own row; the track name follows what the station sends. Up to 24. - Both are decoded by the library built into the DLL (SpotifyHelper), which now plays files and HTTP streams as well as a program's output, reads tags and embedded pictures, and accepts MPEG-2 ADTS from AAC stations. Sound - Five-band equaliser (60 Hz, 250 Hz, 1 kHz, 4 kHz, 12 kHz) and loudness levelling, applied to every source on its way into the game. - Loudness, the speakers option, the equaliser and levelling are now kept in music-settings.json between sessions. Setup page - Remade in the mod's own colours and type, with numbered steps per source. - Queue pinned to the right: now playing, the playlist's songs with covers, find, and press to jump. Spotify's own queue shows once signed in. - Cards for Local files and Online radio; equaliser and levelling under Sound. In game - MUSIC page in the menu: player and queue, sound settings, source status and a button for the setup page. - "Loading song..." notice in the corner for songs that take a moment, with a switch to hide it. Presses made while a song is loading no longer restart it. - The Spotify row is hidden when Spotify is not in use. - Console: "spotify ..." is now "music ..." (music, music setup, music gain, music through, music spotify client|login|logout). Logs - The music sources' working lines (traces, per-song and per-bind lines, librespot's own) are debug only; stack captures are skipped unless debug logging is on. Grind exits: the per-pop line is debug only too. - A "Pace:" line when the game takes the sound faster or slower than real time, to find why music sounds fast after tabbing back in. Chat filter - A run of digits is a number, not a word spelled with look-alikes: a race time such as 2:55.0 is no longer masked.
123 lines
5.7 KiB
C++
123 lines
5.7 KiB
C++
#include "audio_dsp.h"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cmath>
|
|
#include <mutex>
|
|
|
|
namespace dingosdk::audio_dsp {
|
|
namespace {
|
|
std::array<std::atomic<float>, bands> wanted{};
|
|
std::atomic<bool> level{};
|
|
std::atomic<unsigned> changes{1};
|
|
|
|
// One filter of the equaliser, for both channels (the "Audio EQ Cookbook" forms).
|
|
struct Filter {
|
|
float b0{1}, b1{}, b2{}, a1{}, a2{};
|
|
float x1[2]{}, x2[2]{}, y1[2]{}, y2[2]{};
|
|
bool flat{true};
|
|
void set(int which, float decibels, float rate) {
|
|
flat = std::fabs(decibels) < 0.05f;
|
|
if (flat) return;
|
|
const float pi = 3.14159265f;
|
|
const float amount = std::pow(10.0f, decibels / 40.0f);
|
|
const float angle = 2.0f * pi * std::min(centres[which], rate * 0.45f) / rate;
|
|
const float cosine = std::cos(angle), sine = std::sin(angle);
|
|
float a0{};
|
|
if (which == 0 || which == bands - 1) {
|
|
// A shelf: everything below the first band's centre, or above the last one's.
|
|
const float alpha = sine / 2.0f * std::sqrt(2.0f), root = 2.0f * std::sqrt(amount) * alpha;
|
|
const float sign = which == 0 ? 1.0f : -1.0f;
|
|
b0 = amount * ((amount + 1) - sign * (amount - 1) * cosine + root);
|
|
b1 = sign * 2 * amount * ((amount - 1) - sign * (amount + 1) * cosine);
|
|
b2 = amount * ((amount + 1) - sign * (amount - 1) * cosine - root);
|
|
a0 = (amount + 1) + sign * (amount - 1) * cosine + root;
|
|
a1 = sign * -2 * ((amount - 1) + sign * (amount + 1) * cosine);
|
|
a2 = (amount + 1) + sign * (amount - 1) * cosine - root;
|
|
} else {
|
|
const float alpha = sine / (2.0f * 0.9f);
|
|
b0 = 1 + alpha * amount;
|
|
b1 = -2 * cosine;
|
|
b2 = 1 - alpha * amount;
|
|
a0 = 1 + alpha / amount;
|
|
a1 = -2 * cosine;
|
|
a2 = 1 - alpha / amount;
|
|
}
|
|
b0 /= a0, b1 /= a0, b2 /= a0, a1 /= a0, a2 /= a0;
|
|
}
|
|
float run(float in, int channel) {
|
|
const float out = b0 * in + b1 * x1[channel] + b2 * x2[channel] - a1 * y1[channel] - a2 * y2[channel];
|
|
x2[channel] = x1[channel], x1[channel] = in;
|
|
y2[channel] = y1[channel], y1[channel] = std::fabs(out) < 1e-20f ? 0.0f : out; // (no lingering in numbers too small to hear)
|
|
return out;
|
|
}
|
|
};
|
|
|
|
std::mutex mutex; // what is below: the sound comes from one thread at a time, but not always the same one
|
|
std::array<Filter, bands> filters;
|
|
unsigned set_for{}, set_rate{};
|
|
float power{0.02f}, gain{1.0f}; // the loudness of the last seconds (mean square), and what it is levelled by
|
|
|
|
// Nothing over full scale: what nears it is bent under it, not cut off.
|
|
float limited(float sample) {
|
|
const float size = std::fabs(sample);
|
|
if (size <= 0.8f) return sample;
|
|
return std::copysign(0.8f + 0.2f * std::tanh((size - 0.8f) / 0.2f), sample);
|
|
}
|
|
}
|
|
|
|
void set_band(int which, float decibels) noexcept {
|
|
if (which < 0 || which >= bands || !std::isfinite(decibels)) return;
|
|
wanted[static_cast<std::size_t>(which)].store(std::clamp(decibels, -most_decibels, most_decibels), std::memory_order_relaxed);
|
|
changes.fetch_add(1, std::memory_order_relaxed);
|
|
}
|
|
float band(int which) noexcept { return which < 0 || which >= bands ? 0.0f : wanted[static_cast<std::size_t>(which)].load(std::memory_order_relaxed); }
|
|
void set_levelling(bool on) noexcept { level.store(on, std::memory_order_relaxed); }
|
|
bool levelling() noexcept { return level.load(std::memory_order_relaxed); }
|
|
|
|
void process(float* interleaved, unsigned frames, unsigned rate) noexcept {
|
|
if (!interleaved || !frames || rate < 8000) return;
|
|
std::lock_guard lock(mutex);
|
|
if (const auto now = changes.load(std::memory_order_relaxed); now != set_for || rate != set_rate) {
|
|
for (int i = 0; i < bands; ++i) filters[static_cast<std::size_t>(i)].set(i, band(i), static_cast<float>(rate));
|
|
set_for = now;
|
|
set_rate = rate;
|
|
}
|
|
const bool equalised = std::any_of(filters.begin(), filters.end(), [](const Filter& filter) { return !filter.flat; });
|
|
const bool levelled = level.load(std::memory_order_relaxed);
|
|
if (!equalised && !levelled) {
|
|
gain = 1.0f;
|
|
return;
|
|
}
|
|
if (equalised)
|
|
for (auto& filter : filters) {
|
|
if (filter.flat) continue;
|
|
for (unsigned i = 0; i < frames; ++i) {
|
|
interleaved[i * 2] = filter.run(interleaved[i * 2], 0);
|
|
interleaved[i * 2 + 1] = filter.run(interleaved[i * 2 + 1], 1);
|
|
}
|
|
}
|
|
float aim = 1.0f;
|
|
if (levelled) {
|
|
// How loud this piece is, into how loud the last three seconds were; silence between
|
|
// songs is left out, so that the next one does not begin too loud.
|
|
double sum{};
|
|
for (unsigned i = 0; i < frames * 2; ++i) sum += static_cast<double>(interleaved[i]) * interleaved[i];
|
|
const float piece = static_cast<float>(sum / (frames * 2.0));
|
|
if (piece > 1e-5f) {
|
|
const float share = 1.0f - std::exp(-static_cast<float>(frames) / (static_cast<float>(rate) * 3.0f));
|
|
power += (piece - power) * share;
|
|
}
|
|
// Towards a loudness of about -16 dB, by no more than three times up or down.
|
|
aim = std::clamp(0.16f / std::sqrt(std::max(power, 1e-6f)), 1.0f / 3.0f, 3.0f);
|
|
}
|
|
// (It comes down in a fifth of a second and goes up in a second, so it is not heard working.)
|
|
const float down = 1.0f - std::exp(-1.0f / (static_cast<float>(rate) * 0.2f)), up = 1.0f - std::exp(-1.0f / static_cast<float>(rate));
|
|
for (unsigned i = 0; i < frames; ++i) {
|
|
gain += (aim - gain) * (aim < gain ? down : up);
|
|
interleaved[i * 2] = limited(interleaved[i * 2] * gain);
|
|
interleaved[i * 2 + 1] = limited(interleaved[i * 2 + 1] * gain);
|
|
}
|
|
}
|
|
}
|