Edit wav file (up to 16 kHz and 8 bit) using NAudio

I want to change the WAV file to 8 kHz and 8 bits using NAudio.

WaveFormat format1 = new WaveFormat(8000, 8, 1); byte[] waveByte = HelperClass.ReadFully(File.OpenRead(wavFile)); Wave using (WaveFileWriter writer = new WaveFileWriter(outputFile, format1)) { writer.WriteData(waveByte, 0, waveByte.Length); } 

but when I play the output file, the sound only hiss. Is my code correct or what is wrong?

If you install WaveFormat in WaveFormat (44100, 16, 1), it works fine.

Thanks.

+6
source share
3 answers

A few pointers:

  • You need to use WaveFormatConversionStream to actually convert from one sample rate / bit to another - you just put the original sound in a new file with the wrong wave format.
  • You may also need to convert in two steps - first change the sample rate, and then change the bit depth / number of channels. This is because ACM base codecs cannot always do the conversion you need in one step.
  • You should use WaveFileReader to read your input file - you only need to convert the actual part of the audio data to a file, but at present you are copying everything, including RIFF fragments, as if they were audio data into a new file.
  • 8-bit PCM sound usually sounds awful. Use 16 bits, or if you should have 8 bits, use G.711 u-law or a-law.
  • The sound of downsampling can lead to smoothing. To do this, you must first implement a low-pass filter. This, unfortunately, is not easy, but there are sites that will help you generate coefficients for the Chebyshev low-pass filter for the specific reducing sampling that you do.

Here is a sample code showing how to convert from one format to another. Remember that you may need to perform the conversion in several stages, depending on the format of the 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); } } 
+13
source

The following code solved my problem with the G.711 Mu-Law with the vox file extension for the wav file. I continued to receive the β€œNo RIFF Header” message with the 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(); 
+3
source
  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!"); } 
+1
source

Source: https://habr.com/ru/post/892469/


All Articles