Click here to Skip to main content
11,484,402 members (66,963 online)
Click here to Skip to main content

Speex in C#

, 1 Sep 2007 62.2K 2.6K 58
Rate this:
Please Sign up or sign in to vote.
Using the Speex speech codec with the .NET framework
Screenshot - Hierarchy.png

Introduction

I've been working for a while on a voice chat program in C# and encountered a vexing problem. Uncompressed audio data simply wouldn't do for a chat program and yet all of the .NET voice compression solutions I could find were quite expensive. Speex, the license-free open-source voice codec, seemed to be the obvious choice, and yet an exhaustive search turned up no C# implementations of the library. A quick search on SourceForge turned up a few such projects, but all were incomplete and long-abandoned. This is not a comforting fact for someone considering attempting the same feat.

Of course the entire time an exceedingly simple solution was readily available, if only I had been able to see it. The Speex website provides two command-line utilities, speexenc and speexdec. As I was looking for a programmatic solution, it never occurred to me to even look at the syntax of these utilities. If I had, I would've seen how easy it would be to use the utilities from .NET code.

What is Speex?

Speex is a license-free open-source voice codec. It is used for compressing audio data into a smaller format, which is advantageous for transmitting voice over the internet. Keep in mind that it is generally not efficient for non-voice data. This is because (according to Wikipedia) voice codecs work by eliminating frequencies that cannot be made by the human voices and those that are inaudible to human ears. With a reduced number of available frequencies, the audio data can be stored in a more compact form. Speex is usually used in VoIP programs and other, similar internet voice applications, but it can also be used simply for reducing the size of a file on your computer.

More information can be found at speex.org.

Using the Code

This code is quite simple to use. It contains an exception class, two structures for storing various data, and a class with two methods: encode and decode. It also makes use of a heavily modified version of Sujoy G.'s clsWaveProcessor class from his article Wave File Processor in C#. I've added a new field that contains the raw PCM data, removed all methods except for WaveHeaderIN, and edited that method to work on a stream instead of a filename. Here is the meat of the program, the Codec class.

public class Codec
{
    public EncodeReturn Encode(byte[] raw, int bytespersecond,
        int samplespersecond, bool stereo,
        short bitspersample, bool denoise, bool agc)
    {
        //Start speexenc process
        Process encProc = Process.Start("speexenc",
            "-u " + //Ultra wide-band
            (denoise ? "--denoise " : "") + //Denoise before encode
            "--agc " + //Addaptive gain control before encode
            "--bitrate " + bytespersecond * 8 + " " + //Set the bitrate
            "--rate " + samplespersecond + " " + //Set the sample rate
            (stereo ? "--stereo " : "") + //Set the channel count
            (bitspersample != 16 ? "--8bit " : "") + //
            "con con"); //Set console input and output

        //Writes the raw audio data to encproc's StdIn one byte at a time
        foreach (byte b in raw)
        {
            encProc.StandardInput.BaseStream.WriteByte(b);
        }

        //Wait, to ensure that all output has been written
        encProc.WaitForExit();

        //Check for success
        if (encProc.ExitCode != 0)
            throw new EncodeDecodeFailureException(encProc.ExitCode);

        //Skip the first line
        encProc.StandardOutput.ReadLine();

        //Remove output
        BinaryReader br = new BinaryReader(encProc.StandardOutput.BaseStream);

        byte[] retB = new byte[encProc.StandardOutput.BaseStream.Length];

        //In non-verbose mode, the first line of output is the only line on
        //non-audio data
        encProc.StandardOutput.ReadLine();

        //Read the output
        int k = 0;
        while (!encProc.StandardOutput.EndOfStream)
        {
            retB[k++] = br.ReadByte();
        }

        //Clean up
        br.Close();

        //Create the return object
        EncodeReturn retVal = new EncodeReturn(retB);

        //And return it
        return retVal;

    }

    public DecodeReturn Decode(byte[] raw)
    {
        //Create and start the decoding process
        Process decProc = Process.Start("speexdec", "--force-uwb con con");

        //Writes the raw audio data to encproc's StdIn one byte at a time
        foreach (byte b in raw)
        {
            decProc.StandardInput.BaseStream.WriteByte(b);
        }

        //Wait, to ensure that all output has been written
        decProc.WaitForExit();

        //Check for success
        if (decProc.ExitCode != 0)
            throw new EncodeDecodeFailureException(decProc.ExitCode);

        //Skip the first line
        encProc.StandardOutput.ReadLine();

        //Pass the output to clsWaveProcessor
        clsWaveProcessor cwp = new clsWaveProcessor();

        //Process the header and the data
        cwp.WaveHeaderIN(decProc.StandardOutput.BaseStream);

        //Create the output
        DecodeReturn dr = new DecodeReturn(cwp.RawPcmWaveData,
            ((cwp.BitsPerSample / 8) * cwp.SampleRate),
            cwp.SampleRate, cwp.Channels != 1, cwp.BitsPerSample);

        //Return the output
        return dr;
    }
} 

