-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecorder.py
More file actions
55 lines (44 loc) · 2.2 KB
/
Copy pathRecorder.py
File metadata and controls
55 lines (44 loc) · 2.2 KB
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
import tkinter as tk
import speech_recognition as sr
# Does both the recording and the speech recognition.
class Recorder:
def __init__(self):
self.r = sr.Recognizer()
self.textList = []
self.full_text = ""
self.recordingStatus = False
def getRecordingStatus(self):
return self.recordingStatus
def setRecordingStatus(self, status):
self.recordingStatus = status
def save_text_to_file(self, filename="recorded_text.txt"):
# Save the recognized text to a file.
# Args:
# filename (str): The name of the file to save the text to.
# Join the list of strings into a single string with line breaks
self.full_text = "\n".join(self.textList)
# Write the text to a file
with open(filename, "w", encoding="utf-8") as file:
file.write(self.full_text)
# Records small snippets and converts them to text.
def RecordAndConvertToText(self):
try:
# Infinite loop to keep recording continuously as long as recordingStatus is True.
while self.recordingStatus:
with sr.Microphone() as source:
print("Recording will start now")
audio = self.r.listen(source)
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
currentLine = self.r.recognize_google(audio)
print("Google Speech Recognition thinks you said and it has been added to the total list" + currentLine)
self.textList.append(currentLine)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
except KeyboardInterrupt:
print("Recording stopped.")
self.save_text_to_file("my_recorded_text.txt")