This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Tuesday, November 11, 2008

how to use the waveout API from C#|low-level audio player in C#



CLICKHERE TO DOWNLOAD SOURCE CODE

Introduction:
It is no news that the .NET framework does not include any classes for dealing with sound. Some people have worked around this limitation by wrapping high-level system components, such as Windows Media Player or DirectShow?, into .NET-friendly libraries. However, most of these libraries are designed in such a way that they cannot be used to manipulate audio samples on the fly because they do not give you access to the actual sound data.

When developing applications that deal with such low-level issues, the most commonly used technologies are DirectSound? and the waveout API. This article describes a sample application that uses the waveout API in C# through Interop to play a WAV file in a continuous loop.

Using the code:
Most of the work in the sample application is carried out by two classes: WaveStream? and WaveOutPlayer?.

The WaveStream? class extends System.IO.Stream and implements the necessary code to read audio samples from a WAV file. The constructor reads the file header and extracts all the relevant information including the actual length of the stream and the format of the audio samples, which is exposed through the Format property.

One important thing to note is that seeking operations in the WaveStream? class are relative to the sound data stream. This way you don't have to care about the length of the header or about those extra bytes at the end of the stream that don't belong to the audio data chunk. Seeking to 0 will cause the next read operation to start at the beginning of the audio data.

The WaveOutPlayer? class is where the most interesting things happen. For the sake of simplicity, the interface of this class has been reduced to the strict minimum. There are no Start, Stop or Pause methods. Creating an instance of WaveOutPlayer? will cause the system to start streaming immediately.

Let's have a look at the code that creates the WaveOutPlayer? instance. As you can see, the constructor takes five parameters:


private void Play()
{
Stop();
if (m_AudioStream != null)
{
m_AudioStream.Position = 0;
m_Player = new WaveLib?.WaveOutPlayer(-1, m_Format, 16384, 3,
new WaveLib?.BufferFillEventHandler(Filler));
}
}

The first parameter is the ID of the wave device that you want to use. The value -1 represents the default system device, but if your system has more than one sound card, then you can pass any number from 0 to the number of installed sound cards minus one to select a particular device.

The second parameter is the format of the audio samples. In this example, the format is taken directly from the wave stream.

The third and forth parameters are the size of the internal wave buffers and the number of buffers to allocate. You should set these to reasonable values. Smaller buffers will give you less latency, but the audio may stutter if your computer is not fast enough.

The fifth and last parameter is a delegate that will be called periodically as internal audio buffers finish playing, so that you can feed them with new sound data. In the sample application we just read audio data from the wave stream, like this:


private void Filler(IntPtr data, int size)
{
byte[] b = new bytesize;
if (m_AudioStream != null)
{
int pos = 0;
while (pos < size)
{
int toget = size - pos;
int got = m_AudioStream.Read(b, pos, toget);
if (got < toget)
m_AudioStream.Position = 0; // loop if the file ends
pos += got;
}
}
else
{
for (int i = 0; i < b.Length; i++)
bi = 0;
}
System.Runtime.InteropServices.Marshal.Copy(b, 0, data, size);
}

Please note that this delegate may be called from any internal thread created by the WaveOutPlayer? object, so if you want to call into your Windows Forms objects you should use the Invoke mechanism.

To stop playing, just call Dispose on the player object, like this:


private void Stop()
{
if (m_Player != null)
try
{
m_Player.Dispose();
}
finally
{
m_Player = null;
}
}

CLICKHERE TO DOWNLOAD SOURCE CODE

0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Blog Archive

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger