Exemplo n.º 1
0
        public void Start()
        {
            if (IsRunning)
            {
                return;
            }

            if (_timerId == NoTimerId)
            {
                var timeCaps = default(MidiTimerWinApi.TIMECAPS);
                ProcessMmResult(MidiTimerWinApi.timeGetDevCaps(ref timeCaps, (uint)Marshal.SizeOf(timeCaps)));

                _resolution   = Math.Min(Math.Max(timeCaps.wPeriodMin, _interval), timeCaps.wPeriodMax);
                _tickCallback = OnTick;

                ProcessMmResult(MidiTimerWinApi.timeBeginPeriod(_resolution));
                _timerId = MidiTimerWinApi.timeSetEvent(_interval, _resolution, _tickCallback, IntPtr.Zero, MidiTimerWinApi.TIME_PERIODIC);
                if (_timerId == 0)
                {
                    var errorCode = Marshal.GetLastWin32Error();
                    throw new MidiDeviceException("Unable to initialize MIDI clock.", new Win32Exception(errorCode));
                }
            }

            _stopwatch.Start();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Starts a tick generator.
        /// </summary>
        /// <param name="interval">Interval between ticks.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="interval"/> is out of
        /// [<see cref="MinInterval"/>; <see cref="MaxInterval"/>] range.</exception>
        protected override void Start(TimeSpan interval)
        {
            ThrowIfArgument.IsOutOfRange(nameof(interval),
                                         interval,
                                         MinInterval,
                                         MaxInterval,
                                         $"Interval is out of [{MinInterval}, {MaxInterval}] range.");

            var intervalInMilliseconds = (uint)interval.TotalMilliseconds;

            var timeCaps = default(MidiTimerWinApi.TIMECAPS);

            ProcessMmResult(MidiTimerWinApi.timeGetDevCaps(ref timeCaps, (uint)Marshal.SizeOf(timeCaps)));

            _resolution   = Math.Min(Math.Max(timeCaps.wPeriodMin, intervalInMilliseconds), timeCaps.wPeriodMax);
            _tickCallback = OnTick;

            ProcessMmResult(MidiTimerWinApi.timeBeginPeriod(_resolution));
            _timerId = MidiTimerWinApi.timeSetEvent(intervalInMilliseconds, _resolution, _tickCallback, IntPtr.Zero, MidiTimerWinApi.TIME_PERIODIC);
            if (_timerId == NoTimerId)
            {
                var errorCode = Marshal.GetLastWin32Error();
                throw new MidiDeviceException("Unable to start tick generator.", new Win32Exception(errorCode));
            }
        }
Exemplo n.º 3
0
        public void Start()
        {
            if (IsRunning)
            {
                return;
            }

            // TODO: process errors

            var timeCaps = default(MidiTimerWinApi.TIMECAPS);

            MidiTimerWinApi.timeGetDevCaps(ref timeCaps, (uint)Marshal.SizeOf(timeCaps));

            _resolution   = Math.Min(Math.Max(timeCaps.wPeriodMin, _interval), timeCaps.wPeriodMax);
            _tickCallback = OnTick;

            _stopwatch.Start();

            MidiTimerWinApi.timeBeginPeriod(_resolution);
            _timerId = MidiTimerWinApi.timeSetEvent(_interval, _resolution, _tickCallback, IntPtr.Zero, MidiTimerWinApi.TIME_PERIODIC);

            IsRunning = true;
        }