简体   繁体   中英

convert wave to byte array

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM