Click here to Skip to main content
13,866,934 members
Click here to Skip to main content
Add your own
alternative version


131 bookmarked
Posted 16 Dec 2002

Programming Audio Effects in C#

, 16 Dec 2002
Rate this:
Please Sign up or sign in to vote.
This article explains how to create a framework for implementing audio effects in C#.

Sample Image - cswavplayfx.gif


This article is a logical continuation of my article A low-level audio player in C# in which I presented an application that uses the waveout API in C# through Interop to play a WAV file in a continuous loop.

This time I will explain how to create a framework for implementing audio effects and how to extend the basic player to use it.

Audio Effects Framework

The idea of the framework is to provide some helper classes for implementing audio effects. In order to keep things simple, the framework was designed to help you implement effects that don’t modify the length of the sound data, although it can be easily extended to support other kinds of effects.

The basic effect class is called InPlaceEffect. The term in-place means that a single memory buffer is used for input and output. InPlaceEffect is an abstract class; derived classes must implement two methods: Connect and Process.

The Connect method is called when the host application wants to know whether the effect can process samples in a particular format. Sample formats are described by the WaveFormat structure, which holds common properties such as sampling rate, bit depth and number of channels.

Once an effect has been connected, the host application repeatedly calls the Process method passing in buffers with audio data. The audio effect should read data from the buffer and overwrite it with the modified data. In our sample application, the code that applies effects looks like this:

private void Filler(IntPtr data, int size)
    byte[] b = new byte[size];

    // goes the code to fill in the data buffer...

    // Apply all selected effects
    foreach(EffectInfo nfo in m_Effects)
        nfo.Effect.Process(b, 0, b.Length);

    // goes the code to send the data to the sound card...

The InPlaceEffect class does not help you much when it comes to processing audio samples because you still have to deal with all possible sample formats. The InPlaceEffectFloat class was designed to alleviate this situation. This class implements the Process method to convert each sample to floating point format, call a virtual method to process the sample and then convert the sample back to its original format. This approach is indeed far from being computationally efficient, but it lets you concentrate on your own algorithm by making the code much cleaner. “Real-life” audio effects typically deal with each format in a particular way in order to achieve maximum performance.

Another important aspect of the effect is the user interface that allows you to control the effect’s parameters. The FxForm class works in conjunction with the FxParameter attribute to create on the fly a user interface for your effect.

The FxParameter attribute is defined like this:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class FxParameterAttribute : Attribute
    public readonly string Name;
    public readonly float LowerBound;
    public readonly float UpperBound;
    public readonly float Increment;

    public readonly string Unit;
    public FxParameterAttribute(string name, float lowerBound, 
        float upperBound, float increment, string unit)
        Name = name;
        LowerBound = lowerBound;
        UpperBound = upperBound;
        Increment = increment;
        Unit = unit;
    public FxParameterAttribute(string name, float lowerBound, 
        float upperBound, float increment) : 
        this(name, lowerBound, upperBound, increment, string.Empty)

This attribute holds all the necessary information to display a NumericUpDown control that allows you to modify the parameter. At runtime, our framework automatically creates a form, based on the information from the attributes defined in the effect’s properties. The FxForm class then uses Reflection to enumerate properties and create the controls accordingly, like this:

private void CreateFxControls()
    Type t = Effect.GetType();
    object[] attrs = t.GetCustomAttributes
        (typeof(DescriptionAttribute), true);
    if (attrs.Length > 0)
        Text = ((DescriptionAttribute)attrs[0]).Description;

    // get effect parameters
    int y = 15;
    foreach (PropertyInfo prop in t.GetProperties())
        attrs = prop.GetCustomAttributes
            (typeof(FxParameterAttribute), false);
        if (attrs.Length > 0)
            // This is an effect paramater. Create the necessary controls.
            FxParameterAttribute param = (FxParameterAttribute)attrs[0];

            // ... create the necessary controls to handle the parameter

            y += 25;
    this.ClientSize = new Size(this.ClientSize.Width, y);

Other attributes could be defined to display other controls, such as check boxes or sliders. This is left as an exercise to the reader.

A Basic Effect: Volume Control

The code for the volume control is extremely simple since we just have to multiply each sample by a constant value. This approach works well for our purposes, but more sophisticated volume controls usually include some code to prevent zipper noise and to apply dithering at lower volume levels.

[Description("Volume Control")]
public class VolumeEffect : InPlaceEffectFloat
    private float m_Volume = 1.0f;

    [FxParameter("Volume", 0, 2.0f, 0.05f)]
    public float Volume
        get { return m_Volume; }
            m_Volume = value;

    public override bool Connect(WaveFormat format)
        return true;
    protected override float ProcessMono(float x)
        return x * m_Volume;
    protected override void ProcessStereo
            (ref float left, ref float right)
        left *= m_Volume;
        right *= m_Volume;

