· 6 years ago · Apr 23, 2020, 01:42 PM
1package speechtotext;
2
3import com.microsoft.cognitiveservices.speech.*;
4import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
5
6import java.io.*;
7import java.util.concurrent.Future;
8import java.util.Scanner;
9
10import static com.microsoft.cognitiveservices.speech.ResultReason.*;
11
12/**
13 * Quickstart: recognize speech using the Speech SDK for Java.
14 */
15public class Main {
16
17 /**
18 * @param args Arguments are ignored in this sample.
19 */
20 public static void main(String[] args) {
21 //Essenzialmente due for nestati, uno per le directory e uno per i file wav delle directory.
22
23 System.out.print("Premi \"l\" per elencare, premi \"e\" per eseguire\n");
24 Scanner scan = new Scanner(System.in);
25 char option = scan.next().charAt(0);
26
27 //Array abs path dirs
28 //String[] dirs = {"/samba/Lezioni/Algebra","/samba/Lezioni/Architettura",
29 // "/samba/Lezioni/Programmazione","/samba/Lezioni/Organizzazione"};
30 String[] dirs = {"/samba/Lezioni/Organizzazione"};
31
32 for (String folderPath : dirs) {
33 File directoryCorrente = new File(folderPath);
34 System.out.println("Directory corrente: " + folderPath);
35 for (File fileEntry : directoryCorrente.listFiles((d, name) -> name.endsWith(".wav"))) {
36 if(option == 'l'){
37 System.out.println("File attuale: " + fileEntry.getName());
38 }
39 if(option == 'e'){
40 System.out.println("File attuale: " + fileEntry.getName());
41 //API SPEECH
42 try {
43 // Replace below with your own subscription key
44 String speechSubscriptionKey = "6bad8b28f9324488860f00e107afce88";
45 // Replace with your own subscription key and region identifier from here: https://aka.ms/speech/sdkregion
46 String serviceRegion = "westus";
47 // Replace below with your own filename.
48 String audioFileName = folderPath + "/" + fileEntry.getName();
49
50 int exitCode = 1;
51
52 SpeechConfig config = SpeechConfig.fromSubscription(speechSubscriptionKey, serviceRegion);
53 assert(config != null);
54
55 AudioConfig audioInput = AudioConfig.fromWavFileInput(audioFileName);
56 assert(audioInput != null);
57
58 SourceLanguageConfig sourceLanguageConfig = SourceLanguageConfig.fromLanguage("it-IT");
59
60 SpeechRecognizer reco = new SpeechRecognizer(config, sourceLanguageConfig, audioInput);
61 assert(reco != null);
62
63 //new
64 BufferedWriter writer = new BufferedWriter(new FileWriter(fileEntry.getName() + ".txt"));
65 writer.write(fileEntry.getName() + ".txt");
66 String result = "";
67
68 {
69 // Subscribes to events.
70 reco.recognizing.addEventListener((s, e) -> {
71 System.out.println("RECOGNIZING: Text=" + e.getResult().getText());
72 //System.out.println("RECOGNIZING");
73 });
74
75 reco.recognized.addEventListener((s, e) -> {
76 if (e.getResult().getReason() == RecognizedSpeech) {
77 e.getResult().getText();
78 }
79 else if (e.getResult().getReason() == NoMatch) {
80 System.out.println("NOMATCH: Speech could not be recognized.");
81 }
82 });
83
84 reco.canceled.addEventListener((s, e) -> {
85 System.out.println("CANCELED: Reason=" + e.getReason());
86
87 if (e.getReason() == CancellationReason.Error) {
88 System.out.println("CANCELED: ErrorCode=" + e.getErrorCode());
89 System.out.println("CANCELED: ErrorDetails=" + e.getErrorDetails());
90 System.out.println("CANCELED: Did you update the subscription info?");
91 }
92 });
93
94 reco.sessionStarted.addEventListener((s, e) -> {
95 System.out.println("\n Session started event.");
96 });
97
98 reco.sessionStopped.addEventListener((s, e) -> {
99 System.out.println("\n Session stopped event.");
100 });
101
102 // Starts continuous recognition. Uses stopContinuousRecognitionAsync() to stop recognition.
103 System.out.println("Say something...");
104 reco.startContinuousRecognitionAsync().get();
105
106 System.out.println("Press any key to stop");
107 new Scanner(System.in).nextLine();
108
109 reco.stopContinuousRecognitionAsync().get();
110 }
111 writer.close();
112 System.out.println("Scritto File: " + fileEntry.getName() + ".txt");
113 config.close();
114 audioInput.close();
115 reco.close();
116 } catch (Exception ex) {
117 System.out.println("Unexpected exception: " + ex.getMessage());
118
119 assert(false);
120 System.exit(1);
121 }
122
123
124 }
125 }
126 }
127 }
128}