Skip to content

Commit 2e18868

Browse files
authored
Add files via upload
1 parent dd186ad commit 2e18868

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

script/main.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
from recording_voice import Recording_Helper
21
from voice_detect import Voice_Detect_Helper
32

3+
def using_audio_to_talk(file_name = 'test.wav'):
4+
from recording_voice import Recording_Helper
5+
recorder = Recording_Helper(file_name=file_name)
6+
voice_detecter = Voice_Detect_Helper(file_path=file_name)
7+
recorder.recording_voice()
8+
voice_detecter.detect_voice()
9+
# you can get result from voice_detecter.result
10+
11+
def using_detecter_to_talk(file_name = 'test.wav'):
12+
voice_detecter = Voice_Detect_Helper(file_path=file_name)
13+
voice_detecter.listener()
14+
415
if __name__ == '__main__':
5-
recorder = Recording_Helper()
6-
voice_detecter = Voice_Detect_Helper()
7-
recorder()
8-
voice_detecter()
9-
# you can get result from voice_detecter.result
16+
# method 1 using pyaudio to record
17+
# using_audio_to_talk()
18+
19+
# method 2 using speech_recognition to record
20+
using_detecter_to_talk()

script/recording_voice.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import wave
33

44
class Recording_Helper:
5-
def __init__(self, chunk = 1024, sample_format = pyaudio.paInt16, channels = 2, fs = 44100, seconds = 5, filename = "test.wav"):
5+
def __init__(self, chunk = 1024, sample_format = pyaudio.paInt16, channels = 2, fs = 44100, seconds = 5, file_name = "test.wav"):
66
# sample chunk size
77
self.chunk = chunk
88
# sample format: paFloat32, paInt32, paInt24, paInt16, paInt8, paUInt8, paCustomFormat
@@ -14,7 +14,7 @@ def __init__(self, chunk = 1024, sample_format = pyaudio.paInt16, channels = 2,
1414
# recording seconds
1515
self.seconds = seconds
1616
# recording file name
17-
self.filename = filename
17+
self.file_name = file_name
1818

1919
self.p = pyaudio.PyAudio()
2020
# init pyaudio object
@@ -42,7 +42,7 @@ def recording_voice(self):
4242
print('stop recording...')
4343

4444
# open voice file
45-
wf = wave.open(self.filename, 'wb')
45+
wf = wave.open(self.file_name, 'wb')
4646
# set channel
4747
wf.setnchannels(self.channels)
4848
# set format

script/voice_detect.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,25 @@ def __init__(self, language='zh-TW', file_path="test.wav"):
77
self.r = sr.Recognizer()
88
self.result = None
99
def detect_voice(self):
10-
with sr.WavFile(self.file_path) as source:
10+
with sr.AudioFile(self.file_path) as source:
11+
# also you can use with sr.WavFile(self.file_path) as source:
12+
1113
# reduce ambient sound and noise
1214
# self.r.adjust_for_ambient_noise(source)
15+
1316
# load wav
1417
audio = self.r.record(source)
18+
19+
try:
20+
# using google service
21+
self.result = self.r.recognize_google(audio,language=self.language)
22+
print("Transcription: " + self.result)
23+
except:
24+
print("Could not understand audio")
25+
26+
def listener(self):
27+
with sr.Microphone() as source:
28+
audio = self.r.listen(source)
1529
try:
1630
# using google service
1731
self.result = self.r.recognize_google(audio,language=self.language)
@@ -20,4 +34,4 @@ def detect_voice(self):
2034
print("Could not understand audio")
2135

2236
def __call__(self):
23-
self.detect_voice()
37+
self.listener()

0 commit comments

Comments
 (0)