-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_manager.py
More file actions
199 lines (167 loc) · 6.59 KB
/
audio_manager.py
File metadata and controls
199 lines (167 loc) · 6.59 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import sounddevice as sd
import os
import numpy as np
import soundfile as sf
from audio_engine import AudioEngine
class AudioManager:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(AudioManager, cls).__new__(cls)
cls._instance.engine = None
cls._instance.input_id = None
cls._instance.output_id = None
cls._instance.noise_path = None
cls._instance.noise_volume = 0.2
cls._instance.preview_stream = None
cls._instance.preview_data = None
cls._instance.preview_index = 0
cls._instance.preview_samplerate = 44100
cls._instance.preview_channels = 1
return cls._instance
def get_devices(self):
devices = sd.query_devices()
hostapis = sd.query_hostapis()
result = []
for i, dev in enumerate(devices):
api_name = hostapis[dev['hostapi']]['name']
# Only include useful devices
if dev['max_input_channels'] > 0 or dev['max_output_channels'] > 0:
result.append({
"id": i,
"name": dev['name'],
"api": api_name,
"inputs": dev['max_input_channels'],
"outputs": dev['max_output_channels']
})
return result
def get_host_apis(self):
return [api['name'] for api in sd.query_hostapis()]
def set_config(self, input_id, output_id, noise_path, volume):
self.input_id = int(input_id)
self.output_id = int(output_id)
self.noise_path = noise_path
self.noise_volume = float(volume) / 100.0
# If running, update on the fly
if self.engine and self.engine.running:
self.engine.noise_volume = self.noise_volume
if self.noise_path:
self.engine.load_noise(self.noise_path)
else:
self.engine.noise_data = None
def start(self):
if self.engine and self.engine.running:
return {"status": "already_running"}
try:
self.engine = AudioEngine(
self.input_id,
self.output_id,
noise_file_path=self.noise_path,
noise_volume=self.noise_volume
)
self.engine.start()
return {"status": "started"}
except Exception as e:
return {"status": "error", "message": str(e)}
def stop(self):
if self.engine:
self.engine.stop()
self.engine = None
return {"status": "stopped"}
def get_status(self):
if self.engine and self.engine.running:
return "running"
return "stopped"
def play_effect(self, sound_path, volume=1.0):
if self.engine and self.engine.running:
self.engine.play_effect(sound_path, volume)
return {"status": "playing"}
return {"status": "not_running"}
# --- Live meter helpers ---
def get_levels(self):
if self.engine and self.engine.running:
return {
"input": self.engine.last_input_level,
"output": self.engine.last_output_level
}
return {"input": 0.0, "output": 0.0}
# --- Sound preview helpers ---
def _preview_callback(self, outdata, frames, time, status):
if status:
print(status)
if self.preview_data is None:
outdata.fill(0)
return
chunk = np.zeros((frames, self.preview_channels), dtype='float32')
remaining = frames
start = 0
data_len = len(self.preview_data)
while remaining > 0 and data_len > 0:
take = min(remaining, data_len - self.preview_index)
chunk[start:start+take] = self.preview_data[self.preview_index:self.preview_index+take]
self.preview_index = (self.preview_index + take) % data_len
remaining -= take
start += take
outdata[:] = chunk
def resample_audio(self, data, src_rate, target_rate):
if src_rate == target_rate:
return data
ratio = target_rate / src_rate
new_length = int(len(data) * ratio)
# Create time indices for interpolation
x_old = np.linspace(0, len(data), len(data))
x_new = np.linspace(0, len(data), new_length)
# Resample each channel
new_data = np.zeros((new_length, data.shape[1]), dtype='float32')
for i in range(data.shape[1]):
new_data[:, i] = np.interp(x_new, x_old, data[:, i])
return new_data
def start_preview(self, sound_path, output_device=None):
if not os.path.exists(sound_path):
return {"status": "error", "message": "Sound file not found"}
self.stop_preview()
# Always use a separate stream for preview to target the monitoring device (headphones)
# instead of the routing output (virtual cable).
device = output_device if output_device is not None else sd.default.device[1]
# Get device sample rate
try:
dev_info = sd.query_devices(device)
target_fs = int(dev_info['default_samplerate'])
except:
target_fs = 44100 # Fallback
data, fs = sf.read(sound_path, dtype='float32')
if data.ndim == 1:
data = data.reshape(-1, 1)
# Resample to match device
if fs != target_fs:
data = self.resample_audio(data, fs, target_fs)
fs = target_fs
self.preview_samplerate = fs
self.preview_channels = data.shape[1]
self.preview_data = data
self.preview_index = 0
try:
self.preview_stream = sd.OutputStream(
device=device,
samplerate=self.preview_samplerate,
channels=self.preview_channels,
blocksize=1024,
callback=self._preview_callback
)
self.preview_stream.start()
return {"status": "previewing"}
except Exception as e:
self.preview_data = None
self.preview_stream = None
return {"status": "error", "message": str(e)}
def stop_preview(self):
if self.preview_stream:
try:
self.preview_stream.stop()
self.preview_stream.close()
except Exception:
pass
self.preview_stream = None
self.preview_data = None
self.preview_index = 0
return {"status": "stopped"}