MidiSheetMusic.MidiFile.ApplyOptionsToEvents C# (CSharp) Method

ApplyOptionsToEvents() private method

private ApplyOptionsToEvents ( MidiOptions options ) : List[]
options MidiOptions
return List[]
        private List<MidiEvent>[] ApplyOptionsToEvents(MidiOptions options)
        {
            int i;
            if (trackPerChannel) {
            return ApplyOptionsPerChannel(options);
            }

            /* A midifile can contain tracks with notes and tracks without notes.
             * The options.tracks and options.instruments are for tracks with notes.
             * So the track numbers in 'options' may not match correctly if the
             * midi file has tracks without notes. Re-compute the instruments, and
             * tracks to keep.
             */
            int num_tracks = events.Length;
            int[] instruments = new int[num_tracks];
            bool[] keeptracks = new bool[num_tracks];
            for (i = 0; i < num_tracks; i++) {
            instruments[i] = 0;
            keeptracks[i] = true;
            }
            for (int tracknum = 0; tracknum < tracks.Count; tracknum++) {
            MidiTrack track = tracks[tracknum];
            int realtrack = track.Number;
            instruments[realtrack] = options.instruments[tracknum];
            if (options.tracks[tracknum] == false ||
                options.mute[tracknum] == true) {
                keeptracks[realtrack] = false;
            }
            }

            List<MidiEvent>[] newevents = CloneMidiEvents(events);

            /* Set the tempo at the beginning of each track */
            for (int tracknum = 0; tracknum < newevents.Length; tracknum++) {
            MidiEvent mevent = CreateTempoEvent(options.tempo);
            newevents[tracknum].Insert(0, mevent);
            }

            /* Change the note number (transpose), instrument, and tempo */
            for (int tracknum = 0; tracknum < newevents.Length; tracknum++) {
            foreach (MidiEvent mevent in newevents[tracknum]) {
                int num = mevent.Notenumber + options.transpose;
                if (num < 0)
                    num = 0;
                if (num > 127)
                    num = 127;
                mevent.Notenumber = (byte)num;
                if (!options.useDefaultInstruments) {
                    mevent.Instrument = (byte)instruments[tracknum];
                }
                mevent.Tempo = options.tempo;
            }
            }

            if (options.pauseTime != 0) {
            newevents = StartAtPauseTime(newevents, options.pauseTime);
            }

            /* Change the tracks to include */
            int count = 0;
            for (int tracknum = 0; tracknum < keeptracks.Length; tracknum++) {
            if (keeptracks[tracknum]) {
                count++;
            }
            }
            List<MidiEvent>[] result = new List<MidiEvent>[count];
            i = 0;
            for (int tracknum = 0; tracknum < keeptracks.Length; tracknum++) {
            if (keeptracks[tracknum]) {
                result[i] = newevents[tracknum];
                i++;
            }
            }
            return result;
        }