Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Chirz · Apr 07, 2015 at 01:03 AM · loopintroaudio clip

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. alt text

And here's the audio used. https://www.youtube.com/watch?v=xQHHlXNNdtY&feature=youtu.be

loop-visualization.png (79.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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:

  1. How long does the loop last?

  2. 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).

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chirz · Apr 07, 2015 at 02:51 AM 0
Share

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.

avatar image _Gkxd · Apr 07, 2015 at 03:43 AM 0
Share

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.

avatar image Chirz · Apr 07, 2015 at 03:53 AM 0
Share

There are also some music tracks that change tempo. Strange.

avatar image _Gkxd · Apr 07, 2015 at 04:08 AM 0
Share

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):

  1. The length of the loop.

  2. 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.

avatar image Ventura · Oct 27, 2015 at 09:49 AM 0
Share

very usefull thanks :)

avatar image
1

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges