Can't get an AudioClip mixed with other AudioClip's

Hi!
I’m using Unity 4.2.1 for developing a game, and I need to mix some short clips into a full song.
This is the code I’m using:
I create an AudioClip, then I get the data of the full song, and of the short clips, then I try to mix the clips into the song data, and set to the created clip.

	AudioClip clipSong = song.audio.clip;
		AudioClip clipI = Pajaro_I.GetComponent<tk2dUISoundItem>().clickButtonSound;
		AudioClip clipIV = Pajaro_IV.GetComponent<tk2dUISoundItem>().clickButtonSound;
		AudioClip clipV = Pajaro_V.GetComponent<tk2dUISoundItem>().clickButtonSound;
		
		
		bool _3d = false;
		bool stream = false;
		
		AudioClip resultClip = AudioClip.Create("resultSong",clipSong.samples,clipSong.channels,clipSong.frequency,_3d,stream);
		
		float[] songData = new float[(clipSong.samples * clipSong.channels)];
		clipSong.GetData(songData,0);
		float[] clipIData = new float[(clipI.samples * clipI.channels)];
		clipI.GetData(clipIData,0);
		float[] clipIVData = new float[(clipIV.samples * clipIV.channels)];
		clipIV.GetData(clipIVData,0);
		float[] clipVData = new float[(clipV.samples * clipV.channels)];
		clipV.GetData(clipVData,0);
		
		for(int i = 0;i<clipI.samples;i++) {
			songData[i] = songData[i] + clipIData[i];	
		}
		
		resultClip.SetData(songData,0);
		
		bool ok = EditorUtility.ExtractOggFile(resultClip,"song.ogg");
		
		if (ok) Debug.LogWarning("song saved OK");
		else Debug.LogWarning("song saved FAIL");
		
		asource.clip = resultClip;
		
	}

After the GetData from songClip, songData is a big array of 0’s.
As I read, this issue was fixed, but don’t know if it’s not solved, or it’s my fault.

BTW, I’m not using WWW (that I’m aware of). I’ve added an Audio Source to a GameObject (which has an sprite, and some scripts), put the .ogg file from the project files to the box named Aucio Clip.

gregzo suggested me to use a Wave file instead of an OGG.
I exported the OGG to a WAV file in Floating point format, and then GetData seemed to return the correct Data.
But… why it won’t work with an OGG file? :confused:

Anyone has any idea of it?
Thanks!

Problem solved! :smile:

I just changed the load type of the 4 clips, the song in streaming, and the other 3 short clips to decompress on load, and now they work perfectly :slight_smile:

Doh… now with

bool ok = EditorUtility.ExtractOggFile(resultClip,"song.ogg");

I can’t save the file, it creates a file with 0 bytes :frowning:

But I can play the created AudioClip with the mix I wanted.
And I also can export the original song clipSong on an .ogg.

Was about to answer, good to see you figured it out. Indeed, the default “compressed in memory” setting is the only one whith which GetData does not work. About ExtractOggFile, can’t help much, never used it…

Little snippet:

using UnityEngine;
using System.Collections;

[RequireComponent( typeof( AudioSource ) ) ]

public class OggTest : MonoBehaviour {

	public AudioClip publicClip;
	
	void Start () 
	{
		audio.clip = UselessClipCopy( publicClip, true );
		audio.Play ();
	}
	
	AudioClip UselessClipCopy ( AudioClip clip, bool is3D )
	{
		float[] data = new float[ clip.samples * clip.channels ];
		clip.GetData( data, 0 );
		AudioClip retClip = AudioClip.Create("UselessCopy", clip.samples, clip.channels, clip.frequency, is3D, false );
		retClip.SetData(data,0);
		return retClip;
	}
	
}

I modified my code to include your snippet:

void playResult() {
		
		asource.clip = UselessClipCopy( cancion.audio.clip, true );
        asource.Play ();
		
		bool ok = EditorUtility.ExtractOggFile(asource.clip,"cosa_useless.ogg");
		
	}
	
	AudioClip UselessClipCopy ( AudioClip clip, bool is3D )

    {

        float[] data = new float[ clip.samples * clip.channels ];

        clip.GetData( data, 0 );

        AudioClip retClip = AudioClip.Create("UselessCopy", clip.samples, clip.channels, clip.frequency, is3D, false );

        retClip.SetData(data,0);

        return retClip;

    }

And it plays fine, but I can’t save the OGG file still :frowning:

Edit: Now that I’m trying to export the project to Android and iOS… it won’t get the UnityEditor import… now I’m very lost :confused:

Hi,

Editor tools are just that - editor tools.

They are not meant to be used elsewhere. If you need compressing audio to ogg, you should implement a native plugin that handles it.
Or maybe some asset on the Asset Store can help? I haven’t looked, but you never know…

In the Asset Store there’s no plugin about it :frowning:
I’ll have to create a native plugin!

You’ll have to code 2 if you want to support both iOS and Android.

It’s an asset the store really lacks - native audio tools ( resampling, compression, audiocopy etc… ). I thought about doing it a while back, but I fear the market is very small…

I’d be very interested in purchasing an asset that has native audio tools. Any solutions for this? How did you implement AudioCopy for uPhase?

Thanks!