/// <summary> /// Helper to create a drum hit /// </summary> /// <returns></returns> private MPTKNote CreateDrum(int key, float delay) { MPTKNote note = new MPTKNote() { Note = key, Drum = true, Duration = 0, Patch = 0, Velocity = Velocity, Delay = delay, }; return(note); }
/// <summary> /// Helper to create a note /// </summary> /// <returns></returns> private MPTKNote CreateNote(int key, float delay) { MPTKNote note = new MPTKNote() { Note = key, Drum = false, Duration = DelayTimeChange * 1000f, Patch = CurrentPatch, Velocity = Velocity, Delay = delay, }; return(note); }
/// <summary> /// Helper to create a random note (not yet used) /// </summary> /// <returns></returns> private MPTKNote CreateRandomNote() { MPTKNote note = new MPTKNote() { Note = 50 + UnityEngine.Random.Range(0, 4) * 2, Drum = false, Duration = UnityEngine.Random.Range(100, 1000), Patch = CurrentPatch, Velocity = Velocity, Delay = UnityEngine.Random.Range(0, 200), }; return(note); }
public void NotesToPlay(List <MidiNote> notes) { //Debug.Log(notes.Count); foreach (MidiNote note in notes) { if (note.Midi > 40 && note.Midi < 100)// && note.Channel==1) { float z = Mathf.Lerp(minZ, maxZ, (note.Midi - 40) / 60f); Vector3 position = new Vector3(maxX, 8, z); NoteView n = Instantiate <NoteView>(NoteDisplay, position, Quaternion.identity); n.gameObject.SetActive(true); n.midiFilePlayer = midiFilePlayer; n.note = note; n.gameObject.GetComponent <Renderer>().material = n.NewNote; n.zOriginal = position.z; MPTKNote mptkNote = new MPTKNote() { Delay = 0, Drum = false, Duration = 0.2f, Note = 60, Patch = 10, Velocity = 100 }; mptkNote.Play(midiStreamPlayer); } } }
// Update is called once per frame void Update() { if (midiStreamPlayer != null) { float time = Time.realtimeSinceStartup - LastTimeChange; if (time > DelayTimeChange) { // It's time to generate a note LastTimeChange = Time.realtimeSinceStartup; if (NotePlaying != null) { //Debug.Log("Stop note"); // Stop the note (method to simulate a real human on a keyboard : duration is not known when note is triggers) NotePlaying.Stop(); NotePlaying = null; } if (RandomPlay) { // // First method to play notes: send a list of notes directly to the MidiStreamPlayer // Useful for a long list of notes. MPTKNote is the class to do this. // List <MPTKNote> notes = new List <MPTKNote>(); // Very light random notes generator if (!DrumKit) { // Play 3 notes with no delay int rnd = UnityEngine.Random.Range(-8, 8); notes.Add(CreateNote(60 + rnd, 0)); notes.Add(CreateNote(64 + rnd, 0)); notes.Add(CreateNote(67 + rnd, 0)); } else { // Play 3 hit with a short delay notes.Add(CreateDrum(UnityEngine.Random.Range(0, 127), 0)); notes.Add(CreateDrum(UnityEngine.Random.Range(0, 127), 150)); notes.Add(CreateDrum(UnityEngine.Random.Range(0, 127), 300)); } // Send the note to the player. Notes are plays in a thread, so call returns immediately midiStreamPlayer.MPTK_Play(notes); } else { // // Second methods to play notes: Play note from the Play method of MPTKNote // Useful if you want to stop playing the note by script. // Here a big value (99 sec.) is defined for Duration, the note and the waves associated, // are stopped when NotePlaying.Stop() is call (si above) // Notes are plays sequencially. if (++CurrentNote > EndNote) { CurrentNote = StartNote; } NotePlaying = new MPTKNote() { Note = CurrentNote, Delay = 0, Drum = DrumKit, // drum kit is plays (same as Midi canal 10) if true Duration = 99999, Patch = CurrentPatch, Velocity = Velocity // Sound can vary depending on the velocity }; NotePlaying.Play(midiStreamPlayer); } } } }