-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathaudiomixer_demo.py
43 lines (34 loc) · 1.4 KB
/
audiomixer_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# audiomixer_demo.py -- show how to fade up and down playing loops
# note this causes glitches and crashes on RP2040
# 9 Feb 2022 - @todbot / Tod Kurt
import time
import board
import audiocore
import audiomixer
#from audiopwmio import PWMAudioOut as AudioOut # for RP2040 etc
from audioio import AudioOut as AudioOut # for SAMD51/M4 etc
num_voices = 2
# audio pin is almost any pin on RP2040, let's do A0 (RP2040 GPIO226) or RX (RP2040 GPIO1)
# audio pin is A0 on SAMD51 (Trelllis M4, Itsy M4, etc)
audio = AudioOut(board.A0)
mixer = audiomixer.Mixer(voice_count=num_voices, sample_rate=22050, channel_count=1,
bits_per_sample=16, samples_signed=True)
# attach mixer to audio playback
audio.play(mixer)
mixer.voice[0].level = 1.0
mixer.voice[1].level = 0.0
# start drum loop playing
wave0 = audiocore.WaveFile(open("wav/drumsacuff_22k_s16.wav","rb"))
mixer.voice[0].play( wave0, loop=True )
# start synth loop playing
wave1 = audiocore.WaveFile(open("wav/snowpeaks_22k_s16.wav","rb"))
mixer.voice[1].play( wave1, loop=True )
time.sleep(1.0) # let drums play a bit
# fade each channel up and down
up_down_inc = 0.01
while True:
mixer.voice[1].level = min(max(mixer.voice[1].level + up_down_inc, 0), 1)
mixer.voice[0].level = min(max(mixer.voice[0].level - up_down_inc, 0), 1)
if mixer.voice[0].level == 0 or mixer.voice[1].level == 0:
up_down_inc = -up_down_inc
time.sleep(0.1)