Page 322 - 3-2
P. 322

# The Speech API has a streaming limit of 60 seconds of audio*, so keep the
            # connection alive for that long, plus some more to give the API time to figure
            # out the transcription.
            # * https://g.co/cloud/speech/limits#content
            DEADLINE_SECS = 60 * 3 +5
            SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'
            API_Key = '46048281b7a4cf0565742f9f290118304b460181'
            #API_Key = '8d8e15db8366c421910f83f4c412ef79b0114f98'
            #owm = OWM(API_Key)


            def make_channel(host, port):
                """Creates a secure channel with auth credentials from the environment."""
                # Grab application default credentials from the environment
                credentials, _ = google.auth.default(scopes=[SPEECH_SCOPE])


                # Create a secure channel using the credentials.
                http_request = google.auth.transport.requests.Request()
                target = '{}:{}'.format(host, port)


                return google.auth.transport.grpc.secure_authorized_channel(
                    credentials, http_request, target)




            def _audio_data_generator(buff):
                """A generator that yields all available data in the given buffer.
                Args:
                    buff - a Queue object, where each element is a chunk of data.
                Yields:
                    A chunk of data that is the aggregate of all chunks of data in `buff`.
                    The function will block until at least one data chunk is available.
                """
                stop = False
                while not stop:
                    # Use a blocking get() to ensure there's at least one chunk of data.
                    data = [buff.get()]


                    # Now consume whatever other data's still buffered.
                    while True:
                        try:
                            data.append(buff.get(block=False))
                        except queue.Empty:
                            break


                    # `None` in the buffer signals that the audio stream is closed. Yield


                                                         - 322 -
   317   318   319   320   321   322   323   324   325   326   327