Android: High Pass filter
-
So I am trying o make an application which records some audio and then filters all the high-frequency sounds from it and then in turn play only that high- frequency sounds. I am doing so using a filter class i found online. Now the problem I'm facing is, when i press the play button, nothing is played. Now i can't tell if it is filtering out everything and hence playing nothing or that the output file never gets the filtered part. I am posting my code and the filter class code and would really appreciate if someone could help me out with it. Thanks. MAIN CODE
package abc.com.please;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;public class MainActivity extends AppCompatActivity {
private static MediaRecorder mediaRecorder = new MediaRecorder(); private static MediaPlayer mediaPlayer; private static String audioFilePath; private static Button stopButton; private static Button playButton; private static Button recordButton; EditText box1; EditText box2; private boolean isRecording = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity\_main); recordButton = (Button) findViewById(R.id.recordButton); playButton = (Button) findViewById(R.id.playButton); stopButton = (Button) findViewById(R.id.stopButton); box1=(EditText) findViewById(R.id.editText); box2=(EditText) findViewById(R.id.editText2); if (!hasMicrophone()) { stopButton.setEnabled(false); playButton.setEnabled(false); recordButton.setEnabled(false); } else { playButton.setEnabled(false); stopButton.setEnabled(false); } audioFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myaudio.3gp"; recordButton.setOnClickListener(new
-
So I am trying o make an application which records some audio and then filters all the high-frequency sounds from it and then in turn play only that high- frequency sounds. I am doing so using a filter class i found online. Now the problem I'm facing is, when i press the play button, nothing is played. Now i can't tell if it is filtering out everything and hence playing nothing or that the output file never gets the filtered part. I am posting my code and the filter class code and would really appreciate if someone could help me out with it. Thanks. MAIN CODE
package abc.com.please;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;public class MainActivity extends AppCompatActivity {
private static MediaRecorder mediaRecorder = new MediaRecorder(); private static MediaPlayer mediaPlayer; private static String audioFilePath; private static Button stopButton; private static Button playButton; private static Button recordButton; EditText box1; EditText box2; private boolean isRecording = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity\_main); recordButton = (Button) findViewById(R.id.recordButton); playButton = (Button) findViewById(R.id.playButton); stopButton = (Button) findViewById(R.id.stopButton); box1=(EditText) findViewById(R.id.editText); box2=(EditText) findViewById(R.id.editText2); if (!hasMicrophone()) { stopButton.setEnabled(false); playButton.setEnabled(false); recordButton.setEnabled(false); } else { playButton.setEnabled(false); stopButton.setEnabled(false); } audioFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myaudio.3gp"; recordButton.setOnClickListener(new
Himanshu Bhutani wrote:
Now i can't tell if it is filtering out everything and hence playing nothing or that the output file never gets the filtered part.
Have you stepped through the code using your debugger?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
So I am trying o make an application which records some audio and then filters all the high-frequency sounds from it and then in turn play only that high- frequency sounds. I am doing so using a filter class i found online. Now the problem I'm facing is, when i press the play button, nothing is played. Now i can't tell if it is filtering out everything and hence playing nothing or that the output file never gets the filtered part. I am posting my code and the filter class code and would really appreciate if someone could help me out with it. Thanks. MAIN CODE
package abc.com.please;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;public class MainActivity extends AppCompatActivity {
private static MediaRecorder mediaRecorder = new MediaRecorder(); private static MediaPlayer mediaPlayer; private static String audioFilePath; private static Button stopButton; private static Button playButton; private static Button recordButton; EditText box1; EditText box2; private boolean isRecording = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity\_main); recordButton = (Button) findViewById(R.id.recordButton); playButton = (Button) findViewById(R.id.playButton); stopButton = (Button) findViewById(R.id.stopButton); box1=(EditText) findViewById(R.id.editText); box2=(EditText) findViewById(R.id.editText2); if (!hasMicrophone()) { stopButton.setEnabled(false); playButton.setEnabled(false); recordButton.setEnabled(false); } else { playButton.setEnabled(false); stopButton.setEnabled(false); } audioFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myaudio.3gp"; recordButton.setOnClickListener(new
I suggest that you add logging to your application using the default android log utility. This will make your investigation much easier. Using a debugger - as somebody else suggested - is a very valid approach, but where you are looking at real-time effects (which may or may not be relevant) there's no substitute for logging. You can do this as follows: Add this import to your MainActivity and Filter classes:
import android.util.Log;
Then add this static variable as a class variable to each:
private static final String TAG = "Himanshu";
Now add some logging like this:
while ((read = in.read(buff)) > 0) {
Log.i(TAG, "Raw audio: " + read); // *** Nick's suggested log point. ***
out.write(buff, 0, read);
}
out.flush();
byte[] audioBytes = out.toByteArray();
final FloatBuffer fb = ByteBuffer.wrap(audioBytes).asFloatBuffer();
final float[] dst = new float[fb.capacity()];
fb.get(dst);Filter filter = new Filter(15000,44100, Filter.PassType.Highpass,1);
for (int i = 0; i etc etc..In order to view your log you need to use adb. If you work in a windows environment you'd do this in a DOS command shell:
adb logcat Himanshu:* *:s -v time
Issue the above logcat command BEFORE starting your app and triggering the audio record & playback. This command is saying: Capture all log messages (.i, .d, .v, .w, .e etc.. i.e. of category: Information, Debug, Verbose, Warning and Error) and do NOT capture any other log messages, show log timestamps.
I didn't check all of the places in your code where that would also shed light on the problem. It's your code and I'm sure that you can quickly identify these places.
Actually I would have done a couple of other things also before going quite so far as you have:
-
Confirm that you can play audio samples, do this by creating a simple square wave, storing it in memory and squirting it out. You can use PCM at 16 kSamples/sec and a ~200 Hz waveform.
-
Capture recorded audio to a file and check that this is valid audio.
One other thing to mention is that you are using the AMR Narrow Band voice codec. This is not a generic audio encoding algorithm. AMR (NB) has been highly optimised for voice telephony, and as is the case for such codecs. This may be important because
-