Exemple #1
0
        void WriteFromWaveStream(string inputPath, string outputPath, WaveStream reader)
        {
            var waveFormat = reader.WaveFormat;
            int sampleRate = Math.Min(16000, waveFormat.SampleRate);

            using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream(reader))
            {
                WaveFormat newFormat = new WaveFormat(sampleRate, 8, 1);
                using (WaveStream eightBitPcmStream = new WaveFormatConversionStream(newFormat, pcmStream))
                {
                    CppWriter cppWriter = new CppWriter(Path.GetFileName(Path.GetFileNameWithoutExtension(inputPath)), outputPath);
                    cppWriter.Write(sampleRate);

                    List <byte> totalBytes = new List <byte>();
                    int         b          = eightBitPcmStream.ReadByte();
                    while (b != -1)
                    {
                        totalBytes.Add((byte)(b + 128));    // Make it signed 8-bit PCM, as this comes through as unsigned
                        b = eightBitPcmStream.ReadByte();
                    }

                    cppWriter.Write(totalBytes.Count);

                    foreach (byte data in totalBytes)
                    {
                        cppWriter.Write(data);
                    }

                    cppWriter.Finalise();
                    Console.WriteLine("Audio file \"" + Path.GetFullPath(outputPath) + "\" successfully converted");
                }
            }
        }