-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New example for microphone using continuous mode
- Loading branch information
Showing
1 changed file
with
152 additions
and
0 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
...gnizer-examples/src/main/java/br/com/cpqd/asr/recognizer/example/MicContinuousSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/******************************************************************************* | ||
* Copyright 2017 CPqD. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
******************************************************************************/ | ||
package br.com.cpqd.asr.recognizer.example; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URISyntaxException; | ||
|
||
import javax.sound.sampled.AudioFormat; | ||
import javax.sound.sampled.LineUnavailableException; | ||
import javax.websocket.DeploymentException; | ||
|
||
import br.com.cpqd.asr.recognizer.LanguageModelList; | ||
import br.com.cpqd.asr.recognizer.MicAudioSource; | ||
import br.com.cpqd.asr.recognizer.RecognitionException; | ||
import br.com.cpqd.asr.recognizer.RecognitionListener; | ||
import br.com.cpqd.asr.recognizer.SpeechRecognizer; | ||
import br.com.cpqd.asr.recognizer.model.PartialRecognitionResult; | ||
import br.com.cpqd.asr.recognizer.model.RecognitionConfig; | ||
import br.com.cpqd.asr.recognizer.model.RecognitionError; | ||
import br.com.cpqd.asr.recognizer.model.RecognitionResult; | ||
|
||
/** | ||
* Exemplo de uso do cliente Java SE do servidor de reconhecimento de fala, | ||
* utilizando uma entrada de áudio do tipo MicAudioSource, que captura áudio do | ||
* microfone do sistema. A captura é iniciada quando a tecla <ENTER> for | ||
* pressionada. A captura é finalizada ao pressionar novamente o <ENTER>. | ||
* | ||
*/ | ||
public class MicContinuousSample { | ||
|
||
public static void main(String[] args) throws DeploymentException, IOException, URISyntaxException, | ||
RecognitionException, LineUnavailableException { | ||
|
||
if (!(args.length == 2 || args.length == 4)) { | ||
System.out.println("Usage: <ws_url> <lang_uri> [ <user> <password> ]"); | ||
System.out.println(" eg: ws://127.0.0.1:8025/asr-server/asr builtin:grammar/samples/phone"); | ||
System.out.println(" eg2: wss://contact/cpqd/and/request/a/key/ builtin:slm/general myusername mypassword"); | ||
return; | ||
} else { | ||
System.out.println("Running with:"); | ||
System.out.println(" url: " + args[0]); | ||
System.out.println(" lmURI: " + args[1]); | ||
} | ||
|
||
String url = args[0]; | ||
String lmURI = args[1]; | ||
String user = null; | ||
String pwd = null; | ||
try { | ||
user = args[3]; | ||
pwd = args[4]; | ||
} catch (Exception e) { | ||
// credentials not defined | ||
} | ||
|
||
SpeechRecognizer recognizer = getSpeechRecognizer(url, user, pwd); | ||
|
||
// modelo de linguagem de fala livre | ||
LanguageModelList lmList = LanguageModelList.builder().addFromURI(lmURI).build(); | ||
|
||
// leitor da entrada do teclado para controlar o inicio e fim da captura do | ||
// microfone | ||
BufferedReader keyboardReader = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
// realiza um reconhecimento | ||
try { | ||
// cria um objeto MicAudioSource para capturar audio do microfone do sistema | ||
MicAudioSource audioSource = new MicAudioSource(new AudioFormat(8000F, 16, 1, true, false)); | ||
|
||
System.out.println("Press <ENTER> to start, and again to stop recording..."); | ||
keyboardReader.readLine(); | ||
recognizer.recognize(audioSource, lmList); | ||
System.err.println("-- Started --"); | ||
keyboardReader.readLine(); | ||
audioSource.finish(); | ||
System.err.println("-- Stopped --"); | ||
recognizer.waitRecognitionResult(); | ||
} catch (RecognitionException e) { | ||
System.err.println("Generic error: " + e.toString()); | ||
} finally { | ||
// se nao tem mais audio para reconhecer, encerra a sessao | ||
System.out.println("Closing session"); | ||
recognizer.close(); | ||
} | ||
|
||
} | ||
|
||
private static SpeechRecognizer getSpeechRecognizer(String url, String user, String pwd) | ||
throws URISyntaxException, IOException, RecognitionException { | ||
|
||
RecognitionConfig config = RecognitionConfig.builder().maxSentences(2).continuousMode(true).build(); | ||
|
||
// instancia do cliente | ||
SpeechRecognizer recognizer = SpeechRecognizer.builder().serverURL(url) | ||
// informacao opcional para log e estatisticas no servidor | ||
.userAgent("client=JavaSE;app=MicAudioSourceSample") | ||
.credentials(user, pwd) | ||
.recogConfig(config) | ||
.addListener(new RecognitionListener() { | ||
|
||
@Override | ||
public void onSpeechStop(Integer time) { | ||
// System.out.println("-- end of speech --"); | ||
} | ||
|
||
@Override | ||
public void onSpeechStart(Integer time) { | ||
// System.out.println("-- start of speech --"); | ||
} | ||
|
||
@Override | ||
public void onRecognitionResult(RecognitionResult result) { | ||
result.getAlternatives().stream().findFirst().ifPresent(a -> { | ||
System.out.printf(" [%.2f - %.2f][%d] %s%n", result.getSegmentStartTime(), result.getSegmentEndTime(), a.getConfidence(), a.getText()); | ||
}); | ||
} | ||
|
||
@Override | ||
public void onPartialRecognitionResult(PartialRecognitionResult result) { | ||
// System.out.println(String.format("Partial result: %s", result.getText())); | ||
} | ||
|
||
@Override | ||
public void onListening() { | ||
// System.out.println("Server is listening"); | ||
} | ||
|
||
@Override | ||
public void onError(RecognitionError error) { | ||
System.out.println(String.format("Recognition error: [%s] %s", error.getCode(), error.getMessage())); | ||
} | ||
}).build(); | ||
|
||
return recognizer; | ||
} | ||
|
||
} |