Exporting Audio

I was wondering if it’s possible to save an audio file out to web similarly to the image save feature found here:

I think the problem is that I’d need to convert the audioclip to a byte array and export it using the “audio/wav” mimetype.

I haven’t found any documentation on audio exportation. Has it been done?

To convert AudioClip to wav you can use this as a starting point…

    static public void SaveAudioClipToWav(AudioClip audioClip, string filename)
    {
        FileStream fsWrite = File.Open(filename, FileMode.Create);

        BinaryWriter bw = new BinaryWriter(fsWrite);

        Byte[] header = { 82, 73, 70, 70, 22, 10, 4, 0, 87, 65, 86, 69, 102, 109, 116, 32 };
        bw.Write(header);

        Byte[] header2 = { 16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0 };
        bw.Write(header2);

        Byte[] header3 = { 2, 0, 16, 0, 100, 97, 116, 97, 152, 9, 4, 0 };
        bw.Write(header3);

        float[] samples = new float[audioClip.samples];
        audioClip.GetData(samples, 0);
        int i = 0;

        while (i < audioClip.samples)
        {
            int sampleInt = (int)(32000.0 * samples[i++]);

            int msb = sampleInt / 256;
            int lsb = sampleInt - (msb * 256);

            bw.Write((Byte)lsb);
            bw.Write((Byte)msb);
        }

        fsWrite.Close();

    }
1 Like

Much appreciated! I’m giving that a go

It worked perfectly. I can save out a .wav file to the local directory now. My only question is whether or not it’s possible to take the data from the BinaryWriter and put it into a byte array (which I would then add to WWWForm and send over the web).

Okay so, I made some slight changes to the function:

static public byte[] SaveAudioClipToWav(AudioClip audioClip, string filename)
    {
		byte[] buffer;
        FileStream fsWrite = File.Open(filename, FileMode.Create);

 
        BinaryWriter bw = new BinaryWriter(fsWrite);
 

        Byte[] header = { 82, 73, 70, 70, 22, 10, 4, 0, 87, 65, 86, 69, 102, 109, 116, 32 };

        bw.Write(header);

 

        Byte[] header2 = { 16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0 };

        bw.Write(header2);

 

        Byte[] header3 = { 2, 0, 16, 0, 100, 97, 116, 97, 152, 9, 4, 0 };

        bw.Write(header3);

 

        float[] samples = new float[audioClip.samples];

        audioClip.GetData(samples, 0);

        int i = 0;

 

        while (i < audioClip.samples)

        {

            int sampleInt = (int)(32000.0 * samples[i++]);

 

            int msb = sampleInt / 256;

            int lsb = sampleInt - (msb * 256);

 

            bw.Write((Byte)lsb);

            bw.Write((Byte)msb);

        }
		long length = fsWrite.Length;
		int lengthInt = Convert.ToInt32(length);
		buffer = new byte[lengthInt];
		fsWrite.Read(buffer, 0, lengthInt);
		return buffer;

        fsWrite.Close();
    }

The idea is that while creating the file, it also returns said file as a byte array.

I’m calling the function like this:

byteAudio = ExportScript.SaveAudioClipToWav(recording.clip, "newAudio.wav");
form.AddBinaryData("fileUpload", byteAudio, "audio.wav");

However, when I do this - I get a NullReferenceException for the form.AddBinaryData line. It seems as if the variable byteAudio is null, which means the function isn’t returning the byte array like it should. Any thoughts?

I’ve used the SaveAudioClipToWav function, and when I upload it to the server, trough the WWW and WWWform with AddBinaryData, like in the example, it does create the .wav file, but it’s empty, there’s no audio information to reproduce. What could this be? Is there any way to fix this?