A More Complex Effect: Phase Shifter

The “Phase Shifter”, also known as “Phaser”, is a cool audio effect that can be described as a “breathing effect”. More information about how a Phaser works can be found here. I decided to implement a Phaser because this effect is not included with the standard DirectX effects.

As with the volume control, I derived this effect from the InPlaceEffectFloat class. The source code for the PhaseShiftEffect class can be found in the files that accompany this article.


In this article I explained how to extend a low-level audio player to implement support for audio effects.

A modified version of this sample that implements support for DirectX plug-ins is included with the Adapt-X SDK, which is a commercial product that can be found at


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ianier Munoz
Web Developer
Luxembourg Luxembourg
Ianier Munoz lives in France and works as a senior consultant and analyst for an international consulting firm. His specialty is in multimedia applications, and he has authored some popular software, such as American DJ's Pro-Mix, Chronotron and Adapt-X.

You may also be interested in...

Comments and Discussions

QuestionChoose the sound card used for a specific application Pin
CHATAIGNON Clément5-Feb-13 0:39
memberCHATAIGNON Clément5-Feb-13 0:39 
GeneralMy vote of 5 Pin
ronykucse4-Jul-12 22:25
memberronykucse4-Jul-12 22:25 
SuggestionAn Alternative Pin
DadajiIn29-Jun-12 0:33
memberDadajiIn29-Jun-12 0:33 
GeneralMy vote of 3 Pin
asmitajb14-Feb-11 2:36
memberasmitajb14-Feb-11 2:36 
QuestionHow can I pull out every sample into textBox? Pin
Bernard Opala30-Jan-10 13:13
memberBernard Opala30-Jan-10 13:13 
AnswerRe: How can I pull out every sample into textBox? Pin
Bernard Opala25-May-10 11:51
memberBernard Opala25-May-10 11:51 
Generalband-pass filter Pin
ondra0019-Jan-10 7:01
memberondra0019-Jan-10 7:01 
GeneralDirectX Q Pin
moosh2-Mar-09 11:34
membermoosh2-Mar-09 11:34 
QuestionI include "wave visualicer", Somebody have others examples effects codes? Pin
Yulay30-Dec-08 0:40
memberYulay30-Dec-08 0:40 
GeneralPlay non-wav audio samples Pin
Member 411849721-Dec-08 23:49
memberMember 411849721-Dec-08 23:49 
Generalaccessing the data samples... Pin
jshunnar18-Feb-08 5:38
memberjshunnar18-Feb-08 5:38 
GeneralSample MP3 volume level Pin
bradirby2-Aug-07 20:26
memberbradirby2-Aug-07 20:26 
GeneralConvert Wave file to Mp3 file Pin
Le Quoc Do12-Jul-07 18:44
memberLe Quoc Do12-Jul-07 18:44 
Generalproblems with non 44100Hz Pin
wazomba6-Apr-06 4:56
memberwazomba6-Apr-06 4:56 
GeneralRe: problems with non 44100Hz Pin
Ianier Munoz28-Apr-06 21:06
memberIanier Munoz28-Apr-06 21:06 
GeneralInterrupring the recording while doing other tasks Pin
zarkopetrovski2-May-05 16:25
memberzarkopetrovski2-May-05 16:25 
GeneralRe: Interrupring the recording while doing other tasks Pin
Ianier Munoz28-Apr-06 21:15
memberIanier Munoz28-Apr-06 21:15 
GeneralChange Audio sample Pin
srenzi4-Apr-05 8:28
membersrenzi4-Apr-05 8:28 
GeneralImplementing IDisposable in WaveStream class... Pin
dimmr19-Mar-05 7:19
memberdimmr19-Mar-05 7:19 
GeneralRe: Implementing IDisposable in WaveStream class... Pin
Ianier Munoz20-Mar-05 20:05
memberIanier Munoz20-Mar-05 20:05 
GeneralRe: Implementing IDisposable in WaveStream class... [modified] Pin
gumi_r@msn.com15-Mar-07 7:43
membergumi_r@msn.com15-Mar-07 7:43 
GeneralUsing with DirectSound Pin
oversight_failure23-Feb-05 18:22
memberoversight_failure23-Feb-05 18:22 
GeneralRe: Using with DirectSound Pin
Ianier Munoz24-Feb-05 19:55
memberIanier Munoz24-Feb-05 19:55 
GeneralPlaying Sound Simultaneously and Tempo Pin
aniljain218-Jan-05 0:37
memberaniljain218-Jan-05 0:37 
GeneralRe: Playing Sound Simultaneously and Tempo Pin
Ianier Munoz21-Jan-05 6:56
memberIanier Munoz21-Jan-05 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 17 Dec 2002
Article Copyright 2002 by Ianier Munoz
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid