- Home /
Sample-precise sync on multiple audio sources ?
Hi guys, I am making a sort of augmented reality dj machine with unity+vuforia.
I basically have 4 instruments, one audio source per each, and swapping in different loops according to the markers shown in front of the camera. To work nicely they need to be synced as precisely as possible (all the loops are the same lenght, and only one loop per instrument can be played at the same time). I keep a silenced audiosource with one of the loops (so that it has the right lenght) looping endlessly as a reference, so that whenever a new loop is added it starts from the exact same point, therefore giving an illusion of "skill" for the user (i basically just sync each new loop for him).
I am going crazy because of the syncing exactly. I tried the most basic approaches based on time (newLoop.time = refTrack.time) then i moved to the timeSamples to make it more precise. Still something is wrong. (Latency is set to best latency and the files are uncompressed Wavs).
I use a function with ayield return new WaitForFixedUpdate(); called from a StartCoroutine when a new loop has to be played and synced, and I set the audioSourcesvelocityUpdateMode = AudioVelocityUpdateMode.Fixed; In my mind it looks like the most logical way to make sure it happens all at the "same time".
Right after the syncing I print the timeSample value of the ref track and the loop to sync and it looks all fine, they show the same timeSample stamp
switch (track) {
case 0 : drum.Play();
drum.timeSamples = referenceSync.timeSamples;
drum.volume = 1f;
Debug.Log("Ref "+referenceSync.timeSamples);
Debug.Log("Drum "+drum.timeSamples);
break;
}
then, checking on a debug on screen, or even just on the inspector in a public variable, several times they are not. They tend to have an error offset of almost fixed values, that make me feel like it's frame based (470 or 940 usually, but sometimes i ahve some variantions too).
The same identical problem i have in the editor, but also in the Android build. Does anybody have any idea on what is the cause for such a weird behavior and what am I doing wrong?
Thanks in advance, cheers, H
Answer by jerichaosymphony · Aug 05, 2016 at 03:35 AM
AudioSettings.dspTime this is the autio time in seconds based on actual samples heard.
previously i thought:
Im doing the same and AFAIK there is no Time.time samples equivalent, i was thinking of using time.time converted to 44100 to run audio, or system time from the cpu because that is pretty accurate... also it's possible to count the buffers perhaps on the sound card because unity has access to that. perhaps to loop a 44100 long audio file and count num time plays and its position could work, i dont know whic solution is best. did you find what your technical issue was?
Your answer
Follow this Question
Related Questions
[Unity5] AudioSource PlayOneShot gives "Ran out of virtual channels" 0 Answers
How to get decibel's value of game's sound? 0 Answers
AudioSource won't play 2 Answers
Can you play a sound at a certain point (time) in the file? 1 Answer
How can I make Audio Reverb Zones effect only certain sounds? 0 Answers