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+
49from __future__ import print_function
510import pyaudio
611from watson_developer_cloud import SpeechToTextV1
1217except 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
1638speech_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
2244class 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
5184FORMAT = pyaudio .paInt16
5285CHANNELS = 1
5386RATE = 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
7897audio = 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+
91114print ("Enter CTRL+C to end recording..." )
92115stream .start_stream ()
93116
0 commit comments