views:

37

answers:

1

The title is actually the question! Thank you.

+2  A: 

Here is a good sample project for reading and writing WAV files in C#:

http://www.codeproject.com/KB/audio-video/Concatenation%5FWave%5FFiles.aspx

Assuming that your "raw audio" is an array of short (2-byte) integers, this is a simple task. The header of a WAV file is 44 bytes (see note), so you write out the header first (using the code in the sample) followed by the data.

Note: not all WAV files are "canonical", which means they don't all have a 44-byte header followed by the data. The WAV format is actually a RIFF format, which means they can contain all kinds of different data and the header isn't necessarily at the beginning. However, none of this matters since you're just writing WAV files.

Update: If the voice recognition program is expecting a stream (as opposed to a file path), it's easy to create a MemoryStream like this:

byte[] bytes = System.IO.File.ReadAllBytes("c:\whatever.wav"); 
System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes); 

Or you can avoid File I/O altogether and create your WAV file as an in-memory byte array in the first place, and create the MemoryStream from that.

MusiGenesis
Thank you very much for your answer!I had read about the canonical WAV file format etc.The code/project in your link is good for that.What about a stream, can you tell some things about streaming?Also, this file (if finally file and not stream?!) is going to be read by a voice recognition program - so the header etc does somehow matter.
refugee
@refugee: I assume the voice recognition program takes a stream as an input, and continually monitors the stream for new data. You probably need to include more details about this program and what sort of input it expects. The header of a WAV *file* includes a value indicating the size of the data that follows - if the program is expecting an open-ended stream, this wouldn't apply. It may be that your voice recognition program expects an initial call that tells it what format the streamed data will be in, followed by the actual streaming of the data.
MusiGenesis