-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_input.py
More file actions
executable file
·152 lines (123 loc) · 5.03 KB
/
Copy pathselect_input.py
File metadata and controls
executable file
·152 lines (123 loc) · 5.03 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
"""
Audio Input Device Selector for Musico
"""
import pyaudio
import sys
def list_audio_devices():
"""List available audio input devices"""
audio = pyaudio.PyAudio()
print("Available audio input devices:")
input_devices = []
for i in range(audio.get_device_count()):
info = audio.get_device_info_by_index(i)
if info['maxInputChannels'] > 0:
input_devices.append((i, info))
print(f" {i}: {info['name']} (Channels: {info['maxInputChannels']})")
audio.terminate()
return input_devices
def select_device():
"""Let user select audio input device"""
input_devices = list_audio_devices()
if not input_devices:
print("No input devices found!")
return None
if len(input_devices) == 1:
device_id = input_devices[0][0]
print(f"\nOnly one device found. Using: {input_devices[0][1]['name']}")
return device_id
print(f"\nSelect your preferred audio input device:")
while True:
try:
choice = input(f"Enter device number (0-{len(input_devices)-1}): ").strip()
device_id = int(choice)
if 0 <= device_id < len(input_devices):
selected_device = input_devices[device_id]
print(f"\n✅ Selected: {selected_device[1]['name']}")
return device_id
else:
print("Invalid choice. Please try again.")
except (ValueError, KeyboardInterrupt):
print("\nExiting...")
return None
def update_musico_config(device_id):
"""Update Musico.py to use the selected device"""
try:
with open('Musico.py', 'r') as f:
content = f.read()
lines = content.split('\n')
updated = False
stream_start_line = -1
# Find the stream creation block
for i, line in enumerate(lines):
if 'stream = self.audio.open(' in line:
stream_start_line = i
break
if stream_start_line == -1:
print("⚠️ Could not find stream creation in Musico.py")
return False
# Look for existing input_device_index lines in the stream block
input_device_lines = []
stream_end_line = -1
for i in range(stream_start_line, min(stream_start_line + 15, len(lines))):
if 'input_device_index' in lines[i]:
input_device_lines.append(i)
elif ')' in lines[i] and stream_end_line == -1:
stream_end_line = i
break
# Remove all existing input_device_index lines
for line_num in reversed(input_device_lines):
print(f"Removing existing input_device_index line: {lines[line_num].strip()}")
del lines[line_num]
if stream_end_line > line_num:
stream_end_line -= 1
# Add the new input_device_index line before stream_callback or before closing parenthesis
insert_line = -1
for i in range(stream_start_line, min(stream_start_line + 15, len(lines))):
if 'stream_callback=None' in lines[i]:
insert_line = i
break
elif ')' in lines[i] and insert_line == -1:
insert_line = i
break
if insert_line != -1:
new_line = f" input_device_index={device_id}, # Selected audio device"
lines.insert(insert_line, new_line)
updated = True
print(f"Added new input_device_index line: {new_line}")
else:
print("⚠️ Could not find insertion point in stream creation")
return False
if updated:
with open('Musico.py', 'w') as f:
f.write('\n'.join(lines))
print(f"✅ Updated Musico.py to use device {device_id}")
return True
else:
print("⚠️ Could not automatically update Musico.py")
print(f" Please manually set input_device_index={device_id} in the record_audio_sample method")
return False
except Exception as e:
print(f"❌ Error updating Musico.py: {e}")
return False
def main():
"""Main function"""
print("Musico Audio Input Selector")
print("=" * 40)
# List available devices
input_devices = list_audio_devices()
if not input_devices:
print("No audio input devices found!")
return
# Let user select device
device_id = select_device()
if device_id is not None:
# Update Musico configuration
if update_musico_config(device_id):
print(f"\n🎉 Configuration updated!")
print("You can now run Musico with: python Musico.py")
else:
print(f"\n⚠️ Manual configuration required")
print(f"Set input_device_index={device_id} in Musico.py")
if __name__ == "__main__":
main()