# Path: bl/debug.py
# DEBUG = _Debug()


import sys
import math
import warnings
from collections import namedtuple
from twisted.python import log
from twisted.python.failure import Failure
from twisted.internet.selectreactor import SelectReactor
from twisted.internet.task import LoopingCall
from bl.debug import DEBUG
            from twisted.internet import reactor
            from bl.instrument import fsynth
        """
        Synchronize the current ticks based on tick and timestamp (ts) reported
        by the SyncClock.
        """
        # TODO - quiet everything somehow
        delta = tick - self.ticks
        if DEBUG:
            log.msg("We're behind by %s ticks (ticks=%s expected=%s)" %
                    (delta, self.ticks, tick))
        tpm = self.meter.ticksPerMeasure
        if delta > tpm:
            t = tick % tpm
            ct = self.ticks % tpm
            if t < ct:
                t += tpm
            delta = t - ct

        # Do some catchup
        for i in range(delta):
            if DEBUG:
                log.msg('Catch up tick: %d' % i)
            self.ticks += 1
            self.runUntilCurrent()
        # XXX not very smart to do this considering tick based scheduling
        # - i.e. we need to take something already scheduled for tick N
        # and make it instead
        # schedule for N + delta or something (???)

        # First normalize the timed events
        offset = tick - self.ticks

# Path: bl/debug.py
# DEBUG = _Debug()


# Path: bl/scheduler.py
import sys
import math
import warnings
from collections import namedtuple
from twisted.python import log
from twisted.python.failure import Failure
from twisted.internet.selectreactor import SelectReactor
from twisted.internet.task import LoopingCall
from bl.debug import DEBUG
            from twisted.internet import reactor
            from bl.instrument import fsynth

        """
        Synchronize the current ticks based on tick and timestamp (ts) reported
        by the SyncClock.
        """
        # TODO - quiet everything somehow
        delta = tick - self.ticks
        if DEBUG:
            log.msg("We're behind by %s ticks (ticks=%s expected=%s)" %
                    (delta, self.ticks, tick))
        tpm = self.meter.ticksPerMeasure
        if delta > tpm:
            t = tick % tpm
            ct = self.ticks % tpm
            if t < ct:
                t += tpm
            delta = t - ct

        # Do some catchup
        for i in range(delta):
            if DEBUG:
                log.msg('Catch up tick: %d' % i)
            self.ticks += 1
            self.runUntilCurrent()
        # XXX not very smart to do this considering tick based scheduling
        # - i.e. we need to take something already scheduled for tick N
        # and make it instead
        # schedule for N + delta or something (???)

        # First normalize the timed events
        offset = tick - self.ticks

        if DEBUG: