-
Notifications
You must be signed in to change notification settings - Fork 0
/
playi2s.py
109 lines (100 loc) · 2.99 KB
/
playi2s.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#
# Files that have been converted as follows have been tested
# Other formats may work.....
#
"""Audio Format : PCM
Format settings : Little / Signed
Codec ID : 1
Duration : 4 min 5 s
Bit rate mode : Constant
Bit rate : 128 kb/s
Channel(s) : 1 channel
Sampling rate : 8 000 Hz
Bit depth : 16 bits
Stream size : 3.75 MiB (100%)
"""
import board
try:
import bitbangio
except:
import busio as bitbangio
try:
import adafruit_sdcard
except:
try:
import sdcardio as adafruit_sdcard
except:
pass
try:
from pydos_ui import Pydos_ui
except:
try:
from cardputer_repl import Pydos_ui
except:
from sys import stdin as Pydos_ui
import digitalio
import storage
import audiocore
import audiobusio
fname = input("Filename:")
try:
if "SD_SPI" in dir(board):
spi = board.SD_SPI()
elif "SD_SCK" in dir(board):
spi = bitbangio.SPI(board.SD_SCK,board.SD_MOSI,board.SD_MISO)
elif "SPI" in dir(board):
spi = board.SPI()
else:
spi = bitbangio.SPI(board.SCK,board.MOSI,board.MISO)
if "SD_CS" in dir(board):
cs = digitalio.DigitalInOut(board.SD_CS)
elif "SDCARD_CS" in dir(board):
cs = digitalio.DigitalInOut(board.SDCARD_CS)
else:
cs = digitalio.DigitalInOut(board.CS)
try:
sd = adafruit_sdcard.SDCard(spi,cs)
except:
cs.deinit()
if "SD_CS" in dir(board):
sd = adafruit_sdcard.SDCard(spi,board.SD_CS)
elif "SDCARD_CS" in dir(board):
sd = adafruit_sdcard.SDCard(spi,board.SDCARD_CS)
else:
sd = adafruit_sdcard.SDCard(spi,board.CS)
vfs = storage.VfsFat(sd)
storage.mount(vfs,'/sd')
print('SD card mounted on /sd')
except:
pass
f = open(fname, "rb")
wav = audiocore.WaveFile(f)
a = None
if 'I2S_BIT_CLOCK' in dir(board):
# Temporary fix until board defintion update is merged
try:
a = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DATA)
except:
a = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.IS2_DATA)
elif 'I2S_BCK' in dir(board):
a = audiobusio.I2SOut(board.I2S_BCK, board.I2S_WS, board.I2S_DOUT)
elif 'SPEAKER_SCK' in dir(board):
a = audiobusio.I2SOut(board.SPEAKER_SCK, board.SPEAKER_WS, board.SPEAKER_DOUT)
else:
print('No I2S pins defined on the board')
if a is not None:
print("Press Q to quit")
try:
a.play(wav)
while True:
if 'read_keyboard' in dir(Pydos_ui):
cmnd = Pydos_ui.read_keyboard(1)
else:
cmnd = Pydos_ui.read(1)
if cmnd in "qQ":
a.stop()
break
except:
pass
a.deinit()
f.close()