Пример #1
0
        public static ToneThumbEvent ParseEvent(byte[] data)
        {
            var type = (ThumbType)CharToInt(data[0]);
            var dir  = (ThumbDirection)CharToInt(data[1]);
            var ev   = new ToneThumbEvent(type, dir);

            return(ev);
        }
Пример #2
0
        private void handleThumbEvent(ToneThumbEvent e)
        {
            if (!thumbMapping.ContainsKey(e.Type))
            {
                return;
            }

            var val = thumbMapping[e.Type];

            switch (e.Direction)
            {
            case ThumbDirection.Clockwise:
                val += ThumbStep;
                break;

            case ThumbDirection.CounterClockwise:
                val -= ThumbStep;
                break;
            }
            val = Math.Min(1, Math.Max(0, val));

            thumbMapping[e.Type] = val;

            switch (e.Type)
            {
            case ThumbType.CutoffLow:
            case ThumbType.CutoffHigh:
                updateFrequencyFilter();
                break;

            case ThumbType.Attack:
            case ThumbType.Decay:
            case ThumbType.Sustain:
            case ThumbType.Release:
                updateADSR();
                break;
            }
        }
Пример #3
0
        public EventReadResult Read()
        {
            if (!initialized)
            {
                serialPort.BaudRate     = 9600;
                serialPort.ReadTimeout  = 500;
                serialPort.WriteTimeout = 500;
                serialPort.PortName     = SerialPortName;
                serialPort.DataBits     = 8;
                serialPort.Parity       = Parity.None;
                serialPort.StopBits     = StopBits.One;
                serialPort.Handshake    = Handshake.None;
                serialPort.Open();
                initialized = true;
            }

            int typeInt;

            do
            {
                try
                {
                    var typeChar = (char)serialPort.ReadChar();
                    typeInt = (int)char.GetNumericValue(typeChar);
                }
                catch (TimeoutException)
                {
                    return(EventReadResult.Timeout);
                }
            } while (typeInt > 4);

            var type = (ToneEventType)typeInt;

            try
            {
                byte[] buffer;
                switch (type)
                {
                case ToneEventType.Key:
                    buffer = ReadBytes(ToneKeyEvent.DataLength);
                    Event  = ToneKeyEvent.ParseEvent(buffer, 0);
                    break;

                case ToneEventType.Thumb:
                    buffer = ReadBytes(ToneThumbEvent.DataLength);
                    Event  = ToneThumbEvent.ParseEvent(buffer);
                    break;

                case ToneEventType.Filter:
                    Event = new ToneFilterEvent();
                    break;

                case ToneEventType.Wave:
                    Event = new ToneWaveEvent();
                    break;
                }
            }
            catch (TimeoutException)
            {
                return(EventReadResult.Timeout);
            }

            return(EventReadResult.Success);
        }