Click here to Skip to main content
15,861,168 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 518.6K   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

 
QuestionActually send and receive? Pin
Member 1568715225-Jun-22 15:30
Member 1568715225-Jun-22 15:30 
QuestionIs it possible to play a *.wav file? Pin
Rafael C A Ribeiro1-Jul-18 20:53
Rafael C A Ribeiro1-Jul-18 20:53 
Questionhow to get audio to another computer ? Pin
Member 1370618123-Mar-18 23:05
Member 1370618123-Mar-18 23:05 
Questiongood app Pin
Member 1370618123-Mar-18 4:35
Member 1370618123-Mar-18 4:35 
Questionandroid support Pin
sirboy14-Nov-17 20:00
sirboy14-Nov-17 20:00 
QuestionIP Broadcast Pin
Member 1221253717-Aug-17 10:21
Member 1221253717-Aug-17 10:21 
AnswerRe: IP Broadcast Pin
overwraith26-Oct-18 5:35
overwraith26-Oct-18 5:35 
AnswerRe: IP Broadcast Pin
overwraith26-Oct-18 12:18
overwraith26-Oct-18 12:18 
QuestionAudio Compression / Noise Cancelling Pin
Member 125653796-Jun-16 5:00
Member 125653796-Jun-16 5:00 
QuestionSum sound on WavePlayer Pin
jack_j2330-Oct-15 17:27
jack_j2330-Oct-15 17:27 
QuestionCan I Use This Code In Web Application ? Pin
amr ismail24-Mar-15 22:10
amr ismail24-Mar-15 22:10 
AnswerRe: Can I Use This Code In Web Application ? Pin
overwraith26-Oct-18 5:43
overwraith26-Oct-18 5:43 
QuestionWant Same VB.Net code Pin
adil mazher22-Feb-15 7:23
adil mazher22-Feb-15 7:23 
Questionhow to get a stream out of it..?? Pin
Mohammed Owais15-Feb-15 2:48
professionalMohammed Owais15-Feb-15 2:48 
QuestionUsing for Commercial Use Pin
Member 111262592-Oct-14 9:28
Member 111262592-Oct-14 9:28 
AnswerRe: Using for Commercial Use Pin
Ivar Lumi2-Oct-14 19:11
Ivar Lumi2-Oct-14 19:11 
GeneralRe: Using for Commercial Use Pin
Member 111262593-Oct-14 10:10
Member 111262593-Oct-14 10:10 
QuestionGreat program! Audio issues however Pin
Blazing Ardor8-Aug-14 4:59
Blazing Ardor8-Aug-14 4:59 
AnswerRe: Great program! Audio issues however Pin
Blazing Ardor8-Aug-14 7:28
Blazing Ardor8-Aug-14 7:28 
GeneralYou are great! Pin
Zain Ul Abidin22-Jul-14 18:55
Zain Ul Abidin22-Jul-14 18:55 
QuestionNewbie Help Pin
Member 109132312-Jul-14 12:13
Member 109132312-Jul-14 12:13 
AnswerRe: Newbie Help Pin
Member 109132313-Jul-14 11:39
Member 109132313-Jul-14 11:39 
QuestionSend voice over the global net Pin
modarD1-Jul-14 20:55
modarD1-Jul-14 20:55 
AnswerRe: Send voice over the global net Pin
Ivar Lumi2-Jul-14 5:09
Ivar Lumi2-Jul-14 5:09 
GeneralRe: Send voice over the global net Pin
modarD2-Jul-14 6:58
modarD2-Jul-14 6:58 
Thank you for your response, I had the chance to test the demo with one of my friends (I didn't start my Mic), I received only one packet and 200 Bytes and nothing more, I checked my router log and it always shows this when my friend tries to connect to me :

07/03/2014 02:39:14 **UDP Flood to Host** 31.9.55.19, 11000->> 192.168.2.104, 11000 (from PPPoE1 Inbound)

as 31.9.55.19 is my fiend's IP

I think this eliminate the loopback factor, any more Ideas why it's not working over WAN?

thx

modified 2-Jul-14 13:15pm.

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.