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

Producing poor quality sound

Nov 27, 2014 at 12:45 PM
Hi Mark. I am looking for advice from you. Please review my code. Below is WaveinEvent
  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 byteStream = new MemoryStream(converted);
                             WaveFormat waveFormat = new  WaveFormat(8000,16,1);
                                           RawSourceWaveStream rawSourceWaveStream = new RawSourceWaveStream(byteStream, waveFormat);

                                                         int audioBufferSize = 320;
                                                          int offset = 0;

                                                          byte[] buffer2 = new byte[320];

                                                          while (rawSourceWaveStream.Read(buffer2, offset, audioBufferSize - offset) > 0)

                                                                  MemoryStream byteStream133 = new MemoryStream(buffer2);
                                                                  WaveFormat waveFormat33 = WaveFormat.CreateMuLawFormat(32000, 1);
                                                                  RawSourceWaveStream rawSourceWaveStream133 = new RawSourceWaveStream(byteStream133, waveFormat33);
                                                                  WaveStream pcmStream133 = WaveFormatConversionStream.CreatePcmStream(rawSourceWaveStream133);
                                                                  byte[] decoded133 = ConvertNonSeekableStreamToByteArray(pcmStream133);

                                                                  bufferedWaveProvider.AddSamples(decoded133, 0, decoded133.Length);
                                                                  // cleanup

                                                                   MemoryStream byteStream1 = new MemoryStream(converted);


Please Note that i to improve the quality the changes can be made after below mentioned line in above given code:
_ Buffer.BlockCopy(resampleStream.DestBuffer, 0, converted, 0, convertedBytes);_

Thanks in advance.
Nov 28, 2014 at 12:03 PM
No one is here to help me out ? I tried different formats and different waveformat but none of them worked for me. :(

Thanks in advance.
Nov 28, 2014 at 5:07 PM
What is resampleStream? And what is this code actually trying to achieve? It isn't obvious to me what exactly you are trying to do here.
Nov 29, 2014 at 6:08 AM
Thanks for the reply mark.In first I was trying to write byte array to serial port instead of

bufferedWaveProvider.AddSamples(decoded133, 0, decoded133.Length);

line in above code. But i was not hearing clear voice on other side of the phone line so i first tried to listen this on my computer so used mentioned line in my code.
But it is playing same distorted sounds as playing on other side of the line while i write the byte array to serial port.

resampleStream is
var resampleStream = new AcmStream(new WaveFormat(8000, 16, 1),  WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm   , 8000, 1,8000 * 1, 1, 8));
Thanks in advance.
Nov 29, 2014 at 8:25 AM
But what wave format is the device at the other end of the serial port actually expecting. Is it really 8 bit linear PCM? Or could it be mu or a -law?

To troubleshoot issues like this I recommend taking a divide and conquer approach. First, instead of sending your audio to the serial port, write it to a WAV file. Listen to the WAV file in Windows Media Player. Does it sound distorted or OK?

Then create a simple voice recording at the format you plan to send to the serial port. Play that through the serial port by sending it's data a few hundred milliseconds at a time. Does that sound OK?

By answering these questions you will discover which part of the overall system isn't working correctly.
Marked as answer by Rakeshkaumr1684 on 11/30/2014 at 10:34 PM
Nov 29, 2014 at 9:42 AM
Yes , By changing it to muLaw i can hear voice clearly but there is a background click voice after every second. What changes should be done to overcome this problem.
Marked as answer by Rakeshkaumr1684 on 11/29/2014 at 3:38 AM