-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_flask_device_mapping.py
More file actions
146 lines (116 loc) · 4.53 KB
/
test_flask_device_mapping.py
File metadata and controls
146 lines (116 loc) · 4.53 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
#!/usr/bin/env python3
"""
Test Flask app device mapping without running the full server
"""
import sys
# Add src directory to path
sys.path.append("src")
from sdl_device_mapper import SDLDeviceMapper # noqa: E402
def test_flask_device_endpoint():
"""Test the device mapping logic that would be used in Flask endpoint"""
print("🧪 Testing Flask Device Mapping Logic")
print("=" * 50)
try:
mapper = SDLDeviceMapper()
device_info = mapper.get_device_info()
# Format devices for frontend dropdown (same as Flask endpoint)
input_devices = []
for device in device_info["devices"]:
input_devices.append(
{
"id": device["sdl_id"], # Use SDL ID for whisper.cpp
"name": device["display_name"],
"pyaudio_id": device["pyaudio_id"], # For audio level monitoring
"available_for_whisper": device["available_for_whisper"],
"available_for_monitoring": device["available_for_monitoring"],
}
)
# Simulate Flask response
flask_response = {
"success": True,
"input_devices": input_devices,
"output_devices": [], # Not needed since we use SDL devices
"device_mapping_info": {
"sdl_devices": device_info["sdl_device_count"],
"pyaudio_devices": device_info["pyaudio_device_count"],
"mapped_devices": device_info["mapped_devices"],
},
"system_audio_note": "Devices shown are SDL devices that whisper.cpp can use directly",
}
print("✅ Flask endpoint response:")
import json
print(json.dumps(flask_response, indent=2))
return flask_response
except Exception as e:
print(f"❌ Error: {e}")
return None
def test_device_selection_logic():
"""Test device selection logic for whisper.cpp"""
print("\n🧪 Testing Device Selection Logic")
print("=" * 50)
mapper = SDLDeviceMapper()
# Simulate frontend sending SDL device IDs
test_cases = [
{
"mic_device_id": 0,
"system_device_id": 1,
"description": "AirPods + BlackHole",
},
{
"mic_device_id": 1,
"system_device_id": 0,
"description": "BlackHole + AirPods (swapped)",
},
{"mic_device_id": 0, "system_device_id": None, "description": "Mic only"},
{
"mic_device_id": None,
"system_device_id": 1,
"description": "System audio only",
},
]
for test_case in test_cases:
print(f"\n📋 Test: {test_case['description']}")
print(
f" Frontend sends: mic_device_id={test_case['mic_device_id']}, system_device_id={test_case['system_device_id']}"
)
# Convert SDL IDs to PyAudio IDs (for audio monitoring)
mic_sdl_id = test_case["mic_device_id"]
system_sdl_id = test_case["system_device_id"]
mic_pyaudio_id = (
mapper.get_pyaudio_device_id(mic_sdl_id) if mic_sdl_id is not None else None
)
system_pyaudio_id = (
mapper.get_pyaudio_device_id(system_sdl_id)
if system_sdl_id is not None
else None
)
print(f" For whisper.cpp: mic SDL {mic_sdl_id}, system SDL {system_sdl_id}")
print(
f" For audio monitoring: mic PyAudio {mic_pyaudio_id}, system PyAudio {system_pyaudio_id}"
)
# Show what whisper.cpp commands would be generated
if mic_sdl_id is not None:
print(f" Mic whisper.cpp: whisper-stream ... -c {mic_sdl_id}")
if system_sdl_id is not None:
print(f" System whisper.cpp: whisper-stream ... -c {system_sdl_id}")
def main():
"""Main test function"""
# Test 1: Flask endpoint logic
flask_response = test_flask_device_endpoint()
if flask_response:
# Test 2: Device selection logic
test_device_selection_logic()
print("\n🎯 SUMMARY:")
print("=" * 30)
print("✅ SDL device mapping working")
print("✅ Flask endpoint logic working")
print("✅ Device selection logic working")
print()
print("💡 Next steps:")
print("1. Start Flask app: python app.py")
print("2. Test frontend device selection")
print("3. Verify whisper.cpp uses correct SDL device IDs")
else:
print("\n❌ Device mapping failed")
if __name__ == "__main__":
main()