def playnote(self, note_idx, step_idx): r = random.randint(0, 99) if r >= self.prob[step_idx]: self.publish(("printat", None, (step_idx * self.print_note_width + 2, 4, "."))) time.sleep(self.duration / 1000) self.publish(("printat", None, (step_idx * self.print_note_width + 2, 4, " "))) else: duration_on = (self.durations[step_idx] * self.duration / 127) duration_off = self.duration - duration_on duration_on = duration_on / self.ratchets[step_idx] duration_off = duration_off / self.ratchets[step_idx] chosen = self.getnote(self.interval_indexes[note_idx]) vel = self.vel[step_idx] note = (self.outchannel, chosen, vel) noteon = alsamidi.noteonevent(*note) noteoff = alsamidi.noteoffevent(*note) self.publish(("printat", None, (step_idx * self.print_note_width + 2, 4, "*"))) self.publish(("printat", None, (note_idx * self.print_note_width + 2, 3, "*"))) for i in range(self.ratchets[step_idx]): alsaseq.output(noteon) time.sleep(duration_on / 1000) alsaseq.output(noteoff) time.sleep(duration_off / 1000) self.publish(("printat", None, (step_idx * self.print_note_width + 2, 4, " "))) self.publish(("printat", None, (note_idx * self.print_note_width + 2, 3, " ")))
def test(self): from alsamidi import noteoffevent midi_event = (1, 60, 10) data = (1, 60, 10, 0, 0) expected = (7, 1, 0, 253, (0, 0), (0, 0), (0, 0), data) self.assertEqual(expected, noteoffevent(*midi_event))
def finish(): curses.nocbreak() curses.echo() curses.endwin() print 'cleaning up' for channel in range(16): for pitch in range(128): alsaseq.output(alsamidi.noteoffevent(channel, pitch, 0))
def midi_failure_accord(self): _akord = [0,3,6] akord = [self.st.base - 12 + x for x in _akord] for t in akord: alsaseq.output(noteonevent(1, t, 120)) time.sleep(2) for t in akord: alsaseq.output(noteoffevent(1, t, 120))
def midi_success_accord(self): _akord = [0, 12] akord = [self.st.base + 40 + x for x in _akord] for t in akord: alsaseq.output(noteonevent(1, t, 120)) logger.info("midi_success, 1.faza") time.sleep(2) for t in akord: try: alsaseq.output(noteoffevent(1, t, 120)) except Exception as e: logger.error("midi_success: " + str(e)) logger.info("midi_success, posledna faza")
def play(predicted): import alsaseq, alsamidi alsaseq.client('andreas', 1, 1, True) alsaseq.connectto(1, 20, 0) alsaseq.start() for pitches in predicted.T: for i, on in enumerate(pitches): note = i + 50 alsaseq.output(alsamidi.noteoffevent(0, note, 100)) if on: alsaseq.output(alsamidi.noteonevent(0, note, 100)) time.sleep(.1)
def refresh_midi(data=None): global last_time import scikits.audiolab import alsaseq, alsamidi this_time = time.time() if last_time is None: last_time = this_time sleep = max(0, .2 - (this_time - last_time)) print sleep while app.Pending(): app.Dispatch() if data is not None: visualiser.on_training_update(data) out = data[-1, -esn.n_output_units:] out = (out - esn.teacher_shift) / esn.teacher_scaling notes_to_output = collections.defaultdict(list) for i, x in enumerate(out): # if i > 18: # chan = 2 # note = {19: 29, 20: 31, 21: 33}[i] # elif i > 15: # chan = 1 # note = {16: 36, 17: 40, 18: 44}[i] # else: # chan = 0 # note = i + 69 - 24 chan = 0 note = i + 40 alsaseq.output(alsamidi.noteoffevent(chan, note, 100)) if x > .5: notes_to_output[chan].append((x, note)) for chan, notes in notes_to_output.items(): if len(notes) > 3: notes = sorted(notes)[:3] for _, note in notes: alsaseq.output(alsamidi.noteonevent(chan, note, 100)) drawing_time = time.time() - this_time time.sleep(sleep) last_time = time.time() - drawing_time
def refresh_midi(data=None): global last_time import scikits.audiolab import alsaseq, alsamidi this_time = time.time() if last_time is None: last_time = this_time sleep = max(0, .2 - (this_time - last_time)) print sleep while app.Pending(): app.Dispatch() if data is not None: visualiser.on_training_update(data) out = data[-1, -esn.n_output_units:] out = (out - esn.teacher_shift) / esn.teacher_scaling notes_to_output = collections.defaultdict(list) for i, x in enumerate(out): # if i > 18: # chan = 2 # note = {19: 29, 20: 31, 21: 33}[i] # elif i > 15: # chan = 1 # note = {16: 36, 17: 40, 18: 44}[i] # else: # chan = 0 # note = i + 69 - 24 chan = 0 note = i + 40 alsaseq.output(alsamidi.noteoffevent(chan, note, 100)) if x > .5: notes_to_output[chan].append((x, note)) for chan, notes in notes_to_output.items(): if len(notes) > 3: notes = sorted(notes)[:3] for _, note in notes: alsaseq.output(alsamidi.noteonevent(chan, note, 100)) drawing_time = time.time() - this_time time.sleep(sleep) last_time = time.time() - drawing_time
def playnote(self, note_idx): r = random.randint(0, 99) if r >= self.prob[note_idx]: self.publish(("printat", None, (note_idx * self.print_note_width + 2, 3, "."))) time.sleep(self.duration / 1000) self.publish(("printat", None, (note_idx * self.print_note_width + 2, 3, " "))) else: duration_on = self.durations[note_idx] * self.duration / 127 chosen = self.getnote(self.interval_indexes[note_idx]) vel = self.vel[note_idx] note = (0, chosen, vel) noteon = alsamidi.noteonevent(*note) noteoff = alsamidi.noteoffevent(*note) alsaseq.output(noteon) self.publish(("printat", None, (note_idx * self.print_note_width + 2, 3, "*"))) time.sleep(duration_on / 1000) alsaseq.output(noteoff) self.publish(("printat", None, (note_idx * self.print_note_width + 2, 3, " "))) time.sleep((self.duration - duration_on) / 1000)
from alsamidi import noteonevent, noteoffevent #alsaseq.client( 'MIDI through', 1, 1, False ) #alsaseq.connectfrom( 1, 129, 0 ) #alsaseq.connectto( 0, 130, 0 ) alsaseq.client( 'Simple', 1, 1, True ) alsaseq.connectto( 1, 128, 0 ) alsaseq.start() _akord=[0,4,7] akord=[i+12*j for i in _akord for j in range(1)] for base in range(40): events=[(1, 40+base+i, 120) for i in akord] noteons=[noteonevent(*event) for event in events] noteoffs=[noteoffevent(*event) for event in events] s=raw_input("stlac enter") for noteon in noteons: alsaseq.output(noteon) time.sleep(1) for noteoff in noteoffs: alsaseq.output(noteoff) time.sleep(0.2) time.sleep(10) #alsaseq.output( (6, 1, 0, 1, (1, 0), (0, 0), (0, 0), (0, 60, 127, 0, 100)) )
# alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, 1, (0,0), (0,0), (0,0), (0x63, FX2_SEND)) ) # alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, 1, (0,0), (0,0), (0,0), (0x62, 0x48)) ) # alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, 1, (0,0), (0,0), (0,0), (0x05, 0x00)) ) # alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, 1, (0,0), (0,0), (0,0), (0x26, 0x05)) ) # time.sleep(0.5) #alsaseq.output( (6, 1, 0, 1, (5, 0), (0, 0), (0, 0), (0, 60, 127, 0, 100)) ) alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, alsamidi.SND_SEQ_QUEUE_DIRECT, (0, 0), (0, 0), (0, 0), (0xB0 + (CHANNEL-1), 0x63, FX2_SEND, 0xB0 + (CHANNEL-1), 0x62, 0x48, 0xB0 + (CHANNEL-1), 0x05,0x00, 0xB0 + (CHANNEL-1), 0x26,0x05)) ) time.sleep(1000) # mute group 2 -> muted msg_part1 = alsamidi.noteonevent(CHANNEL-1, MUTE_GRP2_NOTE, 0x40) msg_part2 = alsamidi.noteoffevent(CHANNEL-1, MUTE_GRP2_NOTE, 0) alsaseq.output(msg_part1) alsaseq.output(msg_part2) time.sleep(2) # mute group 2 -> unmuted msg_part1 = alsamidi.noteonevent(CHANNEL-1, MUTE_GRP2_NOTE, 0x40-1) msg_part2 = alsamidi.noteoffevent(CHANNEL-1, MUTE_GRP2_NOTE, 0) alsaseq.output(msg_part1) alsaseq.output(msg_part2) time.sleep(1000)
def off(self): alsaseq.output(alsamidi.noteoffevent(self.chan, self.key, self.vel)) self.active = False
def midi_stop_tone(x): alsaseq.output(noteoffevent((1, x, 120))) self.tones_amount -= 1
def midi_play(notes): for note in notes: alsaseq.output(alsamidi.noteonevent(note.channel, note.pitch, 100)) time.sleep(SLEEP_TIME) for note in notes: alsaseq.output(alsamidi.noteoffevent(note.channel, note.pitch, 0))
def StopNote(note,velocity=127): print "stopnote", note alsaseq.output(alsamidi.noteoffevent( 0, note, 127 ))
def midi_stop_tone(self, x): if x in self.tones_playing: alsaseq.output(noteoffevent(1, x, 120)) self.tones_playing.remove(x)
# alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, 1, (0,0), (0,0), (0,0), (0x63, FX2_SEND)) ) # alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, 1, (0,0), (0,0), (0,0), (0x62, 0x48)) ) # alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, 1, (0,0), (0,0), (0,0), (0x05, 0x00)) ) # alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, 1, (0,0), (0,0), (0,0), (0x26, 0x05)) ) # time.sleep(0.5) #alsaseq.output( (6, 1, 0, 1, (5, 0), (0, 0), (0, 0), (0, 60, 127, 0, 100)) ) alsaseq.output( (alsaseq.SND_SEQ_EVENT_NONREGPARAM, 1, 0, alsamidi.SND_SEQ_QUEUE_DIRECT, (0, 0), (0, 0), (0, 0), (0xB0 + (CHANNEL - 1), 0x63, FX2_SEND, 0xB0 + (CHANNEL - 1), 0x62, 0x48, 0xB0 + (CHANNEL - 1), 0x05, 0x00, 0xB0 + (CHANNEL - 1), 0x26, 0x05))) time.sleep(1000) # mute group 2 -> muted msg_part1 = alsamidi.noteonevent(CHANNEL - 1, MUTE_GRP2_NOTE, 0x40) msg_part2 = alsamidi.noteoffevent(CHANNEL - 1, MUTE_GRP2_NOTE, 0) alsaseq.output(msg_part1) alsaseq.output(msg_part2) time.sleep(2) # mute group 2 -> unmuted msg_part1 = alsamidi.noteonevent(CHANNEL - 1, MUTE_GRP2_NOTE, 0x40 - 1) msg_part2 = alsamidi.noteoffevent(CHANNEL - 1, MUTE_GRP2_NOTE, 0) alsaseq.output(msg_part1) alsaseq.output(msg_part2) time.sleep(1000)