snocrash's picture

OpenAL EFX Reverb example

Here is an example of how to create a reverb effect with OpenAL. This is a very useful effect for adding ambiance to an environment. For example a cave, large room, etc...

In order for it to work you need to add a sound file to your data/audio folder, and change the file name 'APACHE.wav' to the name of your sound.

using System;
using System.Threading;
using System.IO;
using OpenTK.Audio;
 
namespace Examples
{
    public class ReverbExample
    {
        static readonly string filename = Path.Combine(Path.Combine("Data", "Audio"), "APACHE.wav");
        public static void Main()
        {
            using (AudioContext context = new AudioContext())
            using (AudioReader sound = new AudioReader(filename))
            {
                Console.WriteLine("Testing WaveReader({0}).ReadToEnd()", filename);
 
                EffectsExtension EFX = new EffectsExtension();
 
                int buffer = AL.GenBuffer();
                int source = AL.GenSource();
                int state;
                int[] slots = new int[1];
                int[] effects = new int[1];
 
                EFX.GenAuxiliaryEffectSlots(1, out slots[0]);
 
                EFX.BindEffect(effects[0], EfxEffectType.Reverb);
                EFX.Effect(effects[0], EfxEffectf.ReverbDecayTime, 3.0f);
                EFX.Effect(effects[0], EfxEffectf.ReverbDecayHFRatio, 0.91f);
                EFX.Effect(effects[0], EfxEffectf.ReverbDensity, 0.7f);
                EFX.Effect(effects[0], EfxEffectf.ReverbDiffusion, 0.9f);
                EFX.Effect(effects[0], EfxEffectf.ReverbRoomRolloffFactor, 3.1f);
                EFX.Effect(effects[0], EfxEffectf.ReverbReflectionsGain, 0.723f);
                EFX.Effect(effects[0], EfxEffectf.ReverbReflectionsDelay, 0.03f);
                EFX.Effect(effects[0], EfxEffectf.ReverbGain, 0.23f);
 
                EFX.AuxiliaryEffectSlot(slots[0], EfxAuxiliaryi.EffectslotEffect, effects[0]);
 
                AL.BufferData(buffer, sound.ReadToEnd());
 
                AL.Source(source, ALSourcef.ConeOuterGain, 1.0f);
                AL.Source(source, ALSourcei.Buffer, buffer);
                AL.SourcePlay(source);
 
                Console.Write("Playing");
 
                // Query the source to find out when it stops playing.
                do
                {
                    Thread.Sleep(250);
                    Console.Write(".");
                    AL.GetSource(source, ALGetSourcei.SourceState, out state);
                }
                while ((ALSourceState)state == ALSourceState.Playing);
 
                Console.WriteLine();
 
                AL.SourceStop(source);
                AL.DeleteSource(source);
                AL.DeleteBuffer(buffer);
                EFX.DeleteEffects(effects);
                EFX.DeleteAuxiliaryEffectSlots(slots);
            }
        }
    }
}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Thanks for the code, do you mind if I add it to Examples.exe?

snocrash's picture

Please do. And more to come.

the Fiddler's picture

Great, just committed to SVN.