diff --git a/JARVIS-API.jar b/lib/JARVIS-API.jar similarity index 100% rename from JARVIS-API.jar rename to lib/JARVIS-API.jar diff --git a/lib/JTransforms-3.1-with-dependencies.jar b/lib/JTransforms-3.1-with-dependencies.jar new file mode 100755 index 0000000..c6d5e1a Binary files /dev/null and b/lib/JTransforms-3.1-with-dependencies.jar differ diff --git a/java-json.jar b/lib/java-json.jar similarity index 100% rename from java-json.jar rename to lib/java-json.jar diff --git a/java-string-similarity-0.19.jar b/lib/java-string-similarity-0.19.jar similarity index 100% rename from java-string-similarity-0.19.jar rename to lib/java-string-similarity-0.19.jar diff --git a/javaFlacEncoder-0.3.1.jar b/lib/javaFlacEncoder-0.3.1.jar similarity index 100% rename from javaFlacEncoder-0.3.1.jar rename to lib/javaFlacEncoder-0.3.1.jar diff --git a/lib/jcommon-1.0.23.jar b/lib/jcommon-1.0.23.jar new file mode 100755 index 0000000..4dbb094 Binary files /dev/null and b/lib/jcommon-1.0.23.jar differ diff --git a/lib/jfreechart-1.0.19.jar b/lib/jfreechart-1.0.19.jar new file mode 100755 index 0000000..10f276c Binary files /dev/null and b/lib/jfreechart-1.0.19.jar differ diff --git a/src/com/company/CommandsListener.java b/src/com/company/CommandsListener.java new file mode 100644 index 0000000..ca3af56 --- /dev/null +++ b/src/com/company/CommandsListener.java @@ -0,0 +1,11 @@ +package com.company; + +/** + * Created by user on 21.11.16. + */ +public interface CommandsListener { + void onStartTraining(); + void onPoseType(String name); + void onCapturePose(); + void onCompleteTraining(); +} diff --git a/src/com/company/CommandsRecognizer.java b/src/com/company/CommandsRecognizer.java index e85d4b8..5578d6a 100644 --- a/src/com/company/CommandsRecognizer.java +++ b/src/com/company/CommandsRecognizer.java @@ -3,6 +3,8 @@ import com.darkprograms.speech.recognizer.GoogleResponse; import info.debatty.java.stringsimilarity.JaroWinkler; +import java.util.ArrayList; +import java.util.EnumMap; import java.util.List; /** @@ -10,7 +12,21 @@ */ public class CommandsRecognizer implements ResponseListener { - JaroWinkler jaroWinkler; + private enum Commands { + startTraining, + poseType, + capturePose, + completeTraining + } + + private List listeners; + + private EnumMap commands; + private EnumMap commandsSimilarity; + + private JaroWinkler jaroWinkler; + + private int commandsCount = 4; private static final String startTraining = "начать обучение"; private static final String poseType = "название новой позы"; @@ -18,8 +34,22 @@ public class CommandsRecognizer implements ResponseListener { private static final String completeTraining = "закончить обучение"; + public CommandsRecognizer() { jaroWinkler = new JaroWinkler(); + commands = new EnumMap(Commands.class); + commandsSimilarity = new EnumMap(Commands.class); + + commands.put(Commands.startTraining, startTraining); + commands.put(Commands.capturePose, capturePose); + commands.put(Commands.poseType, poseType); + commands.put(Commands.completeTraining, completeTraining); + + listeners = new ArrayList(); + } + + public void addListener(CommandsListener listener) { + listeners.add(listener); } @@ -54,9 +84,51 @@ public void onResponce(GoogleResponse response) { } } - System.out.println(startTrainingSimilarity); - System.out.println(poseTypeSimilarity); - System.out.println(capturePoseSimilarity); - System.out.println(completeTrainingSimilarity); + commandsSimilarity.put(Commands.startTraining, startTrainingSimilarity); + commandsSimilarity.put(Commands.poseType, poseTypeSimilarity); + commandsSimilarity.put(Commands.capturePose, capturePoseSimilarity); + commandsSimilarity.put(Commands.completeTraining, completeTrainingSimilarity); + + RecognizedCommand recognizedCommand = new RecognizedCommand(); + + for (Commands command : Commands.values()) { + if(recognizedCommand.Similarity < commandsSimilarity.get(command)) { + recognizedCommand.command = command; + recognizedCommand.Similarity = commandsSimilarity.get(command); + + } + System.out.println(command + " " + commandsSimilarity.get(command)); + } + + double tresHold = 0.7; + + if(recognizedCommand.Similarity > tresHold) { + for (CommandsListener listener : listeners) { + sendCommand(listener, recognizedCommand.command); + } + } + } + + private void sendCommand(CommandsListener listener, Commands command) { + switch (command) { + case startTraining: + listener.onStartTraining(); + break; + case poseType: + listener.onPoseType("New name"); + break; + case capturePose: + listener.onCapturePose(); + break; + case completeTraining: + listener.onCompleteTraining(); + break; + } + } + + + class RecognizedCommand { + public Commands command = null; + public double Similarity = 0; } } diff --git a/src/com/company/FFTTest/GraphTest.java b/src/com/company/FFTTest/GraphTest.java new file mode 100644 index 0000000..3e89bff --- /dev/null +++ b/src/com/company/FFTTest/GraphTest.java @@ -0,0 +1,70 @@ +package com.company.FFTTest; + + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.axis.ValueAxis; +import org.jfree.chart.plot.XYPlot; +import org.jfree.data.time.Millisecond; +import org.jfree.data.time.TimeSeries; +import org.jfree.data.time.TimeSeriesCollection; + +import javax.swing.*; +import java.util.Random; + +/** + * Created by user on 22.11.16. + */ +public class GraphTest { + + static TimeSeries ts = new TimeSeries("data", Millisecond.class); + + public static void main(String[] args) throws InterruptedException { + gen myGen = new gen(); + new Thread(myGen).start(); + + TimeSeriesCollection dataset = new TimeSeriesCollection(ts); + JFreeChart chart = ChartFactory.createTimeSeriesChart( + "GraphTest", + "Time", + "Value", + dataset, + true, + true, + false + ); + final XYPlot plot = chart.getXYPlot(); + ValueAxis axis = plot.getDomainAxis(); + axis.setAutoRange(true); + axis.setFixedAutoRange(60000.0); + + JFrame frame = new JFrame("GraphTest"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + ChartPanel label = new ChartPanel(chart); + frame.getContentPane().add(label); + //Suppose I add combo boxes and buttons here later + + frame.pack(); + frame.setVisible(true); + } + + static class gen implements Runnable { + private Random randGen = new Random(); + + public void run() { + while (true) { + int num = randGen.nextInt(1000); + System.out.println(num); + ts.addOrUpdate(new Millisecond(), num); + try { + Thread.sleep(20); + } catch (InterruptedException ex) { + System.out.println(ex); + } + } + } + } + +} + diff --git a/src/com/company/FFTTest/Main.java b/src/com/company/FFTTest/Main.java new file mode 100755 index 0000000..a7b4420 --- /dev/null +++ b/src/com/company/FFTTest/Main.java @@ -0,0 +1,104 @@ + + +import com.company.SignalReader; +import com.company.SineExample; +import org.jtransforms.fft.*; + +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.data.xy.DefaultXYDataset; +import org.jfree.data.xy.XYDataset; + +public class Main { + + static JFreeChart chart; + + static int i = 0; + + public static void main(String[] args) { + + double[] sine = SineExample.getSine(); + //show(sine, "Sine"); + + double[] sineSpectrum = sine.clone(); + DoubleFFT_1D fftDo = new DoubleFFT_1D(sineSpectrum.length); + fftDo.realForward(sineSpectrum); + + + + show(sineSpectrum, "Sinus Spectrum"); + + while (true) { + + i++; + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + double[] arr = new double[sineSpectrum.length]; + + for(int j = 0; j < sineSpectrum.length; j++) { + arr[j] = i; + } + + XYDataset ds = createDataset(arr, 25000); + + chart = ChartFactory.createXYLineChart("q", + "x", "y", ds, PlotOrientation.VERTICAL, true, true, + false); + + } + + } + + private static void show(double[] array, String Title) { + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + JFrame frame = new JFrame("Charts"); + + frame.setSize(1000, 600); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setVisible(true); + + XYDataset ds = createDataset(array, 25000); + chart = ChartFactory.createXYLineChart(Title, + "x", "y", ds, PlotOrientation.VERTICAL, true, true, + false); + + ChartPanel cp = new ChartPanel(chart); + + + frame.getContentPane().add(cp); + } + }); + + } + + private static XYDataset createDataset(double[] array, double SampleRate) { + + DefaultXYDataset ds = new DefaultXYDataset(); + + double[][] data = new double[2][array.length]; + + + for (int i = 0; i < array.length; i++) { + data[0][i] = ((double)i * SampleRate / 2) / (array.length); + data[1][i] = array[i]; + } + + ds.addSeries("signal", data); + + return ds; + } + + +} diff --git a/src/com/company/FFTTest/SignalReader.java b/src/com/company/FFTTest/SignalReader.java new file mode 100755 index 0000000..9e89c69 --- /dev/null +++ b/src/com/company/FFTTest/SignalReader.java @@ -0,0 +1,56 @@ +package com.company; + +import java.io.*; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * Created by malll on 15.04.2016. + */ +public class SignalReader { + + private File dataFile = new File("data/2015_05_20__13_00_06.dat"); + private int ChannelNumber = 13; + private int[] Signal; + private int signalLenth; + private int sampleSize = 4; + + public SignalReader() { + try (DataInputStream dis = new DataInputStream(new FileInputStream(dataFile))) { + + byte[] signal = new byte[(int) dataFile.length()]; + dis.read(signal); + + signalLenth = signal.length / sampleSize; + Signal = new int[signalLenth]; + + ByteBuffer byteBuffer = ByteBuffer.wrap(signal); + byteBuffer.order(ByteOrder.LITTLE_ENDIAN); + + for(int i = 0; i < signalLenth; i++) { + int value = byteBuffer.getInt(); + Signal[i] = value; + } + + System.out.println(signalLenth); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public double[] readChannel(int channelNumber) { + + double[] ChannelOneSignal = new double[signalLenth / ChannelNumber]; + + for(int i = channelNumber - 1; i < signalLenth; i+= ChannelNumber) { + ChannelOneSignal[i / ChannelNumber] = Signal[i]; + } + for (int i = 0; i < ChannelOneSignal.length; i++) { + // System.out.println(ChannelOneSignal[i]); + } + return ChannelOneSignal; + } +} diff --git a/src/com/company/FFTTest/SineExample.java b/src/com/company/FFTTest/SineExample.java new file mode 100755 index 0000000..38781cf --- /dev/null +++ b/src/com/company/FFTTest/SineExample.java @@ -0,0 +1,31 @@ +package com.company; + +/** + * Created by malll on 18.04.2016. + */ +public class SineExample { + + private final static int SAMPLERATE = 25000; + + public static double[] getSine() { + int frequency = 100; // freq of our sine wave + double lengthInSecs = 0.01024; + int samplesNum = (int) Math.round(lengthInSecs * SAMPLERATE); + + System.out.println("Samplesnum: " + samplesNum); + + double[] audioData = new double[samplesNum]; + int samplePos = 0; + + // http://en.wikibooks.org/wiki/Sound_Synthesis_Theory/Oscillators_and_Wavetables + for (double phase = 0; samplePos < lengthInSecs * SAMPLERATE && samplePos < samplesNum; phase += (2 * Math.PI * frequency) / SAMPLERATE) { + audioData[samplePos++] = Math.sin(phase); + + if (phase >= 2 * Math.PI) + phase -= 2 * Math.PI; + } + + return audioData; + + } +} diff --git a/src/com/company/FFTTest/main2.java b/src/com/company/FFTTest/main2.java new file mode 100644 index 0000000..779756f --- /dev/null +++ b/src/com/company/FFTTest/main2.java @@ -0,0 +1,35 @@ +package com.company.FFTTest; + +/** + * Created by user on 23.11.16. + */ +public class main2 { + + public static void main(String[] args) { + int[] histogram = new int[13]; + + for (int i = 0; i < 13; i++) { + histogram[i] = i; + } + + System.out.println("Histogram of rolls:" ); + printHistogram(histogram); + } + + + public static void printHistogram(int[] array) { + for (int range = 0; range < array.length; range++) { + String label = range + " : "; + System.out.println(label + convertToStars(array[range])); + } + } + + public static String convertToStars(int num) { + StringBuilder builder = new StringBuilder(); + for (int j = 0; j < num; j++) { + builder.append('*'); + } + return builder.toString(); + } + +} diff --git a/src/com/company/Main.java b/src/com/company/Main.java index 475f0ae..875eb34 100644 --- a/src/com/company/Main.java +++ b/src/com/company/Main.java @@ -9,11 +9,7 @@ import com.darkprograms.speech.recognizer.GoogleResponse; import javaFlacEncoder.FLACFileWriter; -/** - * Jarvis Speech API Tutorial - * @author Aaron Gokaslan (Skylion) - * - */ + public class Main { public static void main1 (String[]args) { @@ -32,12 +28,12 @@ public static void main1 (String[]args) { try { mic.captureAudioToFile (file); } catch (Exception ex) { - //Microphone not available or some other error. - System.out.println ("ERROR: Microphone is not availible."); + //com.company.microphone.Microphone not available or some other error. + System.out.println ("ERROR: com.company.microphone.Microphone is not availible."); ex.printStackTrace (); } - /* User records the voice here. Microphone starts a separate thread so do whatever you want + /* User records the voice here. com.company.microphone.Microphone starts a separate thread so do whatever you want * in the mean time. Show a recording icon or whatever. */ try { @@ -75,8 +71,12 @@ public static void main1 (String[]args) { public static void main (String[] args) { RecordingThread recordingThread = new RecordingThread(); - recordingThread.addResponceListener(new CommandsRecognizer()); + CommandsRecognizer commandsRecognizer = new CommandsRecognizer(); + recordingThread.addResponceListener(new temp()); + recordingThread.addResponceListener(commandsRecognizer); + + commandsRecognizer.addListener(new test()); recordingThread.start(); } diff --git a/src/com/company/RecordingThread.java b/src/com/company/RecordingThread.java index c22cf02..738da5c 100644 --- a/src/com/company/RecordingThread.java +++ b/src/com/company/RecordingThread.java @@ -1,18 +1,14 @@ package com.company; -import com.darkprograms.speech.microphone.MicrophoneAnalyzer; +import com.company.microphone.MicrophoneAnalyzer; import com.darkprograms.speech.recognizer.GoogleResponse; import com.darkprograms.speech.recognizer.Recognizer; import javaFlacEncoder.FLACFileWriter; import java.io.File; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; -/** - * Created by user on 20.11.16. - */ public class RecordingThread extends Thread { private String apiKey = "AIzaSyDMRFZsdncfP2udmTbozAQ2owJuL5RRm34"; @@ -21,8 +17,9 @@ public class RecordingThread extends Thread { private int minimumVolumeToStartrecording = 40; private int volumeToStopRecording = 20; - private int checkVolumeSampleTime = 10; - private int sampleTime = 1000; + private int maxSamples = 50; + private int checkVolumeSampleTime = 100; + private int sampleTime = 100; private MicrophoneAnalyzer microphone; private File tempAudioFile; @@ -33,8 +30,7 @@ public class RecordingThread extends Thread { public RecordingThread() { microphone = new MicrophoneAnalyzer(FLACFileWriter.FLAC); - tempAudioFile = new File("temp.flac"); - microphone.setAudioFile(tempAudioFile); + recognizer = new Recognizer(Recognizer.Languages.RUSSIAN, apiKey); listeners = new ArrayList(); @@ -47,26 +43,45 @@ public void addResponceListener(ResponseListener listener) { @Override public void run() { + int curSample = 0; + while (true) { microphone.open(); try { + tempAudioFile = new File("temp.flac"); + microphone.setAudioFile(tempAudioFile); microphone.captureAudioToFile(microphone.getAudioFile()); + + /*double dTms = 32; + int bytes = microphone.getNumOfBytes(dTms/1000); + int freq = microphone.getFrequency(bytes); + int vol = microphone.getAudioVolume((int) dTms); + System.out.println(freq + " " + vol);*/ + Thread.sleep(checkVolumeSampleTime * 3); + double magnitude = microphone.magnitude(120, 122); + int volume = microphone.getAudioVolume(checkVolumeSampleTime); - boolean isSpeaking = (volume > minimumVolumeToStartrecording); + //System.out.println(volume); + //boolean isSpeaking = (volume > minimumVolumeToStartrecording); + boolean isSpeaking = (magnitude > 100); if (isSpeaking) { - DebugLog("RECORDING..."); + DebugLog("Start RECORDING..."); do { + DebugLog("RECORDING proc..."); Thread.sleep(sampleTime);//Updates every second - } while (microphone.getAudioVolume(sampleTime) > volumeToStopRecording); + } while (microphone.magnitude(120, 122) > 50); DebugLog("Recording Complete!"); + microphone.close(); + //Thread.sleep(9000); + DebugLog("Recognizing..."); GoogleResponse response = recognizer.getRecognizedDataForFlac(microphone.getAudioFile(), 3); @@ -74,7 +89,10 @@ public void run() { DebugLog("Looping back");//Restarts loops + } + microphone.getAudioFile().delete(); + } catch (Exception e) { // TODO Auto-generated catch block diff --git a/src/com/company/microphone/Microphone.java b/src/com/company/microphone/Microphone.java new file mode 100644 index 0000000..2b4a976 --- /dev/null +++ b/src/com/company/microphone/Microphone.java @@ -0,0 +1,223 @@ +package com.company.microphone; + +import javax.sound.sampled.*; + +import java.io.Closeable; +import java.io.File; + +/*************************************************************************** + * com.company.microphone.Microphone class that contains methods to capture audio from microphone + * + * @author Luke Kuza, Aaron Gokaslan + ***************************************************************************/ +public class Microphone implements Closeable{ + + /** + * TargetDataLine variable to receive data from microphone + */ + private TargetDataLine targetDataLine; + + + /** + * Enum for current com.company.microphone.Microphone state + */ + public enum CaptureState { + PROCESSING_AUDIO, STARTING_CAPTURE, CLOSED; + } + + /** + * Variable for enum + */ + CaptureState state; + + /** + * Variable for the audios saved file type + */ + private AudioFileFormat.Type fileType; + + /** + * Variable that holds the saved audio file + */ + private File audioFile; + + /** + * Constructor + * + * @param fileType File type to save the audio in
+ * Example, to save as WAVE use AudioFileFormat.Type.WAVE + */ + public Microphone(AudioFileFormat.Type fileType) { + setState(CaptureState.CLOSED); + setFileType(fileType); + initTargetDataLine(); + } + + /** + * Gets the current state of com.company.microphone.Microphone + * + * @return PROCESSING_AUDIO is returned when the Thread is recording Audio and/or saving it to a file
+ * STARTING_CAPTURE is returned if the Thread is setting variables
+ * CLOSED is returned if the Thread is not doing anything/not capturing audio + */ + public CaptureState getState() { + return state; + } + + /** + * Sets the current state of com.company.microphone.Microphone + * + * @param state State from enum + */ + private void setState(CaptureState state) { + this.state = state; + } + + public File getAudioFile() { + return audioFile; + } + + public void setAudioFile(File audioFile) { + this.audioFile = audioFile; + } + + public AudioFileFormat.Type getFileType() { + return fileType; + } + + public void setFileType(AudioFileFormat.Type fileType) { + this.fileType = fileType; + } + + public TargetDataLine getTargetDataLine() { + return targetDataLine; + } + + + public void setTargetDataLine(TargetDataLine targetDataLine) { + this.targetDataLine = targetDataLine; + } + + /** + * Initializes the target data line. + */ + private void initTargetDataLine(){ + DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat()); + try { + setTargetDataLine((TargetDataLine) AudioSystem.getLine(dataLineInfo)); + } catch (LineUnavailableException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } + + } + + + /** + * Captures audio from the microphone and saves it a file + * + * @param audioFile The File to save the audio to + * @throws LineUnavailableException + */ + public void captureAudioToFile(File audioFile) throws LineUnavailableException { + setState(CaptureState.STARTING_CAPTURE); + setAudioFile(audioFile); + + if(getTargetDataLine() == null){ + initTargetDataLine(); + } + + //Get Audio + new Thread(new CaptureThread()).start(); + + + } + + /** + * Captures audio from the microphone and saves it a file + * + * @param audioFile The fully path (String) to a file you want to save the audio in + * @throws LineUnavailableException + */ + public void captureAudioToFile(String audioFile) throws LineUnavailableException { + File file = new File(audioFile); + captureAudioToFile(file); + } + + + /** + * The audio format to save in + * + * @return Returns AudioFormat to be used later when capturing audio from microphone + */ + public AudioFormat getAudioFormat() { + float sampleRate = 8000.0F; + //8000,11025,16000,22050,44100 + int sampleSizeInBits = 16; + //8,16 + int channels = 1; + //1,2 + boolean signed = true; + //true,false + boolean bigEndian = false; + //true,false + return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); + } + + /** + * Opens the microphone, starting the targetDataLine. + * If it's already open, it does nothing. + */ + public void open(){ + if(getTargetDataLine()==null){ + initTargetDataLine(); + } + if(!getTargetDataLine().isOpen() && !getTargetDataLine().isRunning() && !getTargetDataLine().isActive()){ + try { + setState(CaptureState.PROCESSING_AUDIO); + getTargetDataLine().open(getAudioFormat()); + getTargetDataLine().start(); + } catch (LineUnavailableException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return; + } + } + + } + + /** + * Close the microphone capture, saving all processed audio to the specified file.
+ * If already closed, this does nothing + */ + public void close() { + if (getState() == CaptureState.CLOSED) { + } else { + getTargetDataLine().stop(); + getTargetDataLine().close(); + setState(CaptureState.CLOSED); + } + } + + /** + * Thread to capture the audio from the microphone and save it to a file + */ + private class CaptureThread implements Runnable { + + /** + * Run method for thread + */ + public void run() { + try { + AudioFileFormat.Type fileType = getFileType(); + File audioFile = getAudioFile(); + open(); + AudioSystem.write(new AudioInputStream(getTargetDataLine()), fileType, audioFile); + //Will write to File until it's closed. + } catch (Exception ex) { + ex.printStackTrace(); + } + } + } + +} \ No newline at end of file diff --git a/src/com/company/microphone/MicrophoneAnalyzer.java b/src/com/company/microphone/MicrophoneAnalyzer.java new file mode 100644 index 0000000..81695d0 --- /dev/null +++ b/src/com/company/microphone/MicrophoneAnalyzer.java @@ -0,0 +1,361 @@ +package com.company.microphone; + +import com.darkprograms.speech.util.Complex; +import com.darkprograms.speech.util.FFT; + +import javax.sound.sampled.AudioFileFormat; + + +/******************************************************************************************** + * com.company.microphone.Microphone Analyzer class, detects pitch and volume while extending the microphone class. + * Implemented as a precursor to a Voice Activity Detection (VAD) algorithm. + * Currently can be used for audio data analysis. + * Dependencies: FFT.java and Complex.java. Both found in the utility package. + * @author Aaron Gokaslan + ********************************************************************************************/ + +public class MicrophoneAnalyzer extends Microphone { + + /** + * Constructor + * @param fileType The file type you want to save in. FLAC recommended. + */ + public MicrophoneAnalyzer(AudioFileFormat.Type fileType){ + super(fileType); + } + + /** + * Gets the volume of the microphone input + * Interval is 100ms so allow 100ms for this method to run in your code or specify smaller interval. + * @return The volume of the microphone input or -1 if data-line is not available + */ + public int getAudioVolume(){ + return getAudioVolume(100); + } + + /** + * Gets the volume of the microphone input + * @param interval: The length of time you would like to calculate the volume over in milliseconds. + * @return The volume of the microphone input or -1 if data-line is not available. + */ + public int getAudioVolume(int interval){ + return calculateAudioVolume(this.getNumOfBytes(interval/1000d)); + } + + /** + * Gets the volume of microphone input + * @param numOfBytes The number of bytes you want for volume interpretation + * @return The volume over the specified number of bytes or -1 if data-line is unavailable. + */ + private int calculateAudioVolume(int numOfBytes){ + byte[] data = getBytes(numOfBytes); + if(data==null) + return -1; + return calculateRMSLevel(data); + } + + /** + * Calculates the volume of AudioData which may be buffered data from a data-line. + * @param audioData The byte[] you want to determine the volume of + * @return the calculated volume of audioData + */ + public static int calculateRMSLevel(byte[] audioData){ + long lSum = 0; + for(int i=0; imax){ + max = tmp;; + index = i; + } + } + return index; + } + + /** + * Converts bytes from a TargetDataLine into a double[] allowing the information to be read. + * NOTE: One byte is lost in the conversion so don't expect the arrays to be the same length! + * @param bufferData The buffer read in from the target data line + * @return The double[] that the buffer has been converted into. + */ + private double[] bytesToDoubleArray(byte[] bufferData){ + final int bytesRecorded = bufferData.length; + final int bytesPerSample = getAudioFormat().getSampleSizeInBits()/8; + final double amplification = 100.0; // choose a number as you like + double[] micBufferData = new double[bytesRecorded - bytesPerSample + 1]; + for (int index = 0, floatIndex = 0; index < bytesRecorded - bytesPerSample + 1; index += bytesPerSample, floatIndex++) { + double sample = 0; + for (int b = 0; b < bytesPerSample; b++) { + int v = bufferData[index + b]; + if (b < bytesPerSample - 1 || bytesPerSample == 1) { + v &= 0xFF; + } + sample += v << (b * 8); + } + double sample32 = amplification * (sample / 32768.0); + micBufferData[floatIndex] = sample32; + + } + return micBufferData; + } + +} \ No newline at end of file diff --git a/src/com/company/test.java b/src/com/company/test.java new file mode 100644 index 0000000..ba4f9da --- /dev/null +++ b/src/com/company/test.java @@ -0,0 +1,34 @@ +package com.company; + +/** + * Created by user on 21.11.16. + */ +public class test implements CommandsListener { + @Override + public void onStartTraining() { + System.out.println("**************"); + System.out.println("Start Training"); + System.out.println("**************"); + } + + @Override + public void onPoseType(String name) { + System.out.println("**************"); + System.out.println(name); + System.out.println("**************"); + } + + @Override + public void onCapturePose() { + System.out.println("**************"); + System.out.println("Pose Fixation"); + System.out.println("**************"); + } + + @Override + public void onCompleteTraining() { + System.out.println("**************"); + System.out.println("End Training"); + System.out.println("**************"); + } +} diff --git a/temp.flac b/temp.flac index d67b4d3..56e1a56 100644 Binary files a/temp.flac and b/temp.flac differ diff --git a/temp.wav b/temp.wav new file mode 100644 index 0000000..56e1a56 Binary files /dev/null and b/temp.wav differ diff --git a/test_google_speach_api.iml b/test_google_speach_api.iml index d06a801..92755be 100644 --- a/test_google_speach_api.iml +++ b/test_google_speach_api.iml @@ -10,7 +10,7 @@ - + @@ -19,7 +19,18 @@ - + + + + + + + + + + + + @@ -28,14 +39,38 @@ - + - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - \ No newline at end of file