This project has moved and is read-only. For the latest updates, please go here.

Distortion in sound writing to serial port

Nov 23, 2014 at 1:00 PM
Dear Sir
i am wring data from WaveinEvent eventhandler to serial COM port of GSM Modem. But I am hearing distorted sound on other side. Following is my code:

private void button1_Click(object sender, EventArgs e)

        Talking = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);

        wi = new WaveInEvent  ();
       wi.WaveFormat = new WaveFormat(44100, 16, 1);
        int waveInDevices = WaveIn.DeviceCount;
        for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
            WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);


        // wi.BufferMillisconds = 05;

        wi.DeviceNumber = 0;

        int waveInDeviceNumber = 0;

           wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);


void wi_DataAvailable(object sender, WaveInEventArgs e)
byte[] source = e.Buffer;
        Stream  byteStream12 = new MemoryStream(source);
      byte []  bt  = ConvertNonSeekableStreamToByteArray(byteStream12);
      Buffer.BlockCopy(bt, 0, resampleStream.SourceBuffer, 0, bt.Length);

        int sourceBytesConverted = 0;
        var convertedBytes = resampleStream.Convert(bt.Length, out sourceBytesConverted);
        if (sourceBytesConverted != bt.Length)
            MessageBox.Show("We didn't convert everything {0} bytes in, {1} bytes converted");

        var converted = new byte[convertedBytes];

        Buffer.BlockCopy(resampleStream.DestBuffer, 0, converted, 0, convertedBytes);

        MemoryStream byteStream1 = new MemoryStream(converted );

        while (byteStream1.Read(buffer2, offset, audioBufferSize) > 0)
            Talking.Write(buffer2, offset, audioBufferSize);

static byte[] ConvertNonSeekableStreamToByteArray(Stream NonSeekableStream)
        MemoryStream ms = new MemoryStream();
        byte[] buffer = new byte[1024];
        int bytes;

        while ((bytes = NonSeekableStream.Read(buffer, 0, buffer.Length)) > 0)
            ms.Write(buffer, 0, bytes);
        byte[] output = ms.ToArray();
        return output;

Please help me. I shall be thankful to you.

Regards Rakesh Kumar
Nov 26, 2014 at 9:22 AM
Please Mark help me.

and this is my declaration part
  SerialPort Talking;
  private BufferedWaveProvider bwp;
    public WaveInEvent wi;
   private WaveIn recorder;
    private BufferedWaveProvider bufferedWaveProvider;
    AcmStream resampleStream = new AcmStream(new WaveFormat(44100, 16, 2), new WaveFormat(8000, 8, 2));
    private WaveOut player;

I am looking for positive reply

With Regards
Rakesh Kumar
Nov 28, 2014 at 5:16 PM
You can't convert from 44.1kHz 16 bit stereo to 8kHz 8bit stereo in one hit. You need to resample first, then go 16 bit to 8 bit. Also, you are recording in mono, so why is the resampler operating in stereo?
Nov 29, 2014 at 5:35 AM
Edited Nov 29, 2014 at 5:49 AM
Thanks for the reply Mark. I thought that 44.1kHz 16 bit stereo is produced by Microphone so i was using here. But as per your direction i changed it to

var resampleStream = new AcmStream(new WaveFormat(8000, 16, 1), WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm , 8000, 1,8000 * 1, 1, 8));
and set
wi.BufferMilliseconds =20;

There is improvement in sound quality but i cant hear clearly on the other side of phone. I tried everything, and i dont know what i have to change to hear the clear voice.

Please provide me solution, because i have tried everything, and according to me i have no solution for this problem.

Thanks in advance
Nov 29, 2014 at 8:31 AM
Make sure that your recording format is definitely set to 8kHz 16 bit mono. What is WaveFormat of WaveInEvent?
Marked as answer by Rakeshkaumr1684 on 11/30/2014 at 10:34 PM
Nov 29, 2014 at 9:26 AM
wave format is
wi.WaveFormat = new WaveFormat(8000,16,1);
Nov 29, 2014 at 9:32 AM
and what input format is your modem expecting? Have you tried creating a WAV file at that WaveFormat and just sending that?
Nov 29, 2014 at 9:44 AM
Edited Nov 29, 2014 at 11:37 AM
input waveformat expected is 8k,16 bit, mono expected by modem and perhaps same by GSM Network.
Mu law wave format giving clear voice but with a click sound after every second.
Thanks alot Mark. It works like charm. You are like life saver for me. :)
Marked as answer by Rakeshkaumr1684 on 11/29/2014 at 3:37 AM