Recursive AI

Transcribe a float PCM array without a WAV file in C#

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

Wrap mono 16 kHz samples normalized to −1..1 in AudioBuffer. The zip supplies example samples in sample.f32le.

Terminal
dotnet add package RecursiveAI.OneSpeech

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

C#
using System.Runtime.InteropServices;
using OneSpeech;
using OneSpeech.Whisper;
 
var bytes = File.ReadAllBytes("sample.f32le");
var samples = MemoryMarshal
    .Cast<byte, float>(bytes).ToArray();
var audio = new AudioBuffer(samples, 16_000);
using var asr = await WhisperRecognizer
    .LoadAsync(model: "openai/whisper-tiny");
var result = asr.Transcribe(audio, "en");
Console.WriteLine(result.Text);

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Text
Stuffed into you, his belly, cowtled him.

WhisperRecognizer throws ArgumentException if the buffer is not mono 16 kHz; creating an AudioBuffer does not resample it.

If you already have a float[], pass it as samples instead of reading the example file. The sample loader assumes a little-endian machine. The example input is the audio of LibriSpeech test-clean utterance 1089-134686-0001 by Vassil Panayotov, Guoguo Chen, Daniel Povey, and Sanjeev Khudanpur (CC BY 4.0), decoded from FLAC to raw float PCM. The first three floats are approximately 0.001038, 0.001099, and 0.000916; the full array holds 52,400 samples. The transcript above is the model’s output, including its misheard words.

The first LoadAsync downloads missing model files; the recognition call itself uses the local model.

Variations

Don’t specify English. With this multilingual checkpoint, you can omit the language argument. Replace the Transcribe line with:

C#
var result = asr.Transcribe(audio);

The short answer

Wrap a mono, normalized 16 kHz float[] in new AudioBuffer(samples, 16_000) and call WhisperRecognizer.Transcribe with that buffer. AudioBuffer holds a view of the input, not a copy, so don’t modify or recycle the array while inference is using it. You can keep the loaded recognizer for sequential calls; don’t call the same instance concurrently.

What is the difference between PCM audio and WAV audio?

PCM is the sequence of sampled audio amplitudes. WAV is a container that can store PCM and describe it in a header. A float[] has no such header: it doesn’t say whether the samples are at 16 kHz or 48 kHz, or whether they are mono or stereo. The sample rate and channel count you pass to AudioBuffer describe the array you already have. They don’t transform it.

If your app already has PCM from a decoder or recording buffer, there’s no reason to put it in a WAV container before recognition. If you have a WAV, FLAC, or other encoded byte stream instead, don’t reinterpret its bytes as floats. Use the encoded audio stream recipe so the decoder reads the format and produces samples.

How do you convert 16-bit PCM to normalized floats in C#?

For mono, 16 kHz signed PCM already decoded into short samples, divide by 32768f. That maps −32768 to −1 and 32767 to just below +1. The conversion changes the sample scale, not the rate or number of channels. This tiny buffer illustrates the conversion; use real speech as the input to the recognizer.

C#
short[] pcm16 = [-32768, 0, 16384];
float[] normalized = Array.ConvertAll(pcm16,
    sample => sample / 32768f);
Console.WriteLine(string.Join(", ", normalized));
Text
-1, 0, 0.5

For input at another rate, resample before creating the buffer. For stereo input, downmix or select the channel you want first: simply passing channels: 1 doesn’t make interleaved stereo data mono.

All onespeech recipes