0

I want eliminate audio by playing original audio and its inverse at the same time. I have the byte array of the audio but I cannot find a way to take inverse of that byte array. I want to shift the phase to 180. So that it becomes inverse of the original signal. Hence silence.

I followed this link https://www.tutorialspoint.com/android/android_audio_capture.htm to record audio. To convert audio to bytes I followed How to convert video/audio file to byte array and vice versa in android.? this question. And the best answer to this question Android - Playing mp3 from byte[] to play the bytes. I followed http://programminglinuxblog.blogspot.com/2014/07/how-to-flip-all-bits-in-bytearray-java.html link to get inverse. But when I play audio with only the inverse bytes nothing plays.

byte[] toFlip, flipped;

try {
      toFlip = convertAudioToBytes();
      BitSet set = BitSet.valueOf(toFlip);
      set.flip(0, set.length());
      flipped = set.toByteArray();
      Toast.makeText(MainActivity.this, "Inveresed Successfully", Toast.LENGTH_LONG).show();
       } catch (IOException e) {
          e.printStackTrace();
          Toast.makeText(MainActivity.this, "Inveresed Failed" + e, Toast.LENGTH_LONG).show();
          System.out.println("Inveresed Failed" + e);
          }

public byte[] convertAudioToBytes() throws IOException {

    FileInputStream fis = new FileInputStream(AudioSavePathInDevice);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];

    for (int readNum; (readNum = fis.read(b)) != -1; ) {
        bos.write(b, 0, readNum);
    }

    byte[] bytes = bos.toByteArray();

    return bytes;
}

private void playMp3(byte[] mp3SoundByteArray) {
    try {
        // create temp file that will hold byte array
        File tempMp3 = File.createTempFile("kurchina", "mp3", getCacheDir());
        tempMp3.deleteOnExit();
        FileOutputStream fos = new FileOutputStream(tempMp3);
        fos.write(mp3SoundByteArray);
        fos.close();

        // resetting mediaplayer instance to evade problems
        mediaPlayer.reset();

        // In case you run into issues with threading consider new instance like:
        // MediaPlayer mediaPlayer = new MediaPlayer();

        // Tried passing path directly, but kept getting
        // "Prepare failed.: status=0x1"
        // so using file descriptor instead
        FileInputStream fis = new FileInputStream(tempMp3);
        mediaPlayer.setDataSource(fis.getFD());

        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (IOException ex) {
        String s = ex.toString();
        ex.printStackTrace();
    }
}

It should play the inverse when flipped byte array is played. But nothing plays.

Hammad Khan
  • 3
  • 1
  • 2
  • You have numerous conceptual problems, here: 1.) .mp3 is not a [PCM](https://en.wikipedia.org/wiki/Pulse-code_modulation) format; it is compressed. You must pass the bytes through a decompressor in order to properly interpret the waveform. 2.) The inverse of a PCM waveform is not calculated by "flipping" the bits, but by simple negation. 3.) When you add a waveform and its inverse, you end up with an array full of 0's. There are easier ways to do create such an array. It's not clear what you're trying to achieve. 4.) Also not clear where you call `playMp3()`. – greeble31 Jan 16 '19 at 04:52
  • The inverse of a PCM waveform is not calculated by "flipping" the bits, but by simple negation. Yes how can I apply negation to the byte array. Suppose I have an audio that has one voice and then I take inverse signal of that audio and when another voice is added to the original audio I would play the inverse and original together. So I would only hear the third voice. – Hammad Khan Jan 16 '19 at 09:47
  • I use playMp3 function to play the inverse array. – Hammad Khan Jan 16 '19 at 09:48
  • That use case (voice cancellation) makes a lot more sense. However, you should be aware that most [phase](https://en.wikipedia.org/wiki/Phase_(waves)) information is discarded during the .mp3 encoding process. This will likely cause problems for your cancellation approach. Better to use an uncompressed format, like PCM. I recommend you read up on that, and get a very good understanding of PCM vs .mp3 before you attempt to solve this problem. When you do, it should become clear what I mean by "negation". – greeble31 Jan 16 '19 at 15:31
  • I have done research on PCM vs mp3. And I have applied the negation. I have edited my question. The sound I play with inverse it sounds robotic and slow. – Hammad Khan Jan 19 '19 at 11:10

0 Answers0