Thread: Port Audio

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    Port Audio

    ...is a cross-platform audio API. I'm trying to get to grips with it by executing the following in a Win32 Console App:

    Code:
    /*
     * pa_cosine.c
     * Generate a cosine wave
     *
     * Author: Christopher Dobrian
     *
     * This program uses the PortAudio Portable Audio Library.
     * For more information see: http://www.portaudio.com
     *
     */
    
    #include <stdio.h>
    #include <math.h>
    #include "portaudio.h"
    #include "pa_host.h"
    
    #define NUM_SECONDS   (4)
    #define SAMPLE_RATE   (44100)
    #define BUFFER_SIZE   (256)
    #define TWOPI         (6.283185307179586)
    #define FREQUENCY     1000.
    
    typedef struct {
        float amplitude;
        float frequency;
        float phase;
        unsigned long count;
    } paAudioData;
    
    /* This routine will be called by the PortAudio engine when audio is needed. */
    
    static int cosineCallback( void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData );
    
    static int cosineCallback( void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData )
    {
    
        /* Cast data passed through stream to the format of the local structure. */
        paAudioData *data = (paAudioData*)userData;
        // float *in = (float*)inputBuffer; // input buffer only needed for input
        float *out = (float*)outputBuffer;
    	
        unsigned int i; // just a counter
        float A = data->amplitude;
        float twopiFoverR = TWOPI*data->frequency/SAMPLE_RATE;
        float phase = data->phase;
        float y; // temp variable for output sample
    	
        for( i = 0 ; i < framesPerBuffer ; i++ )
        {
            y = A*cos(twopiFoverR*(data->count++)+phase);
            *out++ = y;		/* left channel */
            *out++ = y;		/* right channel*/
        }
    
        return 0;
    }
    
    /*******************************************************************/
    
    static paAudioData data;
    
    int main(void);
    
    int main(void)
    {
    	PortAudioStream *stream;
    	PaError err;
    	
    	printf("PortAudio: Cosine Wave, %.2f Hz.\n", FREQUENCY);
    
    /* Initialize data for use by callback. */
    	data.amplitude = 0.5;
    	data.frequency = FREQUENCY;
            data.phase = 0.;
    
    /* Initialize library before making any other calls. */
    	err = Pa_Initialize();
    	if( err != paNoError ) goto error;
    
    /* Open an audio I/O stream. */
    	err = Pa_OpenDefaultStream(
    				&stream,
    				0,              /* no input channels */
    				2,              /* stereo output */
    				paFloat32,      /* 32 bit floating point output */
    				SAMPLE_RATE,
    				BUFFER_SIZE,            /* frames per buffer */
    				0,              /* number of buffers, if zero then use default minimum */
    				cosineCallback,
    				&data );
    	if( err != paNoError ) goto error;
    
    	err = Pa_StartStream( stream );
    	if( err != paNoError ) goto error;
    
    /* Sleep for several seconds. */
    	Pa_Sleep(NUM_SECONDS*1000);
    
    	err = Pa_StopStream( stream );
    	if( err != paNoError ) goto error;
    
    	err = Pa_CloseStream( stream );
    	if( err != paNoError ) goto error;
    
    	Pa_Terminate();
    	printf("Calculated %ld samples.\n", data.count);
    	printf("Finished.\n");
    	return err;
    
    error:
    	Pa_Terminate();
    	fprintf( stderr, "An error occured while using the portaudio stream.\n" ); 
    	fprintf( stderr, "Error number: %d\n", err );
    	fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    	return err;
    }
    I've added the necessary files to the project (pa_lib.cpp, pa_host.h and portaudio.h) but I get 19 linker errors - all reporting unresolved externals.

    Does anyone have experience of Port Audio? Or maybe someone can help with these error messages...
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What are the unresolved symbols?

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    All the Port Audio functions (they begin with _pa...).
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I bet the problem is that the Port Audio functions you are calling are not implemented in pa_lib.cpp, and instead are actually implemented in a library of some kind. If that's the case, then you need to link to this library before you can call the functions.

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If the functions all begin with _pa why are you calling them Pa_?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by adrianxw
    If the functions all begin with _pa why are you calling them Pa_?
    When the linker reports them as missing symbols it prefixes an underscore.

    Anyhow, my project supervisor has solved the problem. Cheers anyway.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Did you project manager also tell you to use those gotos because if he did I would recommend you talk to his/her manager.
    Woop?

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think it is a common and valid use of goto. The other alternatives are not as readable or have other drawbacks.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    As mouse said, that is a perfectly acceptable use of goto. The flow of
    Code:
    if(error) goto cleanup;
    is widely used, and very readable. Just take a look at the Linux kernel source, and see how many times this is done.

  10. #10
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    For Your Information: my name is not Christopher Dobrian and I, personally, never ever use gotos.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 03:28 AM
  4. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM