Recursive AI

Extract a Whisper log-mel spectrogram in C#

OneSpeech is a free, open-source speech recognition library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Extract Whisper features from one second of 16 kHz mono audio with a synthetic one-sample click (no audio file needed). No model weights needed.

Terminal
dotnet add package RecursiveAI.OneSpeech

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneSpeech;
using OneSpeech.Audio;
 
var samples = new float[16_000];
samples[8_000] = 1;
var audio = new AudioBuffer(samples, 16_000);
var mel = new WhisperLogMelSpectrogram();
var values = mel.Extract(audio);
int frames = values.Length / mel.MelBins;
Console.WriteLine($"{mel.MelBins} x {frames}");

Unzip the example, then dotnet run.

Text
80 x 3000

This is the shape in mel frequency bins by time frames. The array contains 240,000 floats. Extract throws ArgumentException for input that isn’t 16 kHz mono, exceeds 30 seconds, or contains nonfinite samples.

Variations

Use 128 bins for large-v3 or turbo. With the same audio from above:

C#
var wide = new WhisperLogMelSpectrogram(128);
var features = wide.Extract(audio);
int frames128 = features.Length / wide.MelBins;
Console.WriteLine($"{wide.MelBins} x {frames128}");
Text
128 x 3000

Skip padding to inspect the actual clip. With mel and audio from above:

C#
var raw = mel.Extract(audio, padToWindow: false);
Console.WriteLine(raw.Length / mel.MelBins);
Text
100

Load a WAV instead of generating samples. Use a WAV no longer than 30 seconds, such as jfk.wav from the speech-to-text example. OneSpeech installs OneAudio as a dependency. Replace the sample generation and AudioBuffer construction with:

C#
var wav = OneAudio.Audio.Load("jfk.wav",
    new OneAudio.LoadOptions { SampleRate = 16_000, Channels = 1 });
var audio = new AudioBuffer(wav.Samples, 16_000);

The loader converts the WAV to mono 16 kHz float samples. The rest of the example still prints 80 x 3000.

The short answer

Use WhisperLogMelSpectrogram.Extract to turn up to 30 seconds of 16 kHz mono float PCM into Whisper features in C#. The default extractor uses 80 mel bins and pads to 3,000 time frames. It returns a flat float[] with all the frames of the first bin followed by all the frames of the next bin. Extracting features doesn’t load a model, make a network call, or produce a transcript.

What is a log-mel spectrogram in Whisper?

It’s a grid of values that describe the signal over time: one axis is frequency grouped into mel bands and the other is time. The extractor looks at 400-sample windows, stepping 160 samples at a time. At 16 kHz that’s a 25-millisecond window and a 10-millisecond step. It groups spectral power into mel bands, takes a base-10 logarithm with a floor for very small values, then clips and rescales the result. That’s why the features aren’t interchangeable with raw FFT magnitudes or a spectrogram image.

The flat array is mel-major: the value at bin m and frame t is at index m * frames + t. Keep that layout when passing the values to a consumer that expects bins before frames. Choose the bin count for the actual checkpoint: 80 for tiny through large-v2, 128 for large-v3 and turbo. See Whisper features for the transform.

Why are there 3,000 frames in Whisper’s log-mel input?

The default pads the samples to a 30-second window before computing the features. At 100 steps per second that gives 3,000 frames, even for the one-second input here. Padding is applied to the time-domain samples, so it’s not equivalent to just appending zero float values to the feature array. Disabling padding yields sampleCount / 160 frames using integer division, but won’t pad the result to fit a fixed 3,000-frame input. Unpadded extraction rejects clips of 200 samples or fewer because the centered transform needs enough samples to reflect at the edges.

The extractor neither resamples nor mixes channels. Prepare your own audio as 16 kHz mono PCM first. For longer recordings, choose a windowing scheme or use OneSpeech’s file transcription API if your goal is text rather than features.

All onespeech recipes