예제 #1
0
        /// <summary>
        /// Handle key events.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Vkey_KeyboardEvent(object?sender, VirtualKeyboard.KeyboardEventArgs e)
        {
            if (DeviceInputEvent is not null)
            {
                Step step;

                if (e.Velocity != 0)
                {
                    step = new StepNoteOn()
                    {
                        Device         = this,
                        ChannelNumber  = e.ChannelNumber,
                        NoteNumber     = e.NoteId,
                        Velocity       = e.Velocity,
                        VelocityToPlay = e.Velocity,
                        Duration       = new Time(0)
                    };
                }
                else
                {
                    step = new StepNoteOff()
                    {
                        Device        = this,
                        ChannelNumber = e.ChannelNumber,
                        NoteNumber    = e.NoteId,
                        Velocity      = 0,
                    };
                }

                DeviceInputEvent?.Invoke(this, new DeviceInputEventArgs()
                {
                    Step = step
                });
                if (UserSettings.TheSettings.MonitorInput)
                {
                    _logger.Trace($"{TraceCat.RCV} KbdIn:{step}");
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Handle a received message.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OscInput_InputEvent(object?sender, NebOsc.InputEventArgs e)
        {
            // message could be:
            // /note/ channel notenum vel
            // /controller/ channel ctlnum val

            e.Messages.ForEach(m =>
            {
                Step?step = null;

                switch (m.Address)
                {
                case "/note/":
                    if (m.Data.Count == 3)
                    {
                        int channel     = MathUtils.Constrain((int)m.Data[0], 0, 100);
                        double notenum  = MathUtils.Constrain((int)m.Data[1], 0, Definitions.MAX_MIDI);
                        double velocity = MathUtils.Constrain((int)m.Data[2], 0, 1.0);

                        if (velocity == 0)
                        {
                            step = new StepNoteOff()
                            {
                                Device        = this,
                                ChannelNumber = channel,
                                NoteNumber    = notenum,
                                Velocity      = 0
                            };
                        }
                        else
                        {
                            step = new StepNoteOn()
                            {
                                Device         = this,
                                ChannelNumber  = channel,
                                NoteNumber     = notenum,
                                Velocity       = velocity,
                                VelocityToPlay = velocity,
                                Duration       = new Time(0)
                            };
                        }
                    }
                    break;

                case "/controller/":
                    if (m.Data.Count == 3)
                    {
                        int channel  = MathUtils.Constrain((int)m.Data[0], 0, 100);
                        int ctlnum   = (int)m.Data[1];
                        double value = MathUtils.Constrain((int)m.Data[2], 0, 10000);

                        if (Enum.IsDefined(typeof(ControllerDef), ctlnum))
                        {
                            step = new StepControllerChange()
                            {
                                Device        = this,
                                ChannelNumber = channel,
                                ControllerId  = (ControllerDef)ctlnum,
                                Value         = value
                            };
                        }
                        else
                        {
                            _logger.Error($"Invalid controller: {ctlnum}");
                        }
                    }
                    break;

                default:
                    _logger.Error($"Invalid address: {m.Address}");
                    break;
                }

                if (step is not null)
                {
                    // Pass it up for handling.
                    DeviceInputEventArgs args = new() { Step = step };
                    DeviceInputEvent?.Invoke(this, args);
                    if (UserSettings.TheSettings.MonitorInput)
                    {
                        _logger.Trace($"{TraceCat.RCV} OscIn:{step}");
                    }
                }
            });
        }
예제 #3
0
        /// <summary>
        /// Process input midi event. Note that NoteOn with 0 velocity are converted to NoteOff.
        /// </summary>
        void MidiIn_MessageReceived(object?sender, MidiInMessageEventArgs e)
        {
            // Decode the message. We only care about a few.
            MidiEvent me   = MidiEvent.FromRawMessage(e.RawMessage);
            Step?     step = null;

            switch (me)
            {
            case NoteOnEvent evt:
            {
                if (evt.Velocity == 0)
                {
                    step = new StepNoteOff()
                    {
                        Device        = this,
                        ChannelNumber = evt.Channel,
                        NoteNumber    = MathUtils.Constrain(evt.NoteNumber, 0, Definitions.MAX_MIDI),
                        Velocity      = 0.0
                    };
                }
                else
                {
                    step = new StepNoteOn()
                    {
                        Device         = this,
                        ChannelNumber  = evt.Channel,
                        NoteNumber     = evt.NoteNumber,
                        Velocity       = evt.Velocity / Definitions.MAX_MIDI,
                        VelocityToPlay = evt.Velocity / Definitions.MAX_MIDI,
                        Duration       = new Time(0)
                    };
                }
            }
            break;

            case NoteEvent evt:
            {
                step = new StepNoteOff()
                {
                    Device        = this,
                    ChannelNumber = evt.Channel,
                    NoteNumber    = MathUtils.Constrain(evt.NoteNumber, 0, Definitions.MAX_MIDI),
                    Velocity      = evt.Velocity / Definitions.MAX_MIDI
                };
            }
            break;

            case ControlChangeEvent evt:
            {
                step = new StepControllerChange()
                {
                    Device        = this,
                    ChannelNumber = evt.Channel,
                    ControllerId  = (ControllerDef)evt.Controller,
                    Value         = evt.ControllerValue
                };
            }
            break;

            case PitchWheelChangeEvent evt:
            {
                step = new StepControllerChange()
                {
                    Device        = this,
                    ChannelNumber = evt.Channel,
                    ControllerId  = ControllerDef.PitchControl,
                    Value         = MathUtils.Constrain(evt.Pitch, 0, MidiUtils.MAX_PITCH),
                };
            }
            break;
            }

            if (step is not null)
            {
                // Pass it up for handling.
                DeviceInputEventArgs args = new() { Step = step };
                DeviceInputEvent?.Invoke(this, args);
                if (UserSettings.TheSettings.MonitorInput)
                {
                    _logger.Trace($"{TraceCat.RCV} MidiIn:{step}");
                }
            }
        }

        /// <summary>
        /// Process error midi event - Parameter 1 is invalid.
        /// </summary>
        void MidiIn_ErrorReceived(object?sender, MidiInMessageEventArgs e)
        {
            _logger.Error($"Message:0x{e.RawMessage:X8}");
        }

        #endregion
    }