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

43 lines
1.1 KiB
Python

import pyaudio
from flask import Flask, render_template, send_from_directory
from flask_socketio import SocketIO, emit
import wave
import io
app = Flask(__name__)
socketio = SocketIO(app)
@socketio.on('audio')
def handle_audio(audio_data):
# Procesa los datos de audio y almacénalos en un archivo
audio_file = wave.open("audio/audio.wav", "w")
audio_file.setsampwidth(2)
audio_file.setnchannels(1)
audio_file.setframerate(44100)
audio_file.writeframes(io.BytesIO(audio_data).read())
audio_file.close()
print("Audio guardado")
# Reproduce el audio en el servidor utilizando PyAudio
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(2),
channels=1,
rate=44100,
output=True)
with wave.open("audio.wav", "r") as wav:
stream.write(wav.readframes(wav.getnframes()))
stream.stop_stream()
stream.close()
p.terminate()
@app.route('/')
def audio_file():
return render_template("index2.html")
if __name__ == '__main__':
socketio.run(app)