|
Post by chrisoshea on Oct 5, 2015 15:41:23 GMT
(cross posting from Unity forum to see if you have any ideas) I want to run a timer that says play a sound sample every 600 milliseconds. The accuracy is really important, as when played against a backing metronome track, any slight slip in time you can hear easily. I've tried this: float timeLastBeat; Update(){
if(time.timeSinceLevelLoad - timeLastBeat >= 0.6f){ MasterAudio.play() timeLastBeat = time.timeSinceLevelLoad; }
i've also tried using Stopwatch usingSystem.Diagnostics; float timeLastBeat;
Start(){ timer = newStopwatch(); timer.start(); } Update(){
if(timer.ElapsedMilliseconds - timeLastBeat >= 600.0f){ MasterAudio.play() timeLastBeat = timer.ElapsedMilliseconds; }
both create slight variations in gaps between beats, 600ms, 612ms, 650ms etc. I'm guessing this is an Update speed/frequency issue and even a coroutine would be the same. I found this docs.unity3d.com/ScriptReference/AudioSettings-dspTime.htmlbut I don't want to generate a sound from script, I want to play one using MasterAudio. I can't put a MasterAudio call inside OnAudioFilterRead. Any thoughts? Thanks
|
|
|
Post by DarkTonic Dev on Oct 6, 2015 1:10:24 GMT
Definitely any code in a update method won't fire at a consistent time. That's based on your frame rate only.
You could try this. Not guaranteed to work but it may. If it does, you'll need to add more logic to make it not schedule again until after the currently scheduled one (if any) has started playing.
var currentDspTime = AudioSettings.dspTime; const float timeToWait = .5f; var futureTimeToPlay = (float) (currentDspTime + timeToWait); MasterAudio.PlaySound3DFollowTransform("BeatSoundGroup", this.transform, 1f, 1f, futureTimeToPlay);
This *should* play the sound exactly 0.5 seconds after "now". I'm not sure however if the time it takes Master Audio to get to the scheduling code will come into play and make this "off" or not. Please report back your results, but I have no other suggestions.
|
|