BPM Detection in C#, Unity and Godot
The C# port runs the library's offline analysis, analyzeChannelData, on .NET, with both engines: the tempogram and peaks. It gives the same readings as the JS library, proven by the shared conformance vectors on every build. It is pure managed code with no runtime dependencies, and it targets .NET Standard 2.0: Unity, Godot C# and any .NET app.
It is on NuGet as RealtimeBpmAnalyzer:
dotnet add package RealtimeBpmAnalyzerusing RealtimeBpmAnalyzer;
float[] samples = DecodeMono(file); // your decoder: one channel, -1 to 1
Reading? reading = BpmAnalyzer.AnalyzeChannelData(samples, 44100);
if (reading != null)
{
Console.WriteLine($"{reading.Bpm} BPM");
}The reading is null when no tempo was found. Otherwise it has the same fields as the JS reading: Bpm, BpmExact, Confidence (null off the default 90 to 180 BPM range, and for a peaks reading of a quiet or bass-light input found only relative to its own level), Candidates, AudioFrom, AudioTo and Engine.
What it doesn't do
- No decoding. You supply the samples: one channel of floats from -1 to 1, and their sample rate. Mix stereo down first, and divide 16-bit samples by 32768. In Unity,
AudioClip.GetDatagives the floats andAudioClip.frequencythe rate. - Offline only. It analyses a whole buffer. The real-time analyzer isn't ported.
- It takes a while on a long file. Call it off a game's or an app's main thread.
Options
The options and their defaults are the JS ones, in C# casing:
var options = new AnalysisOptions(
engine: Engine.PeaksWith(new Lowpass(frequency: 150)),
tempoRange: new TempoRange(80, 170));
Reading? reading = BpmAnalyzer.AnalyzeChannelData(samples, 44100, options);A JS RangeError is an ArgumentOutOfRangeException here, and empty samples or a NaN sample throw ArgumentException. Invalid TempoRange and Lowpass values throw when they are made.