Ejemplo n.º 1
0
    public static void loadMidi(TextAsset midiText)
    {
        string[,] midiData;		//Parsed Midi Data

        int format;				//0 = single track. 1 = multi track
        int tracks;				//number of tracks, should be 1
        int tempo = 0;			//track tempo.
        int beatsPerBar = 0;
        int bpm;
        int ppq;

        int currentClock;
        int currentChannel;
        int currentInstrumentCode;
        int currentVelocity;
        float currentMs;
        string currentCommand;

        //Load Data file
        midiData = CSVReader.SplitCsvGrid (midiText.text);
        ppq = System.Int32.Parse(midiData [5, 0]);

        //Empty current track to prepare for loading
        CurrentTrack = new Track();

        CurrentTrack.TrackName = midiText.name;

        //Load the Midi into the Track row by row;
        for (int x = 0; x < midiData.GetLength(1); x += 1) {

            if (!System.Int32.TryParse(midiData[1,x], out currentClock))
            {
                break;
            }

            currentCommand = midiData[2,x].Trim ();

            //Get the channel/instrument info only when we have instructions to play a note
            if(currentCommand == "Note_on_c"){
                currentChannel = System.Int32.Parse(midiData[3,x]);
            }else{
                currentChannel = -1;
            }

            //Update tempo only once
            if(currentCommand == "Tempo" && tempo == 0){
                tempo = System.Int32.Parse(midiData[3,x]);
            }

            //Update Time Signature only once
            if(currentCommand == "Time_signature" && beatsPerBar == 0){
                beatsPerBar = System.Int32.Parse(midiData[3,x]);
            }

            //Convert ticks to Milliseconds
            currentMs = tempo / 1000.0f / ppq * currentClock;

            if(currentChannel == 9){

                currentInstrumentCode = System.Int32.Parse(midiData[4,x]);
                currentVelocity = System.Int32.Parse(midiData[5,x]);

                if(currentVelocity !=0){
                    TrackRow trackRow = new TrackRow (currentMs, DrumKit.getInstrumentFromMidiCode(currentInstrumentCode));

                    CurrentTrack.addTrackRow(trackRow);

                }
            }
        }

        bpm = (int) 60000000.0f / tempo;

        CurrentTrack.bpm = bpm;
        CurrentTrack.beatsPerBar = beatsPerBar;
    }