Visualized frequency from played audio file seems wrong

110 views Asked by At

My apps have two feature, for recording and playing back the recorded or other PCM file. Both can visualize frequency while recording and playing the file. But the visualized frequency while recording and playing back seems different. I think the right one is while recording. Anyone now whats wrong?

Here's my playAudio code

public void play() {
    startPlaybackButton.setEnabled(false);
    playTask = new PlayAudio();
    playTask.execute();
    stopPlaybackButton.setEnabled(true);
}

private class PlayAudio extends AsyncTask<Void, double[], Void> {
        @Override
        protected Void doInBackground(Void... params) {
            isPlaying = true;
            int bufferSize = AudioTrack.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);
            short[] audiodata = new short[blockSize];
            double[] toTransform = new double[blockSize];
            try {
                DataInputStream dis = new DataInputStream(
                        new BufferedInputStream(new FileInputStream(item)));
                AudioTrack audioTrack = new AudioTrack(
                        AudioManager.STREAM_MUSIC, frequency,
                        channelConfiguration, audioEncoding, bufferSize,
                        AudioTrack.MODE_STREAM);
                audioTrack.play();
                while (isPlaying && dis.available() > 0) {
                    for (int i = 0; dis.available() > 0 && i < audiodata.length; i++){
                        audiodata[i] = dis.readShort();
                        toTransform[i] = (double) audiodata[i] / 32768.0;
                    }

                    audioTrack.write(audiodata, 0, audiodata.length);
                    transformer.ft(toTransform);
                    publishProgress(toTransform);

                }
                dis.close();
                startPlaybackButton.setEnabled(false);
                stopPlaybackButton.setEnabled(true);
            } catch (Throwable t) {
                Log.e("AudioTrack", "Playback Failed");
            }
            return null;
        }

        protected void onProgressUpdate(double[]... toTransform) {
            canvas.drawColor(Color.BLACK);
            for (int i = 0; i < toTransform[0].length; i++) {
                int x;
                x = i;
                int downy = (int) (100 - (toTransform[0][i] * 10));
                int upy = 100;
                canvas.drawRect(x * 3, downy, x * 3 + 4, upy, paint);
                //canvas.drawLine(x, downy, x, upy, paint);
                imageView.invalidate();
            }
        }
1

There are 1 answers

0
Tatarize On

Best guess. Your sample rate is changed from what you set it to.

http://developer.android.com/reference/android/media/AudioRecord.html

"the sample rate expressed in Hertz. 44100Hz is currently the only rate that is guaranteed to work on all devices, but other rates such as 22050, 16000, and 11025 may work on some devices."