- Home /
Intro to looping music in single audio clip?
I would like to know how to get an audio clip with both an intro and loop to work properly in Unity.
Here's my idea visualized. The yellow part is the loop. The red area is the intro.
And here's the audio used. https://www.youtube.com/watch?v=xQHHlXNNdtY&feature=youtu.be
Answer by _Gkxd · Apr 07, 2015 at 01:49 AM
One way of doing this is setting AudioSource.timeSamples to the sample that you want to go to.
You need 2 pieces of information about the audio track to make a song loop, with or without an intro:
How long does the loop last?
When does the first loop finish in the audio track?
In the case of an audio track without any intro, 1 and 2 are the same. If there is an intro, then 2 will be greater than 1. Once you reach the end of the first loop, subtract the length of the loop from the current time of audio. That will place you back at the beginning of the first loop.
Here is a sample implementation that I wrote for a rhythm game library. The Update
function is the important part. It does exactly what I explained above (with the addition of converting seconds to samples).
I'm still encountering problems. The script I'm looking for could work as follows. BP$$anonymous$$: (128) Signature: (1/4) Intro measure: (1 1/4) Loop start: (3 1/4) Loop end: (35 1/4) $$anonymous$$aybe I'm just not using it correctly.
EDIT: Only the loop length on the script you provided me has functionality. No offense.
That script is meant to be a code example, not for copy pasting. It does things that you don't necessarily need to do.
However, the concept remains the same. Identify the point of time (in samples) that the first complete loop ends. If you're past that time, then subtract the length of the loop (in samples) from the audio sample.
There are also some music tracks that change tempo. Strange.
Yes, songs can have tempo changes, but you only need to know two pieces of information to make an audio track loop (with or without an intro):
The length of the loop.
The time that the first loop ends.
The tempo of the song doesn't actually matter, as long as the song itself loops. I have edited my answer to reflect this, since the previous explanation probably wasn't as clear.
Answer by 5argon · Jan 09, 2016 at 11:51 AM
I have the same problem as you so I made a solution called Introloop. You provide it 2 boundary points and one audio file then the script will handle it. http://forum.unity3d.com/threads/378370/
In your case, the first point is the seam between red and yellow and the second point is at the end of yellow. You should render a bit more at the end though, by copy pasting the yellow section after red to the end for about 1 second so if the schedule lag by some reason, you won't hear the silence. And also for compression artifact problem that a gap will be add to the end.
If you want to implement it yourself, the idea is you should have 2 AudioSources and do a baton-passing continuously. And you should use AudioSettings.dspTime-related function like AudioSource.PlayScheduled , AudioSource.SetScheduledEndTime and AudioSource.SetScheduledStartTime instead of coroutines or PlayDelayed.
The first one play intro and immediately call SetScheduledEndTime on it to stop at the passing point. Where the second one is being SetScheduledStartTime-ed to start at that exact time, and SetScheduledEndTime to stop at the loop's end too. When the transfer is successful, the just-stopped first one should be waiting at the loop end point with SetScheduledStartTime to prepare for the next loop. Repeat for infinite loop. In my case I used 4 AudioSources to handle cross-fading between music with intro. (that's 2 pair of AudioSource)
If you want to pause the music, things will get a bit more complicated because you need to cancel all the already-scheduled audio source, and reschedule them all on resume with regard to what time you are on now on the audio clip before pause. (On the development of my script I remembered having much trouble in pausing-resuming)
Your answer
![](https://koobas.hobune.stream/wayback/20220613192402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[iOS] Streaming audio with loop 1 Answer
How to play loop an audio clip on button hold down? 1 Answer
Intro to looped music 1 Answer
How exactly does AudioClip.Create() work? 3 Answers
Is there a way to call up a IF statement like a function? 3 Answers