@@ -26,6 +26,8 @@ enum TranscribeFormat: String, ExpressibleByArgument {
2626
2727struct ExampleCommand : ParsableCommand {
2828 // -MARK: Command arguments
29+ @Flag ( help: " Show partial results " )
30+ var showPartial = false
2931 @Option ( help: " Language code to transcribe into " )
3032 var lang : String = " en-US "
3133 @Option ( help: " Format of the source audio file " )
@@ -60,9 +62,11 @@ struct ExampleCommand: ParsableCommand {
6062 let audioData = try Data ( contentsOf: fileURL)
6163
6264 // Properties defining the size of audio chunks and the total size of
63- // the audio file in bytes.
65+ // the audio file in bytes. You should try to send chunks that last on
66+ // average 125 milliseconds.
6467
65- let chunkSize = 16384
68+ let chunkSizeInMilliseconds = 125.0
69+ let chunkSize = Int ( chunkSizeInMilliseconds / 1000.0 * Double( sampleRate) * 2.0 )
6670 let audioDataSize = audioData. count
6771
6872 // Create an audio stream from the source data. The stream's job is
@@ -85,7 +89,24 @@ struct ExampleCommand: ParsableCommand {
8589 let audioEvent = TranscribeStreamingClientTypes . AudioStream. audioevent (
8690 . init( audioChunk: dataChunk)
8791 )
88- continuation. yield ( audioEvent)
92+ let yieldResult = continuation. yield ( audioEvent)
93+ switch yieldResult {
94+ case . enqueued( _) :
95+ // The chunk was successfully enqueued into the
96+ // stream. The `remaining` parameter estimates how
97+ // much room is left in the queue, but is ignored here.
98+ break
99+ case . dropped( _) :
100+ // The chunk was dropped because the queue buffer
101+ // is full. This will cause transcription errors.
102+ print ( " Warning: Dropped audio! The transcription will be incomplete. " )
103+ case . terminated:
104+ print ( " Audio stream terminated. " )
105+ continuation. finish ( )
106+ return
107+ default :
108+ print ( " Warning: Unrecognized response during audio streaming. " )
109+ }
89110
90111 currentStart = currentEnd
91112 currentEnd = min ( currentStart + chunkSize, audioDataSize)
@@ -151,12 +172,25 @@ struct ExampleCommand: ParsableCommand {
151172 continue
152173 }
153174
175+ // If showing partial results is enabled and the result is
176+ // partial, show it. Partial results may be incomplete, and
177+ // may be inaccurate, with upcoming audio making the
178+ // transcription complete or by giving more context to make
179+ // transcription make more sense.
180+
181+ if ( result. isPartial && showPartial) {
182+ print ( " [Partial] \( transcript) " )
183+ }
184+
154185 // When the complete fragment of transcribed text is ready,
155186 // print it. This could just as easily be used to draw the
156187 // text as a subtitle over a playing video, though timing
157188 // would need to be managed.
158189
159190 if !result. isPartial {
191+ if ( showPartial) {
192+ print ( " [Final ] " , terminator: " " )
193+ }
160194 print ( transcript)
161195 }
162196 }
0 commit comments