11# You need to install pyaudio to run this example
22# pip install pyaudio
33
4- # Note that you need to record just once. You will not be able to send
5- # more audio after the initial recording.
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
68
79from __future__ import print_function
810import pyaudio
9- import tempfile
1011from watson_developer_cloud import SpeechToTextV1
11- from watson_developer_cloud .websocket import RecognizeCallback
12+ from watson_developer_cloud .websocket import RecognizeCallback , AudioSource
13+ from threading import Thread
1214
15+ try :
16+ from Queue import Queue , Full
17+ except ImportError :
18+ from queue import Queue , Full
19+
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+
37+ # initialize speech to text service
1338speech_to_text = SpeechToTextV1 (
1439 username = 'YOUR SERVICE USERNAME' ,
1540 password = 'YOUR SERVICE PASSWORD' ,
1641 url = 'https://stream.watsonplatform.net/speech-to-text/api' )
1742
18-
19- # Example using websockets
43+ # define callback for the speech to text service
2044class MyRecognizeCallback (RecognizeCallback ):
2145 def __init__ (self ):
2246 RecognizeCallback .__init__ (self )
@@ -36,41 +60,69 @@ def on_inactivity_timeout(self, error):
3660 def on_listening (self ):
3761 print ('Service is listening' )
3862
39- def on_transcription_complete (self ):
40- print ('Transcription completed' )
41-
4263 def on_hypothesis (self , hypothesis ):
4364 print (hypothesis )
4465
66+ def on_data (self , data ):
67+ print (data )
68+
69+ def on_close (self ):
70+ print ("Connection closed" )
71+
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 )
4578
46- mycallback = MyRecognizeCallback ()
47- tmp = tempfile .NamedTemporaryFile ()
79+ ###############################################
80+ #### Prepare the for recording using Pyaudio ##
81+ ###############################################
4882
83+ # Variables for recording the speech
4984FORMAT = pyaudio .paInt16
5085CHANNELS = 1
5186RATE = 44100
52- CHUNK = 1024
53- RECORD_SECONDS = 5
5487
88+ # define callback for pyaudio to store the recording in queue
89+ def pyaudio_callback (in_data , frame_count , time_info , status ):
90+ try :
91+ q .put (in_data )
92+ except Full :
93+ pass # discard
94+ return (None , pyaudio .paContinue )
95+
96+ # instantiate pyaudio
5597audio = pyaudio .PyAudio ()
98+
99+ # open stream using callback
56100stream = audio .open (
57101 format = FORMAT ,
58102 channels = CHANNELS ,
59103 rate = RATE ,
60104 input = True ,
61- frames_per_buffer = CHUNK )
62-
63- print ('recording....' )
64- with open (tmp .name , 'w' ) as f :
65- for i in range (0 , int (RATE / CHUNK * RECORD_SECONDS )):
66- data = stream .read (CHUNK )
67- f .write (data )
68-
69- stream .stop_stream ()
70- stream .close ()
71- audio .terminate ()
72- print ('Done recording...' )
73-
74- with open (tmp .name ) as f :
75- speech_to_text .recognize_with_websocket (
76- audio = f , recognize_callback = mycallback )
105+ frames_per_buffer = CHUNK ,
106+ stream_callback = pyaudio_callback ,
107+ start = False
108+ )
109+
110+ #########################################################################
111+ #### Start the recording and start service to recognize the stream ######
112+ #########################################################################
113+
114+ print ("Enter CTRL+C to end recording..." )
115+ stream .start_stream ()
116+
117+ try :
118+ recognize_thread = Thread (target = recognize_using_weboscket , args = ())
119+ recognize_thread .start ()
120+
121+ while True :
122+ pass
123+ except KeyboardInterrupt :
124+ # stop recording
125+ audio_source .completed_recording ()
126+ stream .stop_stream ()
127+ stream .close ()
128+ audio .terminate ()
0 commit comments