Skip to content

Commit

Permalink
Базовый синхронный и асинхронный проект spechapi
Browse files Browse the repository at this point in the history
  • Loading branch information
3XclusiVe committed Nov 15, 2016
1 parent 69660db commit 27b4fc9
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

.idea

# User-specific stuff:
.idea/workspace.xml
Expand Down
22 changes: 22 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/description.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/project-template.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added CRAudioTest.flac
Binary file not shown.
Binary file added JARVIS-API.jar
Binary file not shown.
Binary file added java-json.jar
Binary file not shown.
Binary file added javaFlacEncoder-0.3.1.jar
Binary file not shown.
46 changes: 46 additions & 0 deletions src/com/company/DuplexMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.company;

import com.darkprograms.speech.microphone.Microphone;
import com.darkprograms.speech.recognizer.GSpeechDuplex;
import com.darkprograms.speech.recognizer.GSpeechResponseListener;
import com.darkprograms.speech.recognizer.GoogleResponse;
import javaFlacEncoder.FLACFileWriter;

import java.io.File;
import java.nio.file.Files;

public class DuplexMain {

public static void main(String[] args) {

GSpeechDuplex dup = new GSpeechDuplex("AIzaSyDMRFZsdncfP2udmTbozAQ2owJuL5RRm34");//Instantiate the API
dup.addResponseListener(new GSpeechResponseListener() {// Adds the listener
public void onResponse(GoogleResponse gr) {
System.out.println("Google thinks you said: " + gr.getResponse());
System.out.println("with " +
((gr.getConfidence() != null) ? (Double.parseDouble(gr.getConfidence()) * 100) : null)
+ "% confidence.");
System.out.println("Google also thinks that you might have said:"
+ gr.getOtherPossibleResponses());
}
});
Microphone mic = new Microphone(FLACFileWriter.FLAC);//Instantiate microphone and have
// it record FLAC file.
File file = new File("CRAudioTest.flac");//The File to record the buffer to.
//You can also create your own buffer using the getTargetDataLine() method.
while (true) {
try {
mic.captureAudioToFile(file);//Begins recording
Thread.sleep(5000);//Records for 10 seconds
mic.close();//Stops recording
//Sends 10 second voice recording to Google
byte[] data = Files.readAllBytes(mic.getAudioFile().toPath());//Saves data into memory.
dup.recognize(data, (int) mic.getAudioFormat().getSampleRate());
mic.getAudioFile().delete();//Deletes Buffer file
//REPEAT
} catch (Exception ex) {
ex.printStackTrace();//Prints an error if something goes wrong.
}
}
}
}
75 changes: 75 additions & 0 deletions src/com/company/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.company;

import javax.sound.sampled.AudioFileFormat.Type;
import javax.sound.sampled.*;
import java.io.File;

import com.darkprograms.speech.microphone.Microphone;
import com.darkprograms.speech.recognizer.Recognizer;
import com.darkprograms.speech.recognizer.GoogleResponse;
import javaFlacEncoder.FLACFileWriter;

/**
* Jarvis Speech API Tutorial
* @author Aaron Gokaslan (Skylion)
*
*/
public class Main {

public static void main (String[]args) {

// Mixer.Info[] infoArray = AudioSystem.getMixerInfo();
// for(Mixer.Info info : infoArray) {
// System.out.println("info: " + info.toString());
// }
AudioFileFormat.Type[] typeArray = AudioSystem.getAudioFileTypes();
for(AudioFileFormat.Type type : typeArray) {
System.out.println("type: " + type.toString());
}

Microphone mic = new Microphone(FLACFileWriter.FLAC);
File file = new File ("/tmp/testfile2.flac"); //Name your file whatever you want
try {
mic.captureAudioToFile (file);
} catch (Exception ex) {
//Microphone not available or some other error.
System.out.println ("ERROR: Microphone is not availible.");
ex.printStackTrace ();
}

/* User records the voice here. Microphone starts a separate thread so do whatever you want
* in the mean time. Show a recording icon or whatever.
*/
try {
System.out.println ("Recording...");
Thread.sleep (5000); //In our case, we'll just wait 5 seconds.
mic.close ();
} catch (InterruptedException ex) {
ex.printStackTrace ();
}

mic.close (); //Ends recording and frees the resources
System.out.println ("Recording stopped.");

Recognizer recognizer = new Recognizer (Recognizer.Languages.ENGLISH_US, "AIzaSyDMRFZsdncfP2udmTbozAQ2owJuL5RRm34");
//Although auto-detect is available, it is recommended you select your region for added accuracy.
try {
int maxNumOfResponses = 4;
System.out.println("Sample rate is: " + (int) mic.getAudioFormat().getSampleRate());
GoogleResponse response = recognizer.getRecognizedDataForFlac (file, maxNumOfResponses, (int) mic.getAudioFormat().getSampleRate ());
System.out.println ("Google Response: " + response.getResponse ());
System.out.println ("Google is " + Double.parseDouble (response.getConfidence ()) * 100 + "% confident in" + " the reply");
System.out.println ("Other Possible responses are: ");
for (String s:response.getOtherPossibleResponses ()) {
System.out.println ("\t" + s);
}
}
catch (Exception ex) {
// TODO Handle how to respond if Google cannot be contacted
System.out.println ("ERROR: Google cannot be contacted");
ex.printStackTrace ();
}

file.deleteOnExit (); //Deletes the file as it is no longer necessary.
}
}
40 changes: 40 additions & 0 deletions test_google_speach_api.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/JARVIS-API.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/javaFlacEncoder-0.3.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/java-json.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MODULE_DIR$/java-json.jar!/" />
</SOURCES>
</library>
</orderEntry>
</component>
</module>

0 comments on commit 27b4fc9

Please sign in to comment.