diff --git a/.doc_gen/metadata/transcribe-streaming_metadata.yaml b/.doc_gen/metadata/transcribe-streaming_metadata.yaml index 752ffafa33e..f01e525cc5c 100644 --- a/.doc_gen/metadata/transcribe-streaming_metadata.yaml +++ b/.doc_gen/metadata/transcribe-streaming_metadata.yaml @@ -19,6 +19,14 @@ transcribe-streaming_StartStreamTranscription: - description: snippet_tags: - transcribe.cpp.stream_transcription_async.code + Swift: + versions: + - sdk_version: 1 + github: swift/example_code/transcribe-streaming + excerpts: + - description: + snippet_tags: + - swift.transcribe-streaming.StartStreamTranscription services: transcribe-streaming: {StartStreamTranscription} transcribe-streaming_StartMedicalStreamTranscription: @@ -34,3 +42,21 @@ transcribe-streaming_StartMedicalStreamTranscription: - transcribe.java-medical-streaming-demo services: transcribe-streaming: {StartMedicalStreamTranscription} +transcribe-streaming_Scenario_StreamEvents: + title: Generate a transcription of an audio stream using an &AWS; SDK + title_abbrev: Transcribe an audio file + synopsis: generate a transcription of a source audio file using &TSC;streaming. + category: Scenarios + languages: + Swift: + versions: + - sdk_version: 1 + github: swift/example_code/transcribe-streaming + sdkguide: + excerpts: + - description: Use &TSC; streaming to transcribe the spoken language in an audio file. + snippet_tags: + - swift.transcribe-streaming.all + - swift.transcribe-streaming.transcribeerror + services: + transcribe-streaming: {StartStreamTranscription} diff --git a/swift/example_code/transcribe-streaming/README.md b/swift/example_code/transcribe-streaming/README.md new file mode 100644 index 00000000000..5a60498c198 --- /dev/null +++ b/swift/example_code/transcribe-streaming/README.md @@ -0,0 +1,108 @@ +# Amazon Transcribe Streaming code examples for the SDK for Swift + +## Overview + +Shows how to use the AWS SDK for Swift to work with Amazon Transcribe Streaming. + + + + +_Amazon Transcribe Streaming produces real-time transcriptions for your media content._ + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder. + + + + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +- [StartStreamTranscription](transcribe-events/Sources/entry.swift#L124) + +### Scenarios + +Code examples that show you how to accomplish a specific task by calling multiple +functions within the same service. + +- [Transcribe an audio file](transcribe-events/Sources/entry.swift) + + + + + +## Run the examples + +### Instructions + +To build any of these examples from a terminal window, navigate into its +directory, then use the following command: + +``` +$ swift build +``` + +To build one of these examples in Xcode, navigate to the example's directory +(such as the `ListUsers` directory, to build that example). Then type `xed.` +to open the example directory in Xcode. You can then use standard Xcode build +and run commands. + + + + + + +#### Transcribe an audio file + +This example shows you how to generate a transcription of a source audio file using Amazon Transcribestreaming. + + + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `swift` folder. + + + + + + +## Additional resources + +- [Amazon Transcribe Streaming Developer Guide](https://docs.aws.amazon.com/transcribe/latest/dg/streaming.html) +- [Amazon Transcribe Streaming API Reference](https://docs.aws.amazon.com/transcribe/latest/APIReference/Welcome.html) +- [SDK for Swift Amazon Transcribe Streaming reference](https://sdk.amazonaws.com/swift/api/awstranscribestreaming/latest/documentation/awstranscribestreaming) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 diff --git a/swift/example_code/transcribe/transcribe-events/Package.swift b/swift/example_code/transcribe-streaming/transcribe-events/Package.swift similarity index 100% rename from swift/example_code/transcribe/transcribe-events/Package.swift rename to swift/example_code/transcribe-streaming/transcribe-events/Package.swift diff --git a/swift/example_code/transcribe/transcribe-events/Sources/TranscribeError.swift b/swift/example_code/transcribe-streaming/transcribe-events/Sources/TranscribeError.swift similarity index 83% rename from swift/example_code/transcribe/transcribe-events/Sources/TranscribeError.swift rename to swift/example_code/transcribe-streaming/transcribe-events/Sources/TranscribeError.swift index dc853fe3b8c..c11ad622e45 100644 --- a/swift/example_code/transcribe/transcribe-events/Sources/TranscribeError.swift +++ b/swift/example_code/transcribe-streaming/transcribe-events/Sources/TranscribeError.swift @@ -1,6 +1,7 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 +// snippet-start:[swift.transcribe-streaming.transcribeerror] /// Errors thrown by the example's functions. enum TranscribeError: Error { /// No transcription stream available. @@ -17,3 +18,4 @@ enum TranscribeError: Error { } } } +// snippet-end:[swift.transcribe-streaming.transcribeerror] diff --git a/swift/example_code/transcribe/transcribe-events/Sources/entry.swift b/swift/example_code/transcribe-streaming/transcribe-events/Sources/entry.swift similarity index 64% rename from swift/example_code/transcribe/transcribe-events/Sources/entry.swift rename to swift/example_code/transcribe-streaming/transcribe-events/Sources/entry.swift index 0eddcbaca27..0efbb5437da 100644 --- a/swift/example_code/transcribe/transcribe-events/Sources/entry.swift +++ b/swift/example_code/transcribe-streaming/transcribe-events/Sources/entry.swift @@ -1,33 +1,35 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // -/// An example that demonstrates how to watch an transcribe event stream. -// snippet-start:[swift.s3.binary-streaming.imports] +// snippet-start:[swift.transcribe-streaming.all] +/// An example that demonstrates how to watch an transcribe event stream to +/// transcribe audio from a file to the console. + +// snippet-start:[swift.transcribe-streaming.imports] import ArgumentParser import AWSClientRuntime import AWSTranscribeStreaming import Foundation -import Smithy -//import SmithyHTTPAPI -import SmithyStreams - -// snippet-end:[swift.s3.binary-streaming.imports] +// snippet-end:[swift.transcribe-streaming.imports] + +// snippet-start:[swift.transcribe-streaming.transcribeformat-enum] +/// Identify one of the media file formats supported by Amazon Transcribe. +enum TranscribeFormat: String, ExpressibleByArgument { + case ogg = "ogg" + case pcm = "pcm" + case flac = "flac" +} +// snippet-end:[swift.transcribe-streaming.transcribeformat-enum] // -MARK: - Async command line tool struct ExampleCommand: ParsableCommand { - enum MediaFormat: String, ExpressibleByArgument { - case ogg = "ogg" - case pcm = "pcm" - case flac = "flac" - } - // -MARK: Command arguments @Option(help: "Language code to transcribe into") var lang: String = "en-US" @Option(help: "Format of the source audio file") - var format: MediaFormat + var format: TranscribeFormat @Option(help: "Sample rate of the source audio file in Hertz") var sampleRate: Int = 16000 @Option(help: "Path of the source audio file") @@ -44,29 +46,19 @@ struct ExampleCommand: ParsableCommand { """ ) - func transcribe() async throws { - let config = try await TranscribeStreamingClient.TranscribeStreamingClientConfiguration( - region: region - ) - let client = TranscribeStreamingClient(config: config) - - let mediaEncoding: TranscribeStreamingClientTypes.MediaEncoding - switch format { - case .flac: - mediaEncoding = .flac - case .ogg: - mediaEncoding = .oggOpus - case .pcm: - mediaEncoding = .pcm - } - - // Create the source audio stream. + // snippet-start:[swift.transcribe-streaming.createaudiostream] + /// Create and return an Amazon Transcribe audio stream from the file + /// specified in the arguments. + /// + /// - Throws: Errors from `TranscribeError`. + /// + /// - Returns: `AsyncThrowingStream` + func createAudioStream() async throws + -> AsyncThrowingStream { let fileURL: URL = URL(fileURLWithPath: path) let audioData = try Data(contentsOf: fileURL) - print("Processing file...") - // Properties defining the size of audio chunks and the total size of // the audio file in bytes. @@ -83,6 +75,10 @@ struct ExampleCommand: ParsableCommand { var currentStart = 0 var currentEnd = min(chunkSize, audioDataSize - currentStart) + // Generate and send chunks of audio data as `audioevent` + // events until the entire file has been sent. Each event is + // yielded to the SDK after being created. + while currentStart < audioDataSize { let dataChunk = audioData[currentStart ..< currentEnd] @@ -95,24 +91,58 @@ struct ExampleCommand: ParsableCommand { currentEnd = min(currentStart + chunkSize, audioDataSize) } + // Let the SDK's continuation block know the stream is over. + continuation.finish() } } + return audioStream + } + // snippet-end:[swift.transcribe-streaming.createaudiostream] + + // snippet-start:[swift.transcribe-streaming] + /// Run the transcription process. + /// + /// - Throws: An error from `TranscribeError`. + func transcribe() async throws { + // Convert the value of the `--format` option into the Transcribe + // Streaming `MediaEncoding` type. + + let mediaEncoding: TranscribeStreamingClientTypes.MediaEncoding + switch format { + case .flac: + mediaEncoding = .flac + case .ogg: + mediaEncoding = .oggOpus + case .pcm: + mediaEncoding = .pcm + } + + // Create the Transcribe Streaming client. + + // snippet-start:[swift.transcribe-streaming.StartStreamTranscription] + let client = TranscribeStreamingClient( + config: try await TranscribeStreamingClient.TranscribeStreamingClientConfiguration( + region: region + ) + ) + // Start the transcription running on the audio stream. let output = try await client.startStreamTranscription( input: StartStreamTranscriptionInput( - audioStream: audioStream, + audioStream: try await createAudioStream(), languageCode: TranscribeStreamingClientTypes.LanguageCode(rawValue: lang), mediaEncoding: mediaEncoding, mediaSampleRateHertz: sampleRate ) ) + // snippet-end:[swift.transcribe-streaming.StartStreamTranscription] - // Iterate over the events in the transcript result stream. Each - // `transcriptevent` contains a list of result fragments which need - // to be concatenated together to build the final transcript. + // Iterate over the events in the returned transcript result stream. + // Each `transcriptevent` contains a list of result fragments which + // need to be concatenated together to build the final transcript. for try await event in output.transcriptResultStream! { switch event { case .transcriptevent(let event): @@ -135,6 +165,7 @@ struct ExampleCommand: ParsableCommand { } } } + // snippet-end:[swift.transcribe-streaming] } // -MARK: - Entry point @@ -155,3 +186,4 @@ struct Main { } } } +// snippet-end:[swift.transcribe-streaming.all]