IEnumerator Play(List <float>[] records) { while (true) { if (loopToPlay.Count != 0) { Record[] tempCopy = new Record[loopToPlay.Count]; loopToPlay.CopyTo(tempCopy); foreach (var rec in tempCopy) { if (rec.sound != -1) { yield return(new WaitForSeconds(rec.delay)); sources[rec.sound].Play(); sources[rec.sound].pitch = CrownController.getMultiplicator(); } } loopToPlay[0] = new Record(-1, Time.time); } } }
// Update is called once per frame void Update() { // Debug.Log("Update ?"); /* * We just play without recording * Key to index mapping : * A = 0; B = 1 ..... Z = 25; * * If pressed twice the beat stops */ foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode))) { if (Input.GetKeyDown(kcode)) { int index = (int)kcode - 97; if (index >= 0 && index <= 26) { sources[index].Play(); sources[index].pitch = CrownController.getMultiplicator(); } if (index == (27 - 97)) { //we pressed ESC int i; for (i = 0; i < 26; i++) { sources[i].Stop(); loopToPlay.Clear(); } } //--------------------------------------------------------------------------- if (Input.GetKey("space")) { if ((int)kcode != 32) { Debug.Log((int)kcode); int indexRecDash = (int)kcode - 97; float t = Time.time; //Fix the xtart record time if (loopToPlay.Count == 0) { loopToPlay.Add(new AudioScript.Record(-1, t)); } float del = Time.time - loopToPlay[0].delay; Debug.Log(del); loopToPlay.Add(new Record(indexRecDash, del)); //Check if we need to insert if (loopToPlay[0].delay + loopToPlay[loopToPlay.Count - 1].delay < t + del) { loopToPlay.Add(new Record(indexRecDash, del)); } else { bool done = false; int i = 1; while (i < loopToPlay.Count && !done) { if (loopToPlay[0].delay + loopToPlay[i].delay < t + del) { loopToPlay.Insert(i, new Record(indexRecDash, del)); done = true; } } } Record last = loopToPlay[loopToPlay.Count - 1]; Debug.Log(last.sound); Debug.Log(last.delay); //loopToPlay.Add(new Record(indexRecDash, del)); } } } } }