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 TheFudster · Mar 08, 2013 at 07:06 AM · audioaudioclipdelaymusictiming

AudioClip Precise Timing and Delay

Hi, I'm trying to create sequence player that will play a series of notes a-g given in arrays. The problem is we need very precise timing to get this to work and sound properly. I've got something that works but there are still a few issues:

  1. a slight delay between when the first audio clip in a sequence is played and the second. However, after that the 3rd and 4th all play without delay.

  2. over time things seem to eventually skew away from the timing when played against a metronome track

The audio files are 48000 Hz MPEG files and the platform is iOS and Android

Thanks for any help in advance

This is the sequencer's update function:

     void Update()
     {
     
         if (!currentlyPlaying) {
             return;
         }
         
         //Get Music Time
         float time = AudioManager.MusicTime();
         Sequence sequence = sequences[sequenceIndex];
                 
         //For each player
         for (int i = 0; i < sequence.tracks.Length; i++) {
             
             SequenceTrack track = sequence.tracks[i];
             
             //Apply notes from another track if the current track is empty. Ensures that all 4 tracks are always played.
             //Have to check each track in case the players changed from the default instruments.
             if (track.notes.Count <= 0 && !testGui) {
                 
                 for (int k = 0; k < sequence.tracks.Length; k++) {
                     
                     if (sequence.tracks[k].notes.Count > 0) {
                         track.notes = sequence.tracks[k].notes;
                         break;
                     } 
                     
                 }
                 
             }
                         
             //Skip this track if it is invalid or not filled with notes
             if (track.notes.Count <= 0 || track.mapIndex < 0 || track.mapIndex >= sequenceMaps.Length) {
                 continue;
             }
             
             SequenceMap map = sequenceMaps[track.mapIndex];
             
             //Find the current interval
             SequenceTrackInterval interval = null;
             for (int j = 0; j < map.intervals.Length; j++) {
             
                 if (time >= map.intervals[j].start && time < map.intervals[j].finish) {
                     interval = map.intervals[j];
                     break;
                 }
                 
             }
             
             //Continue to next track if we didn't find a valid interval
             if (interval == null) {
                 continue;
             }
             
             //Set first note at start of next interval if we're not inside the interval yet
             if (track.noteTimer < interval.start) {
                 track.noteTimer = interval.start;
             }
             
             time = AudioManager.MusicTime();
             
             //Get Next note time
             float timeTillNextNote = track.noteTimer - time;
             
             //Schedule next note to play if we're within a certain time interval of it
             if (timeTillNextNote < 1.0f) {
                 
                 //Get the next note for this track
                 string note = track.notes[track.noteCounter] as string;
                 
                                //These calls eventually call the PlayNote function below
                 if (timeTillNextNote <= 0) {
                     AudioManager.PlayNoteUsingInstrument(track.instrument, note);
                 } else {
                     AudioManager.PlayNoteUsingInstrument(track.instrument, note, timeTillNextNote);
                 }
                 
                 float noteInterval = AudioManager.InstrumentIntervalForNote(track.instrument, note);
                 
                 track.noteTimer += noteInterval;
                 track.noteCounter = (track.noteCounter + 1) % track.notes.Count;
                 
             }
             
         }
         
         //Check if we've looped around or stopped playing
         if (time < currentPlayTime || !AudioManager.IsPlaying()) {            
             currentlyPlaying = false;
             AudioManager.PopMusic();
         }
         
         currentPlayTime = time;
         
     }
 
 //Delay given in seconds
     static public void PlayNote(AudioClip clip, float delay)
     {
         
         ulong delayInSamples = (ulong)Mathf.RoundToInt(clip.frequency * delay);
         
         AudioSource source = null;
         
         //Get an unused audio source
         if (sharedInstance.audioSourceQueue.Count > 0) {
             source = sharedInstance.audioSourceQueue.Dequeue();
         } else {
             //Create a new source if there are no audio sources available    
             source = sharedInstance.gameObject.AddComponent<AudioSource>();
         }
         
         if (source.isPlaying) {
             Debug.LogWarning("Playing on source that is already playing");
         }
         
         source.clip = clip;
         source.Play(delayInSamples);
                 
         //Add source to IsPlaying Queue
         sharedInstance.isPlayingQueue.Enqueue(source);
         
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kramcomposer · Mar 08, 2013 at 08:08 AM

I've personally noticed to give your scene about 1-2 seconds to fully load up to play audio or you get the symptom your getting now. An easy way to do this is to do this:

 private bool sceneNotStarted = true;
 
 private void Start(){
      invoke("sceneStarted",1f);
 }
 
 private void sceneStarted(){
      sceneNotStarted = false;
 }
 
 private void Update(){
      if(sceneNotStarted) return;
      //Your Code Here.....
 }
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

11 People are following this question.

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

Related Questions

Music will not change when an event triggers it 3 Answers

Unity Unable to Reassign Audio Clip 1 Answer

Event at audio position? 1 Answer

Dynamic Backgroundmusic 1 Answer

Audio sync with explosions 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