@Override
protected Boolean doInBackground(Void... params) {
bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, frequency, channelConfiguration, audioEncoding, bufferSize);
short[] buffer = new short[blockSize];
double[] toTransform = new double[blockSize];
audioRecord.startRecording();
System.out.println("hello");
while (started) {
bufferResult = audioRecord.read(buffer, 0, blockSize);
double max=0;
int maxIndex = 0;
for (int i = 0; i < blockSize&&i < bufferResult; i++) {
double k=10*Math.log(Math.abs((double)buffer[i]));
if(k>max)
{
max=k;
maxIndex=i;
}
toTransform[i] = (double) buffer[i]/32768.0;
// 값이 전체범위가 아니고 -1.0~1.0 사이기떄문에 short를 32768.0으로 나눠야한다.
// 형변환을 위해서
}
if(max>MAX_SENSETIVE) {
// System.out.println(max + "max=" + maxIndex);
}
// int result=calculatePowerDb(buffer);
// System.out.println(result);
//
//onProgressUpdate();실행
//totranform인자를 넘겨줌.
//Background에서의 작업을마치고,메인쓰레드작업이끝나고,이제 UI쓰레드변경을함.x
try {
transformer.ft(toTransform);
}catch(Exception e)
{
System.out.println("hello");
}
double frequencyMax=0;
for(int i=0;i<blockSize&&i<bufferResult;i++)
{
if(Math.abs(toTransform[i])>frequencyMax)
{
frequencyMax=Math.abs(toTransform[i]);
maxIndex=i;
}
}
//to Transfrom 30~40에 frequency MAx값이 100이상일경우,사람의 고함소리로 간주한다.
if(maxIndex>30&&maxIndex<40&&frequencyMax>50.0) {
System.out.println("Max Frequeny=" + maxIndex + "=" + frequencyMax);
vib.vibrate(200);
}
publishProgress(toTransform);
}
return true;
}
앱으로 차량경적소리및 사람의 목소리를 구분하려고하는데요,
현재궁금한점이.short형배열에 read로 읽고,
이걸 double로 변환,fft알고리즘에따라서 값을 변환하게되는데
이떄 double[256] 배열중(0~4kHz),제가원하는 주파수대역대의 값이 제일크면,해당한 소리가 들린다.라고 하면 괜찮은 정확도가 나올까요?
또 읽어온 값들이 음수값이 나오는경우에는 왜 음수값이 나오는지 알수있을까요?
그외에 다른 fft알고리즘을 이용한 소리분석에 관한 정보나 알고리즘아시는분은 도와주세요..부탁드립니다