change wav file ( to 16KHz and 8bit ) with using NAudio

后端 未结 3 1665
忘了有多久
忘了有多久 2020-12-05 16:29

I want to change a WAV file to 8KHz and 8bit using NAudio.

            WaveFormat format1 = new WaveFormat(8000, 8, 1);
            byte[] waveByte = HelperC         


        
相关标签:
3条回答
  • 2020-12-05 17:13

    The following code solved my problem dealing with G.711 Mu-Law with a vox file extension to wav file. I kept getting a "No RIFF Header" error with WaveFileReader otherwise.

     FileStream fileStream = new FileStream(fileName, FileMode.Open);
               var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
               var reader = new RawSourceWaveStream(fileStream, waveFormat);
                using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
                {
                    WaveFileWriter.CreateWaveFile(fileName.Replace("vox", "wav"), convertedStream);
                }
               fileStream.Close();
    
    0 讨论(0)
  • 2020-12-05 17:14

    A few pointers:

    • You need to use a WaveFormatConversionStream to actually convert from one sample rate / bit depth to another - you are just putting the original audio into the new file with the wrong wave format.
    • You may also need to convert in two steps - first changing the sample rate, then changing the bit depth / channel count. This is because the underlying ACM codecs can't always do the conversion you want in a single step.
    • You should use WaveFileReader to read your input file - you only want the actual audio data part of the file to get converted, but you are currently copying everything including the RIFF chunks as though they were audio data into the new file.
    • 8 bit PCM audio usually sounds horrible. Use 16 bit, or if you must have 8 bit, use G.711 u-law or a-law
    • Downsampling audio can result in aliasing. To do it well you need to implement a low-pass filter first. This unfortunately isn't easy, but there are sites that help you generate the coefficients for a Chebyshev low pass filter for the specific downsampling you are doing.

    Here's some example code showing how to convert from one format to another. Remember that you might need to do the conversion in multiple steps depending on the format of your input file:

    using (var reader = new WaveFileReader("input.wav"))
    {
        var newFormat = new WaveFormat(8000, 16, 1); 
        using (var conversionStream = new WaveFormatConversionStream(newFormat, reader))
        {
            WaveFileWriter.CreateWaveFile("output.wav", conversionStream);
        } 
    }
    
    0 讨论(0)
  • 2020-12-05 17:14
                openFileDialog openFileDialog = new openFileDialog();
                openFileDialog.Filter = "Wave Files (*.wav)|*.wav|All Files (*.*)|*.*";
                openFileDialog.FilterIndex = 1;
    
    
                WaveFileReader reader = new NAudio.Wave.WaveFileReader(dpmFileDestPath);
    
                WaveFormat newFormat = new WaveFormat(8000, 16, 1);
    
                WaveFormatConversionStream str = new WaveFormatConversionStream(newFormat, reader);
    
                try
                {
                     WaveFileWriter.CreateWaveFile("C:\\Konvertierten_Dateien.wav", str);
                }
                catch (Exception ex)
                {
                     MessageBox.Show(String.Format("{0}", ex.Message));
                }
                finally
                {               
                    str.Close();
                }
    
                     MessageBox.Show("Konvertieren ist Fertig!");
                }
    
    0 讨论(0)
提交回复
热议问题