Android reverse playing pcm file

I have a pcm audio file, or byte array of this file. And i need to reverse it and play it.

If i just reverse byte array like this I'm getting noise, so i should reverse only raws or sample raws of pcm.

public void reverse(byte[] array) {
          if (array == null) {
              return;
          }
          int i = 0;
          int j = array.length - 1;
          byte tmp;
          while (j > i) {
              tmp = array[j];
              array[j] = array[i];
              array[i] = tmp;
              j--;
              i++;
          }
      }

I read a lot, but I didn't find any info of how i can do this.

Data in byte array stores like this(at oneDrive, cuz not enough reputation) : http://1drv.ms/1DJ0jDr

I'll apreciate any help. Thanks a lot!

Upvotes: 3

Views: 740

Answers (1)

jaket
jaket

Reputation: 9341

You will need to first convert the byte array to an array type that is appropriate for you samples and then reverse that array. For example if your samples are 16-bit then you'll want to convert to an array of shorts.

Alternatively, in the 16-bit case you could keep the data in bytes and then reverse - but in pairs of bytes.

public void reverse(byte[] array) {
      if (array == null) {
          return;
      }
      int i = 0;
      int j = array.length - 1;
      byte tmp;
      for (int k = 0 ; i < array.length/2; ++i) {
          tmp = array[j-1];
          array[j-1] = array[i];
          array[i] = tmp;

          tmp = array[j];
          array[j] = array[i+1];
          array[i+1] = tmp;

          j+=2;
          i+=2;
      }
  }

Upvotes: 2

Related Questions