Converting wav file to wav file (changing format) - c#

Trying to find a workaround for my previous question, I'd like to convert a 16k 8bit mono wav which is written in byte[] (which has wav header) to a 8k 8bit mono stream/byte[].
Is there any .Net library with samples available for this kind of conversion?
Thank you.

Thanks for the answers, I ended up using NAudio and with the following snippet, Voila! Everything works like a charm:
WaveFormat target = new WaveFormat(8000, 8 , 1);
WaveStream stream =new WaveFileReader("c:\\test.wav");
WaveFormatConversionStream str = new WaveFormatConversionStream(target, stream);
WaveFileWriter.CreateWaveFile("c:\\converted.wav", str);

Alvas seems to support conversion as well as the usual features:
http://alvas.net/alvas.audio.aspx

Related

NAudio Converting to PCM?

Using the SetOutputToWaveFile method in the SpeechSynthesizer class, I included a SpeechAudioFormatInfo class with these parameters
new SpeechAudioFormatInfo(EncodingFormat.Pcm, 10000, 16, 1, 16000, 2, null);
My intent is to get the output to be 10kHz signed 16-bit little endian headerless raw mono PCM, and am now trying to understand how to harness NAudio to do that. I tried to base code on change wav file ( to 16KHz and 8bit ) with using NAudio with WaveFormat and WaveConversionStream but am not getting the output I need. Can anyone refer me to working sample code?
Thanks.

Unzip Byte Array in C#

I'm working on EBICS protocol and i want to read a data in an XML File to compare with another file.
I have successfull decode data from base64 using
Convert.FromBase64String(OrderData); but now i have a byte array.
To read the content i have to unzip it. I tried to unzip it using Gzip like this example :
static byte[] Decompress(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
But it does not work i have an error message :
the magic number in gzip header is not correct. make sure you are passing in a gzip stream
Now i have no idea how i can unzip it, please help me !
Thanks !
The first four bytes provided by the OP in a comment to another answer: 0x78 0xda 0xe5 0x98 is the start of a zlib stream. It is neither gzip, nor zip, but zlib. You need a ZlibStream, which for some reason Microsoft does not provide. That's fine though, since what Microsoft does provide is buggy.
You should use DotNetZip, which provides ZlibStream, and it works.
Try using SharpZipLib. It copes with various compression formats and is free under the GPL license.
As others have pointed out, I suspect you have a zip stream and not gzip. If you check the first 4 bytes in a hex view, ZIP files always start with 0x04034b50 ZIP File Format Wiki whereas GZIP files start with 0x8b1f GZIP File Format Wiki
I think I finally got it - as usual the problem is not what is in the title. Luckily I've noticed the word EBICS in your post. So, according to EBICS spec the data is first compressed, then encrypted and finally base64 encoded. As you see, after decoding base64 you need first to decrypt the data and then try to unzip it.
UPDATE: If that's not the case, it turns out from the EBICS spec Chapter 16 Appendix: Standards and references that ZIP refers to zlib/deflate format, so all you need to do is to replace GZipStream with the DeflateStream

send array of bytes to System.Media.SoundPlayer in c#

I want send string byte to speaker something like this:
byte[] bt = {12,32,43,74,23,53,24,54,234,253,153};// example array
var ms = new MemoryStream(bt);
var sound = new System.Media.SoundPlayer();
sound.Stream = ms;
sound.Play();
but I get this exception:
my problem pic http://8pic.ir/images/g699b52xe5ap9s8yf0pz.jpg
The first bytes of a WAV stream contain info about length, etc.
You have to send this "WAV-Header" as well in the first few bytes.
See http://de.wikipedia.org/wiki/RIFF_WAVE
As you'll see its perfectly possible to compose these few bytes in the header and send them before your raw audio data,
You can use some library for reading data from microphone or playing it to speakers.
I worked successfuly with:
NAudio - http://naudio.codeplex.com/
I would not recommend building a WAV file yourself, it may be too much effort for this.
Note that this library (and probably some others, Bass - http://www.un4seen.com is also widely used) also have built in functionality for saving and reading WAV files.
NAudio is best app to play that functionality. use sample app provided.It may help.

Decoding ogg files to wav in WP8

I want to convert ogg file to wav and then play it on wp8 devives.
I've already checked many solutions but none of them worked. This looks promising but something doesn't work:
string _audioPath = "/SomeProject;component/Sounds/a_dog.ogg";
var stream = Application.GetResourceStream(new Uri(_audioPath, UriKind.Relative)).Stream;
using (var vorbis = new NVorbis.VorbisReader(stream, true))
{
float[] buf = new float[vorbis.TotalSamples];
vorbis.ReadSamples(buf, 0, (int)vorbis.TotalSamples);
}
When I execute it I see FileNotFoundException at VorbisReader contruction. I also checked if stream is readable and it is. I was able to get the file content using Read method.
Do you have any ideas why it doesn't work? Maybe you know some other library for wp8 which can decode ogg files?
[EDIT] I downloaded source code of NVorbis and used it directly from my project, and when i do this I don't get any FileNotFoundExceptions and everything seems to work. Maybe this exception is caused by missing library? I've got NVorbis reference added...
I think you are running into the same issue as this guy. Short version: I believe your Pack URI should be "pack://application:,,,/SomeProject;Component/Sounds/a_dog.ogg". NVorbis is not actually throwing the exception itself, but is instead causing the resource stream to do so...

C# - Bogus data when loading a *.wav file to byte array

I am trying to load a *.wav file to a byte array using C# 3.0 and .NET 3.5 like this:
var fs = File.Open(filedialog.FileName, FileMode.Open,FileAccess.Read);
long numBytes = new FileInfo(filedialog.FileName).Length;
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((int)numBytes);
From byte[58] and to the end (~50k bytes) all values are 127 or 128 (I guess the first ~58 bytes are header stuff?).
The wave file is playing fine in Windows media player and other players, and I am sure it is nothing wrong with it (it's recorded with the sound recorder in WinXP).
Wave file info:
BitRate: 176kbps
Audio sample size: 8bit
Audio sample rate: 22kHz
Audio format: PCM
When I try to play the byte stream using the .NET SoundPlayer it sounds terrible :-)
Any idèas?
[SOLVED]
This was not the problem after all, so I'll have to continue my search for the real bug.
The code looks all right, as far as I can see.
You could try the simpler code:
byte[] bytes = File.ReadAllBytes(filedialog.FileName);

Categories