-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_speech.py
More file actions
57 lines (41 loc) · 1.58 KB
/
record_speech.py
File metadata and controls
57 lines (41 loc) · 1.58 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
# Description: This script uses the SpeechRecognition module to record speech
# and trancribes it to a file
# Added an __init__ section so that this can be ran as an individual script OR be imported as a module.
# See the main() as an example of how to instantiate this module.
# Import the SpeechRecognition module
import speech_recognition as sr
# Playback text to speech
import pyttsx3
class recordSpeech:
# Setup build the transcribed speech
def transcribe(self):
# Initialize the speech recognizer module.
record = sr.Recognizer()
# Begin recording speech
# mic = sr.Microphone(device_index=0)
mic = sr.Microphone(device_index = None, sample_rate = 48000, chunk_size = 1024)
with mic as source_recording:
# Put recording into audio variable
audio = record.listen(source_recording)
# Return the trancribed speech
return record.recognize_google(audio)
def export(self, transcription):
# open file to write
with open('transcription.txt', mode='w') as file:
file.write("Transcribed Speech: ")
file.write("\n")
file.write(transcription)
return
def main():
# Build the object
sp = recordSpeech()
# Build text to be transcribed. Return it to the text variable
text = sp.transcribe()
# Perform the export of the speech to a file
engine = pyttsx3.init()
print("Spoken text: "+text)
engine.say(text)
engine.runAndWait()
sp.export(text)
if __name__ == '__main__':
main()