-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcobra_demo_mic.py
157 lines (125 loc) · 5.17 KB
/
cobra_demo_mic.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#
# Copyright 2021-2023 Picovoice Inc.
#
# You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
# file accompanying this source.
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
import argparse
import sys
import struct
import wave
from threading import Thread
import pvcobra
from pvrecorder import PvRecorder
class CobraDemo(Thread):
"""
Microphone Demo for Cobra voice activity detection engine.
"""
def __init__(
self,
library_path,
access_key,
output_path=None,
input_device_index=None):
"""
Constructor.
:param library_path: Absolute path to Cobra's dynamic library.
:param access_key AccessKey obtained from Picovoice Console.
:param output_path: If provided recorded audio will be stored in this location at the end of the run.
:param input_device_index: Optional argument. If provided, audio is recorded from this input device. Otherwise,
the default audio input device is used.
"""
super(CobraDemo, self).__init__()
self._library_path = library_path
self._access_key = access_key
self._input_device_index = input_device_index
self._output_path = output_path
def run(self):
"""
Creates an input audio stream, instantiates an instance of Cobra object, and monitors the audio stream for
voice activities.
"""
recorder = None
wav_file = None
try:
cobra = pvcobra.create(access_key=self._access_key, library_path=self._library_path)
except pvcobra.CobraInvalidArgumentError as e:
print(e)
raise e
except pvcobra.CobraActivationError as e:
print("AccessKey activation error")
raise e
except pvcobra.CobraActivationLimitError as e:
print("AccessKey '%s' has reached it's temporary device limit" % self._access_key)
raise e
except pvcobra.CobraActivationRefusedError as e:
print("AccessKey '%s' refused" % self._access_key)
raise e
except pvcobra.CobraActivationThrottledError as e:
print("AccessKey '%s' has been throttled" % self._access_key)
raise e
except pvcobra.CobraError as e:
print("Failed to initialize Cobra")
raise e
print("Cobra version: %s" % cobra.version)
try:
recorder = PvRecorder(frame_length=512, device_index=self._input_device_index)
recorder.start()
if self._output_path is not None:
wav_file = wave.open(self._output_path, "w")
wav_file.setparams((1, 2, 16000, 512, "NONE", "NONE"))
print("Listening...")
while True:
pcm = recorder.read()
if wav_file is not None:
wav_file.writeframes(struct.pack("h" * len(pcm), *pcm))
voice_probability = cobra.process(pcm)
percentage = voice_probability * 100
bar_length = int((percentage / 10) * 3)
empty_length = 30 - bar_length
sys.stdout.write("\r[%3d]|%s%s|" % (
percentage, '█' * bar_length, ' ' * empty_length))
sys.stdout.flush()
except KeyboardInterrupt:
print('Stopping ...')
finally:
if cobra is not None:
cobra.delete()
if wav_file is not None:
wav_file.close()
if recorder is not None:
recorder.delete()
@classmethod
def show_available_devices(cls):
devices = PvRecorder.get_available_devices()
for i in range(len(devices)):
print('index: %d, device name: %s' % (i, devices[i]))
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--access_key',
help='AccessKey provided by Picovoice Console (https://console.picovoice.ai/)')
parser.add_argument(
'--library_path',
help='Absolute path to dynamic library. Default: using the library provided by `pvcobra`')
parser.add_argument('--audio_device_index', help='Index of input audio device.', type=int, default=-1)
parser.add_argument('--output_path', help='Absolute path to recorded audio for debugging.', default=None)
parser.add_argument('--show_audio_devices', action='store_true')
args = parser.parse_args()
if args.show_audio_devices:
CobraDemo.show_available_devices()
else:
if args.access_key is None:
print("Missing AccessKey (--access_key)")
else:
CobraDemo(
library_path=args.library_path,
access_key=args.access_key,
output_path=args.output_path,
input_device_index=args.audio_device_index).run()
if __name__ == '__main__':
main()