wav reading and writing?

Hello,
I know that Unity is capable of reading and writing .txt files. I was wondering if there is a special way to read and write .wav files and how that’s done. Is there a certain pre-ascii signature that I must write to a .wav file in order to make the computer think that the file is a .wav file and not a .txt file?.. and can this signature be done in Unity? Thank you.

Hello?

Here is code I wrote to copy the data from s1.wav to s2.wav…

//Read from s1.wav
var sr = new StreamReader(“assets\resources\sounds\s1.wav”);
var fileContents = sr.ReadToEnd();
var saveBytes : byte[ ] = System.Text.Encoding.UTF8.GetBytes(fileContents);
var s : Stream = new System.IO.MemoryStream(saveBytes);
sr.Close();

//Write to s2.wav
var sw = new StreamWriter(“assets\resources\sounds\s2.wav”);
var s2 : Stream = new System.IO.MemoryStream(saveBytes);
for (i = 0; i < saveBytes.length; i++) {
sw.Write(System.Convert.ToChar(saveBytes*));*
}
sw.Close();

Notice how I even did a byte by byte replica of s2.wav from s1.wav and the computer
can still somehow distinguish that s2 is NOT a valid .wav file. Do I have to go binary and can
Unity do exact binary readings and writings to files?

you will have to create a proper binary header (not signature) in order to use your wavf files in standard player software. then the sound data is stored as 8/16/24/32 bit per sample. so you will have to take a deep look in the wav file format documentation and read/write your data for yourself.

if you just want to play wav files in unity use the audio component. if you want to analyse, create or manipulate the data you need your own read/write functions which can be done with standard mono/net binary encoding functions. so yes unity is capable of doing this but its a bit of work. look after wav classes or tutorials with net/mono in the internet as a place to start. i have used a tutorial for this but it is in german language.

you have to use binary as text is interpreted in a different way and may fuck up your data.

Thank you. I have already tried net/mono’s BinaryReader and BinaryWriter classes. I used…

//Read s1.wav
var sr = new StreamReader(“assets\resources\sounds\s1.wav”);
var fileContents = sr.ReadToEnd();
var saveBytes : byte[ ] = System.Text.Encoding.UTF8.GetBytes(fileContents);
var s : Stream = new System.IO.MemoryStream(saveBytes);
var br = new BinaryReader(s);
sr.Close();

//Write s2.wav
var s2 : Stream = new FileStream(“assets\resources\sounds\s2.wav”, FileMode.Create);
var bw = new BinaryWriter(s2);
bw.Write(saveBytes);
s2.Close();

and it is hard to believe that even though I attempted to make s2.wav an exact binary replica of s1.wav,
the 2 file sizes are the same, but the s2.wav is STILL not recognized as a .wav file.

You are encoding the content of the .wav file as UTF8 data, which makes no sense. If you want to create an exact copy, just write the raw bytes to the binary writer.

http://csharp-tricks.blogspot.com/2011/03/wave-dateien-einlesen.html
http://csharp-tricks.blogspot.com/2011/03/wave-dateien-schreiben.html
http://csharp-tricks.blogspot.com/2011/03/wave-dateien-zusammenmischen.html

as i told the tutorial is in german but the code may give you a hint how to do things. or just search for english tutorials. the file format specification also helps you to interpret the header.

maybe you want to elaborate what you want to achieve with this?