def _parse(self, data): lines = data.split("\n") line = lines.pop(0) while line != 'parameters': print "not parameter " + line line = lines.pop(0) # Reading parameters while len(lines) > 0 and line != 'instruments': if len(line) > 0 and line[0] != '#': line_array = line.split(' ') if line_array[0] == 'speed': self.speed = int(line_array[1]) elif line_array[0] == 'measure_resolution': self.measure_resolution = int(line_array[1]) elif line_array[0] == 'beats_per_measure': self.beats_per_measure = int(line_array[1]) line = lines.pop(0) self._update_sleep_interval() # Reading instruments line = lines.pop(0) while len(lines) > 0 and line != 'rhythm': if len(line) > 0 and line[0] != '#': instrument = instruments.parse(line.split(' '), self.sample_rate, self.buffer_size) self.add_instrument(instrument) line = lines.pop(0) # Reading Rhythm pattern line = lines.pop(0) while len(lines) > 0 and line != 'gains': if len(line) > 0 and line[0] != '#': line = line.split('|')[-1] instr_str = line.split(':')[0].split() instrument_id = int(instr_str[0]) instrument_tone = int(instr_str[1]) rhythm = line.split(':')[1].split()[0:self.measure_resolution] rhythm = [int(c) for c in rhythm] track = Track(instrument_id, instrument_tone, rhythm, rhythm, '') self.add_track(track) line = lines.pop(0)
if __name__ == "__main__": parser = argparse.ArgumentParser(description='MSP homework 9') parser.add_argument('score', type=str) parser.add_argument('-i', '--instrument', type=str, default=DEFAULT_INSTRUMENT) parser.add_argument('-s', '--speed', type=int, default=DEFAULT_SPEED) namespace = parser.parse_args() dac = DAC(bufferSize=2 ** 10, rate=44100) dac.start() try: sequencer = Sequencer(buffer_size=dac.bufferSize, sample_rate=dac.getSamplerate(), speed=namespace.speed) dac.connect(sequencer.callback) instrument = instruments.parse(namespace.instrument.split(), buffer_size=dac.bufferSize, sample_rate=dac.getSamplerate()) sequencer.add_instrument(instrument) file = open(namespace.score, 'r') score_string = file.read() file.close() score = Sequencer.parse_mono_score(score_string) sequencer.play(score=score) finally: dac.stop()