@@ -64,7 +64,7 @@ public static void main(String... args) throws Exception {
64
64
+ "Commands:\n "
65
65
+ "\t syncrecognize | asyncrecognize | streamrecognize | micstreamrecognize \n "
66
66
+ "\t | wordoffsets | auto-punctuation | stream-punctuation \n "
67
- + "\t | enhanced-model | model-selection\n "
67
+ + "\t | enhanced-model | model-selection | multi-channel \n "
68
68
+ "Path:\n \t A file path (ex: ./resources/audio.raw) or a URI "
69
69
+ "for a Cloud Storage resource (gs://...)\n " ,
70
70
Recognize .class .getCanonicalName ());
@@ -112,6 +112,12 @@ public static void main(String... args) throws Exception {
112
112
} else {
113
113
transcribeModelSelection (path );
114
114
}
115
+ } else if (command .equals ("multi-channel" )) {
116
+ if (path .startsWith ("gs://" )) {
117
+ transcribeMultiChannelGcs (path );
118
+ } else {
119
+ transcribeMultiChannel (path );
120
+ }
115
121
}
116
122
}
117
123
@@ -830,4 +836,90 @@ public static void transcribeModelSelectionGcs(String gcsUri) throws Exception {
830
836
}
831
837
}
832
838
// [END speech_transcribe_model_selection_gcs]
839
+
840
+ // [START speech_transcribe_multichannel]
841
+ /**
842
+ * Transcribe a local audio file with multi-channel recognition
843
+ *
844
+ * @param fileName the path to local audio file
845
+ */
846
+ public static void transcribeMultiChannel (String fileName ) throws Exception {
847
+ Path path = Paths .get (fileName );
848
+ byte [] content = Files .readAllBytes (path );
849
+
850
+ try (SpeechClient speechClient = SpeechClient .create ()) {
851
+ // Get the contents of the local audio file
852
+ RecognitionAudio recognitionAudio =
853
+ RecognitionAudio .newBuilder ().setContent (ByteString .copyFrom (content )).build ();
854
+
855
+ // Configure request to enable multiple channels
856
+ RecognitionConfig config =
857
+ RecognitionConfig .newBuilder ()
858
+ .setEncoding (AudioEncoding .LINEAR16 )
859
+ .setLanguageCode ("en-US" )
860
+ .setSampleRateHertz (44100 )
861
+ .setAudioChannelCount (2 )
862
+ .setEnableSeparateRecognitionPerChannel (true )
863
+ .build ();
864
+
865
+ // Perform the transcription request
866
+ RecognizeResponse recognizeResponse = speechClient .recognize (config , recognitionAudio );
867
+
868
+ // Print out the results
869
+ for (SpeechRecognitionResult result : recognizeResponse .getResultsList ()) {
870
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
871
+ // first (most likely) one here.
872
+ SpeechRecognitionAlternative alternative = result .getAlternatives (0 );
873
+ System .out .format ("Transcript : %s\n " , alternative .getTranscript ());
874
+ System .out .printf ("Channel Tag : %s\n " , result .getChannelTag ());
875
+ }
876
+ }
877
+ }
878
+ // [END speech_transcribe_multichannel]
879
+
880
+ // [START speech_transcribe_multichannel_gcs]
881
+ /**
882
+ * Transcribe a remote audio file with multi-channel recognition
883
+ *
884
+ * @param gcsUri the path to the audio file
885
+ */
886
+ public static void transcribeMultiChannelGcs (String gcsUri ) throws Exception {
887
+
888
+ try (SpeechClient speechClient = SpeechClient .create ()) {
889
+
890
+ // Configure request to enable multiple channels
891
+ RecognitionConfig config =
892
+ RecognitionConfig .newBuilder ()
893
+ .setEncoding (AudioEncoding .LINEAR16 )
894
+ .setLanguageCode ("en-US" )
895
+ .setSampleRateHertz (44100 )
896
+ .setAudioChannelCount (2 )
897
+ .setEnableSeparateRecognitionPerChannel (true )
898
+ .build ();
899
+
900
+ // Set the remote path for the audio file
901
+ RecognitionAudio audio = RecognitionAudio .newBuilder ().setUri (gcsUri ).build ();
902
+
903
+ // Use non-blocking call for getting file transcription
904
+ OperationFuture <LongRunningRecognizeResponse , LongRunningRecognizeMetadata > response =
905
+ speechClient .longRunningRecognizeAsync (config , audio );
906
+
907
+ while (!response .isDone ()) {
908
+ System .out .println ("Waiting for response..." );
909
+ Thread .sleep (10000 );
910
+ }
911
+ // Just print the first result here.
912
+ for (SpeechRecognitionResult result : response .get ().getResultsList ()) {
913
+
914
+ // There can be several alternative transcripts for a given chunk of speech. Just use the
915
+ // first (most likely) one here.
916
+ SpeechRecognitionAlternative alternative = result .getAlternativesList ().get (0 );
917
+
918
+ // Print out the result
919
+ System .out .printf ("Transcript : %s\n " , alternative .getTranscript ());
920
+ System .out .printf ("Channel Tag : %s\n " , result .getChannelTag ());
921
+ }
922
+ }
923
+ }
924
+ // [END speech_transcribe_multichannel_gcs]
833
925
}
0 commit comments