添加播放状态检测避免回声录制
- 播放时暂停录音处理 - 显示播放状态提示 - 防止播放的音频被重新录制 - 避免产生回声问题
This commit is contained in:
parent
918bfb24af
commit
c01e6ad1f6
@ -46,6 +46,9 @@ class EnergyBasedRecorder:
|
||||
self.pre_record_buffer = [] # 预录音缓冲区
|
||||
self.pre_record_max_frames = int(self.pre_record_duration * self.RATE / self.CHUNK_SIZE) # 最大预录音帧数
|
||||
|
||||
# 播放状态
|
||||
self.is_playing = False # 是否正在播放
|
||||
|
||||
# 性能监控
|
||||
self.frame_count = 0
|
||||
self.start_time = time.time()
|
||||
@ -155,6 +158,9 @@ class EnergyBasedRecorder:
|
||||
def play_audio(self, filename):
|
||||
"""播放音频文件"""
|
||||
try:
|
||||
# 设置播放状态
|
||||
self.is_playing = True
|
||||
|
||||
with wave.open(filename, 'rb') as wf:
|
||||
channels = wf.getnchannels()
|
||||
width = wf.getsampwidth()
|
||||
@ -194,6 +200,9 @@ class EnergyBasedRecorder:
|
||||
except Exception as e:
|
||||
print(f"❌ 播放失败: {e}")
|
||||
self.play_with_system_player(filename)
|
||||
finally:
|
||||
# 恢复播放状态
|
||||
self.is_playing = False
|
||||
|
||||
def play_with_system_player(self, filename):
|
||||
"""使用系统播放器播放音频"""
|
||||
@ -312,6 +321,14 @@ class EnergyBasedRecorder:
|
||||
if len(data) == 0:
|
||||
continue
|
||||
|
||||
# 如果正在播放,跳过音频处理
|
||||
if self.is_playing:
|
||||
# 显示播放状态
|
||||
status = "🔊 播放中... 跳过录音处理"
|
||||
print(f"\r{status}", end='', flush=True)
|
||||
time.sleep(0.1) # 播放时增加延迟减少CPU使用
|
||||
continue
|
||||
|
||||
# 计算能量和零交叉率
|
||||
energy = self.calculate_energy(data)
|
||||
zcr = self.calculate_zero_crossing_rate(data)
|
||||
@ -343,7 +360,6 @@ class EnergyBasedRecorder:
|
||||
self.stop_recording()
|
||||
|
||||
# 显示录音状态(包含预录音信息)
|
||||
pre_duration = len(self.pre_record_buffer) * self.CHUNK_SIZE / self.RATE
|
||||
bg_energy = np.median(self.energy_history[-10:]) if len(self.energy_history) >= 10 else 0
|
||||
status = f"录音中... {recording_duration:.1f}s | 能量: {energy:.0f} | ZCR: {zcr:.0f} | 背景: {bg_energy:.0f}"
|
||||
print(f"\r{status}", end='', flush=True)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user