A Low-level Audio Player in C#

Published on December 2016 | Categories: Documents | Downloads: 40 | Comments: 0 | Views: 457
of 4
Download PDF   Embed   Report

Audio player using c#

Comments

Content


3/13/2014 A low-level audio player in C# - CodeProject
http://www.codeproject.com/Articles/3352/A-low-level-audio-player-in-C 1/4
10,458,738 members (61,465 online) 309 Sign out


Member 10652045
home quick answers discussions features community
help
Search f or articles, questions, tips
Articles » Multimedia » Audio and Video » Audio

Article
Browse Code
Bugs / Suggestions
Stats
Revisions
Alternatives
Comments &
Discussions (164)
Add your own
alternative version
About Article
This article describes a
sample application that
uses the waveout API in C#.
Type Article
Licence
First Posted 11 Dec 2002
Views 659,433
Bookmarked 229 times
.NET1.0 Win2K WinXP C#
Dev Intermediate

Top News
News on the future of C#
as a language
Get the Insider News free each
morning.
Related Videos
Related Articles
A full-duplex audio player in C#
using the waveIn/waveOut APIs
AMR Audio Encoding
Next
Rate:
3 Tweet 1
A low-level audio player in C#
By Ianier Munoz, 27 Aug 2003
Download source files - 12 Kb
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
4.84 (80 votes)
0
articles
3/13/2014 A low-level audio player in C# - CodeProject
http://www.codeproject.com/Articles/3352/A-low-level-audio-player-in-C 2/4
Programming Audio Effects in
C#
C Sharp Ripper
Low-Level Control of *.wav Data
(Part I)
3 Breakthrough Ways to
Visualize HTML5
Play or Capture Audio Sound.
Send and Receive as Multicast
(RTP)
C# MP3 Compressor
Open Audio Engine
Audio-Gallery-Suite (A
complete audio gallery solution
made with
HTML5/CSS3/jQuery-
JS/PHP/C#)
Text to Speech (tts) for the Web
AAC Encode
Embedding and Playing WAV
Audio Files in a WinForms
Application
Workaround to Fix Audio
Autoplay on Mobile Browsers!
PVS.AVPlayer
Another Article on MP3 Players
using Windows Media Player
nVLC
Unlocking the Power of HTML5
Audio
C# Windows Media Format SDK
Translation
C# MP3 Compressor
Related Research
Fine-Tuning the Engines of SMB
Growth: 4 strategies for growing
your business
Protecting Your Business Data:
Five Do’s and Don’ts
constructor takes five parameters:
Collapse | Copy Code
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:
Collapse | Copy Code
private void Filler(IntPtr data, int size)
{
byte[] b = new byte[size];
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++)
b[i] = 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:
Collapse | Copy Code
private void Stop()
{
if (m_Player != null)
try
{
m_Player.Dispose();
}
finally
{
m_Player = null;
}
}
Conclusion
3/13/2014 A low-level audio player in C# - CodeProject
http://www.codeproject.com/Articles/3352/A-low-level-audio-player-in-C 3/4
Ianier Munoz
Web Developer
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.
Add a Comment or Question

Search this forum Go

This sample demonstrates how to use the waveout API from C#. This is useful for applications that
require more control on the audio stream compared to what other higher level libraries offer.
Typical examples are applications that generate or modify audio samples on the fly, such as digital
effect processors.
A modified version of this sample that implements support for DirectX plug-ins is included in the
Adapt-X SDK, which is a commercial product that can be found at www.chronotron.com.
History
Update 28 Aug 03 - Fixed a bug related to reading the WAV file header.
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
About the Author
Article Top
Comments and Discussions
Profile popups Spacing Relaxed Noise Medium Layout Normal Per page 25
Update
First Prev Next
Member 10217574 22-Aug-13 4:07
Member 10238076 28-Aug-13 15:40
Efi Hoory 29-Aug-13 3:14
The code doesn't work!
Re: The code doesn't work!
Re: The code doesn't work!
3/13/2014 A low-level audio player in C# - CodeProject
http://www.codeproject.com/Articles/3352/A-low-level-audio-player-in-C 4/4
Permalink | Advertise | Privacy | Mobile
Web02 | 2.8.140309.2 | Last Updated 28 Aug 2003
Article Copyright 2002 by Ianier Munoz
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid
Pham Anh Khoa 7-Aug-13 4:31
Raul Iloc 28-May-13 7:08
Super_Marvin_97 9-Mar-13 7:12
aerjienong 12-Nov-12 12:20
Vitor Vilela 30-Jul-12 9:52
SergeyKS 23-Apr-12 10:32
SergeyKS 24-Apr-12 5:07
simonkakaonce 27-Mar-12 22:13
duonmhMTA 19-Nov-11 22:08
bayfatih2000 24-May-11 7:32
Member 4526326 27-Sep-10 17:26
Benetz 8-Feb-10 5:01
Soroush Safaei 19-Oct-09 13:59
Member 4194084 4-Mar-09 22:17
zonksoft 12-Mar-09 12:15
Jörgen Ellberg 9-Sep-08 2:54
bethany 24-Jul-08 2:22
moserwi 9-Jun-08 7:52
xavier.fischer 4-Jul-08 11:27
Claudio Nicora 3-Jun-09 7:03
bethany 22-Apr-08 5:03
Derek Bartram 19-Apr-08 12:39
Last Visit: 12-Mar-14 9:37 Last Update: 12-Mar-14 19:32 Refresh 1 2 3 4 5 6 7 Next »
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
My vote of 5
My vote of 5
My vote of 5
Nice work!
Syncing audio and graphical display
Playback speed correction
Re: Playback speed correction
My vote of 2
My vote of 2
Windows Mobile Sound Record
Problems blocking other audio output?
sound from microphone
Is it possoble to play audio in any Sample
rate?
little error
Re: little error
Can't download
Listener
Playing once
Re: Playing once
Re: Playing once (little fix)
end of recording
Program Errors on play

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close