1

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?

berserk
  • 2,690
  • 3
  • 32
  • 63

1 Answers1

0

You appear to be converting single bytes, but the format of a .wav/RIFF file, after the header (often 44 bytes), is usually 16-bit or 2-byte little-endian samples.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153