示例#1
0
        void Loop()
        {
            byte [] buf = new byte [BufferSize];
            int     idx = 0;

            while (loop)
            {
                if (idx >= buf.Length - 50)
                {
                    throw new Exception("Insufficient buffer.");

                    /* FIXME: enable this once I sorted out why memory access violation happens.
                     *      byte [] newbuf = new byte [buf.Length * 2];
                     *      Array.Copy (buf, 0, newbuf, 0, buf.Length);
                     *      buf = newbuf;
                     */
                }
                // some interval is required to stably receive messages...
                Thread.Sleep((int)Interval.TotalMilliseconds);
                int size = input_device.Read(buf, idx, buf.Length - idx);
                // Console.WriteLine ("{0} bytes, {1:X02}",  size, buf [idx]);
//for (int i = 0; i < size; i++) Console.Write ("{0:X02} ", buf [i]);
                idx += size;
            }
            foreach (var ev in MidiInput.Convert(buf, 0, idx))
            {
                results.Add(ev);
            }
            //using (var fs = File.Create ("tmp.bin"))
            //	fs.Write (buf, 0, idx);
        }
        public void Update()
        {
            if (_input == null)
            {
                return;
            }

            if (_input.HasData)
            {
                // Portmidi reports 4 bytes per event but the buffer has 8 bytes so we multiply count by 2
                int count = (_input.Read(_data, 0, _data.Length)) * 2;
                for (int i = 0; i < count; i += 8)
                {
                    ConvertAndSend(_data[i], _data[i + 1], _data[i + 2]);
                }
            }
        }