corfodemo / audiolib.py
sergiolucero's picture
Create audiolib.py
fda8997 verified
raw
history blame contribute delete
No virus
993 Bytes
import wave
import pyaudio
from ailib import whisper_transcribe
##################################################
# Configuración del audio
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
CHUNK = 1024
##################################################
def wavsave(frames, fn):
wf = wave.open(fn, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(2) #audio.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
def grabar_audio():
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
frames = []
while True:
data = stream.read(CHUNK)
frames.append(data)
if len(frames) >= 50: # Grabar durante 5 segundos
break
stream.stop_stream()
stream.close()
audio.terminate()
return frames
def transcribir_audio(frames):
wavsave(frames, 'temp.wav')
return whisper_transcribe('temp.wav')