sakshigpatil's picture
add python files that includes whisper, gtts, wev2vec2, sounddevice
428ab68 verified
```python
import sounddevice as sd
import numpy as np
import wave
import os
from datetime import datetime
class AudioRecorder:
def __init__(self, sample_rate=44100, channels=1):
self.sample_rate = sample_rate
self.channels = channels
self.recording = False
self.frames = []
def callback(self, indata, frames, time, status):
if self.recording:
self.frames.append(indata.copy())
def start_recording(self):
self.recording = True
self.frames = []
self.stream = sd.InputStream(
samplerate=self.sample_rate,
channels=self.channels,
callback=self.callback,
dtype='float32'
)
self.stream.start()
def stop_recording(self):
self.recording = False
self.stream.stop()
self.stream.close()
return np.concatenate(self.frames)
def save_wav(self, filename, audio_data):
with wave.open(filename, 'wb') as wf:
wf.setnchannels(self.channels)
wf.setsampwidth(2)
wf.setframerate(self.sample_rate)
wf.writeframes((audio_data * 32767).astype(np.int16))
def record_and_save(self, duration=5, output_dir='recordings'):
os.makedirs(output_dir, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(output_dir, f"recording_{timestamp}.wav")
print(f"Recording for {duration} seconds...")
audio_data = sd.rec(int(duration * self.sample_rate), samplerate=self.sample_rate, channels=self.channels)
sd.wait()
self.save_wav(filename, audio_data)
print(f"Saved recording to {filename}")
return filename
```