shukdevdatta123 commited on
Commit
37784fb
·
verified ·
1 Parent(s): 27541b9

Update text_speech_utils.py

Browse files
Files changed (1) hide show
  1. text_speech_utils.py +20 -12
text_speech_utils.py CHANGED
@@ -1,21 +1,28 @@
1
- import sounddevice as sd
2
- import soundfile as sf
3
  import whisper
 
4
  from gtts import gTTS
5
  import os
 
6
 
7
- # Load the Whisper model
8
- model = whisper.load_model("base") # You can use other versions like "small", "medium", or "large"
9
 
10
- # Function to record audio using sounddevice and save it as a .wav file
11
  def record_audio(filename, sec=5, sr=44100):
 
 
 
 
 
12
  print("Recording...")
13
- # Record the audio from the microphone (mono channel)
14
- audio_data = sd.rec(int(sec * sr), samplerate=sr, channels=1, dtype='int16')
15
- sd.wait() # Wait until recording is done
16
 
17
- # Save the audio data to a file using soundfile
18
- sf.write(filename, audio_data, sr)
 
 
 
19
  print(f"Audio saved as {filename}")
20
 
21
  # Function to transcribe audio using Whisper
@@ -33,7 +40,8 @@ def save_text_as_audio(text, audio_filename):
33
  tts.save(audio_filename)
34
  print(f"Audio saved as {audio_filename}")
35
 
36
- # Function to play audio using the system's default audio player
37
  def play_audio(filename):
38
  print("Playing audio...")
39
- os.system(f"start {filename}") # For Windows; for Linux/macOS, use `os.system(f"mpg321 {filename}")`
 
 
1
+ from pydub import AudioSegment
2
+ from pydub.playback import play
3
  import whisper
4
+ import soundfile as sf
5
  from gtts import gTTS
6
  import os
7
+ import tempfile
8
 
9
+ # Load Whisper model
10
+ model = whisper.load_model("base") # You can also try "small", "medium", or "large"
11
 
12
+ # Function to record audio using pydub and save it as a .wav file
13
  def record_audio(filename, sec=5, sr=44100):
14
+ from pydub.generators import Sine
15
+ import io
16
+
17
+ # Generate a sine wave (just as a placeholder for actual recording)
18
+ # In a real-world case, use a microphone input
19
  print("Recording...")
 
 
 
20
 
21
+ # Simulate recording a sound for `sec` seconds at `sr` sample rate
22
+ # NOTE: You'd replace this with actual microphone recording code
23
+ sine_wave = Sine(440).to_audio_segment(duration=sec * 1000) # 440 Hz sine wave for `sec` seconds
24
+
25
+ sine_wave.export(filename, format="wav")
26
  print(f"Audio saved as {filename}")
27
 
28
  # Function to transcribe audio using Whisper
 
40
  tts.save(audio_filename)
41
  print(f"Audio saved as {audio_filename}")
42
 
43
+ # Function to play audio using pydub's playback
44
  def play_audio(filename):
45
  print("Playing audio...")
46
+ audio = AudioSegment.from_wav(filename)
47
+ play(audio)