|
| 1 | +# Circuit Playground 808 Drum machine |
| 2 | +import time |
| 3 | +import board |
| 4 | +import touchio |
| 5 | +import digitalio |
| 6 | + |
| 7 | +try: |
| 8 | + from audiocore import WaveFile |
| 9 | +except ImportError: |
| 10 | + from audioio import WaveFile |
| 11 | + |
| 12 | +try: |
| 13 | + from audioio import AudioOut |
| 14 | +except ImportError: |
| 15 | + try: |
| 16 | + from audiopwmio import PWMAudioOut as AudioOut |
| 17 | + except ImportError: |
| 18 | + pass # not always supported by every board! |
| 19 | + |
| 20 | +bpm = 120 # Beats per minute, change this to suit your tempo |
| 21 | + |
| 22 | +# Enable the speaker |
| 23 | +speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) |
| 24 | +speaker_enable.direction = digitalio.Direction.OUTPUT |
| 25 | +speaker_enable.value = True |
| 26 | + |
| 27 | +# Make the input capacitive touchpads |
| 28 | +capPins = (board.A1, board.A2, board.A3, board.A4, board.A5, |
| 29 | + board.A6, board.TX) |
| 30 | + |
| 31 | +touchPad = [] |
| 32 | +for i in range(7): |
| 33 | + touchPad.append(touchio.TouchIn(capPins[i])) |
| 34 | + |
| 35 | +# The seven files assigned to the touchpads |
| 36 | +audiofiles = ["bd_tek.wav", "elec_hi_snare.wav", "elec_cymbal.wav", |
| 37 | + "elec_blip2.wav", "bd_zome.wav", "bass_hit_c.wav", |
| 38 | + "drum_cowbell.wav"] |
| 39 | + |
| 40 | +audio = AudioOut(board.SPEAKER) |
| 41 | + |
| 42 | + |
| 43 | +def play_file(filename): |
| 44 | + print("playing file " + filename) |
| 45 | + file = open(filename, "rb") |
| 46 | + wave = WaveFile(file) |
| 47 | + audio.play(wave) |
| 48 | + time.sleep(bpm / 960) # Sixteenth note delay |
| 49 | + |
| 50 | + |
| 51 | +while True: |
| 52 | + for i in range(7): |
| 53 | + if touchPad[i].value: |
| 54 | + play_file(audiofiles[i]) |
0 commit comments