def __init__(self, notes, instructions, algorithm, debug=False): self.matcher = Matcher(notes, algorithm, OvertoneInterpreter(), self.position_changed, progress_listener=self.matched, debug=options.debug) self.instructions = instructions self._position_queue = Queue()
class Tester(object): SHUTDOWN_SENTINEL = object() def __init__(self, notes, instructions, algorithm, debug=False): self.matcher = Matcher(notes, algorithm, OvertoneInterpreter(), self.position_changed, progress_listener=self.matched, debug=options.debug) self.instructions = instructions self._position_queue = Queue() def position_changed(self, matcher): pass def matched(self, notes, position): sleep(0.25) self._position_queue.put(position) def test(self): self.matcher.start() try: self._run() finally: self.matcher.shutdown() while self.matcher.running: sleep(0.2) def _describe_interval(self, i): if i >= 0: interval = self.matcher.intervals[i] else: # i = -1, which isn't a real interval # synthesize one here for display purposes interval = Interval(-1.0, 0.0, []) note_string = ', '.join(unparse_note(*note) for note in interval.notes) return '%d (%.3f:%.3f%s)' % (i, interval.start, interval.end, ('; ' + note_string if note_string else '')) def _run(self): last_pos = -1 last_expected_pos = -1 for action, arg, expected_pos in self.instructions: if action == 'pause': print '--> Playing silence.' self.matcher.add([]) elif action == 'play': print '--> Playing %s.' % \ ', '.join(unparse_note(*freq_to_note(f)) for f in arg) self.matcher.add(arg) else: raise ValueError(action) # wait for the matcher to call our matched() method, and for # matched() to send us the matcher's new position through the queue new_pos = self._position_queue.get() if new_pos == last_pos: message = 'Stayed at' else: message = 'Moved to' # assess what happened: stop = False if expected_pos is None: # we didn't care where we went (instruction ended with ?) text_color = 'yellow' elif new_pos != expected_pos: # we didn't go where we expected text_color = 'red' stop = True verb = 'move to' if expected_pos != last_pos else 'stay at' expectation_string = '; expected to %s %s' % \ (verb, self._describe_interval(expected_pos)) else: # we went where we expected text_color = 'green' expectation_string = '' # display what happened if new_pos == last_pos: message = 'Stayed at' else: message = 'Moved to' print color(text_color, '<-- %s %s%s.', message, self._describe_interval(new_pos), expectation_string) if stop: break # if we didn't care where we went on this step (i.e., the line for # this instruction ended with '?'), save our new position as the # last-expected position; otherwise, save the actual last-expected # position last_expected_pos = expected_pos if expected_pos is not None \ else new_pos last_pos = new_pos sleep(0.5)