Launchpad als Kinderspielzeug

Wer hätte gedacht, dass 30 Minuten Scripting für den Kleinen (zweieinhalbjährigen) so einen Spaß machen. Verwendet wurden:

  • Ein bestehendes Launchpad Mini (https://novationmusic.com/de/launch/launchpad-mini)
  • Die dazu passende Python Library (https://github.com/FMMT666/launchpad.py)
  • Python für VSCode (https://marketplace.visualstudio.com/items?itemName=ms-python.python)

Die eigentliche Logik für „Drücken bis rot .. aufnehmen .. abspielen“ war dann als Erstversion schnell gestrickt, viele weitere Versionen mit Farben, blinkenden Buttons u.s.w. werden folgend denke ich 🙂

#!/usr/bin/env python
# based on "Quick button test" from https://github.com/FMMT666/launchpad.py/blob/master/examples/buttons_raw.py

import sys
import time
import winsound
from time import sleep
import sounddevice as sd

try:
    import launchpad_py as launchpad
except ImportError:
    try:
        import launchpad
    except ImportError:
        sys.exit("error loading launchpad.py")

def main():

    lp = launchpad.Launchpad()
    if lp.Open():
        print("Launchpad Mini ready ...")

    lp.Reset()  # turn all LEDs off

    print("\nQUIT: Push one single button ten times in a row.\n")

    lastButtonNr = -1
    lastButtonPressed = -1
    buttonNr = -1
    buttonPressed = -1
    tStart = time.time()
    buttonPressed = 0
    recordingStarted = 0
    data = {}
    while True:
        # read buttons
        buts = lp.ButtonStateRaw()

        # if still same button pressed for > 3sek
        if buttonPressed == 1 and (time.time() - tStart) > 2 and recordingStarted == 0:
            print('start recording')
            lp.LedCtrlRaw(buttonNr, 1, 0)
            data[buttonNr] = sd.rec(
                int(5 * 44100), samplerate=44100, channels=2)
            sd.wait()
            recordingStarted = 1

        # debug make loop slower
        sleep(0.1)

        # if button pressed do further processing
        if buts != []:
            buttonNr = buts[0]
            buttonPressed = buts[1]
            print('Debug: ButtonNr: {} is pressed: {}'.format(
                buttonNr, buttonPressed))

            # button pressed short
            if buttonPressed == 1 and (time.time() - tStart) > 0 and (time.time() - tStart) < 2:
                if buttonNr in data:
                    print('play recording')
                    sd.play(data[buttonNr], 44100)
                    sd.wait()
                else:
                    print('no recording found')

            # stop recording if recording started and same button is released
            if buttonNr == lastButtonNr and buttonPressed == 0 and recordingStarted == 1:
                print('stop recording')
                lp.LedCtrlRaw(buttonNr, 0, 1)
                recordingStarted = 0

            # if a new button is pressed restart timer
            if lastButtonNr != buttonNr or lastButtonPressed != buttonPressed:
                print('reset timer')
                tStart = time.time()

            # set needed variables
            lastButtonNr = buttonNr
            lastButtonPressed = buttonPressed

    print("bye ...")
    lp.Close()  # close the Launchpad

if __name__ == '__main__':
    main()