Converting .vox audio files to .wav or .mp3 with NAudio - c#

I am trying to convert .vox to .mp3 or .wav with NAudio with the code below:-
var bytes = GetBytes(new FileInfo(#"D:\path\to\vox-file.vox"));
using (var writer = new WaveFileWriter(#"D:\path\to\wav-file.wav", new WaveFormat(8000, 8, 2)))
{
writer.Write(bytes, 0, bytes.Length);
}
However the converted .wav file I am getting has very distorted audio and its not listenable. Am I missing some configuration with NAudio?

I was able to convert .vox files to .wav with the following:-
var bytes = GetBytes(new FileInfo(#"path-to.vox"));
using (var writer = new WaveFileWriter(#"path-to.wav", Mp3WaveFormat.CreateMuLawFormat(8000, 1)))
{
writer.Write(bytes, 0, bytes.Length);
}
The important bit here is this part: Mp3WaveFormat.CreateMuLawFormat(8000, 1), this is basically some kind of format/configuration for the .wav file which you want to be created and these values may vary depending upon your .vox files

Related

Convert mp4 file to byte array and string

I working on project to upload files into firebase real-time database using firesharp in WinForm c#.
I searched a lot to understand how to do this.
I know about the option to write File.ReadAllBytes(), but I want to run the app on weak PC, with 4 GB ram, and he is very slow.
I succeeded to upload an image, it's work good.
I find something about Stram option, but I don't know how to do it with converting to String.
Sorry about my English.
You can convert the file to binary, and the binary can be saved as arrays and strings.
//Read file to byte array
FileStream stream = File.OpenRead(#"c:\path\to\your\file\here.txt");
byte[] fileBytes= new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
//Begins the process of writing the byte array back to a file
using (Stream file = File.OpenWrite(#"c:\path\to\your\file\here.txt"))
{
file.Write(fileBytes, 0, fileBytes.Length);
}
https://www.codeproject.com/Questions/636646/Csharp-file-to-Byte-Array-and-Byte-Array-to-File
FileStream st = new FileStream(#"C:\2.jpg", FileMode.Open);
byte[] buffer = new byte[st.Length];
st.Read(buffer, 0, (int)st.Length);
st.Close();
Console.ReadLine();
https://social.msdn.microsoft.com/Forums/en-US/648bd8b2-74ec-4054-9c68-68190b600971/how-to-convert-a-file-to-binary?forum=csharplanguage

How can I save a byte array of audio data using NAudio?

I have a byte array of audio data that is supposedly in 8-bit uLaw format. However, when I try and save it to a wav file, the file is just static. Below is how I am trying to save the byte array. What am I doing wrong?
var ulawFormat = WaveFormat.CreateMuLawFormat(8000, 1);
using (WaveFileWriter w=new WaveFileWriter(AssemblyDirectory + #"\..\..\..\TestAudio\output.wav", ulawFormat))
{
foreach(var kwa in knownWorkingAudio)
{
byte[] data = kwa.Value;
w.Write(data, 0, data.Length);
}
w.Flush();
}
The code sample looks correct. I suspect the audio is not in the format you think its in.

Append WAV Header in NAudio

I am trying to convert audio MP3 files to WAV with a standard rate (48 KHz, 16 bits, 2 channels) by opening with "MediaFoundationReaderRT" and specifying the standard settings in it.
After the file is converted to PCM WAV, when I try to play the WAV file, it gives corrupt output:
Option 1 -
WaveStream activeStream = new MediaFoundationReaderRT([Open "MyFile.mp3"]);
WaveChannel32 waveformInputStream = new WaveChannel32(activeStream);
waveformInputStream.Sample += inputStream_Sample;
I noticed that if I read the audio data into a memory stream (wherein it appends the WAV header via "WaveFileWriter"), then things work fine:
Option 2 -
WaveStream activeStream = new MediaFoundationReaderRT([Open "MyFile.mp3"]);
MemoryStream memStr = new MemoryStream();
byte[] audioData = new byte[activeStream.Length];
int bytesRead = activeStream.Read(audioData, 0, audioData.Length);
memStr.Write(audioData, 0, bytesRead);
WaveFileWriter.CreateWaveFile(memStr, audioData);
RawSourceWaveStream rawSrcWavStr = new RawSourceWaveStream(activeStream,
new WaveFormat(48000, 16, 2));
WaveChannel32 waveformInputStream = new WaveChannel32(rawSrcWavStr);
waveformInputStream.Sample += inputStream_Sample;
However, reading the whole audio into memory is time-consuming. Hence I am looking at "Option 1" as noted above.
I am trying to figure out as to what exactly is the issue. Is it that the WAV header is missing which is causing the problem?
Is there a way in "Option 1" where I can append the WAV header to the "current playing" sample data, instead of converting the whole audio data into memory stream and then appending the header?
I'm not quite sure why you need either of those options. Converting an MP3 file to WAV is quite simple with NAudio:
using(var reader = new MediaFoundationReader("input.mp3"))
{
WaveFileWriter.CreateWaveFile("output.wav", reader);
}
And if you don't need to create a WAV file, then your job is already done - MediaFoundationReader already returns PCM from it's Read method so you can play it directly.

Playing a wav file after processing it with NAudio

I've converted a double array output[] to a wav file using NAudio. The file plays ok in VLC player and Windows Media Player, but when I try to open it in Winamp, or access it in Matlab using wavread() I fail.. (in Matlab I get the error: " Invalid Wave File. Reason: Incorrect chunk size information in WAV file." , which pretty obviously means something's wrong with the header). Any ideas on how to solve this? Here's the code for converting the array to a WAV:
float[] floatOutput = output.Select(s => (float)s).ToArray();
WaveFormat waveFormat = new WaveFormat(16000, 16, 1);
WaveFileWriter writer = new WaveFileWriter("C:\\track1.wav", waveFormat);
writer.WriteSamples(floatOutput, 0, floatOutput.Length);
You must dispose your WaveFileWriter so it can properly fix up the WAV file header. A using statement is the best way to do this:
float[] floatOutput = output.Select(s => (float)s).ToArray();
WaveFormat waveFormat = new WaveFormat(16000, 16, 1);
using (WaveFileWriter writer = new WaveFileWriter("C:\\track1.wav", waveFormat))
{
writer.WriteSamples(floatOutput, 0, floatOutput.Length);
}

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

I want to change a WAV file to 8KHz and 8bit 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 is only sizzle. Is my code is correct or what is wrong?
If I set WaveFormat to WaveFormat(44100, 16, 1), it works fine.
Thanks.
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);
}
}
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();
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!");
}

Categories

Resources