使用at命令和NAudio lib进行语音呼叫c#

时间:2016-04-03 21:30:53

标签: c# serial-port naudio at-command

我正在尝试使用AT命令和Naudio LIB进行呼叫,以便向调制解调器发送和接收语音,并且调制解调器的传入语音工作正常但传出语音混乱(嘈杂)

感谢任何帮助。

以下是代码:

 WaveIn waveSource;
    SerialPort portsend      = new SerialPort("COM20", 57600, Parity.None , 8 , StopBits.One); 
    SerialPort portReceived  = new SerialPort("COM19", 57600, Parity.None , 8 , StopBits.One);
    WaveOut waveOut = new WaveOut();
    WaveFormat format = new WaveFormat(8000, 16,1);

    // button call number
 private void button1_Click(object sender, EventArgs e)
        {
        waveSource = new WaveIn(this.Handle);
        waveSource.WaveFormat = new WaveFormat(8000,16, 1);
        waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);


        waveOut.DesiredLatency = 100;

        portReceived.Open();
        portsend.Open();
        portsend.ReadTimeout = SerialPort.InfiniteTimeout; 
        portsend.WriteLine("ATZ;" + Environment.NewLine);
        Thread.Sleep(1000);
        portsend.WriteLine("ATD01271698522;" + Environment.NewLine);
        portReceived.DataReceived += new SerialDataReceivedEventHandler(modemPort_DataReceived); // received the binary of voice come from the modem 
        Thread.Sleep(1000); 
        portsend.Write("AT^DDSETEX=2" + Environment.NewLine);
        waveSource.StartRecording();
    }

    int i = 0;
    byte[] buffer;
    private BufferedWaveProvider _bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(16000, 16,1));

    void modemPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs args) 
    {
        int x = portReceived.BytesToRead;
        buffer = new byte[x];
        portReceived.Read(buffer, 0, x);
        _bufferedWaveProvider.AddSamples(buffer, 0, x);

        if(i==0)
        {       
            waveOut.Init(_bufferedWaveProvider);
        }

        waveOut.Play();

        ++i;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        portsend.Close();
        portReceived.Close();


    }

    void waveSource_DataAvailable(object sender, WaveInEventArgs e)
    {
        portReceived.Write(e.Buffer, 0, e.BytesRecorded); // send the voice from mic to the modem 
    }

1 个答案:

答案 0 :(得分:1)

你发送给自己,对吗?如果是这样,你如何确定传入的声音是好的,但外向是泥泞的?这一切都不会浑浊吗?

这基本上只是一个流数据应用程序,数据恰好是PCM音频。它是一个串口并不是真正相关的事实(除了确保你有足够的bps来支持音频速率)。

这是我注意到的事情。在麦克风输入端,您有:

    waveSource = new WaveIn(this.Handle);
    waveSource.WaveFormat = new WaveFormat(8000,16, 1);
    waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);

但是,在扬声器输出端,您有:

private BufferedWaveProvider _bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(16000, 16,1));

...

    if(i==0)
    {       
        waveOut.Init(_bufferedWaveProvider);
    }

    waveOut.Play();

    ++i;
}

第一点:您应该使用您用于输入的BufferedWaveProvider初始化WaveFormat

第二点:移动`waveOut.Play()&#39;进入它上面的块,如下:

    if(i==0)
    {       
        waveOut.Init(_bufferedWaveProvider);
        waveOut.Play();
    }

    ++i;

您的代码目前所处的位置,您每次在串行端口上获取新数据时都会调用waveOut.Play() 。这不会很好用!