Search code examples
androidandroid-audiorecord

Android real time audio signal processor output does not work


I am currently developing realtime audio DSP app for android... Signal is taken from mic input(on the phone or tablet) processed and then played back....

When my app starts, if mic and headphone jack is pressent, Audio DSP gets intialized.... For the first second there is output from the speakers or headphones, but after that there is just silence and it seems that playback has stopped, but recording still works in background....

I use AudioRecorder class to get RAW audio data and for playback I use AudioTrack.... For now I have the following code(I wish i knew why it is not working) for stream reading from mic and playing back:

public void ProcessAudio(){
    _track = new  AudioTrack(AudioManager.STREAM_MUSIC, _recorder.getSampleRate(),AudioFormat.CHANNEL_OUT_MONO,_recorder.getAudioFormat(), _bufferSize, AudioTrack.MODE_STATIC);
    boolean first=true;

    while(_IsRecording){
        try {
            byte sData[] = new byte[_bufferSize];
            _recorder.read(sData, 0, _bufferSize);
            //TODO:add audio DSP stuff here

            _track.write(sData,0,_bufferSize);
            if(first){
                first = false;
                _track.play();
            }
        }catch (Exception ex){
            Log.e(StaticVars.SOFTWARE_TAG, "Failed:" + ex.getMessage());
        }
    }
    _track.flush();
    _track.stop();
    _track.release();
}

Solution

  • So.... It turns out that i didn't initialize my AudioTrack correctly... once i changed :

    _track = new  AudioTrack(AudioManager.STREAM_MUSIC, _recorder.getSampleRate(),AudioFormat.CHANNEL_CONFIGURATION_MONO,_recorder.getAudioFormat(), _bufferSize, AudioTrack.MODE_STATIC);
    

    to:

    _track = new  AudioTrack(AudioManager.STREAM_MUSIC, _recorder.getSampleRate(),AudioFormat.CHANNEL_CONFIGURATION_MONO,_recorder.getAudioFormat(), _bufferSize, AudioTrack.MODE_STREAM);
    

    it started to work....

    Edit:

    Beacose of latency in Android the realtime audio processing is not plausible atleast on Android 4.4 and down.