-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmic_demo.py
More file actions
50 lines (39 loc) · 1.42 KB
/
mic_demo.py
File metadata and controls
50 lines (39 loc) · 1.42 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
mic_demo.py – Live-AEC-Demo (Mikrofon ↔ Lautsprecher)
-----------------------------------------------------
Voraussetzungen:
pip install numpy sounddevice
Starte das Skript am besten mit Kopfhörer + externem Lautsprecher, damit ein
spürbares Echo existiert, das der LMS-Filter unterdrücken kann.
"""
import numpy as np
import sounddevice as sd
from lms import lms_filter # <-- setzt die gepatchte Version voraus
FS = 48_000 # Abtastrate
BLOCK = 1024 # Blocklänge = Filterlänge
MU = 5e-4 # Schrittweite (µ)
coeffs = np.zeros(BLOCK, dtype=np.float32) # Startkoeffizienten
def callback(indata, outdata, frames, time, status):
"""Sounddevice-Callback pro Audioblock."""
global coeffs
mic = indata[:, 0] # Mikro-Kanal
ref = outdata[:, 0].copy() # Lautsprecher-Signal als Referenz
# Einmaliges LMS-Update für diesen Block
err, coeffs, _ = lms_filter(
desired_signal=mic,
reference_input=ref,
filter_coeff=coeffs,
step_sizes=[MU],
safe=True
)
# Fehler (Echo-reduziertes Signal) ausgeben
outdata[:, 0] = err.astype(np.float32)
print("Starte Live-Echo-Canceller … (Strg+C zum Abbrechen)")
try:
with sd.Stream(channels=1, samplerate=FS, blocksize=BLOCK, callback=callback):
while True:
sd.sleep(1000)
except KeyboardInterrupt:
print("Beendet.")