-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
81 lines (63 loc) · 2.4 KB
/
main.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import speech_recognition as sr
from pydub import AudioSegment
import logging
# Configure logging
logging.basicConfig(filename='assets/main.log',
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
r = sr.Recognizer()
def record_text():
try:
# Check if a microphone is available
with sr.Microphone() as src:
# Adjusts for ambient noise
r.adjust_for_ambient_noise(source=src, duration=0.2)
print('Listening...')
# Captures the audio
audio2 = r.listen(src)
# Recognize the audio using Google Speech Recognition
text_data = r.recognize_google(audio2)
# Convert the text to lowercase
text_data = text_data.lower()
# Save the audio
audio_file_path = os.path.join('new_audios', f'{text_data}.wav')
if not os.path.exists('audio'):
os.makedirs('audio')
with open(audio_file_path, 'wb') as f:
f.write(audio2.get_wav_data())
return text_data, audio_file_path
except AssertionError as e:
logging.error(f'Assertion error')
print(f'Microphone access error: {e}')
except sr.RequestError as e:
logging.error(f'Request error')
print(f'Request error: {e}')
except sr.UnknownValueError:
logging.error(f'Unknown value error')
print('Unknown value error')
return '', ''
def output_text(text, audio_path):
# Only write non-empty text
if text:
with open('words.txt', 'a') as f:
f.write(f'{text} ({audio_path})')
f.write('\n')
return
# Main loop
try:
while True:
# Record speech and convert to text
text, audio_path = record_text()
# Exit the loop gracefully
if text in ['exit', 'stop']:
break
# Only output non-empty text
if text:
output_text(text, audio_path)
print(f"Text: '{text}' and audio recorded at '{audio_path}' successfully written")
except KeyboardInterrupt:
logging.error(f'Keyboard interruption')
print('\nProgram interrupted. Exiting gracefully...')
finally:
print('Program has stopped.')