in speech/microphone/transcribe_streaming_infinite_v2.py [0:0]
def main(project_id: str) -> None:
"""start bidirectional streaming from microphone input to speech API"""
client = SpeechClient()
recognition_config = cloud_speech_types.RecognitionConfig(
explicit_decoding_config=cloud_speech_types.ExplicitDecodingConfig(
sample_rate_hertz=SAMPLE_RATE,
encoding=cloud_speech_types.ExplicitDecodingConfig.AudioEncoding.LINEAR16,
audio_channel_count=1
),
language_codes=["en-US"],
model="long",
)
streaming_config = cloud_speech_types.StreamingRecognitionConfig(
config=recognition_config,
streaming_features=cloud_speech_types.StreamingRecognitionFeatures(
interim_results=True
)
)
config_request = cloud_speech_types.StreamingRecognizeRequest(
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
streaming_config=streaming_config,
)
def requests(config: cloud_speech_types.RecognitionConfig, audio: list) -> list:
"""Helper function to generate the requests list for the streaming API.
Args:
config: The speech recognition configuration.
audio: The audio data.
Returns:
The list of requests for the streaming API.
"""
yield config
for chunk in audio:
yield cloud_speech_types.StreamingRecognizeRequest(audio=chunk)
mic_manager = ResumableMicrophoneStream(SAMPLE_RATE, CHUNK_SIZE)
print(mic_manager.chunk_size)
sys.stdout.write(YELLOW)
sys.stdout.write('\nListening, say "Quit" or "Exit" to stop.\n\n')
sys.stdout.write("End (ms) Transcript Results/Status\n")
sys.stdout.write("=====================================================\n")
with mic_manager as stream:
while not stream.closed:
sys.stdout.write(YELLOW)
sys.stdout.write(
"\n" + str(STREAMING_LIMIT * stream.restart_counter) + ": NEW REQUEST\n"
)
stream.audio_input = []
audio_generator = stream.generator()
# Transcribes the audio into text
responses_iterator = client.streaming_recognize(
requests=requests(config_request, audio_generator))
listen_print_loop(responses_iterator, stream)
if stream.result_end_time > 0:
stream.final_request_end_time = stream.is_final_end_time
stream.result_end_time = 0
stream.last_audio_input = []
stream.last_audio_input = stream.audio_input
stream.audio_input = []
stream.restart_counter = stream.restart_counter + 1
if not stream.last_transcript_was_final:
sys.stdout.write("\n")
stream.new_stream = True