Skip to content

Commit e8259d2

Browse files
committed
using simpleio tone
1 parent aeec0d2 commit e8259d2

File tree

1 file changed

+3
-37
lines changed

1 file changed

+3
-37
lines changed

CLUE_Metronome/clue_metronome.py

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,19 @@
11
import time
2-
import array
3-
import math
42
import board
5-
from audiocore import RawSample
6-
import audiopwmio
73
import displayio
84
import terminalio
5+
import simpleio
96
from adafruit_display_text import label
107
from adafruit_display_shapes.rect import Rect
11-
from adafruit_display_shapes.circle import Circle
128
from adafruit_clue import clue
139

14-
blink_dot = False # optional blinking dot on accent
1510
blink_light = True # optional flashing backlight on accent
1611
tempo = 120 # in bpm
1712
print("BPM: {}".format(tempo))
1813
time_signature = 4 # Beats per measure
1914
BEEP_DURATION = 0.05
2015
delay = 60 / tempo
2116

22-
# constants for sine wave generation
23-
SIN_LENGTH = 100 # more is less choppy
24-
SIN_AMPLITUDE = 2 ** 15 # 0 (min) to 32768 (max)
25-
SIN_OFFSET = 32767.5 # for 16bit range, (2**16 - 1) / 2
26-
DELTA_PI = 2 * math.pi / SIN_LENGTH # happy little constant
27-
28-
sine_wave = [
29-
int(SIN_OFFSET + SIN_AMPLITUDE * math.sin(DELTA_PI * i)) for i in range(SIN_LENGTH)
30-
]
31-
tones = (
32-
RawSample(array.array("H", sine_wave), sample_rate=1200 * SIN_LENGTH),
33-
RawSample(array.array("H", sine_wave), sample_rate=1800 * SIN_LENGTH),
34-
)
35-
36-
dac = audiopwmio.PWMAudioOut(board.SPEAKER) # CLUE onboard speaker
37-
# dac = audiopwmio.PWMAudioOut(board.A2) # external amp/speaker, but can't use #0 touch pad
38-
3917
clue.display.brightness = 1.0
4018
clue.pixel.brightness = 0.2
4119
screen = displayio.Group(max_size=11)
@@ -56,11 +34,6 @@
5634
bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
5735
screen.append(bg_sprite)
5836

59-
# Downbeat indicator graphic
60-
flash = Circle(184, 216, 16, fill=TEAL, outline=None)
61-
62-
screen.append(flash)
63-
6437
# title box
6538
title_box = Rect(0, 0, 240, 60, fill=GRAY, outline=None)
6639
screen.append(title_box)
@@ -128,19 +101,13 @@
128101
def metronome(accent): # Play metronome sound and flash display
129102
clue.display.brightness = 0.5 # Dim the display slightly
130103
if accent == 1: # Put emphasis on downbeat
131-
if blink_dot:
132-
flash.fill = YELLOW # Flash the indicator
133104
if blink_light:
134105
clue.pixel.fill(YELLOW) # Flash the pixel
135-
dac.play(tones[1], loop=True)
106+
simpleio.tone(board.SPEAKER, 1800, BEEP_DURATION)
136107
else: # All the other beats in the measure
137108
if blink_light:
138109
clue.pixel.fill(LT_TEAL) # Flash the pixel
139-
dac.play(tones[0], loop=True)
140-
time.sleep(BEEP_DURATION) # Play the sound for a while
141-
dac.stop() # Then stop the sound
142-
if blink_dot:
143-
flash.fill = TEAL # Turn off the downbeat indicator
110+
simpleio.tone(board.SPEAKER, 1200, BEEP_DURATION)
144111
if blink_light:
145112
clue.pixel.fill(0) # Turn off the pixel
146113
clue.display.brightness = 1.0 # Restore display to normal brightness
@@ -153,7 +120,6 @@ def metronome(accent): # Play metronome sound and flash display
153120

154121
t0 = time.monotonic() # set start time
155122

156-
157123
while True:
158124

159125
# play/pause

0 commit comments

Comments
 (0)