forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe_streaming_test.py
More file actions
66 lines (52 loc) · 2.11 KB
/
transcribe_streaming_test.py
File metadata and controls
66 lines (52 loc) · 2.11 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
56
57
58
59
60
61
62
63
64
65
66
# Copyright 2016, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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 re
import threading
import time
import transcribe_streaming
class MockPyAudio(object):
def __init__(self, audio_filename):
self.audio_filename = audio_filename
def __call__(self, *args):
return self
def open(self, stream_callback, *args, **kwargs):
self.closed = threading.Event()
self.stream_thread = threading.Thread(
target=self.stream_audio, args=(
self.audio_filename, stream_callback, self.closed))
self.stream_thread.start()
return self
def close(self):
self.closed.set()
def stop_stream(self):
pass
def terminate(self):
pass
@staticmethod
def stream_audio(audio_filename, callback, closed, num_frames=512):
with open(audio_filename, 'rb') as audio_file:
while not closed.is_set():
# Approximate realtime by sleeping for the appropriate time for
# the requested number of frames
time.sleep(num_frames / float(transcribe_streaming.RATE))
# audio is 16-bit samples, whereas python byte is 8-bit
num_bytes = 2 * num_frames
chunk = audio_file.read(num_bytes) or b'\0' * num_bytes
callback(chunk, None, None, None)
def test_main(resource, monkeypatch, capsys):
monkeypatch.setattr(
transcribe_streaming.pyaudio, 'PyAudio',
MockPyAudio(resource('quit.raw')))
transcribe_streaming.main()
out, err = capsys.readouterr()
assert re.search(r'quit', out, re.DOTALL | re.I)