Daniel Forsyth
Daniel Forsyth

Reputation: 343

Portaudio Input Buffer is Always Empty

I'm trying to learn about DSP programming to try to create my own guitar effects, and I'm using the Portaudio library to make it happen. The problem I'm running into is that no matter what I do, whenever I read the input buffer in the callback, it's empty. I've tried several of the different portaudio example programs out there and they all do nothing. I've also tried different inputs, from my audio interface to my internal microphone.

Basic callback code I've tried:

#define SAMPLE_RATE         (44100)
#define PA_SAMPLE_TYPE      paFloat32
#define FRAMES_PER_BUFFER   (64)

static int paCallback(  const void *inputBuffer, 
                            void *outputBuffer,
                            unsigned long framesPerBuffer,
                            const PaStreamCallbackTimeInfo* timeInfo,
                            PaStreamCallbackFlags statusFlags,
                            void *userData )
{
    float *out = (float*)outputBuffer;
    const float *in = (const float*)inputBuffer;

    //unused variables
    (void) timeInfo;
    (void) statusFlags;
    (void) userData;

    for (int i = 0; i < FRAMES_PER_BUFFER; i+=2){
        printf("%.1f ", in[i]);
    }
    printf("\r");

    return paContinue;
}

I first tried writing the input buffer to the output buffer to just play back what it was receiving, then tried to just print it to see if it was getting any data at all. It's always 0.0 or -0.0.

Some things that may be relevant:

My code for initializing the stream is:

PaStreamParameters getInputParameters(){
    // input parameters
    PaStreamParameters inPar;
    inPar.device = Pa_GetDefaultInputDevice();// choose_device();

    inPar.channelCount = 2; // stereo
    inPar.sampleFormat = PA_SAMPLE_TYPE;
    inPar.suggestedLatency = Pa_GetDeviceInfo( inPar.device )->defaultLowInputLatency;
    inPar.hostApiSpecificStreamInfo = NULL;

    return inPar;
}

PaError err;
    printf("Initialising\n");
    err = Pa_Initialize();
    if( err != paNoError ) { printf(  "PortAudio error: %s\n", Pa_GetErrorText( err ) ); }

    printf("Getting parameters\n");
    PaStreamParameters inputParameters = getInputParameters();
    PaStreamParameters outputParameters = getOutputParameters();

    printf("Opening stream\n");
    PaStream *stream;
    /* Open an audio I/O stream. */
    err = Pa_OpenStream(    &stream,
                            &inputParameters,
                            &outputParameters,
                            SAMPLE_RATE,
                            FRAMES_PER_BUFFER,
                            0, //paclipoff
                            paCallback,
                            NULL
    );
    if( err != paNoError ) { printf(  "PortAudio error: %s\n", Pa_GetErrorText( err ) ); }

    ... // wait for input, close stream, etc
}

Is there anything wrong with my code or my setup that would cause this to be the case?

Upvotes: 2

Views: 461

Answers (1)

Daniel Forsyth
Daniel Forsyth

Reputation: 343

It looks like for me this issue was being caused by running the app through my IDE. Catalina has strict permissions, and Visual Studio code was not given access to (or prompting for access to) the microphone. When I run the executable directly it works as intended.

Upvotes: 3

Related Questions