Peter Michael Gits Claude commited on
Commit
aab69ad
Β·
1 Parent(s): 64268ab

fix: Audio buffer size error - handle MediaRecorder format v0.4.6

Browse files

- Fix buffer size error by properly handling MediaRecorder audio format
- Replace numpy.frombuffer with file-based audio processing
- MediaRecorder produces WebM/OGG format, not raw PCM data
- Add process_audio_file_webrtc() method for proper file handling
- Implement file size analysis for WebRTC demo functionality
- Clean up duplicate transcription sending code
- Better error handling and temporary file cleanup

Audio Processing:
- Save received audio data as temporary WebM file
- Analyze file size instead of attempting raw buffer conversion
- Proper cleanup of temporary files after processing

πŸ€– Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (2) hide show
  1. version.py +2 -2
  2. webrtc/server/websocket_handler.py +44 -29
version.py CHANGED
@@ -2,8 +2,8 @@
2
  Version information for ChatCal Voice-Enabled AI Assistant
3
  """
4
 
5
- __version__ = "0.4.5"
6
- __build_date__ = "2025-08-20T15:30:00"
7
  __description__ = "Voice-Enabled ChatCal AI Assistant with Hugging Face deployment"
8
 
9
  def get_version_info():
 
2
  Version information for ChatCal Voice-Enabled AI Assistant
3
  """
4
 
5
+ __version__ = "0.4.6"
6
+ __build_date__ = "2025-08-20T16:00:00"
7
  __description__ = "Voice-Enabled ChatCal AI Assistant with Hugging Face deployment"
8
 
9
  def get_version_info():
webrtc/server/websocket_handler.py CHANGED
@@ -68,37 +68,38 @@ class WebRTCHandler:
68
  try:
69
  logger.info(f"🎀 Received {len(audio_data)} bytes from {client_id}")
70
 
71
- # Convert bytes to numpy array
72
- audio_array = np.frombuffer(audio_data, dtype=np.float32)
 
 
 
73
 
74
- # Add to buffer for potential streaming processing
75
- if client_id not in self.audio_buffers:
76
- self.audio_buffers[client_id] = []
77
-
78
- self.audio_buffers[client_id].append(audio_array)
79
-
80
- # For Phase 1: Process chunk immediately (later we'll do streaming)
81
- if len(audio_array) > 0:
82
- transcription = await self.process_audio_chunk_real_time(audio_array, sample_rate)
83
 
84
- if transcription and transcription != "Error":
85
  # Send transcription back to client
86
  await self.send_message(client_id, {
87
- "type": "transcription",
88
  "text": transcription,
89
  "timestamp": datetime.now().isoformat(),
90
- "audio_length": len(audio_array) / sample_rate,
91
- "is_final": False # For streaming, this will be dynamic
92
  })
93
 
94
  logger.info(f"πŸ“ Transcription sent to {client_id}: {transcription[:50]}...")
95
  else:
96
- # Send error message instead of demo text
97
  await self.send_message(client_id, {
98
  "type": "error",
99
- "message": "STT service unavailable",
100
  "timestamp": datetime.now().isoformat()
101
  })
 
 
 
 
102
 
103
  except Exception as e:
104
  logger.error(f"Error processing audio chunk for {client_id}: {e}")
@@ -108,25 +109,39 @@ class WebRTCHandler:
108
  "timestamp": datetime.now().isoformat()
109
  })
110
 
111
- async def process_audio_chunk_real_time(self, audio_array: np.ndarray, sample_rate: int) -> Optional[str]:
112
- """Process audio chunk for WebRTC demonstration - TO BE REPLACED WITH REAL STT"""
113
  try:
114
- logger.info(f"🎀 WebRTC: Processing {len(audio_array)} samples at {sample_rate}Hz")
115
 
116
- # For WebRTC demonstration, return a simulation based on audio characteristics
117
- # TODO: Replace with real STT service that supports WebSocket/WebRTC
118
- duration = len(audio_array) / sample_rate
119
 
