Пример #1
0
    void processMidiEvent(MidiEvent mEvent)
    {
        if (mEvent.event_type == NOTE_ON)
        {
            //audio.Pause();
            Midi.Events.ChannelEvents.NoteOnEvent noe = (Midi.Events.ChannelEvents.NoteOnEvent)mEvent;
            //print(noe.note_number + " _ " + noe.delta_time);
            //print (midiEventIndex + ": " + noe.note_number + " _ " + noe.delta_time);

            if (noe.note_number > 63)
            {
                if (lastInputTime != float.NegativeInfinity)                 //note was hit early
                {
                    float diff = (audio.time - lastInputTime);
                    print("early! : " + diff);



                    acceptTimeDifference(diff, noe.note_number, lastInput);

                    lastInputTime = float.NegativeInfinity;
                }
                else
                {
                    matchNoteTime         = audio.time;
                    matchingNoteAvailable = true;
                    lastReceivedMatchNote = noe.note_number;
                }
            }
            else
            {
                //AudioSource.PlayClipAtPoint(hitClip, Vector3.zero);
                RelayBlender rb = noteToBlender[noe.note_number];
                relayWrite(rb.pin, Arduino.HIGH);

                //print("tick! " + rb.note);
                StartCoroutine(turnOffAfter(rb, .0125f));
            }
        }
        else if (mEvent.event_type == NOTE_OFF)
        {
            Midi.Events.ChannelEvents.NoteOffEvent noe = (Midi.Events.ChannelEvents.NoteOffEvent)mEvent;
            if (noteToBlender.ContainsKey(noe.note_number))
            {
                RelayBlender rb = noteToBlender[noe.note_number];
                relayWrite(rb.pin, Arduino.LOW);
            }

            //print(noe.parameter_1);
            //noe.
        }
    }
Пример #2
0
        private static MIDIEvent_Length_Tuple next_event(ByteEnumerable track_data, int start_index, byte last_midi_channel)
        {
            var i = start_index - 1;

            MidiEvent midi_event = null;
            {
                var delta_time = 0;
                {
                    var length_temp = new ByteList();
                    do
                    {
                        i += 1;
                        length_temp.Add(track_data.ElementAt(i));
                    } while (track_data.ElementAt(i) > 0x7F);

                    delta_time = VariableLengthUtil.decode_to_int(length_temp);
                }

                i += 1;

                var event_type_value = track_data.ElementAt(i);

                // MIDI Channel Events
                if ((event_type_value & 0xF0) < 0xF0)
                {

                    var midi_channel_event_type = (byte)(event_type_value & 0xF0);
                    var midi_channel = (byte)(event_type_value & 0x0F);
                    i += 1;
                    var parameter_1 = track_data.ElementAt(i);
                    var parameter_2 = (byte)0x00;

                    // One or two parameter type
                    switch (midi_channel_event_type)
                    {
                        // One parameter types
                        case 0xC0:
                            midi_event = new ProgramChangeEvent(delta_time, midi_channel, parameter_1);
                            last_midi_channel = midi_channel;
                            break;
                        case 0xD0:
                            midi_event = new ChannelAftertouchEvent(delta_time, midi_channel, parameter_1);
                            last_midi_channel = midi_channel;
                            break;

                        // Two parameter types
                        case 0x80:
                            i += 1;
                            parameter_2 = track_data.ElementAt(i);
                            midi_event = new NoteOffEvent(delta_time, midi_channel, parameter_1, parameter_2);
                            last_midi_channel = midi_channel;
                            break;
                        case 0x90:
                            i += 1;
                            parameter_2 = track_data.ElementAt(i);
                            midi_event = new NoteOnEvent(delta_time, midi_channel, parameter_1, parameter_2);
                            last_midi_channel = midi_channel;
                            break;
                        case 0xA0:
                            i += 1;
                            parameter_2 = track_data.ElementAt(i);
                            midi_event = new NoteAftertouchEvent(delta_time, midi_channel, parameter_1, parameter_2);
                            last_midi_channel = midi_channel;
                            break;
                        case 0xB0:
                            i += 1;
                            parameter_2 = track_data.ElementAt(i);
                            midi_event = new ControllerEvent(delta_time, midi_channel, parameter_1, parameter_2);
                            last_midi_channel = midi_channel;
                            break;
                        case 0xE0:
                            i += 1;
                            parameter_2 = track_data.ElementAt(i);
                            midi_event = new PitchBendEvent(delta_time, midi_channel, parameter_1, parameter_2);
                            last_midi_channel = midi_channel;
                            break;
                        // Might be a Control Change Messages LSB
                        default:
                            midi_event = new ControllerEvent(delta_time, last_midi_channel, event_type_value, parameter_1);
                            break;
                    }

                    i += 1;
                }
                // Meta Events
                else if (event_type_value == 0xFF)
                {
                    i += 1;
                    var meta_event_type = track_data.ElementAt(i);
                    i += 1;
                    var meta_event_length = track_data.ElementAt(i);
                    i += 1;
                    var meta_event_data = Enumerable.Range(i, meta_event_length).Select(b => track_data.ElementAt(b)).ToArray();

                    switch (meta_event_type)
                    {
                        case 0x00:
                            midi_event = new SequenceNumberEvent(BitConverter.ToUInt16(meta_event_data.Reverse().ToArray<byte>(), 0));
                            break;
                        case 0x01:
                            midi_event = new TextEvent(delta_time, stringEncoder.GetString(meta_event_data));
                            break;
                        case 0x02:
                            midi_event = new CopyrightNoticeEvent(stringEncoder.GetString(meta_event_data));
                            break;
                        case 0x03:
                            midi_event = new SequenceOrTrackNameEvent(stringEncoder.GetString(meta_event_data));
                            break;
                        case 0x04:
                            midi_event = new InstrumentNameEvent(delta_time, stringEncoder.GetString(meta_event_data));
                            break;
                        case 0x05:
                            midi_event = new LyricsEvent(delta_time, stringEncoder.GetString(meta_event_data));
                            break;
                        case 0x06:
                            midi_event = new MarkerEvent(delta_time, stringEncoder.GetString(meta_event_data));
                            break;
                        case 0x07:
                            midi_event = new CuePointEvent(delta_time, stringEncoder.GetString(meta_event_data));
                            break;
                        case 0x20:
                            midi_event = new MIDIChannelPrefixEvent(delta_time, meta_event_data[0]);
                            break;
                        case 0x2F:
                            midi_event = new EndOfTrackEvent(delta_time);
                            break;
                        case 0x51:
                            var tempo =
                                (meta_event_data[2] & 0x0F) +
                                ((meta_event_data[2] & 0xF0) * 16) +
                                ((meta_event_data[1] & 0x0F) * 256) +
                                ((meta_event_data[1] & 0xF0) * 4096) +
                                ((meta_event_data[0] & 0x0F) * 65536) +
                                ((meta_event_data[0] & 0xF0) * 1048576);
                            midi_event = new SetTempoEvent(delta_time, tempo);
                            break;
                        case 0x54:
                            midi_event = new SMPTEOffsetEvent(delta_time, meta_event_data[0], meta_event_data[1], meta_event_data[2], meta_event_data[3], meta_event_data[4]);
                            break;
                        case 0x58:
                            midi_event = new TimeSignatureEvent(delta_time, meta_event_data[0], meta_event_data[1], meta_event_data[2], meta_event_data[3]);
                            break;
                        case 0x59:
                            midi_event = new KeySignatureEvent(delta_time, meta_event_data[0], meta_event_data[1]);
                            break;
                        case 0x7F:
                            midi_event = new SequencerSpecificEvent(delta_time, meta_event_data);
                            break;
                    }

                    i += meta_event_length;
                }
                // System Exclusive Events
                else if (event_type_value == 0xF0 || event_type_value == 0xF7)
                {

                    var event_length = 0;
                    {
                        var length_temp = new ByteList();
                        do
                        {
                            i += 1;
                            length_temp.Add(track_data.ElementAt(i));
                        } while (track_data.ElementAt(i) > 0x7F);

                        event_length = VariableLengthUtil.decode_to_int(length_temp);
                    }

                    i += 1;

                    var event_data = Enumerable.Range(i, event_length).Select(b => track_data.ElementAt(b));

                    midi_event = new SysexEvent(delta_time, event_type_value, event_data);

                    i += event_length;
                }
            }

            switch (midi_event != null)
            {
                case true:
                    return new MIDIEvent_Length_Tuple(new SomeMidiEvent(midi_event), i - start_index, last_midi_channel);
            }

            return new MIDIEvent_Length_Tuple(new NoMidiEvent(), i - start_index, last_midi_channel);
        }