Skip to content

Commit fc37ef1

Browse files
committed
Cosmic Unicorn: Tidy up audio feature test a little.
1 parent a09fde7 commit fc37ef1

File tree

1 file changed

+59
-71
lines changed

1 file changed

+59
-71
lines changed

micropython/examples/cosmic_unicorn/feature_test_with_audio.py

Lines changed: 59 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,43 @@
5656
SUB, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, SUB, -1, SUB, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, SUB, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, SUB, -1, SUB, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, SUB, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, SUB, -1, SUB, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, SUB, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, SUB, -1, SUB, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0)
5757

5858
notes = [melody_notes, rhythm_notes, drum_beats, hi_hat, bass_notes]
59-
channels = [gu.synth_channel(i) for i in range(len(notes))]
59+
channels = [gu.synth_channel(i) for i in range(len(notes) + 1)] # Extra channel for tones
60+
61+
# Configure the synth to play our notes
62+
channels[0].configure(waveforms=Channel.TRIANGLE + Channel.SQUARE,
63+
attack=0.016,
64+
decay=0.168,
65+
sustain=0xafff / 65535,
66+
release=0.168,
67+
volume=10000 / 65535)
68+
69+
channels[1].configure(waveforms=Channel.SINE + Channel.SQUARE,
70+
attack=0.038,
71+
decay=0.300,
72+
sustain=0,
73+
release=0,
74+
volume=12000 / 65535)
75+
76+
channels[2].configure(waveforms=Channel.NOISE,
77+
attack=0.005,
78+
decay=0.010,
79+
sustain=16000 / 65535,
80+
release=0.100,
81+
volume=18000 / 65535)
82+
83+
channels[3].configure(waveforms=Channel.NOISE,
84+
attack=0.005,
85+
decay=0.005,
86+
sustain=8000 / 65535,
87+
release=0.040,
88+
volume=8000 / 65535)
89+
90+
channels[4].configure(waveforms=Channel.SQUARE,
91+
attack=0.010,
92+
decay=0.100,
93+
sustain=0,
94+
release=0.500,
95+
volume=12000 / 65535)
6096

6197

6298
def gradient(r, g, b):
@@ -116,6 +152,8 @@ def outline_text(text):
116152
# The current synth beat
117153
beat = 0
118154

155+
text = ""
156+
119157

120158
def next_beat():
121159
global beat
@@ -145,37 +183,12 @@ def tick(timer):
145183

146184
if gu.is_pressed(CosmicUnicorn.SWITCH_A):
147185
if not was_a_pressed:
148-
# Configure the synth to play our notes
149-
channels[0].configure(waveforms=Channel.TRIANGLE + Channel.SQUARE,
150-
attack=0.016,
151-
decay=0.168,
152-
sustain=0xafff / 65535,
153-
release=0.168,
154-
volume=10000 / 65535)
155-
channels[1].configure(waveforms=Channel.SINE + Channel.SQUARE,
156-
attack=0.038,
157-
decay=0.300,
158-
sustain=0,
159-
release=0,
160-
volume=12000 / 65535)
161-
channels[2].configure(waveforms=Channel.NOISE,
162-
attack=0.005,
163-
decay=0.010,
164-
sustain=16000 / 65535,
165-
release=0.100,
166-
volume=18000 / 65535)
167-
channels[3].configure(waveforms=Channel.NOISE,
168-
attack=0.005,
169-
decay=0.005,
170-
sustain=8000 / 65535,
171-
release=0.040,
172-
volume=8000 / 65535)
173-
channels[4].configure(waveforms=Channel.SQUARE,
174-
attack=0.010,
175-
decay=0.100,
176-
sustain=0,
177-
release=0.500,
178-
volume=12000 / 65535)
186+
channels[0].volume(10000 / 65535)
187+
channels[1].volume(12000 / 65535)
188+
channels[2].volume(18000 / 65535)
189+
channels[3].volume(8000 / 65535)
190+
channels[4].volume(12000 / 65535)
191+
channels[5].volume(0)
179192

180193
# If the synth is not already playing, init the first beat
181194
if not synthing:
@@ -192,37 +205,12 @@ def tick(timer):
192205

