F6: audio, correzioni della revisione
CI / Build e test (pull_request) Successful in 1m32s
CI / Immagine Docker (pull_request) Successful in 31s

I treni suonano solo se si sentono davvero e solo mentre il server li dà in
marcia; il fischio all'arrivo, che non poteva mai suonare, è tolto.

- Il client non riceve mai un moto Loading (all'arrivo il percorso si svuota e
  Projection.Motion non manda nulla): via fischio e tromba all'arrivo da
  AudioManager e documenti; tornerà con una modifica del protocollo decisa a
  parte. Synth.SteamWhistle e Synth.Horn restano pronti.
- Un treno suona solo con un moto ricevuto da meno di 500 ms
  (GameSession.MotionReceivedMs): fermo in stazione o fuori dalla zona
  sottoscritta tace invece di restare sull'ultimo moto Moving.
- I candidati oltre metà diagonale non prendono un canale: niente Play e Stop
  a ogni fotogramma.
- Clic dei pulsanti collegato una volta sola (IsConnected con un Callable
  preparato): MarketPanel riattacca il TradeBox a ogni resoconto.
- Volumi e muto si salvano all'uscita del pannello e di AudioManager se
  cambiati, anche chiudendo la finestra con il pannello aperto; lo sfondo
  chiude solo con il clic sinistro, non con rotella o tasto destro.
- Sbuffi da 1,2 a 10 al secondo fino a 30 m/s, in modo lineare: seguono la
  velocità su tutto l'esercizio; ADR corretto (non è il passo della ruota).
- Tabelle d'onda dei temi limitate alle armoniche sotto il 45 % della
  frequenza di campionamento per nota: niente ripiegamento oltre Nyquist.

Refs #46.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-08 14:05:24 +01:00
1 parent b1ee6879a7
commit e75c8e1586
7 files changed
+126 -77

No files matched your search

