- Home /
Combine 2 audio clip in one
How can i combine half of 2 audio clip in 1 audio clip with script?
Answer by llq664301573 · Jul 13, 2015 at 01:39 PM
public static AudioClip Combine(params AudioClip[] clips)
{
if (clips == null || clips.Length == 0)
return null;
int length = 0;
for (int i = 0; i < clips.Length; i++)
{
if (clips[i] == null)
continue;
length += clips[i].samples * clips[i].channels;
}
float[] data = new float[length];
length = 0;
for (int i = 0; i < clips.Length; i++)
{
if (clips[i] == null)
continue;
float[] buffer = new float[clips[i].samples * clips[i].channels];
clips[i].GetData(buffer, 0);
//System.Buffer.BlockCopy(buffer, 0, data, length, buffer.Length);
buffer.CopyTo(data, length);
length += buffer.Length;
}
if (length == 0)
return null;
AudioClip result = AudioClip.Create("Combine", length / 2, 2, 44100, false, false);
result.SetData(data, 0);
return result;
}
@jmgek, I don't think it is NEEDED, but it's used in case you have a variable number of parameters... in this case, it could be ommited... $$anonymous$$ore info here: params - C# ref - $$anonymous$$icrosoft
Answer by Electricpuppets · Oct 25, 2021 at 06:36 PM
//AudioClip result = AudioClip.Create("Combine", length / 2, 2, 44100, false, false);
AudioClip result = AudioClip.Create("Combine", length / 2, 2, 44100, false);
Drop second false to get around the Obsolete flag., //AudioClip result = AudioClip.Create("Combine", length / 2, 2, 44100, false, false); AudioClip result = AudioClip.Create("Combine", length / 2, 2, 44100, false);
Drop the second false to get around the Obsolete flag.
Your answer
Follow this Question
Related Questions
Audio Clip Getting Cut Short 1 Answer
On Collision sound clip change 1 Answer
Play audio clip on more than one object from another gameobject 2 Answers
Audio sample 1 Answer