简体   繁体   中英

make a voice call c# using at command and NAudio lib

I am trying to making a call using AT command and Naudio LIB to send and receive a voice to the modem and the incoming voice to the modem working fine but outgoing voice is muddled(noisy)

Any help is appreciated.

Here is the code:

 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 
    }

You're sending to yourself, correct? If so, how have you determined that incoming voice is fine, but outgoing is muddy? Wouldn't it all be muddy?

This is basically just a streaming data application, and the data just happens to be PCM audio. The fact that it's a serial port isn't really relevant (except to ensure that you have enough bps to support the audio rate).

Here's something I noticed. On the microphone input side, you have:

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

However, on the speaker output side, you have:

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

...

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

    waveOut.Play();

    ++i;
}

First point: You should initialize the BufferedWaveProvider with the same WaveFormat you're using for the input.

Second point: Move the `waveOut.Play()' into the block above it, as follows:

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

    ++i;

The way your code currently sits, you're calling waveOut.Play() every time you get new data on the serial port. That's not going to work very well!

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