Fabio S.
Fabio S.

Reputation: 491

Android - How to reverse only the audio part of an array?

In a nut shell I don't know exactly where in the array the sound is. Reversing the entire array corrupts the headers, and its no longer playable.

I am recording sound with the following parameters

mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

The code below reverses the array, but clearly it is wrong, since I don't know how many bytes to skip for the headers. Can anyone help out by altering the function below to reverse only the audio part?

private static void reverseAudio(byte[] data) {
  for (int left = 0, right = data.length - 1; left < right; left++, right--) 
  {
     byte temp = data[left];
     data[left]  = data[right];
     data[right] = temp;
  }

Thank you!

@JiangYD I am new to Android so your answer seems promising, but I cant really make sense of it. I am using the MediaRecorder to create the audio file.

Where does the AudioRecord class fit into the picture?

 private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

 private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
    }

 private void startPlaying() {
        mPlayer = new MediaPlayer();
        try {
            File f = new File(mFileName);
            byte[] byteArray = null;

            InputStream inputStream = new FileInputStream(f);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024 * 8];
            int bytesRead = 0;

            while ((bytesRead = inputStream.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }

            byteArray = bos.toByteArray();
            reverseAudio(byteArray);

            File tempMp3 = File.createTempFile("kurchina", "3gpp", getCacheDir());
            tempMp3.deleteOnExit();
            FileOutputStream fos = new FileOutputStream(tempMp3);
            fos.write(byteArray);
            fos.close();

            FileInputStream fis = new FileInputStream(tempMp3);

            mPlayer.setDataSource(fis.getFD());
            mPlayer.prepare();
            mPlayer.start();


        }
        catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }
    }

Upvotes: 0

Views: 529

Answers (1)

Jiang YD
Jiang YD

Reputation: 3311

you need PCM format for audio processing.

new AudioRecord(MediaRecorder.AudioSource.DEFAULT
    , SAMPLE_RATE
    , AudioFormat.CHANNEL_IN_STEREO
    , AudioFormat.ENCODING_PCM_16BIT
    , CONNECT_TIMEOUT * SAMPLE_RATE * 2 / 1000);

the captured audio data will be in short[left], short[right], as you need. You can add 3rd AMR encoder to encode PCM to AMR after processing.

Upvotes: 1

Related Questions