Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Generate AudioClip at Runtime With MP3 Data

Discussion in 'Audio & Video' started by kromenak, Jul 2, 2015.

  1. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    I'm working on a game where I have a ton of sound effects (~1500), but I only need to load subsets of these sound effects at one time. For example, depending on a user's choices, I may only need to load sounds 1-200 and 700-900 and 1200-1350.

    Currently, I'm using Resources.Load to load these sound effects as AudioClips, but I'm not happy with the performance. Loading that amount of data from Resources takes a fair amount of time and incurs a large number of disk reads (one per resource, I'd guess, though I don't know exactly how it works internally).

    An alternative method I'm trying out is to store my audio chunks in large binary files. For example, I can process sounds 1-200 at edit-time and write the WAVE audio data into a single binary file. Then, I can load that single binary file at runtime, parse the bytes, and generate AudioClips using AudioClip.Create. This also works, but the binary file is way too large, since I'm storing raw WAVE data. This method bypasses a handy benefit of the Resources approach, which is that Unity's import process compresses all the audio files quite nicely.

    Where I'm stuck now: I'd like to compress the audio data that I store in my binary file. I'm guessing I could use some sort of encoder (LAME for MP3?) to encode my WAVE files to MP3 and store the compressed data. An alternative handy approach would be if I could somehow take the compressed audio data that Unity generates and save that (is that at all possible?).

    However, I'm a bit at a loss as to how I could create an AudioClip from that MP3 data after loading the audio file. Is there any sort of API or method to do that with Unity? I noticed that AudioClip.Create takes two callback methods. Is the idea supposed to be that when I get that callback, I decode the MP3 data for Unity to use on the fly? If so, are there any recommended libraries that I can look into for that?
     
  2. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    280
    Decoding mp3 files will probably be slower than using Unity's system. If you don't need to use external mp3 files, it's probably not a good idea.

    Here is an mp3 importer that you could take a look at. Namely the Bass.dll one, since the other is definitely too slow.