Spaces:
Runtime error
Runtime error
File size: 1,353 Bytes
9690d29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import pyaudio
import wave
from pynput import keyboard
import speech_recognition as sr
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 10
WAVE_OUTPUT_FILENAME = "2.wav"
frames = []
#def transcribe(wav):
def on_press(key):
#if key == keyboard.Key.esc:
# recording = False
# return False
if key == keyboard.KeyCode.from_char('a'):
print("Recording audio...")
# Start recording
global stream, audio
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
# Stop recording
stream.stop_stream()
stream.close()
audio.terminate()
print("Finished recording audio.")
audio_data = sr.AudioData(b''.join(frames), RATE, 2)
print(audio_data)
#r = sr.Recognizer()
#text = r.recognize_google(audio_data)
#print(text)
def on_release(key):
if key == keyboard.Key.esc:
return False
# Start the listener
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
|