Skip to content

Instantly share code, notes, and snippets.

@pirapira
Created October 23, 2011 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirapira/1307648 to your computer and use it in GitHub Desktop.
Save pirapira/1307648 to your computer and use it in GitHub Desktop.
AudioInput.java
import java.io.*;
import java.io.BufferedWriter;
import java.text.*;
import java.util.*;
import javax.sound.sampled.*;
public class AudioInput implements Runnable{
public static void main(String args[]) {
AudioInput audioinput = new AudioInput();
Thread audioThread = new Thread(audioinput);
audioThread.start();
}
private int calculateRMSLevel(byte[] audioData){
// audioData might be buffered data read from a data line
long lSum = 0;
for(int i=0; i<audioData.length; i++){
lSum = lSum + audioData[i];
}
double dAvg = lSum / audioData.length;
double sumMeanSquare = 0d;
for(int j=0; j<audioData.length; j++){
sumMeanSquare = sumMeanSquare + Math.pow(audioData[j] - dAvg, 2d);
}
double averageMeanSquare = sumMeanSquare / audioData.length;
return (int)(Math.pow(averageMeanSquare,0.5d) + 0.5);
}
public void run(){
_audioData = new byte[20000];
_csvFile = new File("sound_level.csv");
try{
int iLinVol = 0;
double dAveVol = 0;
int[] iVolume = new int[20];
int i = 0;
boolean full = false;
int currentVolume;
AudioFormat linearFormat = new AudioFormat(8000, 16, 1, true, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, linearFormat);
TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
targetDataLine.open(linearFormat);
targetDataLine.start();
AudioInputStream linearStream = new AudioInputStream(targetDataLine);
while(true){
linearStream.read(_audioData,0,_audioData.length);
if(calculateRMSLevel(_audioData) > 0.0){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH':'mm':'ss");
String sTime = sdf.format(date);
try{
currentVolume = calculateRMSLevel(_audioData);
iVolume[i] = currentVolume;
//System.out.println(iVolume[i]);
i++;
if(i == 20){
full = true;
i = 0;
}
if(full){
// calculate average
double iTotal = 0;
for(int j=0; j<20; j++){
iTotal = iTotal + iVolume[j];
}
dAveVol = iTotal/20.0;
// System.out.println(dAveVol);
// calculate Standard Deviation
iTotal = 0;
for(int k=0; k<20; k++){
iTotal += (iVolume[k]-dAveVol)*(iVolume[k]-dAveVol);
}
double dDist = iTotal/20;
double dStan = Math.sqrt(dDist);
System.out.println(dStan);
if(currentVolume > dAveVol + 0.5 * dDist) {
System.out.println("kehai");
}
}
_bw = new BufferedWriter(new FileWriter(_csvFile, true));
_bw.write(sTime+","+currentVolume);
_bw.newLine();
_bw.flush();
}catch (IOException e){
e.printStackTrace();
}
}
//targetDataLine.stop();
//targetDataLine.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
private byte[] _audioData;
private BufferedWriter _bw;
private File _csvFile;
}
// end of MitsubajaExciteInput.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment