This project has moved. For the latest updates, please go here.

Getting realtime ASIO audio and sending over TCP

Sep 4, 2013 at 5:14 AM
Edited Sep 4, 2013 at 5:17 AM
Hello,

I am trying to get the ASIO audio input so that I can stream via TCP. I have it working well with the WaveIn, but I'm only receiving static on the TCP client when I try the same thing with ASIO.
 void asioOut_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
        {
            if (s == null)
            {
                s = myList.AcceptSocket();
            }
                
                var samples = e.GetAsInterleavedSamples();

//lame attempt to convert to byte array
            var byteArray = new byte[samples.Length * 4];
            Buffer.BlockCopy(samples, 0, byteArray, 0, byteArray.Length);
//////

            byte[] encoded = codec.Encode(byteArray, 0, byteArray.Length);
            s.Send(encoded, SocketFlags.None);


            writer.WriteSamples(samples, 0, samples.Length);
        }
I think my problem is 2 fold:
  • the AsioSampleType is being set as int32LSB - I believe I need this as a 16 bit file. Do I have to dither this on the fly before sending?
  • Also, the e.GetAsInterleavedSamples(); is returning as a float array. How I convert to byte array?
The WriteSample is writing the recording to a .wav file, so I know I have my input working.

Thanks! Having a lot of fun experimenting with this.

-Mike
Sep 5, 2013 at 3:15 AM
Edited Sep 5, 2013 at 5:31 AM
Did some more tests - I think something is slowing down the transmission of audio and causing the static sound.

If I comment out the section that transmits audio via TCP the recording sounds crystal clear. If I add it back in, the recording is recorded with static and my voice sounds slowed down.
 var byteArray = new byte[samples.Length * 4];
            Buffer.BlockCopy(samples, 0, byteArray, 0, byteArray.Length);

            byte[] encoded = codec.Encode(byteArray, 0, byteArray.Length);
            s.Send(encoded, SocketFlags.None);
I'm getting the audio on my remote computer but that is also slowed down quite a bit. My voice gets more and mroe spaced out the longer I run the app. Any recommendations for implementing this better?
Coordinator
Sep 9, 2013 at 2:06 PM
Sounds like you need to convert from float to 16 bit samples. Convert each sample by multiplying by 32767 and casting to short. Having made a short array, you can use Buffer.BlockCopy to copy into a byte array for sending. Or you can use NAudio's WaveBuffer which uses a clever trick to let you write into a short array and then send it as a byte array (although take care not to overwrite the array)/
Sep 10, 2013 at 4:13 AM
Thanks for your help Mark, I did manage to get this working by casting to short. I'll keep plugging away. I'm learning so much from this library - thanks!