I’m developing a videogame where I need to mix a long song with short clips and then export the result to an OGG/MP3 file. I’m using Unity and the main target platform is iOS and Android.
I’ve managed to get an AudioClip with the sound I want, but I can’t find any way to export it on a compressed file (I’ve seen scripts exporting to WAV but the size is too much).
As Unity uses FMOD I came here asking for if anyone knows about exporting the clip from a C# script.
Thanks in advance!
- Mixu asked 5 years ago
If you want to export audio to .ogg for playback in the game you can use a tool like [url=http://audacity.sourceforge.net/download/:337edusi]Audacity[/url:337edusi] which is free. If you want to export .ogg from the game (on the phone), there is no inbuilt functionality to do that. You would need to add an ogg vorbis encoder to your game.
If you are familiar with creating native code plugins, the https://www.xiph.org/ provide open source encoders/decoders for Ogg Vorbis. You could try compiling their encoders to native code plugins for Unity and interfacing with them from your C# code. You would also need to find a way to get the raw audio stream out of Unity, I’m not sure whether that is possible.
I’ll take a look at creating native code plugins.
You can get the raw audio of an AudioClip using the GetData function, and passing as parameter an array of floats.
I’m using that for extracting data of each clip, and then mixing mini clips into the main song, then with SetData you can put the raw data into an AudioClip and later playing it.
Here it is a code snippet for if someone needs it:
AudioClip clipSong = song.audio.clip;
AudioClip resultClip = AudioClip.Create("resultSong",clipSong.samples,clipSong.channels,clipSong.frequency,_3d,stream);
float songData = new float[(clipSong.samples * clipSong.channels)];
//modify songData as you would like
Please login first to submit.