@@ -50,7 +50,8 @@ public static class MusicComposer
private sealed class Voice
{
public required float[] Table { get; init; }
/// <summary>Ampiezza della n-esima armonica in Harmonics[n−1].</summary>
public required float[] Harmonics { get; init; }
public float Attack { get; init; } = 0.005f;
/// <summary>Decadimento verso il sostegno (s).</summary>
public float Decay { get; init; } = 0.3f;
@@ -61,19 +62,41 @@ public static class MusicComposer
public float TremoloHz { get; init; }
public float TremoloDepth { get; init; }
public float VibratoDepth { get; init; }
// tabelle per numero di armoniche tenute (una per registro, costruite al primo uso)
private readonly Dictionary<int, float[]> _tables = [];
/// <summary>
/// Forma d'onda per una nota alla frequenza data, con le sole armoniche sotto
/// <see cref="MaxHarmonicShare"/> della frequenza di campionamento: quelle oltre Nyquist
/// tornerebbero indietro come toni stonati.
/// </summary>
public float[] TableFor(float frequency)
{
int n = Math.Clamp((int)(MaxHarmonicShare * Synth.Rate / frequency), 1, Harmonics.Length);
if (!_tables.TryGetValue(n, out var table))
{
table = Table(Harmonics, n);
_tables[n] = table;
}
return table;
}
}
/// <summary>Frazione della frequenza di campionamento sotto cui stanno le armoniche (margine per vibrato e coro).</summary>
private const float MaxHarmonicShare = 0.45f;
private const int TableSize = 2048;
/// <summary>Forma d'onda di un periodo dalla somma di armoniche (ampiezza della n-esima in harmonics[n−1]).</summary>
private static float[] Table(params float[] harmonics)
/// <summary>Forma d'onda di un periodo dalla somma delle prime «count» armoniche (ampiezza della n-esima in harmonics[n−1]).</summary>
private static float[] Table(float[] harmonics, int count)
{
var t = new float[TableSize];
for (int i = 0; i < TableSize; i++)
{
float x = MathF.PI * 2 * i / TableSize;
float v = 0;
for (int h = 0; h < harmonics.Length; h++) v += harmonics[h] * MathF.Sin(x * (h + 1));
for (int h = 0; h < count; h++) v += harmonics[h] * MathF.Sin(x * (h + 1));
t[i] = v;
}
Synth.Normalize(t, 1);
@@ -96,15 +119,15 @@ public static class MusicComposer
private static Voice VoiceFor(Timbre timbre) => timbre switch
{
Timbre.MusicBox => new Voice { Table = Table(1, 0.1f, 0.35f, 0, 0.12f), Attack = 0.002f, Decay = 0.7f, Sustain = 0, Release = 0.6f },
Timbre.Accordion => new Voice { Table = Table(1, 0.5f, 0.6f, 0.3f, 0.25f, 0.15f), Attack = 0.03f, Decay = 0.2f, Sustain = 0.8f, Release = 0.08f, Detune = [0, 0.004f], TremoloHz = 5, TremoloDepth = 0.12f },
Timbre.Organ => new Voice { Table = Table(1, 0.8f, 0.6f, 0.4f, 0, 0.3f, 0, 0.2f), Attack = 0.01f, Decay = 0.1f, Sustain = 0.9f, Release = 0.06f, TremoloHz = 6, TremoloDepth = 0.1f },
Timbre.Vibraphone => new Voice { Table = Table(1, 0, 0, 0.25f), Attack = 0.003f, Decay = 1.1f, Sustain = 0, Release = 0.8f, TremoloHz = 5, TremoloDepth = 0.3f },
Timbre.Strings => new Voice { Table = Table(SawHarmonics(8)), Attack = 0.3f, Decay = 0.5f, Sustain = 0.85f, Release = 0.35f, Detune = [-0.003f, 0, 0.003f], VibratoDepth = 0.003f },
Timbre.Pulse => new Voice { Table = Table(PulseHarmonics(9, 0.25f)), Attack = 0.004f, Decay = 0.22f, Sustain = 0, Release = 0.15f },
Timbre.Saw => new Voice { Table = Table(SawHarmonics(10)), Attack = 0.08f, Decay = 0.4f, Sustain = 0.7f, Release = 0.25f, Detune = [-0.005f, 0.005f], VibratoDepth = 0.002f },
Timbre.PluckBass => new Voice { Table = Table(1, 0.5f, 0.25f, 0.1f), Attack = 0.005f, Decay = 0.35f, Sustain = 0.15f, Release = 0.08f },
_ => new Voice { Table = Table(1, 0.3f, 0.1f), Attack = 0.01f, Decay = 0.4f, Sustain = 0.6f, Release = 0.1f },
Timbre.MusicBox => new Voice { Harmonics = [1, 0.1f, 0.35f, 0, 0.12f], Attack = 0.002f, Decay = 0.7f, Sustain = 0, Release = 0.6f },
Timbre.Accordion => new Voice { Harmonics = [1, 0.5f, 0.6f, 0.3f, 0.25f, 0.15f], Attack = 0.03f, Decay = 0.2f, Sustain = 0.8f, Release = 0.08f, Detune = [0, 0.004f], TremoloHz = 5, TremoloDepth = 0.12f },
Timbre.Organ => new Voice { Harmonics = [1, 0.8f, 0.6f, 0.4f, 0, 0.3f, 0, 0.2f], Attack = 0.01f, Decay = 0.1f, Sustain = 0.9f, Release = 0.06f, TremoloHz = 6, TremoloDepth = 0.1f },
Timbre.Vibraphone => new Voice { Harmonics = [1, 0, 0, 0.25f], Attack = 0.003f, Decay = 1.1f, Sustain = 0, Release = 0.8f, TremoloHz = 5, TremoloDepth = 0.3f },
Timbre.Strings => new Voice { Harmonics = SawHarmonics(8), Attack = 0.3f, Decay = 0.5f, Sustain = 0.85f, Release = 0.35f, Detune = [-0.003f, 0, 0.003f], VibratoDepth = 0.003f },
Timbre.Pulse => new Voice { Harmonics = PulseHarmonics(9, 0.25f), Attack = 0.004f, Decay = 0.22f, Sustain = 0, Release = 0.15f },
Timbre.Saw => new Voice { Harmonics = SawHarmonics(10), Attack = 0.08f, Decay = 0.4f, Sustain = 0.7f, Release = 0.25f, Detune = [-0.005f, 0.005f], VibratoDepth = 0.002f },
Timbre.PluckBass => new Voice { Harmonics = [1, 0.5f, 0.25f, 0.1f], Attack = 0.005f, Decay = 0.35f, Sustain = 0.15f, Release = 0.08f },
_ => new Voice { Harmonics = [1, 0.3f, 0.1f], Attack = 0.01f, Decay = 0.4f, Sustain = 0.6f, Release = 0.1f },
};
private static float Frequency(float midi) => 440f * MathF.Pow(2, (midi - 69) / 12f);
@@ -114,6 +137,7 @@ public static class MusicComposer
{
int rate = Synth.Rate;
float f = Frequency(midi);
var table = v.TableFor(f);
int start = (int)(startS * rate);
float total = durS + v.Release;
int samples = Math.Min(loop.Length, (int)(total * rate));
@@ -138,7 +162,7 @@ public static class MusicComposer
if (phase >= TableSize) phase -= TableSize;
int i0 = (int)phase;
float frac = phase - i0;
float s = v.Table[i0] + (v.Table[(i0 + 1) & (TableSize - 1)] - v.Table[i0]) * frac;
float s = table[i0] + (table[(i0 + 1) & (TableSize - 1)] - table[i0]) * frac;
loop[(start + i) % loop.Length] += s * env * g;
}
}