- Home /
Avoiding audio transcoding in Unity?
I'm working on a project where I only have access to MP3s versions of audio tracks I'd like to use. Unity is trying to be helpful and recompressing everything as Vorbis, which is not ideal due to quality loss. Even if I override the audio clip settings and set it to use MP3, Unity still transcodes the file for me, resulting in quality loss. I can set the clip to PCM, which would avoid re-encoding the file into another lossy format, but the resulting file size is too large to be practical.
In a perfect world I would have WAV versions of all the tracks I want to use, but that is not the case. Is there anyway to tell Unity to use the file the way it is and to leave it alone and not recompress it?
For the record I am using Unity 5.3.1 and my project target is iOS.
There's simply no way to do this, it's just how Unity is. BTW note this point.
@Fattie, you should make your comment into an answer so I can up vote it. It may be there is no way to do this. I was hoping there was an extension or something to work around the limitation with Unity.
No worries. once on a large job, the production company literally hired a top freelance audio guy to "compress the audio nicely"; once this aspect of Unity was explained of course the guy was unneeded! Now there's an anecdote :)
Answer by Fattie · Jan 28, 2016 at 03:48 PM
Surprisingly, there's simply no way to do this, it's just how Unity is.
It's a case of, every single audio engineer who works on a Unity project for the first time, is shocked to learn this!
The internal paradigm of Unity per their AudioClip
import concept is that upon import it completely expands the sound first, and then compresses it according to a couple of choices you have (depending on the version of Unity), and a slider. It's then saved internally .... to be used for the actual shipped product.
To "adjust the compression" of audio clips in Unity, it literally just comes down to the programmer setting a slider - that is literally and absolutely the only choice you have!
So, after building the project the producers or whoever will say "ok, the final build could be a bit bigger so let's increase the sound quality" or "we'll have to compress the sound a bit to make the build a bit smaller" - and it's literally just a case of moving that slider.
Again regarding the audio files you "drop in" to Unity, there is absolutely literally no point doing anything to them - just drop in the full-size totally uncompressed versions.
(Careful audio engineers sometimes care for even the frames of the audio files, so that frames end clean and stuff - this is absolutely pointless when sending to a Unity project as Unity just unpacks them and recompresses/packs as it sees fit!)
Note that Unity tend to noodle with the formats it uses ("compresses to" if you will) each new version, you can usually see the details here or on their change logs http://docs.unity3d.com/Manual/class-AudioClip.html Again as the audio engineer all you can really do is tell the programmers "please choose XYZ compression" (from the list there) and "set the slider to 48%" - !
BTW note this point .. you simply cannot loop mp3s in Unity, if you have to loop, instead just send over wav or whatever.
To be absolutely clear,
Is there anyway to tell Unity to use the file the way it is and to leave it alone and not recompress it?
(i) there is NOT any such button in Unity (ii) it's not conceptually possible based on how Unity works: every audio file is taken apart by Unity and "handled" by the Unity pipeline entirely; it does not, at all, "use what you give it". In contrast with say PNG assets it actually "uses the binary data you give it"; this is just not the case with audio assets (much to the surprise of audio engineers everywhere!)
It's not really a suprise that the Unity engine doesn't have mp3 support. It's a licensing thing. Since you as developer are using a middleware product: Unity, you would have to pay license fees when using mp3 encoder / decoder in your application. Actually those licenses and patents are no longer valid in most countries, but not all.
If you think you are free from licensing and patent restrictions you can use the mp3sharp library which is a C# port of a Java library which was released under LGPL. The nice thing about that library is, it's 100% pure managed code and doesn't rely on native libraries. That means it does also work in webplayer builds (haven't tested webGL but in theory it should work as well).
The library allows you to decode mp3s into PC$$anonymous$$ samples in memory.
Actually on the iOS platform, which has built in $$anonymous$$P3 support (I assume Apple pays all the patent dues), you CAN select $$anonymous$$P3. The problem is it will take a perfectly good $$anonymous$$P3 and then recompresses it, lowering the quality.
Obviously not the answer I was hoping for but thanks for giving such an in-depth explanation. This is some truly head scratching design from Unity. It's great that they take care of things for you but it would nice if they let users who knew what they were doing override the behavior.