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
0
Question by psdev · Apr 08, 2012 at 11:48 AM · audiosyncloops

Why does audio get out of sync with multiple loops playing?

Hi,

Can anyone explain why this code sort of works, but then gets out of sync if I keep stopping and starting loops?

I have 3 audio loop sources loaded as WAVs and set to Compressed (MPEG) and Compressed in Memory and Gapless Looping ON (though Gapless looping off also seems to be gapless?).

I am clicking/tapping on 3 objects which represent each of the loops. I want to stop (or mute) them individually and then clicking again should restart that loop at the same point in time as the other playing loops continue playing.

I've tried using .time but that does the same thing - I read that .timeSamples is more precise so I assumed this would work. In this code, I stop the loops if they are playing, and if not I see which one is playing and set it's timeSamples to that one and play it. If none are playing I reset timeSamples to 0 and restart (that is the only way to get them back in sync if they get out of sync).

So with this pretty simple code, what is happening with this when they do get out of sync? Is it the Debug statements? I've tried this in Editor and iPad and it gets out of sync on both, but usually only if I tap fast turning them on and off quickly.

Just assume melody, bass, and drums are AudioSources and my CheckObject function fires when one of the corresponding gameobjects of these is tapped (This snippet below also uses FingerGestures and is in an OnTap method for a FingerGestureRecognizer -- is FingerGestures slowing this down?)

