@@ -48,8 +48,10 @@ def __enter__(self):
48
48
self ._audio_interface = pyaudio .PyAudio ()
49
49
self ._audio_stream = self ._audio_interface .open (
50
50
format = pyaudio .paInt16 ,
51
- channels = 1 , rate = self ._rate ,
52
- input = True , frames_per_buffer = self ._chunk ,
51
+ channels = 1 ,
52
+ rate = self ._rate ,
53
+ input = True ,
54
+ frames_per_buffer = self ._chunk ,
53
55
# Run the audio stream asynchronously to fill the buffer object.
54
56
# This is necessary so that the input device's buffer doesn't
55
57
# overflow while the calling thread makes network requests, etc.
@@ -97,7 +99,7 @@ def generator(self):
97
99
except queue .Empty :
98
100
break
99
101
100
- yield b'' .join (data )
102
+ yield b"" .join (data )
101
103
102
104
103
105
def listen_print_loop (responses ):
@@ -106,45 +108,46 @@ def listen_print_loop(responses):
106
108
The responses passed is a generator that will block until a response
107
109
is provided by the server.
108
110
"""
109
- translation = ''
111
+ translation = ""
110
112
for response in responses :
111
113
# Once the transcription settles, the response contains the
112
114
# END_OF_SINGLE_UTTERANCE event.
113
- if (response .speech_event_type ==
114
- SpeechEventType .END_OF_SINGLE_UTTERANCE ):
115
+ if response .speech_event_type == SpeechEventType .END_OF_SINGLE_UTTERANCE :
115
116
116
- print (u' \n Final translation: {0}' .format (translation ))
117
+ print (u" \n Final translation: {0}" .format (translation ))
117
118
return 0
118
119
119
120
result = response .result
120
121
translation = result .text_translation_result .translation
121
122
122
- print (u' \n Partial translation: {0}' .format (translation ))
123
+ print (u" \n Partial translation: {0}" .format (translation ))
123
124
124
125
125
126
def do_translation_loop ():
126
- print (' Begin speaking...' )
127
+ print (" Begin speaking..." )
127
128
128
129
client = media .SpeechTranslationServiceClient ()
129
130
130
131
speech_config = media .TranslateSpeechConfig (
131
- audio_encoding = 'linear16' ,
132
- source_language_code = 'en-US' ,
133
- target_language_code = 'es-ES' )
132
+ audio_encoding = "linear16" ,
133
+ source_language_code = "en-US" ,
134
+ target_language_code = "es-ES" ,
135
+ )
134
136
135
137
config = media .StreamingTranslateSpeechConfig (
136
- audio_config = speech_config , single_utterance = True )
138
+ audio_config = speech_config , single_utterance = True
139
+ )
137
140
138
141
# The first request contains the configuration.
139
142
# Note that audio_content is explicitly set to None.
140
- first_request = media .StreamingTranslateSpeechRequest (
141
- streaming_config = config )
143
+ first_request = media .StreamingTranslateSpeechRequest (streaming_config = config )
142
144
143
145
with MicrophoneStream (RATE , CHUNK ) as stream :
144
146
audio_generator = stream .generator ()
145
- mic_requests = (media .StreamingTranslateSpeechRequest (
146
- audio_content = content )
147
- for content in audio_generator )
147
+ mic_requests = (
148
+ media .StreamingTranslateSpeechRequest (audio_content = content )
149
+ for content in audio_generator
150
+ )
148
151
149
152
requests = itertools .chain (iter ([first_request ]), mic_requests )
150
153
@@ -159,14 +162,14 @@ def do_translation_loop():
159
162
def main ():
160
163
while True :
161
164
print ()
162
- option = input (' Press any key to translate or \' q \ ' to quit: ' )
165
+ option = input (" Press any key to translate or 'q ' to quit: " )
163
166
164
- if option .lower () == 'q' :
167
+ if option .lower () == "q" :
165
168
break
166
169
167
170
do_translation_loop ()
168
171
169
172
170
- if __name__ == ' __main__' :
173
+ if __name__ == " __main__" :
171
174
main ()
172
175
# [END mediatranslation_translate_from_mic]
0 commit comments