This is a basic user guide to NVorbis. Please feel free to ask questions on the Discussions page.

Installation

Copy "NVorbis.dll" to your libraries folder, then add a reference to it from your project. If NAudio support is needed, repeat for "NVorbis.NAudioSupport.dll".

Usage

NVorbis provides an easy-to-use API for reading metadata and raw audio data from an Ogg Vorbis file. The basic usage of NVorbis is as follows:

using (var vorbis = new NVorbis.VorbisReader(@"C:\PATH\TO\OGG\FILE.ogg"))
{
    int channels = vorbis.Channels;
    int sampleRate = vorbis.SampleRate;
    var duration = vorbis.TotalTime;

    var buffer = new float[16384];
    int count;
    while ((count = vorbis.ReadSamples(buffer, 0, buffer.Length)) > 0)
    {
        // Do stuff with the samples returned...
        // Sample value range is -0.99999994f to 0.99999994f
        // Samples are interleaved (chan0, chan1, chan0, chan1, etc.)
    }
}

Using NVorbis with NAudio is also easy:

// Console application version
using (var vorbis = new NVorbis.NAudioSupport.VorbisWaveReader(@"C:\PATH\TO\OGG\FILE.ogg"))
using (var waveOut = new NAudio.Wave.WaveOut())
{
    waveOut.Init(vorbis);
    waveOut.Play();

    while (waveOut.PlaybackState != NAudio.Wave.PlaybackState.Stopped)
    {
        Thread.Sleep(100);
    }
}

// WinForms / WPF version with WaveOut (NAudio supports a lot more outputs)
NVorbis.NAudioSupport.VorbisWaveReader _reader;
NAudio.Wave.WaveOut _waveOut;

// Wire this up to a button on your UI
void OnClickHandler(object sender, EventArgs e)
{
    _reader = new NVorbis.NAudioSupport.VorbisWaveReader(@"C:\PATH\TO\OGG\FILE.ogg");

    _waveOut = new NAudio.Wave.WaveOut();
    _waveOut.PlaybackStopped += OnStoppedHandler;
    _waveOut.Init(_reader);
    _waveOut.Play();
}

void OnStoppedHandler(object sender, EventArgs e)
{
    _waveOut.PlaybackStopped -= OnStoppedHandler;
    _waveOut = null;

    _reader.Dispose();
    _reader = null;
}

Building from Source

NVorbis is a Visual Studio 2010 project (currently). Normal VS usage applies.

Last edited Jun 27, 2013 at 7:44 PM by ioctlLR, version 3

Comments

No comments yet.