Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[?]Play MIDI song: Create Thread to Static Class

Discussion in 'Scripting' started by IsGreen, Oct 14, 2016.

  1. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    I can easily create rhythms and songs from a midi sequencer as: https://onlinesequencer.net/

    Also, I can create sound effects using musical notation and a multitude of instruments.
    And all this from a .dll plugin 68 KB with 128 instruments: http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/VisualStudio/HowToPlayMIDIInstruments

    This plugin uses a static class MidiPlayer, and the static method Play to listen to songs and musical notes.

    This static class makes Unity remains frozen when you hear a song file .mid.

    I've been using System.Threading to create a unique thread for MidiPlayer.Play but I have not succeeded.

    Some help?

    Not to start from scratch. Here is the link of the Project:https://forum.unity3d.com/attachments/unitymidi-unitypackage.205244/?temp_hash=2eb13a5b59f9e4640f57dc1f59bbf6ff

    Code (CSharp):
    1. using UnityEngine;
    2. using Toub.Sound.Midi;
    3. using System.Collections;
    4. using System.Threading;
    5.  
    6. public class PlayMusic : MonoBehaviour {
    7.  
    8.     public int escala = 3;
    9.     //public KeyCode pressed;
    10.     public GeneralMidiInstruments instrumento = GeneralMidiInstruments.AcousticGrand;
    11.     public KeyCode[] teclas = new KeyCode[] {   KeyCode.Q,KeyCode.W,KeyCode.E,
    12.                                                 KeyCode.R,KeyCode.T,KeyCode.Y,
    13.                                                 KeyCode.U,KeyCode.I,KeyCode.O,
    14.                                                 KeyCode.P,KeyCode.Semicolon,KeyCode.Equals,
    15.                                                 KeyCode.Alpha2,KeyCode.Alpha3,
    16.                                                 KeyCode.Alpha5,KeyCode.Alpha6,KeyCode.Alpha7};
    17.  
    18.     private string[] notas = new string[] { "CX", "DX", "EX", "FX", "GX", "AX", "BX", "CY", "DY", "EY", "FY", "GY", "C#X", "D#X", "F#X", "G#X", "A#X" };
    19.     private byte canal = 0;
    20.     private PlayMIDI playMIDI;
    21.     private Thread thread;
    22.     private string path;
    23.  
    24.     // Use this for initialization
    25.     void Start () {
    26.    
    27.         MidiPlayer.OpenMidi();
    28.         MidiPlayer.Play(new ProgramChange(0, this.canal, this.instrumento));
    29.         //this.path = Application.dataPath + "/cancion.mid";
    30.         //Debug.Log(path);
    31.         //this.thread = new Thread(func);
    32.     }
    33.  
    34.     void func()
    35.     {
    36.         MidiPlayer.OpenMidi();
    37.         MidiPlayer.Play(this.path);    
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update () {
    42.         /*
    43.         if (Input.anyKey)
    44.         {
    45.             KeyCode[] keys= System.Enum.GetValues(typeof(KeyCode)) as KeyCode[];
    46.             for(int w = 0; w < keys.Length; w++)
    47.        
    48.                 if (Input.GetKey(keys[w])) this.pressed = keys[w];
    49.             }
    50.         }
    51.         */
    52.  
    53.         if (Input.GetKeyDown(KeyCode.Space))
    54.         {
    55.             //this.playMIDI = new PlayMIDI(Application.dataPath + "/cancion.mid");
    56.             //if (this.playMIDI.IsDone) this.playMIDI.Start();
    57.             //this.playMIDI.Start();
    58.             //this.thread.Start();
    59.             Debug.Log("cancion");
    60.         }
    61.  
    62.         for (int i=0;i<this.notas.Length;i++)
    63.         {
    64.        
    65.             KeyCode k = this.teclas[i];
    66.             if (Input.GetKeyDown(k))
    67.             {
    68.                 string nota = this.notas[i].Replace("X", this.escala.ToString());
    69.                 nota = nota.Replace("Y", (this.escala + 1).ToString());
    70.                 this.StartCoroutine(this.Play(k, nota, this.canal));
    71.             }
    72.         }
    73.     }
    74.  
    75.     IEnumerator Play(KeyCode key, string nota, byte canal)
    76.     {
    77.         MidiPlayer.Play(new NoteOn(0, canal, nota, 127));
    78.         while (Input.GetKey(key)) yield return null;
    79.         MidiPlayer.Play(new NoteOff(0, canal, nota, 127));
    80.     }
    81.  
    82.    
    83.  
    84.     void OnDestroy()
    85.     {
    86.         MidiPlayer.CloseMidi();
    87.     }
    88. }
    89.  
     

    Attached Files:

    Last edited: Oct 16, 2016
  2. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Bump.
     
  3. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Last Bump.