def next(): global note pianohat.set_led(current_note(), False) time.sleep(0.1) note += 1 note %= len(melody) pianohat.set_led(current_note(), True)
def deselect(self): if pianohat: for i in xrange(16): pianohat.set_led(i, False) pianohat.auto_leds(True) self.enabled = FlipFlopState() pygame.mixer.stop() pygame.mixer.quit()
def note_off(self, channel, octave): """Fade the key's soundwave to nothing """ if channel == 12: return if pianohat: pianohat.set_led(channel, False) for t in self.wavetypes: self.notes[t][channel].fadeout(self.RELEASE_MS)
def toggle(self, t): """Toggle the on off state of the sine,square, or saw """ self.t2c[t] en = self.enabled.toggle(t) ns = ' on' if en else ' off' if pianohat: pianohat.set_led(self.t2c[t], en) return t + ns
def volume_down(i: int, pressed: bool) -> None: global volume pianohat.set_led(i, pressed) if pressed: volume -= 0.1 volume = 0 if volume < 0 else volume print('Volume: %.2f' % volume) set_volume(volume)
def volume_up(i: int, pressed: bool) -> None: global volume pianohat.set_led(i, pressed) if pressed: volume += 0.1 volume = 1 if volume > 1 else volume print('Volume: %.2f' % volume) set_volume(volume)
def select(self): if pianohat: pianohat.auto_leds(False) for i in xrange(16): pianohat.set_led(i, False) self.enabled = FlipFlopState() pygame.mixer.pre_init(self.SAMPLERATE, -self.BITRATE, 1, 1024) pygame.mixer.init(channels=1) #pygame.mixer.set_num_channels(32) self.toggle('sine') # by default enable sine. return "C2=sine,v=square,^saw"
def play_sample(channel, pressed): """Handles the piano keys Any enabled samples are played, and *all* samples are turned off is a key is released """ pianohat.set_led(channel, pressed) if pressed: for t in wavetypes: if enabled[t]: notes[t][channel].play(loops=-1, fade_ms=ATTACK_MS) else: for t in wavetypes: notes[t][channel].fadeout(RELEASE_MS)
def pause_music(i: int, pressed: bool) -> None: global paused if playing and pressed: if paused: print("Unpausing music.") pygame.mixer.music.unpause() else: print("Pausing music.") pygame.mixer.music.pause() paused = not paused pianohat.set_led(i, paused)
def note_on(self, channel, octave): """Generate the soundwave """ if channel == 12: return self.toggle('sine') if pianohat: pianohat.set_led(channel, True) note_name = key_name(channel) for t in self.wavetypes: if self.enabled[t]: note_name += ' ' + t self.notes[t][channel].play(-1, fade_ms=self.ATTACK_MS) return note_name
def handle_note(channel, pressed): # handler for key presses global note global correct if channel < 13 and pressed: # Only for note keys if str(channel) == note: # Did the player get it right? print('correct, it was a ' + str(NOTES[note][0]) ) pianohat.auto_leds(False) for x in range(16): # Flash all the lights to celebrate pianohat.set_led(x, True) time.sleep(0.05) for x in range(16): # Them turn them off pianohat.set_led(x,False) pianohat.auto_leds(True) correct = True else: print('wrong, try again')
def nextStep(): global position recarray = recordings[position] recbank = recarray[0] # check if there is a recording if (recbank != -1): # todo play all sounds not just 1 # sounds[recbank][i].play(loops=0) for index in range(1, len(recarray)): sounds[recbank][recarray[index]].play(loops=0) #led pianohat.set_led(validkeys[position], False) position += 1 if (position == 8): position = 0 pianohat.set_led(validkeys[position], True) # next threading.Timer(bpm, nextStep).start()
def startup_lights(callback=None): """Cycle the leds on the PianoHAT in a pretty way to show things started up. """ if not pianohat: return pianohat.auto_leds(False) led = FlipFlopState() for i in xrange(16): if i < 13: pianohat.set_led(i, True) if i - 3 >= 0: pianohat.set_led(i - 3, False) j = 13 + (i % 3) pianohat.set_led(j, led.toggle(j)) time.sleep(_STARTUP_DELAY) if callback and callable(callback): callback() for i in xrange(16): pianohat.set_led(1, False) pianohat.auto_leds(True)
def update_leds() -> None: """ Consider the state of the program and set the LEDs on the Piano Hat accordingly. """ leds_off() if playing: pianohat.set_led(current_song_index, True) elif paused: pianohat.set_led(15, paused) else: # Play frame of "animated" light sequence for i in range(13): onOrOff = True if i is current_led_index else False pianohat.set_led(i, onOrOff)
if channel < len(samples) and pressed: print('Playing Sound: {}'.format(files[channel])) samples[channel].play(loops=0) next() def handle_instrument(channel, pressed): pass def handle_octave_up(channel, pressed): pass def handle_octave_down(channel, pressed): pass for x in range(16): pianohat.set_led(x, False) pianohat.on_note(handle_note) pianohat.on_octave_up(handle_octave_up) pianohat.on_octave_down(handle_octave_down) pianohat.on_instrument(handle_instrument) pianohat.set_led(current_note(), True) signal.pause()
def leds_off() -> None: """ Turn off all LEDs on the Piano Hat """ for i in range(15): pianohat.set_led(i, False)
import pianohat import time import threading from pygame import mixer import settings pianohat.auto_leds(False) pianohat.set_led(13, False) pianohat.set_led(14, True) pianohat.set_led(15, True) # 120 beats per minute is 0.125 sec per position bpm = 0.125 bank = 1 position = 0 mixer.init(22050, -16, 2, 512) mixer.set_num_channels(13) validkeys = [0, 2, 4, 5, 7, 9, 11, 12] keymap = [0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7] sounds = [] recordings = [[-1], [-1], [-1], [-1], [-1], [-1], [-1], [-1]] # begin wit a basic beat # recordings = [[1,0],[0,-1],[0,-1],[0,-1],[1,0],[0,-1],[0,-1],[0,-1]] rec = True def handleNote(key, pressed): global position
def changeSettings(ch, evt): global bank global recordings global rec if (evt == True): if (ch == 13): bank = 0 pianohat.set_led(13, True) pianohat.set_led(14, False) if (ch == 14): bank = 1 pianohat.set_led(13, False) pianohat.set_led(14, True) # toggle record on off, off also erases if (ch == 15): if (rec == True): rec = False recordings = [[-1], [-1], [-1], [-1], [-1], [-1], [-1], [-1]] pianohat.set_led(15, False) else: rec = True pianohat.set_led(15, True)
def update_leds(): """Updates the Instrument and Octave LEDs to show enabled samples""" pianohat.set_led(15, enabled['sine']) pianohat.set_led(14, enabled['saw']) pianohat.set_led(13, enabled['square'])
def lights_on(): for light in range(13): pianohat.set_led(light, True)
def hint_on(): pianohat.set_led(notes_to_play[note_position][0], True)
#!/usr/bin/env python import time import pianohat pianohat.auto_leds(False) while True: for x in range(16): pianohat.set_led(x, True) time.sleep(0.1) pianohat.set_led(x, False) time.sleep(0.1)
def lights_out(): for light in range(16): pianohat.set_led(light, False)