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); }