Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Problem Converting To GSM610

$
0
0

Yes, I'm using the Gsm610WaveFormat class and I've already ensured the PCM is at 8kHz.

I've also tried generating the PCM file myself so that I know it's at the correct sample rate.  Should the following work?

 

string pcmFilepath = @"D:\Temp\GeneratedPCM.wav";
string gsmFilepath = @"D:\Temp\ConvertedGSM.wav";

int sampleRate = 8000;
WaveFormat pcmFormat = new WaveFormat(sampleRate, 16, 1);

// Generate 8 kHz PCM audio:
using (WaveFileWriter pcmWriter = new WaveFileWriter(pcmFilepath, pcmFormat))
{
   int frequency = 440;
   short amplitude = 10000;
   int duration = 2;
   double omega = frequency * 2 * Math.PI / sampleRate;

   short[] buffer = new short[100];
   int bufferPos = 0;

   for (int i = 0; i < sampleRate * duration; i++)
   {
       short sample = (short)(amplitude * Math.Sin((double)i * omega));
       buffer.SetValue(sample, bufferPos);
       bufferPos++;

       if (bufferPos == buffer.Length)
       {
           pcmWriter.WriteSamples(buffer, 0, buffer.Length);
           bufferPos = 0;
       }
   }
}

// Convert to GSM:
using (WaveFileReader reader = new WaveFileReader(pcmFilepath))
{
   using (WaveFormatConversionStream converter = new WaveFormatConversionStream(new Gsm610WaveFormat(), reader))
   {
       WaveFileWriter.CreateWaveFile(gsmFilepath, converter);
   }
}

Thanks for helping by the way!

Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>