193206
if gu.is_pressed(CosmicUnicorn.SWITCH_B):
194207
if not was_b_pressed:
195-
# Configure the synth to play our notes, but with only one channel audable
196-
channels[0].configure(waveforms=Channel.TRIANGLE + Channel.SQUARE,
197-
attack=0.016,
198-
decay=0.168,
199-
sustain=0,
200-
release=0.168,
201-
volume=0)
202-
channels[1].configure(waveforms=Channel.SINE + Channel.SQUARE,
203-
attack=0.038,
204-
decay=0.300,
205-
sustain=0,
206-
release=0,
207-
volume=12000 / 65535)
208-
channels[2].configure(waveforms=Channel.NOISE,
209-
attack=0.005,
210-
decay=0.010,
211-
sustain=16000 / 65535,
212-
release=0.100,
213-
volume=0)
214-
channels[3].configure(waveforms=Channel.NOISE,
215-
attack=0.005,
216-
decay=0.005,
217-
sustain=8000 / 65535,
218-
release=0.040,
219-
volume=0)
220-
channels[4].configure(waveforms=Channel.SQUARE,
221-
attack=0.010,
222-
decay=0.100,
223-
sustain=0,
224-
release=0.500,
225-
volume=0)
208+
channels[0].volume(0)
209+
channels[1].volume(12000 / 65535)
210+
channels[2].volume(0)
211+
channels[3].volume(0)
212+
channels[4].volume(0)
213+
channels[5].volume(0)
226214

227215
# If the synth is not already playing, init the first beat
228216
if not synthing:
@@ -242,7 +230,8 @@ def tick(timer):
242230
# Stop synth (if running) and play Tone A
243231
timer.deinit()
244232
tone_a = 400
245-
channels[0].play_tone(tone_a, 0.06)
233+
channels[5].play_tone(tone_a, 0.06)
234+
channels[5].volume(12000 / 65535)
246235

247236
gu.play_synth()
248237
synthing = False
@@ -257,7 +246,8 @@ def tick(timer):
257246
timer.deinit()
258247
tone_b = 600
259248

260-
channels[1].play_tone(tone_b, 0.06, attack=0.5)
249+
channels[5].play_tone(tone_b, 0.06, attack=0.5)
250+
channels[5].volume(12000 / 65535)
261251

262252
gu.play_synth()
263253
synthing = False
@@ -270,25 +260,25 @@ def tick(timer):
270260
if tone_b > 0: # Zero means tone not playing
271261
# Increase Tone B
272262
tone_b = min(tone_b + 10, 20000)
273-
channels[1].frequency(tone_b)
263+
channels[5].frequency(tone_b)
274264

275265
if gu.is_pressed(CosmicUnicorn.SWITCH_BRIGHTNESS_DOWN):
276266
if tone_b > 0: # Zero means tone not playing
277267
# Decrease Tone B
278268
tone_b = max(tone_b - 10, 10)
279-
channels[1].frequency(max(tone_b, 10))
269+
channels[5].frequency(max(tone_b, 10))
280270

281271
if gu.is_pressed(CosmicUnicorn.SWITCH_VOLUME_UP):
282272
if tone_a > 0: # Zero means tone not playing
283273
# Increase Tone A
284274
tone_a = min(tone_a + 10, 20000)
285-
channels[0].frequency(tone_a)
275+
channels[5].frequency(tone_a)
286276

287277
if gu.is_pressed(CosmicUnicorn.SWITCH_VOLUME_DOWN):
288278
if tone_a > 0: # Zero means tone not playing
289279
# Decrease Tone A
290280
tone_a = max(tone_a - 10, 10)
291-
channels[0].frequency(tone_a)
281+
channels[5].frequency(tone_a)
292282

293283
if gu.is_pressed(CosmicUnicorn.SWITCH_SLEEP):
294284
if not was_z_pressed:
@@ -322,8 +312,6 @@ def tick(timer):
322312
# print("white gradient")
323313
gradient(255, 255, 255)
324314

325-
text = ""
326-
327315
if gu.is_pressed(CosmicUnicorn.SWITCH_A):
328316
text = "PlaySyn"
329317

0 commit comments

Comments
 (0)