F6: audio, correzioni della revisione
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:
1 parent
b1ee6879a7
commit
e75c8e1586
7 files changed
+126
-77
No files matched your search
@@ -12,9 +12,9 @@ namespace RailEnterprise.Client.Audio;
|
||||
/// <summary>
|
||||
/// Regia dell'audio, figlia di <c>Main</c> per tutta la vita del client: bus e volumi, effetti
|
||||
/// dell'interfaccia (clic di ogni pulsante, esito dei comandi), notifiche, cantieri completati,
|
||||
/// treni vicini al centro della telecamera (rotolamento, motore, sbuffi, fischio all'arrivo),
|
||||
/// ambiente dal terreno sotto la telecamera e musica per era. Non decide nulla del gioco: reagisce
|
||||
/// a ciò che il client riceve (esiti, notifiche, <c>DailyDigest</c>, moti dei treni) e alla vista.
|
||||
/// treni in moto vicini al centro della telecamera (rotolamento, motore, sbuffi), ambiente dal
|
||||
/// terreno sotto la telecamera e musica per era. Non decide nulla del gioco: reagisce a ciò che il
|
||||
/// client riceve (esiti, notifiche, <c>DailyDigest</c>, moti dei treni) e alla vista.
|
||||
/// I suoni si sintetizzano all'avvio su un altro thread (<see cref="Synth"/>, <see cref="MusicComposer"/>);
|
||||
/// nel ciclo per fotogramma non si alloca (elenchi preparati, nessuna LINQ).
|
||||
/// </summary>
|
||||
@@ -34,7 +34,8 @@ public partial class AudioManager : Node
|
||||
private bool _loaded;
|
||||
|
||||
// suoni singoli
|
||||
private AudioStreamWav? _click, _confirm, _error, _build, _whistle, _horn, _chuff;
|
||||
private AudioStreamWav? _click, _confirm, _error, _build, _chuff;
|
||||
private readonly Callable _clickCallable;
|
||||
private readonly AudioStreamWav?[] _notify = new AudioStreamWav?[3];
|
||||
private AudioStreamWav[] _birds = [];
|
||||
private AudioStreamWav? _diesel, _hum;
|
||||
@@ -66,7 +67,13 @@ public partial class AudioManager : Node
|
||||
private readonly TrainChannel[] _channels = new TrainChannel[AudioMix.TrainChannels];
|
||||
private readonly int[] _nearIds = new int[AudioMix.TrainChannels];
|
||||
private readonly float[] _nearDistances = new float[AudioMix.TrainChannels];
|
||||
private readonly Dictionary<int, byte> _trainStates = [];
|
||||
|
||||
/// <summary>
|
||||
/// Età massima (ms) dell'ultimo moto di un treno perché suoni: il server manda i moti dei treni
|
||||
/// in marcia ogni 100 ms; un moto più vecchio è di un treno fermo in stazione (durante la sosta
|
||||
/// non arrivano moti) o uscito dalla zona sottoscritta, e non va più ascoltato.
|
||||
/// </summary>
|
||||
private const long StaleMotionMs = 500;
|
||||
|
||||
// musica: due riproduttori per la dissolvenza incrociata
|
||||
private const float MusicLevel = 0.6f;
|
||||
@@ -87,7 +94,8 @@ public partial class AudioManager : Node
|
||||
private MapView? _map;
|
||||
private Dictionary<string, VehicleDef> _vehicles = [];
|
||||
private SynthRng _rng = new(0xA0D10);
|
||||
private ulong _lastUiMs, _lastNotifyMs, _lastWhistleMs;
|
||||
private ulong _lastUiMs, _lastNotifyMs;
|
||||
private bool _settingsDirty;
|
||||
|
||||
// pannello delle impostazioni
|
||||
private CanvasLayer? _overlay;
|
||||
@@ -98,6 +106,7 @@ public partial class AudioManager : Node
|
||||
{
|
||||
_enabled = enabled;
|
||||
Name = "AudioManager";
|
||||
_clickCallable = Callable.From(PlayClick);
|
||||
}
|
||||
|
||||
public AudioManager() : this(true)
|
||||
@@ -119,6 +128,8 @@ public partial class AudioManager : Node
|
||||
{
|
||||
if (Instance == this) Instance = null;
|
||||
Unbind();
|
||||
// chiusura della finestra con un volume appena cambiato: si salva comunque
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
/// <summary>Ere di eras.json (id e anno d'inizio, crescenti): scelgono il tema musicale.</summary>
|
||||
@@ -147,6 +158,7 @@ public partial class AudioManager : Node
|
||||
public void SetVolume(int bus, float value)
|
||||
{
|
||||
Settings.Volumes[bus] = Math.Clamp(value, 0, 1);
|
||||
_settingsDirty = true;
|
||||
ApplySettings();
|
||||
SettingsChanged?.Invoke();
|
||||
}
|
||||
@@ -154,6 +166,7 @@ public partial class AudioManager : Node
|
||||
public void SetMuted(bool muted)
|
||||
{
|
||||
Settings.Muted = muted;
|
||||
_settingsDirty = true;
|
||||
ApplySettings();
|
||||
SettingsChanged?.Invoke();
|
||||
}
|
||||
@@ -161,10 +174,16 @@ public partial class AudioManager : Node
|
||||
public void ToggleMute()
|
||||
{
|
||||
SetMuted(!Settings.Muted);
|
||||
Settings.Save();
|
||||
SaveSettings();
|
||||
}
|
||||
|
||||
public void SaveSettings() => Settings.Save();
|
||||
/// <summary>Scrive volumi e muto in <c>user://settings.cfg</c> se sono cambiati dall'ultimo salvataggio.</summary>
|
||||
public void SaveSettings()
|
||||
{
|
||||
if (!_settingsDirty) return;
|
||||
_settingsDirty = false;
|
||||
Settings.Save();
|
||||
}
|
||||
|
||||
/// <summary>Apre il pannello «Audio» sopra a tutto (menu o partita).</summary>
|
||||
public void ShowSettings()
|
||||
@@ -212,7 +231,6 @@ public partial class AudioManager : Node
|
||||
if (_session is not null) _session.MessageApplied -= OnMessage;
|
||||
_session = null;
|
||||
_map = null;
|
||||
_trainStates.Clear();
|
||||
}
|
||||
|
||||
private void OnMessage(IMessage message)
|
||||
@@ -220,11 +238,6 @@ public partial class AudioManager : Node
|
||||
if (!_loaded) return;
|
||||
switch (message)
|
||||
{
|
||||
case TrainMotion m:
|
||||
byte previous = _trainStates.TryGetValue(m.TrainId, out byte p) ? p : (byte)255;
|
||||
_trainStates[m.TrainId] = m.State;
|
||||
if (previous == (byte)TrainState.Moving && m.State == (byte)TrainState.Loading) Whistle(m.TrainId);
|
||||
break;
|
||||
case CommandResultMessage r:
|
||||
PlayUi(r.Ok ? _confirm : _error, r.Ok ? 0.7f : 0.85f);
|
||||
break;
|
||||
@@ -243,9 +256,6 @@ public partial class AudioManager : Node
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Welcome:
|
||||
_trainStates.Clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +276,9 @@ public partial class AudioManager : Node
|
||||
|
||||
private void OnNodeAdded(Node node)
|
||||
{
|
||||
if (node is BaseButton button) button.Pressed += PlayClick;
|
||||
// un pulsante staccato e riattaccato (MarketPanel lo fa a ogni resoconto) è già collegato
|
||||
if (node is BaseButton button && !button.IsConnected(BaseButton.SignalName.Pressed, _clickCallable))
|
||||
button.Connect(BaseButton.SignalName.Pressed, _clickCallable);
|
||||
}
|
||||
|
||||
private void PlayClick() => PlayUi(_click, 0.6f);
|
||||
@@ -290,13 +302,13 @@ public partial class AudioManager : Node
|
||||
/// <summary>Campioni di tutti gli effetti e degli anelli, sintetizzati fuori dal thread principale.</summary>
|
||||
private sealed class SoundBank
|
||||
{
|
||||
public required float[] Click, Confirm, Error, Construction, Whistle, Horn, Chuff, Rumble, Diesel, Hum, Wind, River, Sea, City;
|
||||
public required float[] Click, Confirm, Error, Construction, Chuff, Rumble, Diesel, Hum, Wind, River, Sea, City;
|
||||
public required float[][] Notify, Birds;
|
||||
|
||||
public static SoundBank Create() => new()
|
||||
{
|
||||
Click = Synth.Click(), Confirm = Synth.Confirm(), Error = Synth.Error(), Construction = Synth.BuildComplete(),
|
||||
Whistle = Synth.SteamWhistle(), Horn = Synth.Horn(), Chuff = Synth.Chuff(), Rumble = Synth.RailRumbleLoop(),
|
||||
Chuff = Synth.Chuff(), Rumble = Synth.RailRumbleLoop(),
|
||||
Diesel = Synth.DieselDroneLoop(), Hum = Synth.ElectricHumLoop(), Wind = Synth.WindLoop(), River = Synth.RiverLoop(),
|
||||
Sea = Synth.SeaLoop(), City = Synth.CityLoop(),
|
||||
Notify = [Synth.Notify(0), Synth.Notify(1), Synth.Notify(2)],
|
||||
@@ -342,8 +354,6 @@ public partial class AudioManager : Node
|
||||
_confirm = Wav(bank.Confirm);
|
||||
_error = Wav(bank.Error);
|
||||
_build = Wav(bank.Construction);
|
||||
_whistle = Wav(bank.Whistle);
|
||||
_horn = Wav(bank.Horn);
|
||||
_chuff = Wav(bank.Chuff);
|
||||
for (int i = 0; i < _notify.Length; i++) _notify[i] = Wav(bank.Notify[i]);
|
||||
_birds = bank.Birds.Select(b => Wav(b)).ToArray();
|
||||
@@ -517,15 +527,20 @@ public partial class AudioManager : Node
|
||||
int count = 0;
|
||||
Vector2 center = default;
|
||||
float zoom = 1, radius = 1;
|
||||
if (w is not null && map is not null && w.Speed > 0)
|
||||
if (w is not null && map is not null && _session is { } session && w.Speed > 0)
|
||||
{
|
||||
center = map.Camera.GetScreenCenterPosition();
|
||||
zoom = map.Zoom;
|
||||
radius = map.GetViewportRect().Size.Length() / 2;
|
||||
long now = (long)Time.GetTicksMsec();
|
||||
foreach (var (id, head) in map.Trains.Heads)
|
||||
{
|
||||
if (!w.Motions.TryGetValue(id, out var m) || m.State != (byte)TrainState.Moving || m.Speed < 0.5f) continue;
|
||||
count = AudioMix.InsertNearest(_nearIds, _nearDistances, count, id, head.DistanceTo(center) * zoom);
|
||||
if (!session.MotionReceivedMs.TryGetValue(id, out long at) || now - at > StaleMotionMs) continue;
|
||||
// oltre il raggio non si sentirebbe: niente canale (eviterebbe Play e Stop a ogni fotogramma)
|
||||
float distance = head.DistanceTo(center) * zoom;
|
||||
if (distance >= radius) continue;
|
||||
count = AudioMix.InsertNearest(_nearIds, _nearDistances, count, id, distance);
|
||||
}
|
||||
}
|
||||
// i canali restano ai loro treni finché sono tra i più vicini; gli altri sfumano
|
||||
@@ -625,19 +640,4 @@ public partial class AudioManager : Node
|
||||
}
|
||||
return Traction.Steam;
|
||||
}
|
||||
|
||||
/// <summary>Fischio (vapore) o tromba all'arrivo in stazione di un treno visibile vicino al centro.</summary>
|
||||
private void Whistle(int train)
|
||||
{
|
||||
var w = _session?.World;
|
||||
if (w is null || _map is null || !_map.Trains.Heads.TryGetValue(train, out var head)) return;
|
||||
ulong now = Time.GetTicksMsec();
|
||||
if (now - _lastWhistleMs < 700) return;
|
||||
float zoom = _map.Zoom;
|
||||
float d = head.DistanceTo(_map.Camera.GetScreenCenterPosition()) * zoom;
|
||||
float gain = AudioMix.TrainGain(d, _map.GetViewportRect().Size.Length() * 0.6f, AudioMix.FullSpeedMs, zoom);
|
||||
if (gain < 0.03f) return;
|
||||
_lastWhistleMs = now;
|
||||
Play(_sfx, TractionOf(train, w) == Traction.Steam ? _whistle : _horn, gain * 0.9f, _rng.Range(0.97f, 1.03f), _busSfx);
|
||||
}
|
||||
}
|
||||
@@ -30,8 +30,10 @@ public static class AudioMix
|
||||
public const int TrainChannels = 3;
|
||||
/// <summary>Velocità (m/s) a cui il rotolamento è al massimo.</summary>
|
||||
public const float FullSpeedMs = 25f;
|
||||
/// <summary>Metri percorsi tra due sbuffi: ruota motrice di circa 1,6 m, quattro sbuffi a giro.</summary>
|
||||
public const float ChuffSpacingM = 1.26f;
|
||||
/// <summary>Sbuffi al secondo da fermo (al passo) e alla velocità <see cref="ChuffFullSpeedMs"/>.</summary>
|
||||
public const float ChuffMinRate = 1.2f, ChuffMaxRate = 10f;
|
||||
/// <summary>Velocità (m/s, circa 110 km/h) a cui gli sbuffi sono più fitti.</summary>
|
||||
public const float ChuffFullSpeedMs = 30f;
|
||||
|
||||
public static float Clamp01(float x) => x < 0 ? 0 : x > 1 ? 1 : x;
|
||||
|
||||
@@ -83,8 +85,14 @@ public static class AudioMix
|
||||
/// <summary>Intonazione del motore (diesel o elettrico) con la velocità.</summary>
|
||||
public static float EnginePitch(float speedMs) => 0.8f + 0.5f * Clamp01(speedMs / FullSpeedMs);
|
||||
|
||||
/// <summary>Secondi tra due sbuffi della vaporiera alla velocità data.</summary>
|
||||
public static float ChuffInterval(float speedMs) => Math.Clamp(ChuffSpacingM / MathF.Max(speedMs, 0.5f), 0.09f, 1.2f);
|
||||
/// <summary>
|
||||
/// Secondi tra due sbuffi della vaporiera alla velocità data: la frequenza cresce in modo lineare
|
||||
/// da <see cref="ChuffMinRate"/> a <see cref="ChuffMaxRate"/> fino a <see cref="ChuffFullSpeedMs"/>,
|
||||
/// così segue la velocità su tutto l'esercizio normale. È una resa sonora, non il passo reale della
|
||||
/// ruota: sullo schermo il tempo dei treni corre decine di volte più veloce e gli sbuffi reali
|
||||
/// sarebbero un ronzio continuo.
|
||||
/// </summary>
|
||||
public static float ChuffInterval(float speedMs) => 1f / (ChuffMinRate + (ChuffMaxRate - ChuffMinRate) * Clamp01(speedMs / ChuffFullSpeedMs));
|
||||
|
||||
/// <summary>
|
||||
/// Inserisce un candidato nell'elenco ordinato dei più vicini (al più ids.Length): nessuna
|
||||
|
||||
@@ -6,8 +6,9 @@ namespace RailEnterprise.Client.Audio;
|
||||
|
||||
/// <summary>
|
||||
/// Pannello «Audio» sopra a tutto: un cursore per bus (generale, musica, ambiente, effetti,
|
||||
/// interfaccia) e il muto generale. I cambi si sentono subito; alla chiusura si salvano in
|
||||
/// <c>user://settings.cfg</c>. Esc o un clic fuori chiudono.
|
||||
/// interfaccia) e il muto generale. I cambi si sentono subito; quando il pannello esce dall'albero
|
||||
/// (anche chiudendo la finestra del gioco) si salvano in <c>user://settings.cfg</c>. Esc o un clic
|
||||
/// sinistro fuori chiudono.
|
||||
/// </summary>
|
||||
public partial class AudioSettingsPanel : Control
|
||||
{
|
||||
@@ -27,7 +28,8 @@ public partial class AudioSettingsPanel : Control
|
||||
shade.SetAnchorsPreset(LayoutPreset.FullRect);
|
||||
shade.GuiInput += e =>
|
||||
{
|
||||
if (e is InputEventMouseButton { Pressed: true }) Close();
|
||||
// solo il clic sinistro: la rotella e il tasto destro arrivano anch'essi come pressioni
|
||||
if (e is InputEventMouseButton { Pressed: true, ButtonIndex: MouseButton.Left }) Close();
|
||||
};
|
||||
AddChild(shade);
|
||||
var center = new CenterContainer { MouseFilter = MouseFilterEnum.Ignore };
|
||||
@@ -75,7 +77,11 @@ public partial class AudioSettingsPanel : Control
|
||||
Refresh();
|
||||
}
|
||||
|
||||
public override void _ExitTree() => _audio.SettingsChanged -= Refresh;
|
||||
public override void _ExitTree()
|
||||
{
|
||||
_audio.SettingsChanged -= Refresh;
|
||||
_audio.SaveSettings();
|
||||
}
|
||||
|
||||
private void Refresh()
|
||||
{
|
||||
@@ -99,7 +105,6 @@ public partial class AudioSettingsPanel : Control
|
||||
private void Close()
|
||||
{
|
||||
if (IsQueuedForDeletion()) return;
|
||||
_audio.SaveSettings();
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user