tracker_note_found findPrevNote(float t, bool tIsBeat = false) { if (!tIsBeat) { //COVERT T TO REALTIME t = this.tToBeat(t); } float curBeat = Mathf.Floor(t); tracker_note_found noteFound = new tracker_note_found(); noteFound.noteIndex = -1; noteFound.distanceFromT = 0; if (t > endT) { if (notesToPlay.loop) { var totalPlays = Mathf.Floor(t / endT); t = t - (totalPlays * endT); curBeat = Mathf.Floor(t); } else { curBeat = Mathf.Floor(endT); } } //RETURN LAST NOTE float highestNoteT = startT; int highestNote = -1; if (notes_per_beat.ContainsKey(curBeat)) { foreach (var curBeatInfo in notes_per_beat[curBeat]) { var curNote = notesToPlay.notes[curBeatInfo]; if ((curNote.triggerOn > highestNoteT || highestNote == -1) && curNote.triggerOn <= t) { highestNoteT = curNote.triggerOn; highestNote = curBeatInfo; } } if (highestNote != -1) { noteFound.noteIndex = highestNote; noteFound.distanceFromT = t - highestNoteT; } } return(noteFound); }
tracker_note_found findNextNote(float t, bool tIsBeat = false) { if (!tIsBeat) { //COVERT T TO REALTIME t = this.tToBeat(t); } float curBeat = Mathf.Floor(t); tracker_note_found noteFound = new tracker_note_found(); noteFound.noteIndex = -1; noteFound.distanceFromT = 0; if (t < startT) { //RETURN FIRST NOTE noteFound.noteIndex = 0; noteFound.distanceFromT = startT - t; } if (t <= endT) { //FIND THE BEAT - LOOP THROUGH THE NOTES IN THE BEAT float lowestNoteT = endT; int lowestNote = -1; foreach (var curBeatInfo in notes_per_beat[curBeat]) { var curNote = notesToPlay.notes[curBeatInfo]; if (curNote.triggerOn < lowestNote || lowestNote == -1) { lowestNoteT = curNote.triggerOn; lowestNote = curBeatInfo; } } if (lowestNote != -1) { noteFound.noteIndex = lowestNote; noteFound.distanceFromT = t - lowestNoteT; } } return(noteFound); }
// Update is called once per frame void Update() { //BEAT PER T beat += (bpm / 60) * Time.deltaTime; //lastNote = findPrevNote(beat,true); lastNote = findPrevNote(beat, true); if (lastNote.noteIndex < notesToPlay.notes.Length && lastNote.noteIndex >= 0) { var curNoteInfo = notesToPlay.notes[lastNote.noteIndex]; notesToPlay.instrument.play_frequency = notesToPlay.notes[lastNote.noteIndex].frequency; if (lastNote.distanceFromT < curNoteInfo.triggerLength) { notesToPlay.instrument.trigger = true; } else { notesToPlay.instrument.trigger = false; } } }