forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmix.py
67 lines (63 loc) · 1.81 KB
/
mix.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import audiocore
import audiopwmio
import audiomixer
import board
import array
import time
import math
CHANNELS = 2
RATE = 8000
SAMPLE_TYPE = "H"
OFFSET = 2**15 - 1
BUFFER_SIZE = 640
SINGLE_BUFFER = True
LOOP = True
# (frequency, amp_left, amp_right)
VOICES = ((200, 1, 0), (400, 0, 1), (100, 1, 1))
def play(
voices=VOICES,
channels=CHANNELS,
rate=RATE,
sample_type=SAMPLE_TYPE,
offset=OFFSET,
buffer_size=BUFFER_SIZE,
single_buffer=SINGLE_BUFFER,
loop=LOOP,
):
waves = []
samples = []
for v in voices:
print(v)
sample_length = int(rate // v[0])
wave = array.array(sample_type, [offset] * sample_length * channels)
for i in range(0, sample_length):
if channels == 1:
wave[i] = int(
math.sin(math.pi * 2 * i / sample_length) * v[1] * (2**15 - 1) + offset
)
else:
wave[2 * i] = int(
math.sin(math.pi * 2 * i / sample_length) * v[1] * (2**15 - 1) + offset
)
wave[2 * i + 1] = int(
math.sin(math.pi * 2 * i / sample_length) * v[2] * (2**15 - 1) + offset
)
waves.append(wave)
samples.append(
audiocore.RawSample(
wave, sample_rate=rate, channel_count=channels, single_buffer=single_buffer
)
)
mixer = audiomixer.Mixer(
voice_count=len(voices),
sample_rate=rate,
channel_count=channels,
bits_per_sample=16,
samples_signed=False,
buffer_size=buffer_size,
)
pwm = audiopwmio.PWMAudioOut(left_channel=board.D12, right_channel=board.D13)
pwm.play(mixer)
for i in range(len(samples)):
mixer.voice[i].play(samples[i], loop=loop)
mixer.voice[i].level = 0.5