| 66 | outdata.fill(0) |
| 67 | |
| 68 | async def process_audio(self): |
| 69 | async with websockets.connect(self.server_url) as ws: |
| 70 | while self.running: |
| 71 | if not self.audio_queue.empty(): |
| 72 | # Get recorded audio |
| 73 | audio_data = self.audio_queue.get() |
| 74 | print(f'Data from microphone:{audio_data.shape, audio_data.dtype, audio_data.min(), audio_data.max()}') |
| 75 | |
| 76 | # Convert to base64 |
| 77 | audio_b64 = base64.b64encode(audio_data.tobytes()).decode('utf-8') |
| 78 | |
| 79 | # Send to server |
| 80 | time_sent = time.time() |
| 81 | await ws.send(f"data:audio/raw;base64,{audio_b64}") |
| 82 | |
| 83 | # Receive processed audio |
| 84 | response = await ws.recv() |
| 85 | response = response.split(",")[1] |
| 86 | time_received = time.time() |
| 87 | print(f"Data sent: {audio_b64[:10]}. Data received: {response[:10]}. Received in {(time_received - time_sent) * 1000:.2f} ms") |
| 88 | processed_audio = np.frombuffer( |
| 89 | base64.b64decode(response), |
| 90 | dtype=np.int16 |
| 91 | ).reshape(-1, CHANNELS) |
| 92 | print(f'Data from model:{processed_audio.shape, processed_audio.dtype, processed_audio.min(), processed_audio.max()}') |
| 93 | |
| 94 | self.output_queue.put(processed_audio) |
| 95 | |
| 96 | def start(self): |
| 97 | self.running = True |