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_timing.c (6298B)


      1 /** @file patest_timing.c
      2 	@ingroup test_src
      3 	@brief Play a sine wave for several seconds, and spits out a ton of timing info while it's at it. Based on patest_sine.c
      4 	@author Bjorn Roche
      5 	@author Ross Bencina <rossb@audiomulch.com>
      6     @author Phil Burk <philburk@softsynth.com>
      7 */
      8 /*
      9  * $Id: patest_timing.c 578 2003-09-02 04:17:38Z rossbencina $
     10  *
     11  * This program uses the PortAudio Portable Audio Library.
     12  * For more information see: http://www.portaudio.com/
     13  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
     14  *
     15  * Permission is hereby granted, free of charge, to any person obtaining
     16  * a copy of this software and associated documentation files
     17  * (the "Software"), to deal in the Software without restriction,
     18  * including without limitation the rights to use, copy, modify, merge,
     19  * publish, distribute, sublicense, and/or sell copies of the Software,
     20  * and to permit persons to whom the Software is furnished to do so,
     21  * subject to the following conditions:
     22  *
     23  * The above copyright notice and this permission notice shall be
     24  * included in all copies or substantial portions of the Software.
     25  *
     26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     29  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
     30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
     31  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     32  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     33  */
     34 
     35 /*
     36  * The text above constitutes the entire PortAudio license; however, 
     37  * the PortAudio community also makes the following non-binding requests:
     38  *
     39  * Any person wishing to distribute modifications to the Software is
     40  * requested to send the modifications to the original developer so that
     41  * they can be incorporated into the canonical version. It is also 
     42  * requested that these non-binding requests be included along with the 
     43  * license above.
     44  */
     45 
     46 #include <stdio.h>
     47 #include <math.h>
     48 #include "portaudio.h"
     49 
     50 #define NUM_SECONDS   (5)
     51 #define SAMPLE_RATE   (44100)
     52 #define FRAMES_PER_BUFFER  (64)
     53 
     54 #ifndef M_PI
     55 #define M_PI  (3.14159265)
     56 #endif
     57 
     58 #define TABLE_SIZE   (200)
     59 typedef struct
     60 {
     61     PaStream *stream;
     62     PaTime start;
     63     float sine[TABLE_SIZE];
     64     int left_phase;
     65     int right_phase;
     66 }
     67 paTestData;
     68 
     69 /* This routine will be called by the PortAudio engine when audio is needed.
     70 ** It may called at interrupt level on some machines so don't do anything
     71 ** that could mess up the system like calling malloc() or free().
     72 */
     73 static int patestCallback( const void *inputBuffer, void *outputBuffer,
     74                             unsigned long framesPerBuffer,
     75                             const PaStreamCallbackTimeInfo* timeInfo,
     76                             PaStreamCallbackFlags statusFlags,
     77                             void *userData )
     78 {
     79     paTestData *data = (paTestData*)userData;
     80     float *out = (float*)outputBuffer;
     81     unsigned long i;
     82 
     83     (void) timeInfo; /* Prevent unused variable warnings. */
     84     (void) statusFlags;
     85     (void) inputBuffer;
     86 
     87     printf( "Timing info given to callback: Adc: %g, Current: %g, Dac: %g\n",
     88             timeInfo->inputBufferAdcTime,
     89             timeInfo->currentTime,
     90             timeInfo->outputBufferDacTime );
     91 
     92     printf( "getStreamTime() returns: %g\n", Pa_GetStreamTime(data->stream) - data->start );
     93     
     94     for( i=0; i<framesPerBuffer; i++ )
     95     {
     96         *out++ = data->sine[data->left_phase];  /* left */
     97         *out++ = data->sine[data->right_phase];  /* right */
     98         data->left_phase += 1;
     99         if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
    100         data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
    101         if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
    102     }
    103     
    104     return paContinue;
    105 }
    106 
    107 /*******************************************************************/
    108 int main(void);
    109 int main(void)
    110 {
    111     PaStreamParameters outputParameters;
    112     PaStream *stream;
    113     PaError err;
    114     paTestData data;
    115     int i;
    116 
    117     
    118     printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
    119     
    120     /* initialise sinusoidal wavetable */
    121     for( i=0; i<TABLE_SIZE; i++ )
    122     {
    123         data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
    124     }
    125     data.left_phase = data.right_phase = 0;
    126     
    127     err = Pa_Initialize();
    128     if( err != paNoError ) goto error;
    129 
    130     outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
    131     outputParameters.channelCount = 2;       /* stereo output */
    132     outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
    133     outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
    134     outputParameters.hostApiSpecificStreamInfo = NULL;
    135 
    136     err = Pa_OpenStream(
    137               &stream,
    138               NULL, /* no input */
    139               &outputParameters,
    140               SAMPLE_RATE,
    141               FRAMES_PER_BUFFER,
    142               paClipOff,      /* we won't output out of range samples so don't bother clipping them */
    143               patestCallback,
    144               &data );
    145     data.stream = stream;
    146     data.start = Pa_GetStreamTime(stream);
    147     if( err != paNoError ) goto error;
    148 
    149     err = Pa_StartStream( stream );
    150     data.start = Pa_GetStreamTime(stream);
    151     if( err != paNoError ) goto error;
    152 
    153     printf("Play for %d seconds.\n", NUM_SECONDS );
    154     Pa_Sleep( NUM_SECONDS * 1000 );
    155 
    156     err = Pa_StopStream( stream );
    157     if( err != paNoError ) goto error;
    158 
    159     err = Pa_CloseStream( stream );
    160     if( err != paNoError ) goto error;
    161 
    162     Pa_Terminate();
    163     printf("Test finished.\n");
    164     printf("The tone should have been heard for about 5 seconds and all the timing info above should report that about 5 seconds elapsed (except Adc, which is undefined since there was no input device opened).\n");
    165     
    166     return err;
    167 error:
    168     Pa_Terminate();
    169     fprintf( stderr, "An error occured while using the portaudio stream\n" );
    170     fprintf( stderr, "Error number: %d\n", err );
    171     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    172     return err;
    173 }