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.