The code is fairly straightforward. In each method, I start a process based on the users needs. Note that I simplified encode for my needs. You may wish to add more parameters. The most important thing to note here is that both the input and output files for speexenc and speexdec are set to con. This allows me to write the input PCM data, as well as read the output, without having to use temporary files.

Then I simply read and write the data. Note that I ignore one line of output data in each method. This is because, regardless of whether or not the output is redirected to the console, both utilities write one line of extra information before they begin to write the output file. This is why it's very important that verbose mode is left off.

Pretty much all the rest of the relevant info can be found in the comments.

Note

I just want to mention that I plan updating this soon. The code has not been tested yet and I plan on doing a demo application.

History

Update: 1 September, 2007

  • Made EncodeReturn and DecodeReturn members public.
  • Added a section on Speex itself.

License

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

Share

About the Author

Alex Flood

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 Pin
qwsaqwsa123453-Oct-12 1:01
memberqwsaqwsa123453-Oct-12 1:01 
Generallove! Pin
AnthemSword14-Jun-12 22:52
memberAnthemSword14-Jun-12 22:52 
GeneralSpeex Encoder/Decoder in managed code Pin
balistof28-Nov-10 20:48
memberbalistof28-Nov-10 20:48 
QuestionHow to use this speex class for encoding or decoding wav files Pin
jibin.mn28-Oct-10 1:49
memberjibin.mn28-Oct-10 1:49 
Generalspeex included in this sip sdk Pin
Alejandro Bacha18-Oct-10 23:24
memberAlejandro Bacha18-Oct-10 23:24 
GeneralMy vote of 3 Pin
Option Greek15-Aug-10 20:52
memberOption Greek15-Aug-10 20:52 
GeneralUsage in Compact Framework Pin
KanchanP10-Aug-09 3:23
memberKanchanP10-Aug-09 3:23 
Generalspeex argument "con con" wont work Pin
carl morey29-Jan-09 17:19
membercarl morey29-Jan-09 17:19 
AnswerRe: speex argument "con con" wont work Pin
cantruchd27-Mar-09 8:41
membercantruchd27-Mar-09 8:41 
GeneralRe: speex argument "con con" wont work Pin
carl morey30-Mar-09 16:48
membercarl morey30-Mar-09 16:48 
GeneralError in Remotable Object Pin
shonaa110-Apr-08 0:58
membershonaa110-Apr-08 0:58 
GeneralData transfering Pin
shonaa16-Apr-08 4:13
membershonaa16-Apr-08 4:13 
GeneralRe: Data transfering Pin
Ghzanfar Ali6-Apr-08 5:59
memberGhzanfar Ali6-Apr-08 5:59 
GeneralRe: Data transfering Pin
shonaa110-Apr-08 1:19
membershonaa110-Apr-08 1:19 
GeneralAn exception pops up in .Net 2.0 Pin
Ghzanfar Ali18-Mar-08 0:33
memberGhzanfar Ali18-Mar-08 0:33 
GeneralRe: An exception pops up in .Net 2.0 Pin
albin_t@yahoo.com28-May-08 7:49
memberalbin_t@yahoo.com28-May-08 7:49 
GeneralRe: An exception pops up in .Net 2.0 Pin
josemora0017-Sep-08 11:39
memberjosemora0017-Sep-08 11:39 
Generalwrapper instead of .exe caller Pin
raulgspan28-Nov-07 19:43
memberraulgspan28-Nov-07 19:43 
GeneralUseless Pin
SilentGlider2-Oct-07 4:33
memberSilentGlider2-Oct-07 4:33 
GeneralRe: Useless Pin
Robin Debnath27-Apr-08 23:13
memberRobin Debnath27-Apr-08 23:13 
GeneralRe: Useless Pin
kaka sipahe14-Aug-08 20:59
memberkaka sipahe14-Aug-08 20:59 
GeneralRe: Useless Pin
donperry20-Jun-09 16:59
memberdonperry20-Jun-09 16:59 
GeneralGood one ... Pin
Vasudevan Deepak Kumar2-Sep-07 1:24
memberVasudevan Deepak Kumar2-Sep-07 1:24 
GeneralNice, but... Pin
Setharian1-Sep-07 22:49
memberSetharian1-Sep-07 22:49 
GeneralVoice Conference Pin
tempsh23-Aug-07 22:53
membertempsh23-Aug-07 22:53 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 1 Sep 2007
Article Copyright 2007 by Alex Flood
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid