-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_engine.py
More file actions
292 lines (233 loc) · 10.6 KB
/
audio_engine.py
File metadata and controls
292 lines (233 loc) · 10.6 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import sounddevice as sd
import numpy as np
import soundfile as sf
import sys
import os
class AudioEngine:
def __init__(self, input_device_id, output_device_id, noise_file_path=None, noise_volume=0.2, sample_rate=44100, block_size=1024):
self.input_device_id = input_device_id
self.output_device_id = output_device_id
self.sample_rate = sample_rate
self.block_size = block_size
self.noise_volume = noise_volume
self.stream = None
self.running = False
self.last_input_level = 0.0
self.last_output_level = 0.0
self.noise_data = None
self.noise_index = 0
self.preview_data = None
self.preview_index = 0
self.active_effects = [] # List of currently playing effects
if noise_file_path:
self.load_noise(noise_file_path)
def load_noise(self, file_path):
if not os.path.exists(file_path):
print(f"File not found: {file_path}")
self.noise_data = None
return
print(f"Loading noise file: {file_path}")
try:
data, fs = sf.read(file_path, dtype='float32')
# Handle channels first
if len(data.shape) == 1:
data = data.reshape(-1, 1)
# Resample if necessary
if fs != self.sample_rate:
print(f"Resampling noise from {fs} to {self.sample_rate}...")
data = self.resample_audio(data, fs, self.sample_rate)
self.noise_data = data
self.noise_index = 0 # Reset index
print(f"Noise loaded. Shape: {self.noise_data.shape}")
except Exception as e:
print(f"Error loading noise file: {e}")
self.noise_data = None
def load_preview(self, file_path):
if not file_path or not os.path.exists(file_path):
self.preview_data = None
return
try:
data, fs = sf.read(file_path, dtype='float32')
if len(data.shape) == 1:
data = data.reshape(-1, 1)
if fs != self.sample_rate:
data = self.resample_audio(data, fs, self.sample_rate)
self.preview_data = data
self.preview_index = 0
except Exception as e:
print(f"Error loading preview: {e}")
self.preview_data = None
def stop_preview(self):
self.preview_data = None
self.preview_index = 0
def play_effect(self, file_path, volume=1.0):
if not os.path.exists(file_path):
return
try:
data, fs = sf.read(file_path, dtype='float32')
if len(data.shape) == 1:
data = data.reshape(-1, 1)
if fs != self.sample_rate:
# Resample
data = self.resample_audio(data, fs, self.sample_rate)
self.active_effects.append({
'data': data,
'index': 0,
'volume': volume
})
except Exception as e:
print(f"Error playing effect: {e}")
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 callback(self, indata, outdata, frames, time, status):
if status:
print(status, file=sys.stderr)
# 1. Start with Input
mixed = indata.copy()
# Track RMS input level
if indata.size:
self.last_input_level = float(np.sqrt(np.mean(np.square(indata))))
else:
self.last_input_level = 0.0
# 2. Mix Noise if available
if self.noise_data is not None:
noise_len = len(self.noise_data)
current_idx = self.noise_index
remaining = frames
noise_chunk = np.zeros((frames, self.noise_data.shape[1]), dtype='float32')
# Fill chunk with looping
chunk_idx = 0
while remaining > 0:
take = min(remaining, noise_len - current_idx)
noise_chunk[chunk_idx:chunk_idx+take] = self.noise_data[current_idx:current_idx+take]
remaining -= take
chunk_idx += take
current_idx = (current_idx + take) % noise_len
self.noise_index = current_idx
# Match channels
if noise_chunk.shape[1] != indata.shape[1]:
if noise_chunk.shape[1] == 1:
noise_chunk = np.tile(noise_chunk, (1, indata.shape[1]))
else:
new_noise = np.zeros_like(indata)
min_ch = min(noise_chunk.shape[1], indata.shape[1])
new_noise[:, :min_ch] = noise_chunk[:, :min_ch]
noise_chunk = new_noise
mixed += noise_chunk * self.noise_volume
# 2.5 Mix Preview if available (Looping)
if self.preview_data is not None:
prev_len = len(self.preview_data)
current_idx = self.preview_index
remaining = frames
prev_chunk = np.zeros((frames, self.preview_data.shape[1]), dtype='float32')
chunk_idx = 0
while remaining > 0:
take = min(remaining, prev_len - current_idx)
prev_chunk[chunk_idx:chunk_idx+take] = self.preview_data[current_idx:current_idx+take]
remaining -= take
chunk_idx += take
current_idx = (current_idx + take) % prev_len
self.preview_index = current_idx
# Match channels
if prev_chunk.shape[1] != indata.shape[1]:
if prev_chunk.shape[1] == 1:
prev_chunk = np.tile(prev_chunk, (1, indata.shape[1]))
else:
new_prev = np.zeros_like(indata)
min_ch = min(prev_chunk.shape[1], indata.shape[1])
new_prev[:, :min_ch] = prev_chunk[:, :min_ch]
prev_chunk = new_prev
mixed += prev_chunk # Full volume for preview
# 3. Mix Active Effects
# Iterate backwards to allow removal
for i in range(len(self.active_effects) - 1, -1, -1):
effect = self.active_effects[i]
data = effect['data']
idx = effect['index']
vol = effect['volume']
remaining = frames
effect_len = len(data)
if idx >= effect_len:
self.active_effects.pop(i)
continue
take = min(remaining, effect_len - idx)
# Get chunk
effect_chunk = data[idx:idx+take]
# Match channels
if effect_chunk.shape[1] != indata.shape[1]:
if effect_chunk.shape[1] == 1:
effect_chunk = np.tile(effect_chunk, (1, indata.shape[1]))
else:
# Truncate or pad
# For simplicity, just take min channels or pad
temp = np.zeros((take, indata.shape[1]), dtype='float32')
min_ch = min(effect_chunk.shape[1], indata.shape[1])
temp[:, :min_ch] = effect_chunk[:, :min_ch]
effect_chunk = temp
# Add to mix (handle if effect is shorter than frame)
# We need to add it to the correct part of 'mixed'
# Since we are processing the whole frame at once, and effects might end mid-frame,
# we actually need to be careful.
# Simplified: We just add what we have to the start of the mix buffer (since we don't support sub-frame timing for start yet)
# But wait, if we are in a loop for noise, we are filling a buffer.
# For effects, we just take the next 'take' samples and add them to mixed[:take]
mixed[:take] += effect_chunk * vol
effect['index'] += take
if effect['index'] >= effect_len:
self.active_effects.pop(i)
# 4. Clip and Output
np.clip(mixed, -1, 1, out=mixed)
outdata[:] = mixed
if mixed.size:
self.last_output_level = float(np.sqrt(np.mean(np.square(mixed))))
else:
self.last_output_level = 0.0
def start(self):
if self.running:
return
self.running = True
try:
# Check device capabilities to match channels
in_info = sd.query_devices(self.input_device_id)
out_info = sd.query_devices(self.output_device_id)
# Determine channels (min of both)
channels = min(in_info['max_input_channels'], out_info['max_output_channels'])
print(f"Starting stream with {channels} channels...")
# Create and start the stream
self.stream = sd.Stream(device=(self.input_device_id, self.output_device_id),
samplerate=self.sample_rate,
blocksize=self.block_size,
channels=channels,
callback=self.callback)
self.stream.start()
print("Audio stream started.")
except Exception as e:
print(f"\nError starting stream: {e}")
self.running = False
raise e
def stop(self):
if not self.running:
return
print("Stopping stream...")
self.running = False
if self.stream:
self.stream.stop()
self.stream.close()
self.stream = None
if __name__ == "__main__":
# Test with default devices if run directly
print("Running AudioEngine test with default devices...")
engine = AudioEngine(input_device_id=sd.default.device[0],
output_device_id=sd.default.device[1])
engine.start()