Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Convert AudioClip to Byte[]

Discussion in 'Editor & General Support' started by jos_valentin, Oct 29, 2020.

  1. jos_valentin

    jos_valentin

    Joined:
    Aug 4, 2020
    Posts:
    23
    Hi, i need to convert an audioclip to a byte array, i know GetData from audioclip doesn't help because i tied writing a wav file with it and it can't be reproduced. Someone who can help me?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    GetData is the way to get audio data from an AudioClip. That doesn't mean you can just dump that data into a .wav file and expect it to be playable. I'm not sure how WAV files work but there's probably more to it than that.

    I found some code online that might help if you're wanting to write a WAV file: https://gist.github.com/darktable/2317063
     
  3. jos_valentin

    jos_valentin

    Joined:
    Aug 4, 2020
    Posts:
    23
    In fact, i have used that, it creates the wav file first and the write more data over it, it's good but i need to have the bytes for sending to a server, this server must be able to read it. What i am doing now is using that code you send me, but after create the file, i make unity read all bytes of that file and then upload to the server, something weird :(
     
  4. darklord849

    darklord849

    Joined:
    Jan 11, 2018
    Posts:
    6
    I needed a straight forward solution for converting audio clip to byte array :

    Code (CSharp):
    1.   public static byte[] Convert(AudioClip clip)
    2.     {
    3.         var samples = new float[clip.samples];
    4.         clip.GetData(samples, 0);
    5.  
    6.         MemoryStream stream = new MemoryStream();
    7.         BinaryWriter writer = new BinaryWriter(stream);
    8.  
    9.         int length = samples.Length;
    10.         writer.Write(length);
    11.  
    12.         foreach (var sample in samples)
    13.         {
    14.             writer.Write(sample);
    15.         }
    16.  
    17.         return stream.ToArray();
    18.     }
     
  5. cadburia

    cadburia

    Joined:
    Sep 22, 2022
    Posts:
    9
    I am having difficulty converting audio clip to a byte array (using darklord's solution). It seems that the float array is populated by NULL after I use GetData and I have no idea why.

    Also, why are you writing the value of the float array length into the memory stream?
     
  6. JPoenisch

    JPoenisch

    Joined:
    Mar 2, 2018
    Posts:
    4
    `float` can not be `NULL` .. and `GetData` doesn't reassign the `samples` variable .. the issue seems to be somewhere else

    And you prepend the length of the array so that on receiver side you can recreate the array again by

    - first reading 4 bytes -> length
    - create `new float[length]`
    - now copy the rest into the float array with given length