title: Create a .WAV file from Unity AudioClip
body: <p>So the problem is i want to create a .wav file from an Audioclip but i can't figure out how to do it. I've been looking for how a .wav file structure is but i'm not able to understand it as well as i want (Also i don't get how to get all the data from an Audioclip and convert it into bytes and set it to the file). </p>
  
  <p>Here's what i've been trying so far:</p>
  
  <pre><code>   public static bool ToWAV(this AudioClip audio, string path)
      {
          try
          {
              int samples = audio.samples;
              int channels = audio.channels;
              int sampleRate = audio.frequency;
  
              FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
              BinaryWriter bw = new BinaryWriter(stream);
  
              //RIFF HEADER
              bw.Write(System.Text.Encoding.ASCII.GetBytes("RIFF"));
              bw.Write(36 + samples * channels);
              bw.Write(System.Text.Encoding.ASCII.GetBytes("WAVE"));
  
              //FMT SUBCHUNK
              bw.Write(System.Text.Encoding.ASCII.GetBytes("fmt "));
              bw.Write(16); //Chunk size
              bw.Write(1); //Compression
              bw.Write(channels); //Num Channels
              bw.Write(sampleRate); //SampleRate
              bw.Write(sampleRate * channels); //ByteRate
              bw.Write(channels); //BlockAlign
              bw.Write(8); //BitsPerSample
  
              //DATA SUBCHUNK
              bw.Write(System.Text.Encoding.ASCII.GetBytes("data"));
              bw.Write(samples * channels); //Chunk size
  
              float[] data = new float[audio.samples * audio.channels];
              audio.GetData(data, 0);
  
              for (int d = 0; d &lt; data.Length; d++)
              {
                  bw.Write((byte)data[d]);
              }
  
              return true;
          }
          catch (System.Exception e)
          {
              Debug.LogError("Failed to create .WAV at: " + path + " - Error: " + e.Message);
              return false;
          }
      }
  </code></pre>