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.
6-
74from __future__ import print_function
85import pyaudio
9- import tempfile
106from watson_developer_cloud import SpeechToTextV1
11- from watson_developer_cloud .websocket import RecognizeCallback
7+ from watson_developer_cloud .websocket import RecognizeCallback , AudioSource
8+ from threading import Thread
9+
10+ try :
11+ from Queue import Queue , Full
12+ except ImportError :
13+ from queue import Queue , Full
1214
15+ # initialize speech to text service
1316speech_to_text = SpeechToTextV1 (
1417 username = 'YOUR SERVICE USERNAME' ,
1518 password = 'YOUR SERVICE PASSWORD' ,
1619 url = 'https://stream.watsonplatform.net/speech-to-text/api' )
1720
18-
19- # Example using websockets
21+ # define callback for the service
2022class MyRecognizeCallback (RecognizeCallback ):
2123 def __init__ (self ):
2224 RecognizeCallback .__init__ (self )
@@ -39,35 +41,65 @@ def on_listening(self):
3941 def on_hypothesis (self , hypothesis ):
4042 print (hypothesis )
4143
44+ def on_data (self , data ):
45+ print (data )
4246
43- mycallback = MyRecognizeCallback ()
44- tmp = tempfile . NamedTemporaryFile ( )
47+ def on_close ( self ):
48+ print ( "Connection closed" )
4549
50+ # Variables for recording the speech
4651FORMAT = pyaudio .paInt16
4752CHANNELS = 1
4853RATE = 44100
4954CHUNK = 1024
50- RECORD_SECONDS = 5
51-
55+ # Note: It will discard if the websocket client can't consumme fast enough
56+ BUF_MAX_SIZE = CHUNK * 10
57+
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 ):
63+ try :
64+ q .put (in_data )
65+ except Full :
66+ pass # discard
67+ return (None , pyaudio .paContinue )
68+
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+
77+ # instantiate pyaudio
5278audio = pyaudio .PyAudio ()
79+
80+ # open stream using callback
5381stream = audio .open (
5482 format = FORMAT ,
5583 channels = CHANNELS ,
5684 rate = RATE ,
5785 input = True ,
58- frames_per_buffer = CHUNK )
59-
60- print ('recording....' )
61- with open (tmp .name , 'w' ) as f :
62- for i in range (0 , int (RATE / CHUNK * RECORD_SECONDS )):
63- data = stream .read (CHUNK )
64- f .write (data )
65-
66- stream .stop_stream ()
67- stream .close ()
68- audio .terminate ()
69- print ('Done recording...' )
70-
71- with open (tmp .name ) as f :
72- speech_to_text .recognize_with_websocket (
73- audio = f , recognize_callback = mycallback )
86+ frames_per_buffer = CHUNK ,
87+ stream_callback = callback ,
88+ start = False
89+ )
90+
91+ print ("Enter CTRL+C to end recording..." )
92+ stream .start_stream ()
93+
94+ try :
95+ recognize_thread = Thread (target = recognize_using_weboscket , args = ())
96+ recognize_thread .start ()
97+
98+ while True :
99+ pass
100+ except KeyboardInterrupt :
101+ # stop recording
102+ audio_source .completed_recording ()
103+ stream .stop_stream ()
104+ stream .close ()
105+ audio .terminate ()
0 commit comments