ciyidogan commited on
Commit
0484fc1
·
verified ·
1 Parent(s): ff3b132

Update websocket_manager.py

Browse files
Files changed (1) hide show
  1. websocket_manager.py +56 -0
websocket_manager.py CHANGED
@@ -269,6 +269,62 @@ class WebSocketManager:
269
  message_type = message.get("type")
270
 
271
  if message_type == "audio_chunk":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  # Audio data from client
273
  await self.event_bus.publish(Event(
274
  type=EventType.AUDIO_CHUNK_RECEIVED,
 
269
  message_type = message.get("type")
270
 
271
  if message_type == "audio_chunk":
272
+ # Audio data from client
273
+ audio_data_base64 = message.get("data")
274
+
275
+ if audio_data_base64:
276
+ # Debug için audio analizi
277
+ try:
278
+ import base64
279
+ import struct
280
+
281
+ # Base64'ten binary'ye çevir
282
+ audio_data = base64.b64decode(audio_data_base64)
283
+
284
+ # Session için debug counter
285
+ if not hasattr(self, 'audio_debug_counters'):
286
+ self.audio_debug_counters = {}
287
+
288
+ if session_id not in self.audio_debug_counters:
289
+ self.audio_debug_counters[session_id] = 0
290
+
291
+ # İlk 5 chunk için detaylı log
292
+ if self.audio_debug_counters[session_id] < 5:
293
+ log_info(f"🔊 Audio chunk analysis #{self.audio_debug_counters[session_id]}",
294
+ session_id=session_id,
295
+ size_bytes=len(audio_data),
296
+ base64_size=len(audio_data_base64))
297
+
298
+ # İlk 20 byte'ı hex olarak göster
299
+ if len(audio_data) >= 20:
300
+ log_debug(f" First 20 bytes (hex): {audio_data[:20].hex()}")
301
+
302
+ # Linear16 (little-endian int16) olarak yorumla
303
+ samples = struct.unpack('<10h', audio_data[:20])
304
+ log_debug(f" First 10 samples: {samples}")
305
+ log_debug(f" Max amplitude (first 10): {max(abs(s) for s in samples)}")
306
+
307
+ # Tüm chunk'ı analiz et
308
+ total_samples = len(audio_data) // 2
309
+ if total_samples > 0:
310
+ all_samples = struct.unpack(f'<{total_samples}h', audio_data[:total_samples*2])
311
+ max_amp = max(abs(s) for s in all_samples)
312
+ avg_amp = sum(abs(s) for s in all_samples) / total_samples
313
+
314
+ # Sessizlik kontrolü
315
+ silent = max_amp < 100 # Linear16 için düşük eşik
316
+
317
+ log_info(f" Audio stats - Max: {max_amp}, Avg: {avg_amp:.1f}, Silent: {silent}")
318
+
319
+ # Eğer çok sessizse uyar
320
+ if max_amp < 50:
321
+ log_warning(f"⚠️ Very low audio level detected! Max amplitude: {max_amp}")
322
+
323
+ self.audio_debug_counters[session_id] += 1
324
+
325
+ except Exception as e:
326
+ log_error(f"Error analyzing audio chunk: {e}")
327
+
328
  # Audio data from client
329
  await self.event_bus.publish(Event(
330
  type=EventType.AUDIO_CHUNK_RECEIVED,