Port Audio

This is a discussion on Port Audio within the Windows Programming forums, part of the Platform Specific Boards category; ...is a cross-platform audio API. I'm trying to get to grips with it by executing the following in a Win32 ...

  1. #1
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368

    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...
    MSVC++ 6.0

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

  3. #3
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    All the Port Audio functions (they begin with _pa...).
    MSVC++ 6.0

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,254
    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,831
    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
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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.
    MSVC++ 6.0

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    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,254
    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
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    For Your Information: my name is not Christopher Dobrian and I, personally, never ever use gotos.
    MSVC++ 6.0

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, 02:58 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  3. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21