I looked into NAudio classes but couldn't see a class that converts byte array to WAV file. If there is no class like this, how can I convert byte array to WAV file using NAudio?
I want to send a text to RS232 as bytes, then I will get back those bytes into a byte[] buffer. After getting back data I want to save them as a WAV file using NAudio.
I tried to use WaveBuffer class but I think I am trying a wrong way.
This blog post explains how to use the WaveFileWriter class for this purpose:
byte[] testSequence = new byte[] { 0x1, 0x2, 0xFF, 0xFE };
using (WaveFileWriter writer = new WaveFileWriter(fileName, waveFormat))
{
writer.WriteData(testSequence, 0, testSequence.Length);
}
Related
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
In C#, I am trying to save off PCM data to a WAV file. Using NAudio, I create a WaveFileWriter.
this.fileWriter = new WaveFileWriter(#"c:\temp\test.wav", new WaveFormat(this.audioParameters.SampleRate, this.channels));
I am capturing the PCM packet in a float array and write the samples.
var arraySize = noOfFrames * this.channels;
var buffer = new float[arraySize];
Marshal.Copy(data, buffer, 0, arraySize);
this.fileWriter?.WriteSamples(buffer, 0, buffer.Length);
The outputted audio file is of the proper length but the audio is sounds horrible. I can tell it is the same audio but it is not right.
// Non-interlaced 32-bit float format
// ChannelLayout = LayoutStereo
// FramesPerBuffer = 1024
// SampleRate = 44100
// channels = 2
In NAudio, how do I create a WAV file from a PCM stream with the above information?
You are putting 32 bit floating point audio samples into a 16 bit WAV file. Use WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels) for the WaveFormat of the WaveFileWriter.
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.
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.
I have an image that is encoded in a byte array and I would like to add it as a shape in an excel document but unfortunetly the only available function I see to do this requires me to save the image to the drive and then read it. As you see this is a really slow operation and I would like to simply read the image from the byte stream and decode it into a bitmap.
I have encoded it like this :
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.QualityLevel = 100;
byte[] bit = null;
using (var ms = new MemoryStream())
{
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(ms);
bit = ms.ToArray();
}
Now, how to add it to the worksheet ?
The method Shapes.AddPicture accepts only a filename and cannot read from a stream.
The Excel object model doesn't provide any method for reading a byte array and then add it as a shape. So, the only possible solution is to save the byte array as a file on the disk and then add it as a shape as you stated earlier:
to save the image to the drive and then read it.