Attempting to generate sound, and set cells automatically

This commit is contained in:
max
2026-05-02 23:26:52 +02:00
parent 2c338ad7d9
commit a262410616
16 changed files with 442 additions and 285 deletions

View File

@@ -2,7 +2,8 @@
using SFML.Window;
using SFML.System;
using System.Diagnostics;
using FluidSim.Core;
using FluidSim.Scenarios;
using FluidSim.Audio;
namespace FluidSim;
@@ -10,6 +11,8 @@ public class Program
{
private const int SampleRate = 44100;
private static volatile bool running = true;
// Global step counter incremented every simulation step
private static long stepCount = 0;
public static void Main()
{
@@ -22,47 +25,71 @@ public class Program
soundEngine.Volume = 70;
soundEngine.Start();
double lastAudioTime = 0.0;
var stopwatch = Stopwatch.StartNew();
int warmupSamples = SampleRate / 2;
// --- Warmup: fill audio buffer with silence ---
int warmupSamples = SampleRate / 2; // 0.5 s
float[] warmup = new float[warmupSamples];
for (int i = 0; i < warmupSamples; i++)
warmup[i] = 0;
soundEngine.WriteSamples(warmup, warmupSamples);
lastAudioTime = stopwatch.Elapsed.TotalSeconds;
// Reset timer after warmup this is the “realtime zero”
stopwatch.Restart();
stepCount = 0; // simulation steps start now
// --- Initialise the simulation scenario ---
Simulation.Initialize(SampleRate);
const int chunkSize = 2048;
float[] buffer = new float[chunkSize];
Simulation.Initialize(SampleRate);
double lastLogTime = 0.0; // for periodic speed printout
while (window.IsOpen)
{
window.DispatchEvents();
// --- Compute how many audio samples are needed since last frame ---
double currentTime = stopwatch.Elapsed.TotalSeconds;
double elapsed = currentTime - lastAudioTime;
int samplesNeeded = (int)(elapsed * SampleRate);
double elapsed = currentTime; // since stopwatch was reset
int samplesNeeded = (int)(elapsed * SampleRate) - (int)(stepCount);
// (stepCount is total generated samples, so we just need the remainder)
// --- Generate the required number of simulation steps ---
while (samplesNeeded > 0 && running)
{
int toGenerate = Math.Min(samplesNeeded, chunkSize);
for (int i = 0; i < toGenerate; i++)
{
buffer[i] = Simulation.Process();
stepCount++;
}
soundEngine.WriteSamples(buffer, toGenerate);
samplesNeeded -= toGenerate;
}
lastAudioTime = currentTime;
// --- Display speed ---
double simTime = stepCount / (double)SampleRate;
double wallTime = stopwatch.Elapsed.TotalSeconds;
double speed = (wallTime > 0) ? simTime / wallTime : 0.0;
// Update window title with instant speed
window.SetTitle($"FluidSim | Speed: {speed:F3}× | Steps: {stepCount}");
// Console log once per second
if (wallTime - lastLogTime >= 1.0)
{
Console.WriteLine($"Speed: {speed:F3}× ({stepCount} steps, {wallTime:F2}s wall)");
lastLogTime = wallTime;
}
// --- Rendering (placeholder) ---
window.Clear(Color.Black);
window.Display();
}
// --- Cleanup ---
soundEngine.Dispose();
window.Dispose();
}