streda 16. mája 2012

Audio recording and playback in C# .NET

Many times developers who are using MS Visual Studio and C# .NET need to record audio signal from microphone and work with it in low level. It is no news that the .NET framework does not include any classes for dealing with sound. There are some high-level wrappers on web using the system commponents like Direct Show or Windows Media Player wich allow developers to create an audio player or recorder but there are only few that allow to work with actual data received from the microphone or sending data directly into speakers. I myself had this kind of problem when I was trying to develop speech recognition application for my diploma thesis few years ago.

Therefore I decided to design a C# library that can help developers to easily create a reliable audio recorder and player in the matter of minutes. It is based on article of Ianier Munoz at www.codeproject.com but I have upgraded the code to be easy to use and quite reliable. You can download the library here for free.

Using the library

Download the library from the link above and add the reference to your project in Visual Studio. To create the low level audio recorder you have to create the instance of Recorder class as follows:


The attributes are preaty intuitive. "Output.wav" is the name and path of output file, format is the instance of WaveFormat class with the informations about the output wave stream. Next attribute is the device ID from the list in recorder.GetDevidesList(); or you can type -1 for default device. The last attribute is the buffer size which is here for example 4096. It means that if the sample rate is 8192 samples per second, the buffer will be filled twice in second and you will get the date twice a second as well. If you need the audio date more often, you should use the smaller size of buffer. The OnRecord method is the Event called every time the data arrive.

The usage of the Player class is quite similar. Again the name of input file, and device ID from the list.


There is also a Generator class ready to use. It only needs to adjust the property values and it is ready to generate a desired signal.


Conclusion

The samples show how to use the AudioLib library to record, playback and generate low-level audio data. This is usefull when you need to access the data on fly, such as speech recognition or applying some effect. 

4 komentáre:

  1. The blog is pretty good and have some useful information regarding DOT NET Development Services.

    OdpovedaťOdstrániť
  2. Is it possible to pause recording and resume where you left off?

    OdpovedaťOdstrániť
  3. Yes, by using following method:
    player.Pause(); // pause the current stream
    player.Play(); // resume playback

    OdpovedaťOdstrániť
  4. On what license is that lib ?
    Can I use it in commercial project ?
    Can you share with the source code of it ?
    Greetings

    OdpovedaťOdstrániť