sergiolucero commited on
Commit
fda8997
1 Parent(s): e37cc30

Create audiolib.py

Browse files
Files changed (1) hide show
  1. audiolib.py +35 -0
audiolib.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import wave
2
+ import pyaudio
3
+ from ailib import whisper_transcribe
4
+ ##################################################
5
+ # Configuración del audio
6
+ FORMAT = pyaudio.paInt16
7
+ CHANNELS = 1
8
+ RATE = 16000
9
+ CHUNK = 1024
10
+ ##################################################
11
+ def wavsave(frames, fn):
12
+ wf = wave.open(fn, 'wb')
13
+ wf.setnchannels(CHANNELS)
14
+ wf.setsampwidth(2) #audio.get_sample_size(FORMAT))
15
+ wf.setframerate(RATE)
16
+ wf.writeframes(b''.join(frames))
17
+ wf.close()
18
+
19
+ def grabar_audio():
20
+ audio = pyaudio.PyAudio()
21
+ stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
22
+ frames = []
23
+ while True:
24
+ data = stream.read(CHUNK)
25
+ frames.append(data)
26
+ if len(frames) >= 50: # Grabar durante 5 segundos
27
+ break
28
+ stream.stop_stream()
29
+ stream.close()
30
+ audio.terminate()
31
+ return frames
32
+
33
+ def transcribir_audio(frames):
34
+ wavsave(frames, 'temp.wav')
35
+ return whisper_transcribe('temp.wav')