I am using this FFT library to perform the FFT on the sound caught by device microphone. I am using following code:
int bufferSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
frequency, channelConfiguration, audioEncoding, bufferSize);
int bufferReadResult;
try {
audioRecord.startRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
Log.e("Recording failed", e.toString());
}
while (true) {
bufferReadResult = audioRecord.read(buffer, 0, blockSize);
if (isCancelled())
break;
boolean bln = false;
for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
toTransform[i] = (double) buffer[i] / 32768.0;
if (buffer[i] != 0) {
bln = true;
}
}
if (bln) {
transformer.ft(toTransform);
doStuff(toTransform);
} else {
try {
audioRecord = new AudioRecord(
MediaRecorder.AudioSource.DEFAULT, frequency,
channelConfiguration, audioEncoding, bufferSize);
audioRecord.startRecording();
} catch (IllegalStateException e) {
e.printStackTrace();
Log.e("Recording failed", e.toString());
}
}
if (isCancelled())
break;
}
try {
audioRecord.stop();
} catch (IllegalStateException e) {
e.printStackTrace();
Log.e("Stop failed", e.toString());
}
It works fine for microphone. But I want to do this transformation on sound file(in raw folder). I tried using InputStreamReader to read the sound file content, but how to use this file data to apply transformation on file? I tried using a buffer using following code:
InputStream ins;
ByteArrayOutputStream outputStream;
int size = 0;
byte[] buffer = new byte[1024];
ins = getResources().openRawResource(R.raw.fire);
outputStream = new ByteArrayOutputStream();
transformer = new RealDoubleFFT(blockSize);
while ((size = ins.read(buffer, 0, 1024)) >= 0) {
outputStream.write(buffer, 0, size);
if (isCancelled())
break;
boolean bln = false;
for (int i = 0; i < buffer.length; i++) {
toTransform[i] = (double) buffer[i] / 32768.0;
if (buffer[i] != 0) {
bln = true;
}
}
if (bln) {
transformer.ft(toTransform);
doStuff(toTransform);
}
}
ins.close();
But it is not returning the correct data. Maybe the data received from file is not what the library wants. Any ideas how to apply FFT on sound file?