-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathi2smic.py
More file actions
109 lines (86 loc) · 3.77 KB
/
Copy pathi2smic.py
File metadata and controls
109 lines (86 loc) · 3.77 KB
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
# SPDX-FileCopyrightText: 2020 Melissa LeBlanc-Williams for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import subprocess
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()
PRODUCT_NAME = "I2S Microphone"
OVERLAY = "googlevoicehat-soundcard"
CARD_NAME_FALLBACK = "sndrpigooglevoi"
def get_card_name(overlay):
"""Try to load the overlay at runtime and discover the ALSA card id
from arecord -l. Returns (card_name, detected) where detected is True
only when the name was read from a live arecord -l line; otherwise
the documented in-tree default is returned and detected is False.
"""
try:
active = subprocess.run(["dtoverlay", "-l"], capture_output=True, text=True)
if overlay not in active.stdout:
subprocess.run(["dtoverlay", overlay], check=False)
except FileNotFoundError:
print(" (dtoverlay command not found; skipping runtime overlay load)")
try:
output = subprocess.check_output(["arecord", "-l"], stderr=subprocess.DEVNULL).decode()
for line in output.splitlines():
if "googlevoice" in line.lower():
# Line looks like: "card 1: sndrpigooglevoi [snd_rpi_..."
return line.split(":")[1].strip().split(" ")[0], True
print(" (no googlevoicehat card present yet — it will appear after reboot)")
except FileNotFoundError:
print(" (arecord not installed; install 'alsa-utils' to record audio)")
except Exception as exc:
print(f" (could not parse arecord -l output: {exc})")
return CARD_NAME_FALLBACK, False
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"an {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...")
# Locate boot config
config = shell.get_boot_config()
if config is None:
shell.bail("No Device Tree Detected, not supported")
print(f"\nAdding Device Tree Entry to {config}")
if shell.pattern_search(config, f"^dtoverlay={OVERLAY}$"):
print("dtoverlay already active")
else:
shell.write_text_file(config, f"dtoverlay={OVERLAY}")
reboot = True
print("\nLoading overlay and detecting ALSA card name...")
card_name, detected = get_card_name(OVERLAY)
if detected:
print(f"Card name: {card_name}")
else:
print(f"Card name (default for this overlay): {card_name}")
print(" After reboot, run 'arecord -l' to confirm the actual card id.")
print("\n" + colored.green("All done!"))
print(f"""
After rebooting, you can list capture devices with:
arecord -l
And record a mono WAV with:
arecord -D plughw:{card_name} -c1 -r 48000 -f S32_LE -t wav -V mono -v test.wav
Or stereo (two mics, one wired SEL=GND, one wired SEL=3.3V):
arecord -D plughw:{card_name} -c2 -r 48000 -f S32_LE -t wav -V stereo -v test.wav
See the learn guide for full wiring and volume-control instructions:
https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout/raspberry-pi-wiring-test
""")
if reboot and not shell.argument_exists('noreboot'):
shell.prompt_reboot(force_arg="reboot")
# Main function
if __name__ == "__main__":
shell.require_root()
main()