0

I'm new in c# and i want to send voice over ip but I have a problem here.

private Byte[] arr;

private void send()
{
     arr = File.ReadAllBytes("wave path");
     con = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

     // ...

     con.Send(arr, 0, arr.length, 0);
     con.Close();
}

Now its ok i can convert wave to byte by

File.ReadAllBytes("wave path");

But actually I want to send wave from microphone Directly (not from wave File)

So I use NAudio.dll for record audio and this is the code

private void button2_Click(object sender, EventArgs e)
{
    int devicenum = 0;
    sourcestream = new NAudio.Wave.WaveIn();
    sourcestream.DeviceNumber = devicenum;
    sourcestream.WaveFormat = new NAudio.Wave.WaveFormat(4410, NAudio.Wave.WaveIn.GetCapabilities(devicenum).Channels);
    NAudio.Wave.WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(sourcestream);

    waveOut = new NAudio.Wave.DirectSoundOut();
    waveOut.Init(waveIn);
    sourcestream.StartRecording();
    waveOut.Play();
}

and i test it its play audio from microphone

how can i convert waveOut to array byte so i can send it

 waveOut.Play();

I found some videos write audio to wave file and again read byte from file. Any way to convert audio Directly to array byte?

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • 1
    Possible duplicate of [How to write NAudio WaveStream to a Memory Stream?](http://stackoverflow.com/questions/11500222/how-to-write-naudio-wavestream-to-a-memory-stream) – Backs Oct 04 '15 at 02:08
  • i see it new Mp3FileReader ? i dont have mp3 –  Oct 04 '15 at 02:10

1 Answers1

0

Initialize the WaveOut instance with a BufferedWaveProvider. This will allow you to add blocks of samples to be played by writing them to the BufferedWaveProvider, like this:

waveOut = new WaveOut())
var provider = new BufferedWaveProvider(waveFormat);
waveOut.Init(provider);
waveOut.Play();

provider.AddSamples(sampleBuffer, 0, sampleBuffer.Length);

So in your network receiver code that is getting the samples from the remote sender you add the buffers (after decoding, of course) into the BufferedWaveProvider and it will play.

Corey
  • 15,524
  • 2
  • 35
  • 68
  • can i Send it as array byte with socket ? –  Oct 05 '15 at 15:58
  • Sockets send nothing _but_ arrays of bytes, but you have no guarantee that they won't get broken up into smaller chunks or put together into larger ones. You'll probably want to look into UDP sockets for VOIP work. – Corey Oct 05 '15 at 17:51
  • that is my problem how to convert the audio to array of bytes for send it –  Oct 05 '15 at 19:08
  • Oh... that's what you get out of the WaveIn object while recording. It gives you sets of samples as byte arrays. – Corey Oct 05 '15 at 22:12
  • waveOut ? how can i send it by socket ? –  Oct 05 '15 at 22:15
  • 1) You don't send 'waveOut' by socket, you feed sample data into it. 2) You will have to find the best option for networking yourself. I've already suggested you look into UDP. There are plenty of good resources on how these things works, including here on SO. – Corey Oct 06 '15 at 01:30