Any help getting audio loops to start at EXACTLY where the other playing one is would be appreciated! Thanks! :)

     if( CheckObject( source.Position, melodyObject ) )
     {
         Debug.Log ("melody clicked");
         if(melody.isPlaying)
         {
             melody.Stop();
         }
         else
         {
             if(bass.isPlaying)
             {
                 melody.timeSamples = bass.timeSamples;
                 melody.Play();
             }
             else
             {
                 if(drums.isPlaying)
                 {
                     melody.timeSamples = drums.timeSamples;
                     melody.Play();
                 }
                 else
                 {
                     melody.timeSamples = 0;
                     bass.timeSamples = 0;
                     drums.timeSamples = 0;
                     melody.Play();
                     bass.Play();
                     drums.Play();
                 }
             }
         }
         
     }
     if( CheckObject( source.Position, bassObject ) )
     {
         Debug.Log ("bass clicked");
         if(bass.isPlaying)
         {
             bass.Stop();
         }
         else
         {
             if(melody.isPlaying)
             {
                 bass.timeSamples = melody.timeSamples;
                 bass.Play();
             }
             else
             {
                 if(drums.isPlaying)
                 {
                     bass.timeSamples = drums.timeSamples;
                     bass.Play();
                 }
                 else
                 {
                     melody.timeSamples = 0;
                     bass.timeSamples = 0;
                     drums.timeSamples = 0;
                     melody.Play();
                     bass.Play();
                     drums.Play();
                 }
             }
         }            
     }
     if( CheckObject( source.Position, drumsObject ) )
     {
         Debug.Log ("drums clicked");
         if(drums.isPlaying)
         {
             drums.Stop();
         }
         else
         {
             if(bass.isPlaying)
             {
                 drums.timeSamples = bass.timeSamples;
                 drums.Play();
             }
             else
             {
                 if(melody.isPlaying)
                 {
                     drums.timeSamples = melody.timeSamples;
                     drums.Play();
                 }
                 else
                 {
                     melody.timeSamples = 0;
                     bass.timeSamples = 0;
                     drums.timeSamples = 0;
                     melody.Play();
                     bass.Play();
                     drums.Play();
                 }
             }
         }
Comment
Add comment · Show 6
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 keld-oelykke · Apr 08, 2012 at 01:06 PM 0
Share

$$anonymous$$aybe latency variance on play?

A workaround could be just to mute the audio sources that should not be heard and let them keep playing.

avatar image psdev · Apr 09, 2012 at 10:12 AM 0
Share

Thanks for responding. How can I get around latency variance on play? Is there any way to programmatically deter$$anonymous$$e that so I can add time to the amount before playing and have it be device independent?

The muting is a good idea changing volumes, however I need to be able to have far more than 3 audio tracks that are playable (but only 3 at a time) so that means I couldn't have 30 playing and 27 muted performance-wise so that solution wont help unfortunately.

avatar image psdev · Apr 09, 2012 at 11:01 AM 0
Share

I just tried adding this to see what the system thinks the .timeSamples are for each audio source. But what is weird is they sound out of sync when the debug prints that they are in sync?

 void Start() 
 {
      melody.Play();
     bass.Play();
     drums.Play();
     InvokeRepeating("PrintSamples", 2F, 2F);
 }
 void PrintSamples()
 {
     Debug.Log("melody = "+ melody.timeSamples + "\n bass = " +bass.timeSamples + "\n drums = "+ drums.timeSamples+"\n -----------");
 }
avatar image gregzo · Apr 09, 2012 at 11:03 AM 1
Share

As I write in my answer, try not compressing the wavs!

And are you 100% sure each track is of the same, sample accurate, length?

avatar image psdev · Apr 09, 2012 at 11:13 AM 0
Share

I just noticed something else. This whole out of sync problem seems to only occur (on both PC, $$anonymous$$ac & iPad) whenever the AudioSources are set to "Compressed ($$anonymous$$PEG)" if I set them all to "Native (WAV)" this sync problem goes away - but that means that memory usage is higher - Anyone have any thoughts on all of this or what the best way forward is? thanks :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by gregzo · Apr 09, 2012 at 10:36 AM

If you want sample level accuracy, do not compress.

Your code should work then. If not, you could try an offset:

 sourceToSync.timeSamples = (playingSource.timeSamples+offset)%playingSource.clip.samples;
 sourceToSync.Play(offset);

I use this a lot to start playing an audioSource on the beat! for example, at 60 bpm, 44.1 khz, one beat is 44100 samples:

 beatSamples = 44100*(60/bpm);
 
 offset = beatSamples - playingSource.timeSamples%beatSamples;
 sourceToPlayOnTheNextBeat.timeSamples = (playingSource.timeSamples+offset)%playingSource.clip.samples;
  sourceToPlayOnTheNextBeat.Play(offset);


Comment
Add comment · Show 4 · 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 psdev · Apr 09, 2012 at 11:38 AM 0
Share

Thanks for your reply. I was worried about memory usage etc - if I have 30+ loops (with only 3 playing at a time). When I first posted this question I didn't realize that "compressed" doesn't seem to report the correct timeSamples to set the playhead to. It seems that only works when they are Native (WAV).

I probably can't use "Load Into $$anonymous$$emory" with them? I know with 3 loaded into memory it works fine as above as long as I don't set them to "compressed". Do you think "Stream From Disc" will work on mobiles if I have 30 or so loops to choose from?

Thanks also for your offset code - yes I am 100% sure they are all identical lengths and sample rates.

I'll try your code for starting on the beat, that will sound better I expect than just starting instantly. :)

avatar image gregzo · Apr 09, 2012 at 11:50 AM 0
Share

I have 49 10s loops (mono) in my iPad app, all set to Load into $$anonymous$$emory. No problem, even on an iPad 1...

It depends how much ram you need for the rest of your game, I guess. I haven't tried stream from disc, because my app is an instrument and I need quick access to all 49 clips!

avatar image psdev · Apr 09, 2012 at 11:56 AM 0
Share

Great. Thanks for mentioneing that- it's good to know that will work even on iPad 1!

avatar image gregzo · Apr 09, 2012 at 11:59 AM 0
Share

You're welcome. Check my posts here and on the forum in case you need help audio wise: I was a total noob 6 months ago, and am now a proud noob+! Unity 3.5 is great for audio if you know how to work with it. The new GetData and SetData functions, in particular, can be used effectively to solve a lot of synching problems.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Syncing audio clips to speech or audio waveform. 0 Answers

Samplesync Audioplayer 2 Answers

AudioClip sync across a network: help needed! 0 Answers

I need a way to run code much more frequently than update. Ideally, every 3 milliseconds 2 Answers

What's wrong with this? (audio.timeSamples) 1 Answer


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