0

I recorded an audio file A and saved it as testaudio0.gp Then I recorded an audio file B while audio file A was playing at the same time and saved it as testaudio.gp

Of course, in audio file B I hear audio file A as well.

I use the normal MediaPlayer and MediaRecorder classes in Android. The audio files are of the same length. The file size is 6,81Kb for both.

Here is my Code:

final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/testaudio.3gp");
try {
    recorder.prepare();
} catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}

Uri myUri = Uri.parse("/sdcard/testaudio0.3gp"); // initialize Uri here
final MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
    mediaPlayer.setDataSource(getApplicationContext(), myUri);
} catch (IOException e) {
    e.printStackTrace();
}
try {
    mediaPlayer.prepare();
} catch (IOException e) {
    e.printStackTrace();
}
mediaPlayer.start();




recorder.start();   // Recording is now started
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        recorder.stop();
        recorder.reset();   // You can reuse the object by going back to setAudioSource() step
        recorder.release(); // Now the object cannot be reused
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.release();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "Recording Stopped!", Toast.LENGTH_SHORT).show();
            }
        });

    }
}, 4000);

As you can see I changed recorder.setAudioSource to VOICE_COMMUNICATION since after doing some research on echo cancellation I read that this should help. It seems like it helps a bit, but there is still a very good hearable background audio of file A existing.

I would like to subtract as much as possible of audio file A . I did not find a stack exchange question answering how to do this. Please be patient that I do not understand those complicated mathematical operations to do. A code example would help a lot.

Any help is appreciated. Thank you.

fameman
  • 3,451
  • 1
  • 19
  • 31
  • 1
    What you want requires in depth knowledge of digitial signal processing, especially as it relates to audio data and codecs. This is closer to "write a book" length knowledge than a stack overflow question. – Gabe Sechan Apr 13 '17 at 17:42
  • So It's a bad question because you don't have enough knowledge to help? – fameman Apr 13 '17 at 17:43
  • No, its a bad question because it would take several dozen pages to do the basics. What you want is seriously hard. Its too large a scope for this website. – Gabe Sechan Apr 13 '17 at 17:44
  • Please keep in mind I don't want a book. Instead I am searching for a solution of code. And if someone has this knowledge, he could maybe create a bit of code and explain a bit. That's why I said: I need code. – fameman Apr 13 '17 at 17:45
  • (And there are several others out there who have exact the same question but got formulas etc. instead of code. So if you close this questions, which is only asking for a code example and maybe some explanation instead of a whole book of formulas, they don't get the answer which would be very bad.) – fameman Apr 13 '17 at 17:47
  • First off, you're on the wrong website for that. The purpose of this website is to help others write the code, not to be given code. Secondly a pure code answer would not be considered a good answer on this site- it would require the explanation. So its off topic for here as too broad. – Gabe Sechan Apr 13 '17 at 17:47
  • So please be patient. ;-) – fameman Apr 13 '17 at 17:47
  • Yeah, I need help writing the code. I already started... And I said: Some explanation. – fameman Apr 13 '17 at 17:48
  • Yes, but I understand: Closing a question because it is maybe a little bit too broad is better than answering a question which could help other people too. – fameman Apr 13 '17 at 17:50
  • 1
    What you recorded is not just a+b, but a converted to analog, run through speakers, bounced off the walls, run through a microphone, and converted to digital + b. The result of this will be very different from the original a. Cancelling this out is a whole science. You may get a practical answer on dsp.stackexchange.com, but please check whether your question is on topic there first before posting. – m69's been on strike for years Apr 13 '17 at 19:46
  • Thank you @m69 for being patient and giving these tips. I understand that the sound is being reflected many times and confused. Does it help if I know that it is completely quiet in the room? I already reqad a question about this topic on dsp.stackexchange.com but there were only formulas which I am not able to understanf. Thank you for trying to help me. ;-) – fameman Apr 13 '17 at 22:01
  • Feedback cancellation is part of Voice-over-IP software like Skype. Maybe if you found open-source VoIP software you could check out their feedback cancellation code? – m69's been on strike for years Apr 13 '17 at 22:13

1 Answers1

1

To subtract one audio file A from another audio file B where B contains new signal C together with a playback of file A is conceptually simple ... ignoring all the artifacts of signal A bouncing off walls etc as found in file B ... simply take the negative of audio curve A and overlay it atop audio curve B ... signal A will get erased from audio B leaving you with just the additional signal C

I believe you can do this using the audio workstation Audacity ... yet its not too hard to write custom code to perform the same transformation ... in pseudo code we do this below

For simplicity lets consider signal A to be a sin curve starting at zero radians ... we have all seen this sin curve ... if we synthesize this signal with radians varying from 0 to 2*pi it will generate one cycle of the sin wave ... enough for below demonstration ... now if we simultaneously synthesize the negative of signal A which will happen if we use a phase shift of pi radians this negative curve will be the exact opposite shape

for x = 0; x < 2*pi; x += 2*pi/100 {

     A = sin(x)       // your original data of file A
  negA = sin(x + pi)  //  if you plot this negA will == A * (-1)

  new_signal = cos(5*x) // your new signal which go into file B

     B = A + new_signal // your audio data of file B

     C = B + negA  # this is same as following C = B - A == new_signal
     C = B - A     #  desired output : (signal A + new signal) minus A
                   #  this will result in simply new signal which is your goal
                   #   do you now see that C = new_signal
}
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104