websoket/prueba.py
developertrinidad08 7e6cf29479 first commit
2023-01-16 18:11:14 -03:00

28 lines
870 B
Python

import asyncio
import aiohttp
from aiohttp import web
from aiohttp.web_ws import WebSocketResponse
from pydub import AudioSegment
import pyaudio
p = pyaudio.PyAudio()
async def handler(request):
ws = WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.BINARY:
audio_data = msg.data
# Guardar audio en archivo raw
with open("audio.bin", "wb+",buffering=4096) as audio_file:
audio_file.write(audio_data)
# Convertir archivo raw a formato wav
audio = AudioSegment.from_raw("audio.bin", sample_width=2, channels=1, frame_rate=44100)
audio.export("audio1.wav", format="wav")
return ws
app = web.Application()
app.add_routes([web.get('/', handler)])
web.run_app(app, host='localhost', port=8000)