1
1
import time
2
- import array
3
- import math
4
2
import board
5
- from audiocore import RawSample
6
- import audiopwmio
7
3
import displayio
8
4
import terminalio
5
+ import simpleio
9
6
from adafruit_display_text import label
10
7
from adafruit_display_shapes .rect import Rect
11
- from adafruit_display_shapes .circle import Circle
12
8
from adafruit_clue import clue
13
9
14
- blink_dot = False # optional blinking dot on accent
15
10
blink_light = True # optional flashing backlight on accent
16
11
tempo = 120 # in bpm
17
12
print ("BPM: {}" .format (tempo ))
18
13
time_signature = 4 # Beats per measure
19
14
BEEP_DURATION = 0.05
20
15
delay = 60 / tempo
21
16
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
-
39
17
clue .display .brightness = 1.0
40
18
clue .pixel .brightness = 0.2
41
19
screen = displayio .Group (max_size = 11 )
56
34
bg_sprite = displayio .TileGrid (color_bitmap , x = 0 , y = 0 , pixel_shader = color_palette )
57
35
screen .append (bg_sprite )
58
36
59
- # Downbeat indicator graphic
60
- flash = Circle (184 , 216 , 16 , fill = TEAL , outline = None )
61
-
62
- screen .append (flash )
63
-
64
37
# title box
65
38
title_box = Rect (0 , 0 , 240 , 60 , fill = GRAY , outline = None )
66
39
screen .append (title_box )
128
101
def metronome (accent ): # Play metronome sound and flash display
129
102
clue .display .brightness = 0.5 # Dim the display slightly
130
103
if accent == 1 : # Put emphasis on downbeat
131
- if blink_dot :
132
- flash .fill = YELLOW # Flash the indicator
133
104
if blink_light :
134
105
clue .pixel .fill (YELLOW ) # Flash the pixel
135
- dac . play ( tones [ 1 ], loop = True )
106
+ simpleio . tone ( board . SPEAKER , 1800 , BEEP_DURATION )
136
107
else : # All the other beats in the measure
137
108
if blink_light :
138
109
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 )
144
111
if blink_light :
145
112
clue .pixel .fill (0 ) # Turn off the pixel
146
113
clue .display .brightness = 1.0 # Restore display to normal brightness
@@ -153,7 +120,6 @@ def metronome(accent): # Play metronome sound and flash display
153
120
154
121
t0 = time .monotonic () # set start time
155
122
156
-
157
123
while True :
158
124
159
125
# play/pause
0 commit comments