0

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");
        }
    }
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Fabio S.
  • 460
  • 7
  • 22
  • You might refer this post, I think that will help you. http://stackoverflow.com/questions/5625573/byte-array-to-short-array-and-back-again-in-java/5626003#5626003 – Chauyan Jun 08 '15 at 05:33
  • A sound is not just `raw data`. It has a header and possibly a footer. You have to study the `specific audio format` you want to use. – Phantômaxx Jun 08 '15 at 06:51
  • 1
    Your audio is compressed with the AMR-NB codec, so even if you got the headers right, just switching compressed audio bytes around is probably going to leave you with garbage. – Michael Jun 08 '15 at 08:12

1 Answers1

1

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.

Shoshi
  • 2,254
  • 1
  • 29
  • 43
Jiang YD
  • 3,205
  • 1
  • 14
  • 20