在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#多媒体编程 → Speex编解码调用

Speex编解码调用

C#多媒体编程

下载此实例
  • 开发语言:C#
  • 实例大小:5.43KB
  • 下载次数:13
  • 浏览次数:370
  • 发布时间:2016-02-02
  • 实例类别:C#多媒体编程
  • 发 布 人:usepccom
  • 文件格式:.zip
  • 所需积分:2
 相关标签: speex libspeex .net speex

实例介绍

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.'sclsWaveProcessor 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;
    }
} 

实例下载地址

Speex编解码调用

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警