Skip to content

A wrapper for OpenAL to simplify audio playback

License

Notifications You must be signed in to change notification settings

sinshu/DrippyAL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DrippyAL

DrippyAL is a wrapper for OpenAL to simplify audio playback.

Installation

The NuGet package is available:

Install-Package DrippyAL

All the classes are in the DrippyAL namespace:

using DrippyAL;

Play 1 sec sine wave

var sampleRate = 44100;
var frequency = 440;
var data = Enumerable
    .Range(0, sampleRate)
    .Select(t => (short)(32000 * MathF.Sin(2 * MathF.PI * frequency * t / sampleRate)))
    .ToArray();

using (var device = new AudioDevice())
using (var channel = new AudioChannel(device))
using (var clip = new AudioClip(device, sampleRate, 1, data))
{
    channel.Play(clip);

    // Wait until any key is pressed.
    Console.ReadKey();
}

Play streaming sine wave

var sampleRate = 44100;
var frequency = 440;

using (var device = new AudioDevice())
using (var stream = new AudioStream(device, sampleRate, 1))
{
    var phase = 0F;
    var delta = 2 * MathF.PI * frequency / sampleRate;

    stream.Play(block =>
    {
        for (var t = 0; t < block.Length; t++)
        {
            block[t] = (short)(32000 * MathF.Sin(phase));
            phase = (phase + delta) % (2 * MathF.PI);
        }
    });

    // Wait until any key is pressed.
    Console.ReadKey();
}

Play wave file with NAudio

int sampleRate;
int channelCount;
short[] data;
using (var reader = new WaveFileReader("tada.wav"))
{
    sampleRate = reader.WaveFormat.SampleRate;
    channelCount = reader.WaveFormat.Channels;
    data = new short[reader.Length / 2];
    reader.Read(MemoryMarshal.Cast<short, byte>(data));
}

using (var device = new AudioDevice())
using (var channel = new AudioChannel(device))
using (var clip = new AudioClip(device, sampleRate, channelCount, data))
{
    channel.Play(clip);

    // Wait until any key is pressed.
    Console.ReadKey();
}

MIDI synthesis with MeltySynth

var sampleRate = 44100;
var synthesizer = new Synthesizer("TimGM6mb.sf2", sampleRate);
var sequencer = new MidiFileSequencer(synthesizer);
var midiFile = new MidiFile("flourish.mid");

sequencer.Play(midiFile, true);

using (var device = new AudioDevice())
using (var stream = new AudioStream(device, sampleRate, 2))
{
    stream.Play(data => sequencer.RenderInterleavedInt16(data));

    // Wait until any key is pressed.
    Console.ReadKey();
}

About

A wrapper for OpenAL to simplify audio playback

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages