Search in sources :

Example 1 with StartStreamTranscriptionRequest

use of software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest in project aws-doc-sdk-examples by awsdocs.

the class BidirectionalStreaming method convertAudio.

// snippet-start:[transcribe.java2.bidir_streaming.main]
public static void convertAudio(TranscribeStreamingAsyncClient client) throws Exception {
    try {
        StartStreamTranscriptionRequest request = StartStreamTranscriptionRequest.builder().mediaEncoding(MediaEncoding.PCM).languageCode(LanguageCode.EN_US).mediaSampleRateHertz(16_000).build();
        TargetDataLine mic = Microphone.get();
        mic.start();
        AudioStreamPublisher publisher = new AudioStreamPublisher(new AudioInputStream(mic));
        StartStreamTranscriptionResponseHandler response = StartStreamTranscriptionResponseHandler.builder().subscriber(e -> {
            TranscriptEvent event = (TranscriptEvent) e;
            event.transcript().results().forEach(r -> r.alternatives().forEach(a -> System.out.println(a.transcript())));
        }).build();
        // Keeps Streaming until you end the Java program
        client.startStreamTranscription(request, publisher, response);
    } catch (TranscribeStreamingException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) TargetDataLine(javax.sound.sampled.TargetDataLine) LanguageCode(software.amazon.awssdk.services.transcribestreaming.model.LanguageCode) MediaEncoding(software.amazon.awssdk.services.transcribestreaming.model.MediaEncoding) TranscriptEvent(software.amazon.awssdk.services.transcribestreaming.model.TranscriptEvent) AudioFormat(javax.sound.sampled.AudioFormat) DataLine(javax.sound.sampled.DataLine) TranscribeStreamingAsyncClient(software.amazon.awssdk.services.transcribestreaming.TranscribeStreamingAsyncClient) StartStreamTranscriptionRequest(software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest) AudioInputStream(javax.sound.sampled.AudioInputStream) Region(software.amazon.awssdk.regions.Region) TranscribeStreamingException(software.amazon.awssdk.services.transcribestreaming.model.TranscribeStreamingException) AudioSystem(javax.sound.sampled.AudioSystem) StartStreamTranscriptionResponseHandler(software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionResponseHandler) TranscriptEvent(software.amazon.awssdk.services.transcribestreaming.model.TranscriptEvent) StartStreamTranscriptionRequest(software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest) StartStreamTranscriptionResponseHandler(software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionResponseHandler) TranscribeStreamingException(software.amazon.awssdk.services.transcribestreaming.model.TranscribeStreamingException) TargetDataLine(javax.sound.sampled.TargetDataLine)

Example 2 with StartStreamTranscriptionRequest

use of software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest in project aws-doc-sdk-examples by awsdocs.

the class StreamingRetryApp method main.

public static void main(String[] args) throws URISyntaxException, ExecutionException, InterruptedException, LineUnavailableException, FileNotFoundException {
    /**
     * Create Amazon Transcribe streaming retry client.
     */
    TranscribeStreamingRetryClient client = new TranscribeStreamingRetryClient(EnvironmentVariableCredentialsProvider.create(), endpoint, region);
    StartStreamTranscriptionRequest request = StartStreamTranscriptionRequest.builder().languageCode(language).mediaEncoding(encoding).mediaSampleRateHertz(sample_rate).build();
    /**
     * Start real-time speech recognition. The Amazon Transcribe streaming java client uses the Reactive-streams
     * interface. For reference on Reactive-streams:
     *     https://github.com/reactive-streams/reactive-streams-jvm
     */
    CompletableFuture<Void> result = client.startStreamTranscription(/**
     * Request parameters. Refer to API documentation for details.
     */
    request, /**
     * Provide an input audio stream.
     * For input from a microphone, use getStreamFromMic().
     * For input from a file, use getStreamFromFile().
     */
    new AudioStreamPublisher(new FileInputStream(new File("FileName"))), /**
     * Object that defines the behavior on how to handle the stream
     */
    new StreamTranscriptionBehaviorImpl());
    /**
     * Synchronous wait for stream to close, and close client connection
     */
    result.get();
    client.close();
}
Also used : StartStreamTranscriptionRequest(software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

StartStreamTranscriptionRequest (software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 AudioFormat (javax.sound.sampled.AudioFormat)1 AudioInputStream (javax.sound.sampled.AudioInputStream)1 AudioSystem (javax.sound.sampled.AudioSystem)1 DataLine (javax.sound.sampled.DataLine)1 TargetDataLine (javax.sound.sampled.TargetDataLine)1 Region (software.amazon.awssdk.regions.Region)1 TranscribeStreamingAsyncClient (software.amazon.awssdk.services.transcribestreaming.TranscribeStreamingAsyncClient)1 LanguageCode (software.amazon.awssdk.services.transcribestreaming.model.LanguageCode)1 MediaEncoding (software.amazon.awssdk.services.transcribestreaming.model.MediaEncoding)1 StartStreamTranscriptionResponseHandler (software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionResponseHandler)1 TranscribeStreamingException (software.amazon.awssdk.services.transcribestreaming.model.TranscribeStreamingException)1 TranscriptEvent (software.amazon.awssdk.services.transcribestreaming.model.TranscriptEvent)1