-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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. | ||
} | ||
} | ||
} | ||
} |
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. | ||
} | ||
} |
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> |