Sei sulla pagina 1di 4

Speech To Text using Java API

Sunday, February 6, 2011

In this post, we are going to learn about speech to text conversion using java speech API.
We will be discussing the following topics :

Brief about Speech Recognition.


Setup Speech Recognition engine
Verifying the Speech Recognition engine installation
Sample code
Conclusion

Speech Recognition is the process of converting spoken input to digital output, such as text.
Speech recognition systems provide computers with the ability to listen to user speech and
determine what is said.

The Speech Recognition process can be divided into these four steps:

1. Speech is converted to digital signals.


2. Actual speech sounds are extracted from the sounds (based on energy of the
sounds).
3. The extracted sounds are put together into 'speech frames.'
4. The speech frames are compared with words from the grammar file to determine the
spoken word.

We are going to use a third party java speech recognizer engine TalkingJava SDK which is
a full implementation of Sun's Java Speech API providing Text-To-Speech and Speech-
Recognition engines.

TalkingJava SDK website has been shutdown. I have shared the old SDK You can
download from here. Click Here
Four easy step to set up TalkingJava SDK

1. Download TalkingJava SDK Installer file : Download


2. Click on TalkingJavaSDK-1xx.jar and follow the instruction to install SDK.
3. Unpack TalkingJavaSDK-1xx.jar file. Then unpack packet.jar (available inside
TalkingJavaSDK-1xx directory).
4. Copy cgjsapi.jar and cgjsapi1xx.dll files available inside packet directory to your
Java\jdk1.x.x.x\jre\lib\ext\ direcoty.
5. Include your packet directory path into your CLASSPATH environment variable.

You can also refer to the installation guide here -cloudgarden installation

We are going to verify whether the speech recognition engine has been installed
successfully or not using a simple java program.
TestRecognizerConfig.java
?
1 package com.sarf.talkingjava;
2 package com.sarf.talkingjava;
3
4 import java.util.Locale;
5 import javax.speech.Central;
6 import javax.speech.EngineList;
7 import javax.speech.recognition.RecognizerModeDesc;
8
9 public class TestRecognizerConfig {
10 public static void main(String[] args) {
11 try
12 {
13 Central.registerEngineCentral
14 ("com.cloudgarden.speech.CGEngineCentral");
15 RecognizerModeDesc desc =
16 new RecognizerModeDesc(Locale.US,Boolean.TRUE);
17 EngineList el = Central.availableRecognizers(desc);
18 if(el.size() < 1){
19 System.out.println("Recognition Engine is not available");
20 System.exit(1);
21 }else{
22 System.out.println("Recognition Engine is available");
23 System.exit(1);
24 }
25 }catch(Exception exception)
26 {
27 exception.printStackTrace();
28 }
29 }
30}
Run this class file and check the output. If output is Recognition Engine is available then
you have installed recognition engine successfully and you are good to go.

SpeechToTextConverter.java
?
1 package com.sarf.talkingjava;
2
3 import javax.speech.Central;
4 import javax.speech.recognition.*;
5 import java.io.FileReader;
6 import java.util.Locale;
7
8 public class SpeechToTextConverter extends ResultAdapter {
9 static Recognizer recognizer;
10 public void resultAccepted(ResultEvent resultEvent) {
11 Result result = (Result)(resultEvent.getSource());
12 ResultToken resultToken[] = result.getBestTokens();
13 for (int nIndex = 0; nIndex < resultToken.length; nIndex++){
14 System.out.print(resultToken[nIndex].getSpokenText() + " ");
15 }
16 try {
17 // Deallocate the recognizer
18 recognizer.forceFinalize(true);
19 recognizer.deallocate();
20 }catch (Exception exception) {
21 exception.printStackTrace();
22 }
23 System.exit(0);
24 }
25
26 public static void main(String args[]) {
27 try {
28 Central.registerEngineCentral
29 ("com.cloudgarden.speech.CGEngineCentral");
30 RecognizerModeDesc desc =
31 new RecognizerModeDesc(Locale.US,Boolean.TRUE);
32 // Create a recognizer that supports US English.
33 recognizer = Central.createRecognizer(desc);
34 // Start up the recognizer
35 recognizer.allocate();
36 // Load the grammar from a file, and enable it
37 FileReader fileReader =
38 new FileReader("D:\\my_grammar.grammar");
39 RuleGrammar grammar = recognizer.loadJSGF(fileReader);
40 grammar.setEnabled(true);
41 // Add the listener to get results
42 recognizer.addResultListener(new SpeechToTextConverter());
43 // Commit the grammar
44 recognizer.commitChanges();
45 recognizer.waitEngineState(Recognizer.LISTENING);
46 // Request focus and start listening
47 recognizer.requestFocus();
48 recognizer.resume();
49 recognizer.waitEngineState(Recognizer.FOCUS_ON);
50 recognizer.forceFinalize(true);
51 recognizer.waitEngineState(Recognizer.DEALLOCATED);
52 } catch (Exception e) {
53 e.printStackTrace();
54 System.exit(0);
55 }
56 }
57}
A Little Grammer
The JSpeech Grammar Format (JSGF) is a platform-independent, vendor-independent
textual representation of grammars for use in speech recognition. Grammars are used by
speech recognizers to determine what the recognizer should listen for, and so describe the
utterances a user may say. JSGF adopts the style and conventions of the JavaTM
Programming Language in addition to use of traditional grammar notations. To know more
about JSGF.

Potrebbero piacerti anche