using System;
//using System.IO;
using UnityEngine;
public static class Statics
{
public static byte[] ClipToWav(AudioClip clip)
{
float[] floats = new float[clip.samples * clip.channels];
clip.GetData(floats, 0);
byte[] bytes = new byte[floats.Length * 2];
for (int ii = 0; ii < floats.Length; ii++)
{
short uint16 = (short)(floats[ii] * short.MaxValue);
byte[] vs = BitConverter.GetBytes(uint16);
bytes[ii * 2] = vs[0];
bytes[ii * 2 + 1] = vs[1];
}
byte[] wav = new byte[44 + bytes.Length];
byte[] header = {0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00,
0x57, 0x41, 0x56, 0x45, 0x66, 0x6D, 0x74, 0x20,
0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61 };
Buffer.BlockCopy(header, 0, wav, 0, header.Length);
Buffer.BlockCopy(BitConverter.GetBytes(36 + bytes.Length), 0, wav, 4, 4);
Buffer.BlockCopy(BitConverter.GetBytes(clip.channels), 0, wav, 22, 2);
Buffer.BlockCopy(BitConverter.GetBytes(clip.frequency), 0, wav, 24, 4);
Buffer.BlockCopy(BitConverter.GetBytes(clip.frequency * clip.channels * 2), 0, wav, 28, 4);
Buffer.BlockCopy(BitConverter.GetBytes(clip.channels * 2), 0, wav, 32, 2);
Buffer.BlockCopy(BitConverter.GetBytes(bytes.Length), 0, wav, 40, 4);
Buffer.BlockCopy(bytes, 0, wav, 44, bytes.Length);
//File.WriteAllBytes(Application.dataPath + "/my.wav", wav);
return wav;
}
}
See on GitHub gist: Statics.cs · GitHub
It's me again :P I found a solution on this post: http://forum.unity3d.com/threads/119295-Writing-AudioListener.GetOutputData-to-wav-problem?p=859871&viewfull=1#post859871 I'm using the C# example and it's working very good.
– GmtPlease post your own answer as a proper answer and mark it as correct, so other can see it that it is solved. Welcome to Unity Answers.
– Rochellerochemontand how i do that? i've written the answer on the question and put [solved] in title
– GmtInstead of pressing "add new comment" fill the text box bellow and press "Post your answer". Once your answer is posted you can mark it as correct. Then this question becomes "green" and gets removed from "Unanswered" list.
– RochellerochemontThis is awesome! Does it work for mobile as well? I assume it has a different folder structure for saving files.
– Toodleoo