120
- if duration > 2.0: # Longer audio
121
- transcription = f"WebRTC test: Audio received ({duration:.1f}s, {sample_rate}Hz)"
122
- elif duration > 0.5: # Medium audio
123
- transcription = f"WebRTC test: Short audio ({duration:.1f}s)"
124
- else: # Very short audio
125
- transcription = "WebRTC test: Brief audio detected"
 
 
 
 
126
 
127
  logger.info(f"🎀 WebRTC Demo: {transcription}")
128
  return transcription
129
 
 
 
 
 
 
 
 
 
 
 
 
130
  except Exception as e:
131
  logger.error(f"WebRTC audio processing failed: {e}")
132
  return None
 
68
  try:
69
  logger.info(f"🎀 Received {len(audio_data)} bytes from {client_id}")
70
 
71
+ # MediaRecorder typically produces WebM/OGG/WAV format, not raw PCM
72
+ # For WebRTC demo, we'll save the audio data temporarily and process it
73
+ with tempfile.NamedTemporaryFile(suffix='.webm', delete=False) as tmp_file:
74
+ tmp_file.write(audio_data)
75
+ tmp_file_path = tmp_file.name
76
 
77
+ try:
78
+ # Process the audio file directly (WebRTC demo mode)
79
+ transcription = await self.process_audio_file_webrtc(tmp_file_path, sample_rate)
 
 
 
 
 
 
80
 
81
+ if transcription:
82
  # Send transcription back to client
83
  await self.send_message(client_id, {
84
+ "type": "transcription",
85
  "text": transcription,
86
  "timestamp": datetime.now().isoformat(),
87
+ "audio_size": len(audio_data),
88
+ "format": "webm/audio"
89
  })
90
 
91
  logger.info(f"πŸ“ Transcription sent to {client_id}: {transcription[:50]}...")
92
  else:
93
+ # Send error message
94
  await self.send_message(client_id, {
95
  "type": "error",
96
+ "message": "Audio processing failed",
97
  "timestamp": datetime.now().isoformat()
98
  })
99
+ finally:
100
+ # Clean up temporary file
101
+ if os.path.exists(tmp_file_path):
102
+ os.unlink(tmp_file_path)
103
 
104
  except Exception as e:
105
  logger.error(f"Error processing audio chunk for {client_id}: {e}")
 
109
  "timestamp": datetime.now().isoformat()
110
  })
111
 
112
+ async def process_audio_file_webrtc(self, audio_file_path: str, sample_rate: int) -> Optional[str]:
113
+ """Process audio file for WebRTC demonstration - handles MediaRecorder formats"""
114
  try:
115
+ logger.info(f"🎀 WebRTC: Processing audio file {audio_file_path}")
116
 
117
+ # Get file size for demo analysis
118
+ file_size = os.path.getsize(audio_file_path)
 
119
 
120
+ # For WebRTC demonstration, analyze file characteristics
121
+ # TODO: Replace with real STT service that supports WebSocket/WebRTC
122
+ if file_size > 10000: # Larger audio files
123
+ transcription = f"WebRTC test: Large audio file ({file_size} bytes, {sample_rate}Hz)"
124
+ elif file_size > 2000: # Medium audio files
125
+ transcription = f"WebRTC test: Medium audio file ({file_size} bytes)"
126
+ elif file_size > 100: # Small but valid audio
127
+ transcription = f"WebRTC test: Small audio file ({file_size} bytes)"
128
+ else: # Very small or empty
129
+ transcription = "WebRTC test: Minimal audio data detected"
130
 
131
  logger.info(f"🎀 WebRTC Demo: {transcription}")
132
  return transcription
133
 
134
+ except Exception as e:
135
+ logger.error(f"WebRTC audio file processing failed: {e}")
136
+ return None
137
+
138
+ async def process_audio_chunk_real_time(self, audio_array: np.ndarray, sample_rate: int) -> Optional[str]:
139
+ """Legacy method - kept for compatibility"""
140
+ try:
141
+ logger.info(f"🎀 WebRTC: Processing {len(audio_array)} samples at {sample_rate}Hz")
142
+ duration = len(audio_array) / sample_rate
143
+ transcription = f"WebRTC test: Audio array ({duration:.1f}s, {sample_rate}Hz)"
144
+ return transcription
145
  except Exception as e:
146
  logger.error(f"WebRTC audio processing failed: {e}")
147
  return None