Advertisement
Guest User

Untitled

a guest
Jul 11th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "portaudio.h"
  3.  
  4. #define SAMPLE_RATE (44100)
  5.  
  6.  
  7. typedef struct
  8. {
  9.     float left_phase;
  10.     float right_phase;
  11. }
  12. paTestData;
  13.  
  14. static int patestCallback( const void *inputBuffer, void *outputBuffer,
  15.                            unsigned long framesPerBuffer,
  16.                            const PaStreamCallbackTimeInfo* timeInfo,
  17.                            PaStreamCallbackFlags statusFlags,
  18.                            void *userData )
  19. {
  20.     /* Cast data passed through stream to our structure. */
  21.     paTestData *data = (paTestData*)userData;
  22.     float *out = (float*)outputBuffer;
  23.     unsigned int i;
  24.     (void) inputBuffer; /* Prevent unused variable warning. */
  25.  
  26.     for( i=0; i<framesPerBuffer; i++ )
  27.     {
  28.         *out++ = data->left_phase;  /* left */
  29.         *out++ = data->right_phase;  /* right */
  30.         /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
  31.         data->left_phase += 0.01f;
  32.         /* When signal reaches top, drop back down. */
  33.         if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f;
  34.         /* higher pitch so we can distinguish left and right. */
  35.         data->right_phase += 0.03f;
  36.         if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f;
  37.     }
  38.     return 0;
  39. }
  40.  
  41.  
  42.     static paTestData data;
  43.  
  44. int main (void) {
  45.     PaStream *stream;
  46.     PaError err;
  47. err = Pa_OpenDefaultStream( &stream,
  48.                                 0,          /* no input channels */
  49.                                 2,          /* stereo output */
  50.                                 paFloat32,  /* 32 bit floating point output */
  51.                                 SAMPLE_RATE,
  52.                                 256,        /* frames per buffer, i.e. the number
  53.                                                    of sample frames that PortAudio will
  54.                                                    request from the callback. Many apps
  55.                                                    may want to use
  56.                                                    paFramesPerBufferUnspecified, which
  57.                                                    tells PortAudio to pick the best,
  58.                                                    possibly changing, buffer size.*/
  59.                                 patestCallback, /* this is your callback function */
  60.                                 &data ); /*This is a pointer that will be passed to
  61.                                                    your callback*/
  62.                              err = Pa_StartStream( stream );
  63.                              if( err != paNoError ) goto error;
  64.  
  65.                              Pa_Sleep(9*1000);
  66.                             err = Pa_StopStream( stream );
  67.                             if( err != paNoError ) goto error;
  68.  
  69.                             err = Pa_CloseStream( stream );
  70.                             if( err != paNoError ) goto error;
  71.  
  72.                             err = Pa_Terminate( );
  73.                             if( err != paNoError ) goto error;
  74.  
  75.                             printf("Test finished.\n");
  76.                             return err;
  77.                         error:
  78.                             Pa_Terminate();
  79.  
  80.                             return err;
  81.  
  82.  
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement