Syncronizing music with variables

Hi all,

I’m trying to make an app with a Guitar Hero like style.
The thing is… I’m having problems sync’ing the music with my variable update loop.

So far, i have a midi file with the time and notes (4 types of note). From this midi, i’ve converted it to a txt file with the time, note and a flag telling me when each note started and finished to play.

With this txt loaded into a list, i do this:

	IEnumerator UpdateVars () {
		print(tempoCont + " " + listaNotas[contNotas].tempo);
		if (tempoCont == listaNotas[contNotas].tempo) // if the actual time (tick) is equal to the time of the next note to be played
		{
			// Update Variables
		}
		tempoCont++; // inc time tick
		yield return new WaitForSeconds(coefTempo);
		StartCoroutine(UpdateVars()); // wait or a time calculated based on the BPM (beats per minute) of each song
		}
	}

The problem is that the time is not synced. It looks like WaitForSeconds doesn’t support coefTempo and rounds it up. In my test, coefTempo = 0.0024305

Does WaitForSeconds really have this limitation ?
If anyone have any other suggestion on how can i do this, i appreciate.

Sorry for my english,

Raphael

the problem is that yield and WaitForSeconds, Invoke, StartCoroutine, etc… have not parallel execution with others gameobjects or component… they are serial !!!

I have the same problems (take a look here and here) and it seems that the final solution is represented in using threads.

But if someone have some different solutions, please… post it.

Thanks.

Thanks for the reply Apache !
I’ll try to use threads instead of coroutines.
I’ve tried once the use of threads, but i had a serious problem of apparently memory leak.
But i’ll give another try on them.

Well… it’s not so simple: take a look here !!!

At the moment you can do it if you will not call Unity’s APIs in the thread you will spawn… the better solution could be, for now, represented by using something like

	/// <summary>
	/// called to start the sound play
	/// </summary>
	public override void playSound () {
		startTime = Time.time;
		if (audio) {
			StartCoroutine (startPlaying ());
		} // if
	} // playSound

	/// <summary>
	/// it starts to play the 3 audio clips
	/// </summary>	
	private IEnumerator startPlaying () {
		audio.clip = sounds[0];
		audio.loop = false;
		audio.Play ();
		soundPlaying = true;
		while (audio.isPlaying) {
			yield return new WaitForEndOfFrame (); 
		} // while
		audio.clip = sounds[1];
		float currentLoopTime = 0f;
		while (true) {
			if (!audio.isPlaying) {
				if (currentLoopTime < loopLength) {
					audio.Play ();
					currentLoopTime += sounds[1].length;
				} else {
					break;
				} // if-else
			} // if
			yield return new WaitForEndOfFrame ();
		} // while
		audio.clip = sounds[2];
		audio.Play ();
		while (audio.isPlaying) {
			yield return new WaitForEndOfFrame (); 
		} // while
		soundPlaying = false;
	} // startPlaying

please note that this is C# code !!!

Let me know if you will manage through threads issue :wink: