-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_i2s_wav.py
55 lines (47 loc) · 1.26 KB
/
play_i2s_wav.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
import os
from machine import I2S
from machine import Pin
# ======= I2S CONFIGURATION =======
SCK_PIN = 13
WS_PIN = 14
SD_PIN = 17
I2S_ID = 0
WAV_FILE = "1.wav"
audio_out = I2S(
I2S_ID,
sck=Pin(SCK_PIN),ws=Pin(WS_PIN),sd=Pin(SD_PIN),
mode = I2S.TX,
bits = 16,
format = I2S.MONO,
#format = I2S.STEREO,
rate = 8000,
ibuf = 10000,
)
wav = open(WAV_FILE, "rb")
pos = wav.seek(44) # advance to first byte of Data section in WAV file
# allocate sample array
# memoryview used to reduce heap allocation
wav_samples = bytearray(10000)
wav_samples_mv = memoryview(wav_samples)
# continuously read audio samples from the WAV file
# and write them to an I2S DAC
print("========== START PLAYBACK ==========")
try:
i = 0;
while True:
num_read = wav.readinto(wav_samples_mv)
i = i + num_read
print(str(i/1024.0)+" KBytes")
# end of WAV file?
if num_read == 0:
# end-of-file, advance to first byte of Data section
# _ = wav.seek(44)
break;
else:
_ = audio_out.write(wav_samples_mv[:num_read])
except (KeyboardInterrupt, Exception) as e:
print("caught exception {} {}".format(type(e).__name__, e))
# cleanup
wav.close()
audio_out.deinit()
print("Done")