Search code examples
javaandroidandroid-audiorecordpuredatalibpd

Not able to get audio input for simple microphone app


I am in the process of learning out to use libpd with Android and have ran into a problem. I wanted to see if I could get a simple microphone app working. Just an ~adc -> bpfilter -> *2 -> ~dac. I verified that the patched worked with Pure Data and MobMuPlat.

I wrote over the example program "Circle of Fifths" to make sure libpd was included properly. When I modified it using a tutorial to run my own patch, I was unable to get input from the phone's microphone. The following line seemed to be the issue.

PdAudio.initAudio(sampleRate, inpch, 2, 8, true);    

If I have the input channels to 0, the app will open but obviously no sound will come out.This is unless I change the patch to just play a tone and set inpch to 0. When the input channels are set inpch to 1,2, or AudioParameters.suggestInputChannels();the application will not open.

I have also tried small sample rates but I had the same issue. Any ideas?

Here is the full main activity:

/**
 * 
 * @author Peter Brinkmann ([email protected])
 * 
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 * 
 */

package org.puredata.android.fifths;

import java.io.File;
import java.io.IOException;

import org.puredata.android.io.AudioParameters;
import org.puredata.android.io.PdAudio;
import org.puredata.android.utils.PdUiDispatcher;
import org.puredata.core.PdBase;
import org.puredata.core.utils.IoUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioGroup;
import android.widget.Toast;

public class CircleOfFifths extends Activity {

    private static final String TAG = "GuitarTuner";
    private PdUiDispatcher dispatcher;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initGui();
        try {
            initPd();
            loadPatch();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
            finish();
        }
    }

    private void initGui() {
        setContentView(R.layout.main);
    }

    private void initPd() throws IOException {
        // Configure the audio glue
        // int sampleRate = AudioParameters.suggestSampleRate();
        int sampleRate = 64;

        int inpch = AudioParameters.suggestInputChannels();
        PdAudio.initAudio(sampleRate, inpch, 2, 8, true);
        // Create and install the dispatcher dispatcher = new PdUiDispatcher();
        // PdBase.setReceiver(dispatcher);
    }

    private void loadPatch() throws IOException {
        File dir = getFilesDir();
        IoUtils.extractZipResource(getResources().openRawResource(R.raw.patch),
                dir, true);
        File patchFile = new File(dir, "microphone.pd");
        PdBase.openPatch(patchFile.getAbsolutePath());
        PdAudio.startAudio(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        PdAudio.startAudio(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        PdAudio.stopAudio();
    }
}

Solution

  • You need a permission for recording audio. Try adding

    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    

    to your manifest.