NAudio (.NET)
The in-browser demo below uses Web Audio because these docs live in a browser. The code snippet on this page applies on the .NET runtime.
Understanding which backend runs where matters when you cross-compile a shared alphaTab integration to another runtime. On .NET, alphaTab's synth pipeline ends at NAudioSynthOutput โ a WaveProvider32 implementation that hands 44.1 kHz stereo float samples to a DirectSoundOut device from NAudio.
What runs on the audio threadโ
NAudioSynthOutput extends NAudio.Wave.WaveProvider32. Under the hood:
- Samples are buffered through a
CircularSampleBuffersized from theplayer.bufferTimeInMillisecondssetting, then multiplied by four to absorb NAudio's own internal buffering. DirectSoundOutis initialized with a 40 ms latency target and pulls float samples on demand viaRead.- Every read triggers
samplesPlayed, which drives the alphaTab position cursor and themidiEventsPlayedevent.
You do not construct NAudioSynthOutput yourself โ the WPF and WinForms controls wire it up when the player is enabled. What you can configure is which DirectSound device receives the audio.
Selecting an output deviceโ
AlphaTabApi.EnumerateOutputDevices returns every DirectSound sink except the null GUID. SetOutputDevice tears down the current DirectSoundOut and rebuilds it against the chosen device, so live switching is supported.
Show it in codeโ
using AlphaTab;
using AlphaTab.Synth;
var api = GetAlphaTabApi(); // your WPF or WinForms AlphaTabApi instance
api.PlayerReady.On(async () =>
{
var devices = await api.EnumerateOutputDevices();
foreach (var device in devices)
{
Console.WriteLine($"{device.Label} (default: {device.IsDefault})");
}
// Route to a specific device; pass null to fall back to the system default.
var preferred = devices.FirstOrDefault(d => d.Label.Contains("Headphones"));
await api.SetOutputDevice(preferred);
});
GetAlphaTabApi() is a placeholder for the platform-specific step of obtaining the API instance โ on .NET you get one from the AlphaTab WPF control or the WinForms host you embedded.
Where to look nextโ
- Audio buffer size โ how
bufferTimeInMillisecondsmaps onto the NAudio buffer count. - Selecting an output device (Web) โ the same shape of API on the browser side.
- Web Audio and AudioWorklet and AudioTrack (Android) โ the sibling backends on the other runtimes.
api.enumerateOutputDevicesandapi.setOutputDeviceโ the routing API in the reference.NAudioSynthOutputsource on GitHub.