Skip to content

Commit 225fbc3

Browse files
authored
Merge pull request #2579 from adafruit/mixer_example
Adding mixer example
2 parents 206efbd + 177395b commit 225fbc3

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import audiobusio
6+
import audiocore
7+
import board
8+
import digitalio
9+
import keypad
10+
import analogio
11+
import audiomixer
12+
13+
SOUND_FILE = "StreetChicken.wav"
14+
15+
power = digitalio.DigitalInOut(board.EXTERNAL_POWER)
16+
power.switch_to_output(value=True)
17+
18+
analog_pin = analogio.AnalogIn(board.A0)
19+
20+
keys = keypad.Keys((board.BUTTON,), value_when_pressed=False)
21+
22+
i2s = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
23+
music = audiocore.WaveFile(SOUND_FILE)
24+
mixer = audiomixer.Mixer(voice_count=1, sample_rate=music.sample_rate, channel_count=1,
25+
bits_per_sample=music.bits_per_sample, samples_signed=True)
26+
i2s.play(mixer)
27+
28+
sound = False
29+
last_pot = 0
30+
31+
while True:
32+
if abs(last_pot - analog_pin.value) > 1000:
33+
last_pot = analog_pin.value
34+
volume = 1.0 - last_pot / 65535
35+
print(volume)
36+
mixer.voice[0].level = volume
37+
38+
if sound and not mixer.voice[0].playing:
39+
print("Playing now!")
40+
mixer.voice[0].play(music)
41+
42+
event = keys.events.get()
43+
if event and event.pressed:
44+
print("click")
45+
sound = not sound
46+
mixer.voice[0].stop()

0 commit comments

Comments
 (0)