Search Unity

Audio How to create an AudioClip from WAV data in memory?

Discussion in 'Audio & Video' started by JoeStrout, Jun 23, 2019.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'd like to load an AudioClip from data I've extracted into a byte[] from a zip file. But the only way I can find to load WAV data at runtime is UnityWebRequestMultimedia.GetAudioClip, which won't work here since I don't have a URL.

    Is there any way to do this, short of looking up the WAV format and decoding it myself?

    Thanks,
    - Joe
     
  2. Deleted User

    Deleted User

    Guest

    The first idea that comes into my mind is implementing OnAudioFilterRead in a behavior script (docs with example: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnAudioFilterRead.html). You just write your bytes into the float array (scale it to -1.0 to 1.0 first) and this should work. Note that this requires an AudioSource component to be present, so you probably need a small sound that is constantly played and overwritten with your audio.
     
  3. Lesha-VH

    Lesha-VH

    Joined:
    Jul 3, 2012
    Posts:
    96
    Actually, I also would like to create AudioClip from wav or mp3 memory byte[] array.

    This functionality already exists in Unity Engine, DownloadHandlerAudioClip class implements this, but requires UnityWebRequest.

    Should be added creation of AudioClip from byte[] array by Unity3D developers)))
    Thanks you!