Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#
Article

Sending and playing microphone audio over network

Rate me:
Please Sign up or sign in to vote.
4.92/5 (65 votes)
3 Aug 20072 min read 525.2K   42.9K   336   172
Sending and playing microphone audio over network

Screenshot - view.jpg

Introduction

This example shows you how to receive data from a microphone and stream it over UDP to another computer. The example application can act like a direct phone, if both endpoints listen for data and send microphone data to each other. One would probably suspect that no source code exists for that, but of course it does. I hate those who will do commercial advertising. There is also a second related project what will contain a UDP server that we need to send/receive audio and compress it with g711 codec.Though only UDP is not the best way to transport audio data, RTP is the right way to go. RTP adds quality of service to transported audio, you can see how many packets are lost and can even arrange disordered packets. I will try to add an RTP example soon, so be calm, it's under way. There are some similar example applications, but most of them aren't well commented and missing some parts, so I will try to fill this part.

The package contains:

  • LumiSoft.Media - Audio related API (Included in example app)
  • LumiSoft.Net - UDP server, G711 codec
  • Example Application

Using the code

  • WaveIn - class provides a simple way to receive audio from a microphone.
    Actually all what you need to do is:
    WavIn.Devices - returns all available input devices from where we can get data.
    C#
    /// <summary>
    /// Application main class.
    /// </summary>
    public class Test
    {
        private WavIn m_pSoundReceiver = null;
    
        /// <summary>
        /// Default constructor.
        /// </summary>
        public Test()
        {
            // G711 needs 8KHZ 16 bit 1 channel audio, 
            // 400kb buffer gives us 25ms audio frame.
            m_pSoundReceiver = new WavIn(WavIn.Devices[0],8000,16,1,400);
            m_pSoundReceiver.BufferFull += new BufferFullHandler 
                                             (m_pSoundReceiver_BufferFull);
            m_pSoundReceiver.Start();
        }
    
        /// <summary>
        /// This method is called when recording buffer is full 
        /// and we need to process it.
        /// </summary>
        /// <param name="buffer">Recorded data.</param>
        private void m_pSoundReceiver_BufferFull(byte[] buffer)
        {
            // Just store audio data or stream it over the network ... 
        }
    }

  • WaveOut - class provides methods for playing out streaming data.
    The only thing you need to do is just call waveoutInstance.Play method.
    In my opinion, the whole example application has been coded well enough, so dig into the code.

    Note: Sound quality depends on network delay jittering, if there will be too big a variance in delays, voice will have holes in it. In addition, UDP packet loss and disordered packets will affect it too.

History

  • 03.08.2006 - Initial release

Links

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


Written By
Estonia Estonia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: audio transfer and bad audio quality on other end Pin
Khurram Shakoor8-Jun-09 18:21
Khurram Shakoor8-Jun-09 18:21 
GeneralEcho Problem Pin
Khurram Shakoor11-May-09 23:39
Khurram Shakoor11-May-09 23:39 
GeneralRe: Echo Problem Pin
Ivar Lumi11-May-09 23:41
Ivar Lumi11-May-09 23:41 
GeneralRe: Echo Problem Pin
Khurram Shakoor12-May-09 20:23
Khurram Shakoor12-May-09 20:23 
GeneralRe: Echo Problem Pin
Khurram Shakoor12-May-09 20:51
Khurram Shakoor12-May-09 20:51 
GeneralMy vote of 1 Pin
hitesh_parashar4-Dec-08 4:45
hitesh_parashar4-Dec-08 4:45 
QuestionLicense Pin
evgeniyn29-Oct-08 23:13
evgeniyn29-Oct-08 23:13 
AnswerRe: License Pin
Ivar Lumi30-Oct-08 5:27
Ivar Lumi30-Oct-08 5:27 
Hi,

(license blelow: the main purpose of it is to protect my work, while allow evry one to use it free)

General usage terms:

*) If you use/redistribute compiled binary, there are no restrictions.
You can use it in any project, commercial and no-commercial.

*) It's allowed to complile source code parts to your application,
but then you may not rename class names and namespaces.

*) Anything is possible, if special agreement between LumiSoft.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
GeneralRe: License Pin
evgeniyn30-Oct-08 12:00
evgeniyn30-Oct-08 12:00 
GeneralRe: License Pin
Brendan Tompkins12-Jun-09 16:14
Brendan Tompkins12-Jun-09 16:14 
QuestionRTP implementation? Pin
Markus Gruber16-Jul-08 0:55
Markus Gruber16-Jul-08 0:55 
AnswerRe: RTP implementation? Pin
Ivar Lumi16-Jul-08 2:33
Ivar Lumi16-Jul-08 2:33 
GeneralRe: RTP implementation? Pin
naffets25-Sep-08 23:07
naffets25-Sep-08 23:07 
GeneralRe: RTP implementation? Pin
Ivar Lumi25-Sep-08 23:27
Ivar Lumi25-Sep-08 23:27 
GeneralRe: RTP implementation? [modified] Pin
naffets28-Sep-08 21:41
naffets28-Sep-08 21:41 
GeneralRe: RTP implementation? Pin
Ivar Lumi28-Sep-08 22:38
Ivar Lumi28-Sep-08 22:38 
GeneralRe: RTP implementation? [modified] Pin
naffets31-Oct-08 1:13
naffets31-Oct-08 1:13 
GeneralRe: RTP implementation? Pin
Ivar Lumi31-Oct-08 3:23
Ivar Lumi31-Oct-08 3:23 
QuestionRecord Microphone input Pin
pankaj motiyar15-Jul-08 2:00
pankaj motiyar15-Jul-08 2:00 
AnswerRe: Record Microphone input Pin
Ivar Lumi16-Jul-08 2:28
Ivar Lumi16-Jul-08 2:28 
QuestionRe: Record Microphone input Pin
pankaj motiyar17-Jul-08 19:34
pankaj motiyar17-Jul-08 19:34 
AnswerRe: Record Microphone input Pin
Ivar Lumi17-Jul-08 19:46
Ivar Lumi17-Jul-08 19:46 
QuestionRe: Record The Voice conversation done on Mobile phone Pin
pankaj motiyar18-Jul-08 2:36
pankaj motiyar18-Jul-08 2:36 
AnswerRe: Record The Voice conversation done on Mobile phone Pin
Ivar Lumi18-Jul-08 4:59
Ivar Lumi18-Jul-08 4:59 
GeneralRe: Record The Voice conversation done on Mobile phone Pin
pankaj motiyar20-Jul-08 18:30
pankaj motiyar20-Jul-08 18:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.