Skip to content

Commit d779454

Browse files
committed
test(microphone): Make microphone example more readable
1 parent a06b920 commit d779454

2 files changed

Lines changed: 42 additions & 19 deletions

File tree

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ install:
2020
- "python -m pip install --upgrade pip"
2121

2222
# Install the build dependencies of the project.
23-
- "pip install -r dev-requirements.txt"
23+
- "pip install -r requirements-dev.txt"
2424

2525
build: off
2626

examples/microphone-speech-to-text.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# You need to install pyaudio to run this example
22
# pip install pyaudio
33

4+
# When using a microphone, the AudioSource `input` parameter would be
5+
# initialised as a queue. The pyaudio stream would be continuosly adding
6+
# recordings to the queue, and the websocket client would be sending the
7+
# recordings to the speech to text service
8+
49
from __future__ import print_function
510
import pyaudio
611
from watson_developer_cloud import SpeechToTextV1
@@ -12,13 +17,30 @@
1217
except ImportError:
1318
from queue import Queue, Full
1419

20+
###############################################
21+
#### Initalize queue to store the recordings ##
22+
###############################################
23+
CHUNK = 1024
24+
# Note: It will discard if the websocket client can't consumme fast enough
25+
# So, increase the max size as per your choice
26+
BUF_MAX_SIZE = CHUNK * 10
27+
# Buffer to store audio
28+
q = Queue(maxsize=int(round(BUF_MAX_SIZE / CHUNK)))
29+
30+
# Create an instance of AudioSource
31+
audio_source = AudioSource(q, True, True)
32+
33+
###############################################
34+
#### Prepare Speech to Text Service ########
35+
###############################################
36+
1537
# initialize speech to text service
1638
speech_to_text = SpeechToTextV1(
1739
username='YOUR SERVICE USERNAME',
1840
password='YOUR SERVICE PASSWORD',
1941
url='https://stream.watsonplatform.net/speech-to-text/api')
2042

21-
# define callback for the service
43+
# define callback for the speech to text service
2244
class MyRecognizeCallback(RecognizeCallback):
2345
def __init__(self):
2446
RecognizeCallback.__init__(self)
@@ -47,33 +69,30 @@ def on_data(self, data):
4769
def on_close(self):
4870
print("Connection closed")
4971

72+
# this function will initiate the recognize service and pass in the AudioSource
73+
def recognize_using_weboscket(*args):
74+
mycallback = MyRecognizeCallback()
75+
speech_to_text.recognize_using_websocket(audio=audio_source,
76+
content_type='audio/l16; rate=44100',
77+
recognize_callback=mycallback)
78+
79+
###############################################
80+
#### Prepare the for recording using Pyaudio ##
81+
###############################################
82+
5083
# Variables for recording the speech
5184
FORMAT = pyaudio.paInt16
5285
CHANNELS = 1
5386
RATE = 44100
54-
CHUNK = 1024
55-
# Note: It will discard if the websocket client can't consumme fast enough
56-
BUF_MAX_SIZE = CHUNK * 10
5787

58-
# Buffer to store audio
59-
q = Queue(maxsize=int(round(BUF_MAX_SIZE / CHUNK)))
60-
61-
# define callback to store the recording in queue
62-
def callback(in_data, frame_count, time_info, status):
88+
# define callback for pyaudio to store the recording in queue
89+
def pyaudio_callback(in_data, frame_count, time_info, status):
6390
try:
6491
q.put(in_data)
6592
except Full:
6693
pass # discard
6794
return (None, pyaudio.paContinue)
6895

69-
# get ready with service params
70-
audio_source = AudioSource(q, True, True)
71-
def recognize_using_weboscket(*args):
72-
mycallback = MyRecognizeCallback()
73-
speech_to_text.recognize_using_websocket(audio=audio_source,
74-
content_type='audio/l16; rate=44100',
75-
recognize_callback=mycallback)
76-
7796
# instantiate pyaudio
7897
audio = pyaudio.PyAudio()
7998

@@ -84,10 +103,14 @@ def recognize_using_weboscket(*args):
84103
rate=RATE,
85104
input=True,
86105
frames_per_buffer=CHUNK,
87-
stream_callback=callback,
106+
stream_callback=pyaudio_callback,
88107
start=False
89108
)
90109

110+
#########################################################################
111+
#### Start the recording and start service to recognize the stream ######
112+
#########################################################################
113+
91114
print("Enter CTRL+C to end recording...")
92115
stream.start_stream()
93116

0 commit comments

Comments
 (0)