Skip to content

Commit 4e14baa

Browse files
authored
Clean up Transcribe example (awsdocs#7208)
1 parent 8f1411d commit 4e14baa

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

swift/example_code/transcribe-streaming/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift
3333

3434
Code excerpts that show you how to call individual service functions.
3535

36-
- [StartStreamTranscription](transcribe-events/Sources/entry.swift#L145)
36+
- [StartStreamTranscription](transcribe-events/Sources/entry.swift#L132)
3737

3838
### Scenarios
3939

swift/example_code/transcribe-streaming/transcribe-events/Sources/entry.swift

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,13 @@ struct ExampleCommand: ParsableCommand {
126126
/// Run the transcription process.
127127
///
128128
/// - Throws: An error from `TranscribeError`.
129-
func transcribe() async throws {
130-
// Convert the value of the `--format` option into the Transcribe
131-
// Streaming `MediaEncoding` type.
132-
133-
let mediaEncoding: TranscribeStreamingClientTypes.MediaEncoding
134-
switch format {
135-
case .flac:
136-
mediaEncoding = .flac
137-
case .ogg:
138-
mediaEncoding = .oggOpus
139-
case .pcm:
140-
mediaEncoding = .pcm
141-
}
142-
129+
func transcribe(encoding: TranscribeStreamingClientTypes.MediaEncoding) async throws {
143130
// Create the Transcribe Streaming client.
144131

145132
// snippet-start:[swift.transcribe-streaming.StartStreamTranscription]
146133
let client = TranscribeStreamingClient(
147134
config: try await TranscribeStreamingClient.TranscribeStreamingClientConfiguration(
148-
region: region
135+
region: region
149136
)
150137
)
151138

@@ -155,7 +142,7 @@ struct ExampleCommand: ParsableCommand {
155142
input: StartStreamTranscriptionInput(
156143
audioStream: try await createAudioStream(),
157144
languageCode: TranscribeStreamingClientTypes.LanguageCode(rawValue: lang),
158-
mediaEncoding: mediaEncoding,
145+
mediaEncoding: encoding,
159146
mediaSampleRateHertz: sampleRate
160147
)
161148
)
@@ -200,6 +187,26 @@ struct ExampleCommand: ParsableCommand {
200187
}
201188
}
202189
// snippet-end:[swift.transcribe-streaming]
190+
191+
/// Convert the value of the `--format` command line option into the
192+
/// corresponding Transcribe Streaming `MediaEncoding` type.
193+
///
194+
/// - Returns: The `MediaEncoding` equivalent of the format specified on
195+
/// the command line.
196+
func getMediaEncoding() -> TranscribeStreamingClientTypes.MediaEncoding {
197+
let mediaEncoding: TranscribeStreamingClientTypes.MediaEncoding
198+
199+
switch format {
200+
case .flac:
201+
mediaEncoding = .flac
202+
case .ogg:
203+
mediaEncoding = .oggOpus
204+
case .pcm:
205+
mediaEncoding = .pcm
206+
}
207+
208+
return mediaEncoding
209+
}
203210
}
204211

205212
// -MARK: - Entry point
@@ -212,7 +219,7 @@ struct Main {
212219

213220
do {
214221
let command = try ExampleCommand.parse(args)
215-
try await command.transcribe()
222+
try await command.transcribe(encoding: command.getMediaEncoding())
216223
} catch let error as TranscribeError {
217224
print("ERROR: \(error.errorDescription ?? "Unknown error")")
218225
} catch {

0 commit comments

Comments
 (0)