-
Notifications
You must be signed in to change notification settings - Fork 160
/
i2samp.py
137 lines (114 loc) · 4.07 KB
/
i2samp.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
try:
from adafruit_shell import Shell
from clint.textui import colored
except ImportError:
raise RuntimeError("The library 'adafruit_shell' was not found. To install, try typing: sudo pip3 install adafruit-python-shell")
shell = Shell()
BLACKLIST = "/etc/modprobe.d/raspi-blacklist.conf"
PRODUCT_NAME = "I2S Amplifier"
def driver_loaded(driver_name):
return shell.run_command(f"lsmod | grep -q '{driver_name}'", suppress_message=True)
def main():
reboot = False
shell.clear()
if not shell.is_raspberry_pi():
shell.bail("Non-Raspberry Pi board detected.")
print("\nThis script will install everything needed to use\n"
f"{PRODUCT_NAME}.\n")
print(colored.red("--- Warning ---"))
print("\nAlways be careful when running scripts and commands\n"
"copied from the internet. Ensure they are from a\n"
"trusted source.\n")
if not shell.prompt("Do you wish to continue?"):
print("\nAborting...")
shell.exit()
print("\nChecking hardware requirements...")
# Enable I2S overlay
config = "/boot/firmware/config.txt"
if not os.path.exists(config):
config = "/boot/config.txt"
if not os.path.exists(config):
shell.bail("No Device Tree Detected, not supported")
print(f"\nAdding Device Tree Entry to {config}")
if shell.pattern_search(config, "^dtoverlay=max98357a$"):
print("dtoverlay already active")
else:
shell.write_text_file(config, "dtoverlay=max98357a")
reboot = True
if os.path.exists(BLACKLIST):
print("\nCommenting out Blacklist entry in", BLACKLIST)
shell.pattern_replace(BLACKLIST, "^blacklist[[:space:]]*snd_soc_max98357a.*", "#blacklist snd_soc_max98357a")
shell.pattern_replace(BLACKLIST, "^blacklist[[:space:]]*snd_soc_max98357a_i2c.*", "#blacklist snd_soc_max98357a_i2c")
shell.pattern_replace(BLACKLIST, "^blacklist[[:space:]]*snd_soc_max98357a.*", "#blacklist snd_soc_max98357a")
print("Configuring sound output")
if os.path.exists("/etc/asound.conf"):
if os.path.exists("/etc/asound.conf.old"):
shell.remove("/etc/asound.conf.old")
shell.move("/etc/asound.conf", "/etc/asound.conf.old")
shell.write_text_file("~/asound.conf",
"""
pcm.speakerbonnet {
type hw card 0
}
pcm.dmixer {
type dmix
ipc_key 1024
ipc_perm 0666
slave {
pcm "speakerbonnet"
period_time 0
period_size 1024
buffer_size 8192
rate 44100
channels 2
}
}
ctl.dmixer {
type hw card 0
}
pcm.softvol {
type softvol
slave.pcm "dmixer"
control.name "PCM"
control.card 0
}
ctl.softvol {
type hw card 0
}
pcm.!default {
type plug
slave.pcm "softvol"
}
""")
shell.move("~/asound.conf", "/etc/asound.conf")
print("Installing aplay systemd unit")
shell.write_text_file("/etc/systemd/system/aplay.service", """
[Unit]
Description=Invoke aplay from /dev/zero at system start.
[Service]
ExecStart=/usr/bin/aplay -D default -t raw -r 44100 -c 2 -f S16_LE /dev/zero
[Install]
WantedBy=multi-user.target""", append=False)
shell.run_command("sudo systemctl daemon-reload")
shell.run_command("sudo systemctl disable aplay")
print("\nYou can optionally activate '/dev/zero' playback in\n"
"the background at boot. This will remove all\n"
"popping/clicking but does use some processor time.\n\n")
if shell.prompt("Activate '/dev/zero' playback in background? [RECOMMENDED]\n", default="y"):
shell.run_command("sudo systemctl enable aplay")
reboot = True
if driver_loaded("max98357a"):
print(f"\nWe can now test your {PRODUCT_NAME}")
shell.warn("Set your speakers at a low volume if possible!")
if shell.prompt("Do you wish to test your system now?"):
print("Testing...")
shell.run_command("speaker-test -l5 -c2 -t wav")
print("\n" + colored.green("All done!"))
print("\nEnjoy your new $productname!")
if reboot:
shell.prompt_reboot()
# Main function
if __name__ == "__main__":
shell.require_root()
main()