see also:
import whisper
import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wav
# Load the Whisper model
model = whisper.load_model("base") #this will automatically download the model to user\.cache\whisper if not already downloaded
# Function to record audio from the microphone
def record_audio(duration, fs):
print("Recording...")
recording = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
sd.wait() # Wait until the recording is finished
return recording
# Function to save the recorded audio to a WAV file
def save_audio(filename, recording, fs):
wav.write(filename, fs, recording)
# Function to transcribe audio using Whisper
def transcribe_audio(filename):
result = model.transcribe(filename)
return result['text']
if __name__ == "__main__":
duration = 10 # Duration of the recording in seconds
fs = 16000 # Sample rate
# Record audio
recording = record_audio(duration, fs)
# Save the recorded audio to a file
audio_filename = "recorded_audio.wav"
save_audio(audio_filename, recording, fs)
# Transcribe the audio file
transcription = transcribe_audio(audio_filename)
print("Transcription: ", transcription)
Record Audio: The sounddevice library is used to record audio from the microphone. The recording duration and sample rate are specified. Save Audio: The recorded audio is saved to a WAV file using the scipy.io.wavfile module.