Search in sources :

Example 1 with StartStreamTranscriptionResponseHandler

use of software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionResponseHandler 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)

Aggregations

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 StartStreamTranscriptionRequest (software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest)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