gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

patest_buffer.c (7284B)


      1 /** @file patest_buffer.c
      2 	@ingroup test_src
      3 	@brief Test opening streams with different buffer sizes.
      4 	@author Phil Burk  http://www.softsynth.com
      5 */
      6 /*
      7  * $Id$
      8  *
      9  * This program uses the PortAudio Portable Audio Library.
     10  * For more information see: http://www.portaudio.com
     11  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
     12  *
     13  * Permission is hereby granted, free of charge, to any person obtaining
     14  * a copy of this software and associated documentation files
     15  * (the "Software"), to deal in the Software without restriction,
     16  * including without limitation the rights to use, copy, modify, merge,
     17  * publish, distribute, sublicense, and/or sell copies of the Software,
     18  * and to permit persons to whom the Software is furnished to do so,
     19  * subject to the following conditions:
     20  *
     21  * The above copyright notice and this permission notice shall be
     22  * included in all copies or substantial portions of the Software.
     23  *
     24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     27  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
     28  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
     29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     31  */
     32 
     33 /*
     34  * The text above constitutes the entire PortAudio license; however, 
     35  * the PortAudio community also makes the following non-binding requests:
     36  *
     37  * Any person wishing to distribute modifications to the Software is
     38  * requested to send the modifications to the original developer so that
     39  * they can be incorporated into the canonical version. It is also 
     40  * requested that these non-binding requests be included along with the 
     41  * license above.
     42  */
     43 
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <math.h>
     47 #include "portaudio.h"
     48 #define NUM_SECONDS   (3)
     49 #define SAMPLE_RATE   (44100)
     50 #ifndef M_PI
     51 #define M_PI  (3.14159265)
     52 #endif
     53 #define TABLE_SIZE   (200)
     54 
     55 #define BUFFER_TABLE  14
     56 long buffer_table[] = {paFramesPerBufferUnspecified,16,32,64,128,200,256,500,512,600,723,1000,1024,2345};
     57 
     58 typedef struct
     59 {
     60     short sine[TABLE_SIZE];
     61     int left_phase;
     62     int right_phase;
     63     unsigned int sampsToGo;
     64 }
     65 paTestData;
     66 PaError TestOnce( int buffersize, PaDeviceIndex );
     67 
     68 /* This routine will be called by the PortAudio engine when audio is needed.
     69 ** It may called at interrupt level on some machines so don't do anything
     70 ** that could mess up the system like calling malloc() or free().
     71 */
     72 static int patest1Callback( const void *inputBuffer, void *outputBuffer,
     73                             unsigned long framesPerBuffer,
     74                             const PaStreamCallbackTimeInfo* timeInfo,
     75                             PaStreamCallbackFlags statusFlags,
     76                             void *userData )
     77 {
     78     paTestData *data = (paTestData*)userData;
     79     short *out = (short*)outputBuffer;
     80     unsigned int i;
     81     int finished = 0;
     82     (void) inputBuffer; /* Prevent "unused variable" warnings. */
     83 
     84     if( data->sampsToGo < framesPerBuffer )
     85     {
     86         /* final buffer... */
     87 
     88         for( i=0; i<data->sampsToGo; i++ )
     89         {
     90             *out++ = data->sine[data->left_phase];  /* left */
     91             *out++ = data->sine[data->right_phase];  /* right */
     92             data->left_phase += 1;
     93             if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
     94             data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
     95             if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
     96         }
     97         /* zero remainder of final buffer */
     98         for( ; i<framesPerBuffer; i++ )
     99         {
    100             *out++ = 0; /* left */
    101             *out++ = 0; /* right */
    102         }
    103 
    104         finished = 1;
    105     }
    106     else
    107     {
    108         for( i=0; i<framesPerBuffer; i++ )
    109         {
    110             *out++ = data->sine[data->left_phase];  /* left */
    111             *out++ = data->sine[data->right_phase];  /* right */
    112             data->left_phase += 1;
    113             if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
    114             data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
    115             if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
    116         }
    117         data->sampsToGo -= framesPerBuffer;
    118     }
    119     return finished;
    120 }
    121 
    122 /*******************************************************************/
    123 int main(int argc, char **args);
    124 int main(int argc, char **args)
    125 {
    126     int i;
    127     int device = -1;
    128     PaError err;
    129     printf("Test opening streams with different buffer sizes\n");
    130     if( argc > 1 ) {
    131        device=atoi( args[1] );
    132        printf("Using device number %d.\n\n", device );
    133     } else {
    134        printf("Using default device.\n\n" );
    135     }
    136 
    137     for (i = 0 ; i < BUFFER_TABLE; i++)
    138     {
    139         printf("Buffer size %ld\n", buffer_table[i]);
    140         err = TestOnce(buffer_table[i], device);
    141         if( err < 0 ) return 0;
    142 
    143     }
    144     return 0;
    145 }
    146 
    147 
    148 PaError TestOnce( int buffersize, PaDeviceIndex device )
    149 {
    150     PaStreamParameters outputParameters;
    151     PaStream *stream;
    152     PaError err;
    153     paTestData data;
    154     int i;
    155     int totalSamps;
    156     /* initialise sinusoidal wavetable */
    157     for( i=0; i<TABLE_SIZE; i++ )
    158     {
    159         data.sine[i] = (short) (32767.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
    160     }
    161     data.left_phase = data.right_phase = 0;
    162     data.sampsToGo = totalSamps =  NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
    163     err = Pa_Initialize();
    164     if( err != paNoError ) goto error;
    165     
    166     if( device == -1 )
    167         outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    168     else
    169         outputParameters.device = device ;
    170 
    171     if (outputParameters.device == paNoDevice) {
    172       fprintf(stderr,"Error: No default output device.\n");
    173       goto error;
    174     }
    175 
    176     outputParameters.channelCount = 2;                      /* stereo output */
    177     outputParameters.sampleFormat = paInt16;                /* 32 bit floating point output */
    178     outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
    179     outputParameters.hostApiSpecificStreamInfo = NULL;
    180     err = Pa_OpenStream(
    181               &stream,
    182               NULL,                         /* no input */
    183               &outputParameters,
    184               SAMPLE_RATE,
    185               buffersize,                   /* frames per buffer */
    186               (paClipOff | paDitherOff),
    187               patest1Callback,
    188               &data );
    189     if( err != paNoError ) goto error;
    190 
    191     err = Pa_StartStream( stream );
    192     if( err != paNoError ) goto error;
    193     printf("Waiting for sound to finish.\n");
    194     Pa_Sleep(1000*NUM_SECONDS);
    195     err = Pa_CloseStream( stream );
    196     if( err != paNoError ) goto error;
    197     Pa_Terminate();
    198     return paNoError;
    199 error:
    200     Pa_Terminate();
    201     fprintf( stderr, "An error occured while using the portaudio stream\n" );
    202     fprintf( stderr, "Error number: %d\n", err );
    203     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    204     fprintf( stderr, "Host Error message: %s\n", Pa_GetLastHostErrorInfo()->errorText );
    205     return err;
    206 }