Click here to Skip to main content
Click here to Skip to main content

Sending and playing microphone audio over network

By , 3 Aug 2007
 

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.
    /// <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

About the Author

Ivar Lumi

Estonia Estonia
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDevice disconnect = memory leak and crashmemberLuis Miguel Jacobetty Soares11-Dec-12 11:56 
Hi!
 
I'm using your wonderful solution in an XNA project (but using Lidgren for network sending and receiving).
 
I have a problem regarding a memory leak that occurs when a device is disconnected. Currently, the only way to know if any devices changed is to check the Devices array for any changes in Length. I also reset every WaveIn and WaveOut (basically calling .Dispose()), yet memory consumption rises well above 500MB (record was 1.2GB), and the game becomes really slow.
 
Do you have any solution for this problem?
 
Best regards,
Luis.
QuestionMemory leaksmemberallen3311-May-12 17:51 
Hi, DO you know of any memory leaks in your code?
 
I get a OutOfMemoryException in my code and I'm not sure why?
AnswerRe: Memory leaksmemberIvar Lumi1-May-12 18:49 
Hi,
 
You shoud diable some code blocka nas see if memory usage increases, while find palce whats affects it. Also another way is to use some .NET profiling tool, which shows objects consuming memory.
GeneralRe: Memory leaksmemberMember 77299501-Aug-12 1:15 
The problem is maybe a lack of the "using" keyword and disposing streams? I haven't taken a look at the code, but it would be my guess.
QuestionExample Panasonic TDA100D PBX and I want to get the calls recordsmembermedop8214-Dec-11 0:18 
Hi, I have a Panasonic TDA100D PBX for our phone exchnage. It has Serial and USA connection. I have already tried the Serial and I am ble to get the calls records as it will automatically throw it
đ

QuestionPlease help me? About get CDR from PBX?membermedop8214-Dec-11 0:13 
Dear All and Ivar Lumi!
Dear All and Ivar Lumi!
Current i have a problem about read files cdr in PBX over serial port (COM). So that i need to help of everybody anh Ivar Lumi. if everybody send for me the example about it
.
Thanks you very much!
đ

QuestionStream to two clientsmemberallen33129-Nov-11 17:37 
Is it possible using your library to stream to multiple clients.
 
Like you want to after speaking to a microphone send that audio to multiple listeners?
AnswerRe: Stream to two clientsmemberIvar Lumi29-Nov-11 19:58 
>Is it possible using your library to stream to multiple clients.
Yes you can steream to multiple endpoints.
 
also you can receive audio from multiple endpoints, but audio mixing is not implemented.
Questionnewbie questionmemberMember 79558493-Sep-11 19:43 
First of all, thank you Mr. Ivan for such a good code.
I have used your library + examples in my final year project, but I want to know,
for the beginning, I just want to record and play the sound only in 1 computer, but I got something wrong, the speaker didn't play the sound that captured from the mic. It was only produced sound like an impulse, only "bip bip bip".
AnswerRe: newbie questionmemberIvar Lumi3-Sep-11 19:45 
Hi,
 
Normally this happens when you record one hz/bits and paly on another hz/bits ... .
GeneralRe: newbie questionmemberMember 79558494-Sep-11 6:09 
hmm.. where can I checked the hz/bit from the code?
GeneralRe: newbie questionmemberIvar Lumi4-Sep-11 18:57 
In code you alwyas set hz/bit for audio recorder class and player, otherwise you nver can play raw audio data.
GeneralRe: newbie questionmemberMember 79558494-Sep-11 19:20 
These ones did you mean?
m_pWaveIn = new WaveIn(WaveIn.Devices[0], 8000, 16, 1, 400);
m_pWaveOut = new WaveOut(WaveOut.Devices[0], 8000, 16, 1);

GeneralRe: newbie questionmemberIvar Lumi4-Sep-11 19:21 
Yes.
GeneralRe: newbie questionmemberMember 79558494-Sep-11 19:28 
hmm.. I have set the hz/bit for both recorder and player in the same value, but the sound didn't play correctly, maybe do you have any experience for my problem?
GeneralRe: newbie questionmemberIvar Lumi4-Sep-11 19:37 
Then there can be only timing issues. You don't play audio samples with right rate(ms).
GeneralRe: newbie questionmemberMember 795584916-Sep-11 1:24 
sorry to bother you again Mr. Ivan. But I want to know, why there are so much noise? The noise almost covered all the sound. How to clear the noise or at least, the noise is much more quiet than my voice..
GeneralRe: newbie questionmemberIvar Lumi16-Sep-11 1:29 
If you are sending through netowrk, fisrt you may try send locally. This shows if network delay causes it.
GeneralRe: newbie questionmemberMember 795584916-Sep-11 1:38 
Actually, I just use this program to receive the sound from the mic and directly play the recorded sound in the same computer. If I use your program with using the same ip address for sender and receiver, the result is still the same. there are so much noise.
GeneralRe: newbie questionmemberIvar Lumi16-Sep-11 1:40 
You may try my another project if it does same ...
Simple SIP (VOIP) based phone in C#[^]
GeneralRe: newbie questionmemberMember 795584916-Sep-11 4:24 
hmm.. still the same. in the output the sound that heard was like a robot.
GeneralDelay in Audiomemberallen33117-Apr-11 18:04 
Hi,
 
When I am using this code to encode/decode audio,
 
Sometimes there is significant delay in the audio reaching the listener.
 
For eg, someone says something on a microphone and it takes maybe 10 seconds for the listener at the other end to hear it...
 
Maybe its a network issue and there is nothing I can do about it but I was wondering if there was anything I could do to make this delay less.
 
Thanks
GeneralRe: Delay in AudiomemberIvar Lumi17-Apr-11 19:01 
Hi,
 
You should try RTP audio instead:
http://www.lumisoft.ee/lswww/download/downloads/Examples[^]
GeneralRe: Delay in AudiomemberZypper22-Jan-12 3:30 
This delay problem seems also be in RTP audio.
 
I have tested both versions WavePlayer and RTP Audio over a VPN with a roundtrip of about 42mS
and the delay is increasing over time, if i am connected for about 1 hour the delay can be up to 1.5 sek
sometime i have had several seconds in delay.
 
I have another voice application named IPSOUND that works OK, no delay what so ever, but that software is out of support and upgrades.
 
I had thoughts about making a new VOIP similar to IPSOUND that uses RTP audio, but first i have to sort out this delay problem.
 
Have you Ivar noticed any of this problem ?
 
Regards
Pete
GeneralRe: Delay in AudiomemberIvar Lumi22-Jan-12 6:14 
Hi,
 

>I had thoughts about making a new VOIP similar to IPSOUND
You can try:
Simple SIP (VOIP) based phone in C#[^]
 
You can use local IP instead of domain, you can call 1 phone to another.
Though you need 2 computers for that.
See if it works ok.
 
For WavePlayer and rtp audio, first you need to sort out if mic or play out causing delay.
There are no adaptive sound buffering, so i can't see how delay increases ... .

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 3 Aug 2007
Article Copyright 2007 by Ivar Lumi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid