Пример #1
0
        /// <summary>Parse a voice event from the data stream.</summary>
        /// <param name="deltaTime">The previously parsed delta-time for this event.</param>
        /// <param name="messageType">The previously parsed type of message we're expecting to find.</param>
        /// <param name="channel">The previously parsed channel for this message.</param>
        /// <param name="data">The data stream from which to read the event information.</param>
        /// <param name="pos">The position of the start of the event information.</param>
        /// <returns>The parsed voice MIDI event.</returns>
        private static MidiEvent ParseVoiceEvent(long deltaTime, byte messageType, byte channel, byte [] data, ref long pos)
        {
            try
            {
                MidiEvent tempEvent = null;

                // Create the correct voice event based on its message id/type
                switch(messageType)
                {
                        // NOTE OFF
                    case 0x8:
                        tempEvent = new NoteOff(deltaTime, channel, data[pos], data[pos+1]);
                        pos += 2;
                        break;

                        // NOTE ON
                    case 0x9:
                        tempEvent = new NoteOn(deltaTime, channel, data[pos], data[pos+1]);
                        pos += 2;
                        break;

                        // AFTERTOUCH
                    case 0xA:
                        tempEvent = new Aftertouch(deltaTime, channel, data[pos], data[pos+1]);
                        pos += 2;
                        break;

                        // CONTROLLER
                    case 0xB:
                        tempEvent = new Controller(deltaTime, channel, data[pos], data[pos+1]);
                        pos += 2;
                        break;

                        // PROGRAM CHANGE
                    case 0xC:
                        tempEvent = new ProgramChange(deltaTime, channel, data[pos]);
                        pos += 1;
                        break;

                        // CHANNEL PRESSURE
                    case 0xD:
                        tempEvent = new ChannelPressure(deltaTime, channel, data[pos]);
                        pos += 1;
                        break;

                        // PITCH WHEEL
                    case 0xE:
                        int position = ((data[pos] << 8) | data[pos+1]);
                        byte upper, lower;
                        MidiEvent.Split14BitsToBytes(position, out upper, out lower);
                        tempEvent = new PitchWheel(deltaTime, channel, upper, lower);
                        pos += 2;
                        break;

                        // UH OH!
                    default: throw new ArgumentOutOfRangeException("messageType", messageType, "Not a voice message.");
                }

                // Return the newly parsed event
                return tempEvent;
            }
                // Something bad happened; wrap it in a parser exception
            catch(Exception exc) { throw new MidiParserException("Unable to parse voice MIDI event.", exc, pos); }
        }
Пример #2
0
        // TODO
        private void NoteOn( int note, int velocity )
        {
            if ( velocity > 126 )
            {
                velocity = 126;
            }

            byte temp = (byte)( note / 10 );
            byte velocityValue = (byte)(velocity);

            if (temp == LastNoteNo)
                return;

            if (LastNoteNo != 255)
                NoteOff();

            // Convert the raw val to a MIDI note by dividing by 10
            LastNoteNo = temp;

            Toub.Sound.Midi.NoteOn _n = new NoteOn( 0, 0x0, LastNoteNo, velocityValue );
            Toub.Sound.Midi.MidiPlayer.Play(_n);
        }
        private static MidiEvent ParseVoiceEvent(long deltaTime, byte messageType, byte channel, byte[] data, ref long pos)
        {
            MidiEvent event3;
            try
            {
                MidiEvent event2 = null;
                switch (messageType)
                {
                    case 8:
                        event2 = new NoteOff(deltaTime, channel, data[(int) ((IntPtr) pos)], data[(int) ((IntPtr) (pos + 1L))]);
                        pos += 2L;
                        break;

                    case 9:
                        event2 = new NoteOn(deltaTime, channel, data[(int) ((IntPtr) pos)], data[(int) ((IntPtr) (pos + 1L))]);
                        pos += 2L;
                        break;

                    case 10:
                        event2 = new Aftertouch(deltaTime, channel, data[(int) ((IntPtr) pos)], data[(int) ((IntPtr) (pos + 1L))]);
                        pos += 2L;
                        break;

                    case 11:
                        event2 = new Controller(deltaTime, channel, data[(int) ((IntPtr) pos)], data[(int) ((IntPtr) (pos + 1L))]);
                        pos += 2L;
                        break;

                    case 12:
                        event2 = new ProgramChange(deltaTime, channel, data[(int) ((IntPtr) pos)]);
                        pos += 1L;
                        break;

                    case 13:
                        event2 = new ChannelPressure(deltaTime, channel, data[(int) ((IntPtr) pos)]);
                        pos += 1L;
                        break;

                    case 14:
                    {
                        byte num2;
                        byte num3;
                        int bits = (data[(int) ((IntPtr) pos)] << 8) | data[(int) ((IntPtr) (pos + 1L))];
                        MidiEvent.Split14BitsToBytes(bits, out num2, out num3);
                        event2 = new PitchWheel(deltaTime, channel, num2, num3);
                        pos += 2L;
                        break;
                    }
                    default:
                        throw new ArgumentOutOfRangeException("messageType", messageType, "Not a voice message.");
                }
                event3 = event2;
            }
            catch (Exception exception)
            {
                throw new MidiParserException("Unable to parse voice MIDI event.", exception, pos);
            }
            return event3;
        }