Example #1
0
 def __init__(self, **kwargs):
     super(Sequencer, self).__init__(**kwargs)
     self.TickObj = MidiTick()
     self.time_signature_divisor = 4
     self.beats_per_measure = 4
     tick_int = bpm_to_seconds(self.tempo) / self.tick_resolution
     clock_int = tick_int / 4
     self.clock = MasterClock(tick_interval=tick_int, clock_interval=clock_int)
     self.clock.start()
     #self.clock.add_raw_tick_callback(self.on_clock_tick)
     self.clock.add_callback(self.on_clock_tick)
Example #2
0
 def __init__(self, **kwargs):
     super(TapTempo, self).__init__(**kwargs)
     self._clock_is_local = False
     clock = kwargs.get('clock')
     if not clock:
         clock = MasterClock()
         clock.start()
         self._clock_is_local = True
     self.clock = clock
     self.last_taps = collections.deque()
     self.bind(tap=self._on_tap)
Example #3
0
class Sequencer(BaseObject):
    _mbt_keys = ['measure', 'beat', 'tick', 'total_beats']
    _Properties = {'tempo':dict(default=120., min=30., max=300.), 
                   'playing':dict(default=False), 
                   'tick_resolution':dict(default=120), 
                   'position_mbt':dict(default=dict(zip(_mbt_keys, [1]*4)), quiet=True), 
                   'position_seconds':dict(default=0., quiet=True)}
    _SettingsProperties = ['tempo', 'tick_resolution']
    def __init__(self, **kwargs):
        super(Sequencer, self).__init__(**kwargs)
        self.TickObj = MidiTick()
        self.time_signature_divisor = 4
        self.beats_per_measure = 4
        tick_int = bpm_to_seconds(self.tempo) / self.tick_resolution
        clock_int = tick_int / 4
        self.clock = MasterClock(tick_interval=tick_int, clock_interval=clock_int)
        self.clock.start()
        #self.clock.add_raw_tick_callback(self.on_clock_tick)
        self.clock.add_callback(self.on_clock_tick)
        
    def play(self, **kwargs):
        if self.playing:
            return
        p_mbt = kwargs.get('position_mbt')
        if p_mbt is not None:
            self.Properties['position_mbt'].set_value(p_mbt)
        self.start_mbt = self.position_mbt.copy()
        self.start_time = None
        self.playing = True
        
    def stop(self):
        self.playing = False
        
    def on_clock_tick(self, clock, seconds):
        if not self.playing:
            return
        if self.start_time is None:
            self.start_time = seconds
        self.calc_position(seconds)
        self.TickObj += 1
        
    def calc_position(self, seconds):
        total_seconds = seconds - self.start_time
        beats = total_seconds / (60. / self.tempo)
        offset = beats + self.start_mbt['total_beats'] - 1
        old_ticks = self.position_mbt['tick']
        self.mbt_set_local = True
        self.Properties['position_mbt'].set_value(self.calc_mbt(offset))
        self.mbt_set_local = False
        if self.position_mbt['tick'] != old_ticks + 1 and self.position_mbt['tick'] != 0:
            self.LOG.warning(self, 'skipped tick', old_ticks + 1)
        #self.seconds_set_local = True
        #self.position_seconds = seconds
        #self.seconds_set_local = False
        
    def calc_mbt(self, beats):
        mbt = {}
        mbt['measure'] = int(beats / self.beats_per_measure)
        #if mbt['measure'] > 0:
        mbt['beat'] = int(beats - (mbt['measure'] * self.beats_per_measure)) + 1
        #else:
        #    mbt['beat'] = int(beats)
        mbt['tick'] = int((beats - int(beats)) * self.tick_resolution)
        mbt['total_beats'] = beats
        #print ':'.join(['%03d' % (mbt[key]) for key in ['measure', 'beat', 'tick']])
        return mbt