简体   繁体   English

将wave转换为字节数组

[英]convert wave to byte array

I'm new in c# and i want to send voice over ip but I have a problem here. 我是C#的新手,我想通过ip发送语音,但是我在这里遇到了问题。

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 现在可以将wave转换为byte

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 所以我使用NAudio.dll录制音频,这是代码

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转换为数组字节,以便发送

 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 . 使用BufferedWaveProvider初始化WaveOut实例。 This will allow you to add blocks of samples to be played by writing them to the BufferedWaveProvider , like this: 这将允许您通过将样本块写入BufferedWaveProvider来添加要播放的样本块,如下所示:

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. 因此,在从远程发送器获取样本的网络接收器代码中,将缓冲区(当然是在解码后)添加到BufferedWaveProvider ,它将开始播放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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