working for snippeds < 1 min

This commit is contained in:
Christian Rute 2025-01-06 18:05:19 +01:00
parent cf935a16bc
commit 5ae6356c04

View File

@ -4,6 +4,7 @@ import sounddevice as sd
import numpy as np
import wave
import gradio as gr
import time
# Setup device
device = "cuda:0" if torch.cuda.is_available() else "cpu"
@ -32,13 +33,15 @@ pipe = pipeline(
SAMPLE_RATE = 16000 # Whisper prefers 16 kHz
FILENAME = "recorded_audio.wav"
is_recording = False
start_time = None # Track the recording start time
recorded_audio = None
def start_recording():
"""Starts recording audio."""
global is_recording, recorded_audio
global is_recording, recorded_audio, start_time
is_recording = True
start_time = time.time() # Record the start time
print("Recording started...")
recorded_audio = sd.rec(int(SAMPLE_RATE * 60), samplerate=SAMPLE_RATE, channels=1, dtype=np.float32)
return "Recording... Click 'Stop Recording' to finish."
@ -46,13 +49,14 @@ def start_recording():
def stop_recording():
"""Stops recording audio and saves it."""
global is_recording, recorded_audio
global is_recording, recorded_audio, start_time
if not is_recording:
return "Not recording!"
sd.stop()
is_recording = False
print("Recording stopped.")
save_audio_to_wav(recorded_audio, FILENAME)
elapsed_time = time.time() - start_time # Calculate elapsed time
print(f"Recording stopped. Duration: {elapsed_time:.2f} seconds.")
save_audio_to_wav(recorded_audio[:int(SAMPLE_RATE * elapsed_time)], FILENAME) # Truncate to actual duration
return "Recording stopped. Click 'Transcribe' to see the result."