3/13/2014
A l ow- l evel audi o player i n C# - C odePr oj ect 10,458,738 members (61,465 online)
h ome
articles
quick answers
di scussions
Me mb mb er er 1 06 06 52 52 04 04 5
features
Sign out
309
community Search for articles, questions, tips
help Articles » Multimedia » Audio and Video » Audio
Article
Browse Code Bugs / Suggestions Stats
Next
A low-level audio player in C# C#
About Article
Munoz, 27 Aug 2003 By Ianier Munoz,
This article describes a sample application that uses the waveout API in C#.
Rate: 4.84 (80 votes)
Revisions
Tweet
1
3
0
Alternatives Com ments & Comments Discussions (164)
Type
Article
Licence
Download source files - 12 Kb
Add your own own alternative alter native version version
First Posted
11 Dec 2002
Views
659,433
Bookmarked
229 times
. NE NET 1. 1. 0 Win2K WinXP C # Devv In De Inte terme rmedi diat ate e
Introduction It is no news t hat the .NET framework does does not includ include e any classes for dealing with sound. Some people have have worked arou around nd this lilimitation mitation by by wrapping high-level system components, such as Windowss Media Window M edia Player or DirectShow, into .NET-friendly libraries. libraries. However, m ost of these libraries are designed designed in such a way that they cannot be used to manipulate audio audio samples on the fly because they because they do not give you access access to the actual sound data. When developing developing applications that deal deal with such lo issues es,, the most commonly used low-level w-level issu technologies are DirectSound and the waveout API. This article describes a sample application that uses the waveout API in C# through I nterop to play a WAV file in a continuous loop.
Using the code
Top News News on the future of C# as a language Get the Insider News free News free each morning.
Related Videos
Most of the work in the sample application application is carried out by two cla classes: sses: WaveStream and WaveOutPlayer . The WaveStream class extends System.IO.Stream and implements the n ecessary code code to read audio samples from a WAV file. The constructor reads the file header and extracts all the relevant information including including the actual length of t he stream and the format of the audio samples, which Format property. 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 t hat don't belong to the audio data chunk. Seeking Seeking to 0 will cause the next read operation operation to start at the beginning of the audio data. The WaveOutPlayer class is where the most interesting things happen. F or the s ake of simplicity, simplicity, the interface of this class has been reduced to the strict minimum. There are no Start, Stop or Pause methods. Cr eating an instance of WaveOutPlayer will cause cause the syst em to start streaming immediately. Let's have a look at the code that creates the WaveOutPlayer instance. As you can see, the http://www.codepr oj ect.com/Ar ti cl es/3352/A- l ow- l evel - audi o- pl ayer - i n- C
Related Articles A full-duplex audio player in C# using the waveIn/waveOut APIs AMR Audio Encoding
1/4
3/13/2014
A low-level audio player in C# - CodeProject Programming Audio Effects in C#
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)); } }
C Sharp Ripper Low-Level Control of *.wav Data (Part I) 3 Breakthrough Ways to Visualize HTML5 Play or C apture Audio Sound. Send and Receive as Multicast (RTP) C# MP3 C ompressor
The first parameter is the ID of the wave device that you want to use. The value -1 represents the default syst em device, but if your sy stem has more t han one sound card, then you can pass any number from 0 to the number of installed sound cards minus one to s elect a particular device. The second parameter is the format of t he audio samples. In this example, the format is taken directly from th e wave stream. The third and forth parameters are t he size of the internal wave buffers and t he number of buffers to allocate. You should s et these to reasonable values. Smaller buffers will give you less latency, but the audio may stut ter 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 t hat you can feed them with new sound data. I n 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; // loo p if the fil e e nds pos += got; } } else { for (int i = 0; i < b.Length; i++) b[i] = 0; } System.Runtime.InteropServices.Marshal.Copy(b, 0, data, size); }
Open Audio Engine Audio-Gallery-Suite (A complete audio gallery solution made with HTML5/CSS3/jQueryJS/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 HT ML5 Audio C# Windows M edia Format SDK Translation C# MP3 C ompressor
Related Research
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.
Fine-Tuning the Engines of SMB Growth: 4 strategies for growing your business
To stop playing, just call Dispose on t he player object, like this: Collapse | Copy Code
private void Stop() { if (m_Player != null) try { m_Player.Dispose(); } finally { m_Player = null; } }
Protecting Your Business Data: Five Do’s and Don’ts
Conclusion http://www.codeproject.com/Articles/3352/A-low-level-audio-player-in-C
2/4
3/13/2014
A low-level audio player in C# - CodeProject
This sample demonstrates how t o 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 D irectX 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. I f in doubt please contact the author via the discussion board below. A list of licenses authors might us e can be found here
About the Author Ianier Munoz Web Developer Luxembourg
Ianier Munoz lives in F rance 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 P ro-Mix, Chronotron and Adapt-X. Article Top
Comments and Discussions Add a Comment or Question Search this forum
Profile popups Spacing Relaxed
Noise Medium
Layout Normal
Go
Per page 25 Update
First Prev Next
The code doesn't work! Re: The code doesn't work! Re: The code doesn't work! http://www.codeproject.com/Articles/3352/A-low-level-audio-player-in-C
Member 10217574
22-Aug-13 4:07
Member 10238076
28-Aug-13 15:40
Efi Hoory
29-Aug-13 3:14
3/4
3/13/2014
A low-level audio player in C# - CodeProject Pham Anh Khoa
My vote of 5
7-Aug-13 4:31
My vote of 5
Raul Iloc
My vote of 5
Super_Marvin_97
Nice work!
aerjienong
12-Nov-12 12:20
Syncing audio and graphical display
Vitor Vilela
30-Jul-12 9:52
Playback speed correction
SergeyKS
Re: Playback speed correction
28-May-13 7:08 9-Mar-13 7:12
23-Apr-12 10:32
SergeyKS
24-Apr-12 5:07
My vote of 2
simonkakaonce
27-Mar-12 22:13
My vote of 2
duonmhMTA
19-Nov-11 22:08
Windows Mobile Sound Record
bayfatih2000
24-May-11 7:32
Problems blocking other audio output?
Member 4526326
sound from microphone
Benetz
Is it possoble to play audio in any Sample rate?
Soroush Safaei
little error
Member 4194084
4-Mar-09 22:17
zonksoft
12-Mar-09 12:15
Re: little error
27-Sep-10 17:26 8-Feb-10 5:01 19-Oct-09 13:59
Can't download
Jörgen Ellberg
9-Sep-08 2:54
Listener
bethany
24-Jul-08 2:22
Playing once
moserwi
9-Jun-08 7:52
Re: Playing once Re: Playing once (little fix)
xavier.fischer
4-Jul-08 11:27
Claudio Nicora
3-Jun-09 7:03
end of recording
bethany
Program Errors on play
Derek Bartram
Last Visit: 12-Mar-14 9:37 General
News
Last Update: 12-Mar-14 19:32 Suggestion
Question
Refresh Bug
Answer
22-Apr-08 5:03 19-Apr-08 12:39 1 2 3 4 5 6 7 Next »
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, C trl+Up/Down to switc h threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Web02 | 2.8.140309.2 | Last Updated 28 Aug 2003
http://www.codeproject.com/Articles/3352/A-low-level-audio-player-in-C
Layout: fixed | fluid
Article Copyright 2002 by Ianier Muno z Everything else Copyright © CodeProject, 1999-2014 Terms of Use
4/4