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

patest1.c (7095B)


      1 /** @file patest1.c
      2 	@ingroup test_src
      3 	@brief Ring modulate the audio input with a sine wave for 20 seconds.
      4 	@author Ross Bencina <rossb@audiomulch.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 <math.h>
     46 #include "portaudio.h"
     47 
     48 #ifndef M_PI
     49 #define M_PI (3.14159265)
     50 #endif
     51 
     52 #define SAMPLE_RATE (44100)
     53 
     54 typedef struct
     55 {
     56     float sine[100];
     57     int phase;
     58     int sampsToGo;
     59 }
     60 patest1data;
     61 
     62 static int patest1Callback( const void *inputBuffer, void *outputBuffer,
     63                             unsigned long framesPerBuffer,
     64                             const PaStreamCallbackTimeInfo* timeInfo,
     65                             PaStreamCallbackFlags statusFlags,
     66                             void *userData )
     67 {
     68     patest1data *data = (patest1data*)userData;
     69     float *in = (float*)inputBuffer;
     70     float *out = (float*)outputBuffer;
     71     int framesToCalc = framesPerBuffer;
     72     unsigned long i = 0;
     73     int finished;
     74 
     75     if( data->sampsToGo < framesPerBuffer )
     76     {
     77         framesToCalc = data->sampsToGo;
     78         finished = paComplete;
     79     }
     80     else
     81     {
     82         finished = paContinue;
     83     }
     84 
     85     for( ; i<framesToCalc; i++ )
     86     {
     87         *out++ = *in++ * data->sine[data->phase];  /* left */
     88         *out++ = *in++ * data->sine[data->phase++];  /* right */
     89         if( data->phase >= 100 )
     90             data->phase = 0;
     91     }
     92 
     93     data->sampsToGo -= framesToCalc;
     94 
     95     /* zero remainder of final buffer if not already done */
     96     for( ; i<framesPerBuffer; i++ )
     97     {
     98         *out++ = 0; /* left */
     99         *out++ = 0; /* right */
    100     }
    101     
    102     return finished;
    103 }
    104 
    105 int main(int argc, char* argv[]);
    106 int main(int argc, char* argv[])
    107 {
    108     PaStream                *stream;
    109     PaError                 err;
    110     patest1data             data;
    111     int                     i;
    112     PaStreamParameters      inputParameters, outputParameters;
    113     const PaHostErrorInfo*  herr;
    114 
    115     printf("patest1.c\n"); fflush(stdout);
    116     printf("Ring modulate input for 20 seconds.\n"); fflush(stdout);
    117     
    118     /* initialise sinusoidal wavetable */
    119     for( i=0; i<100; i++ )
    120         data.sine[i] = sin( ((double)i/100.) * M_PI * 2. );
    121     data.phase = 0;
    122     data.sampsToGo = SAMPLE_RATE * 20;        /* 20 seconds. */
    123 
    124     /* initialise portaudio subsytem */
    125     err = Pa_Initialize();
    126 
    127     inputParameters.device = Pa_GetDefaultInputDevice();    /* default input device */
    128     if (inputParameters.device == paNoDevice) {
    129       fprintf(stderr,"Error: No input default device.\n");
    130       goto done;
    131     }
    132     inputParameters.channelCount = 2;                       /* stereo input */
    133     inputParameters.sampleFormat = paFloat32;               /* 32 bit floating point input */
    134     inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
    135     inputParameters.hostApiSpecificStreamInfo = NULL;
    136 
    137     outputParameters.device = Pa_GetDefaultOutputDevice();  /* default output device */
    138     if (outputParameters.device == paNoDevice) {
    139       fprintf(stderr,"Error: No default output device.\n");
    140       goto done;
    141     }
    142     outputParameters.channelCount = 2;                      /* stereo output */
    143     outputParameters.sampleFormat = paFloat32;              /* 32 bit floating point output */
    144     outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
    145     outputParameters.hostApiSpecificStreamInfo = NULL;
    146 
    147     err = Pa_OpenStream(
    148                         &stream,
    149                         &inputParameters,
    150                         &outputParameters,
    151                         (double)SAMPLE_RATE, /* Samplerate in Hertz. */
    152                         512,                 /* Small buffers */
    153                         paClipOff,           /* We won't output out of range samples so don't bother clipping them. */
    154                         patest1Callback,
    155                         &data );
    156     if( err != paNoError ) goto done;
    157 
    158     err = Pa_StartStream( stream );
    159     if( err != paNoError ) goto done;
    160     
    161     printf( "Press any key to end.\n" ); fflush(stdout);
    162          
    163     getc( stdin ); /* wait for input before exiting */
    164 
    165     err = Pa_AbortStream( stream );
    166     if( err != paNoError ) goto done;
    167     
    168     printf( "Waiting for stream to complete...\n" );
    169 
    170     /* sleep until playback has finished */
    171     while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(1000);
    172     if( err < 0 ) goto done;
    173 
    174     err = Pa_CloseStream( stream );
    175     if( err != paNoError ) goto done;
    176 
    177 done:
    178     Pa_Terminate();
    179 
    180     if( err != paNoError )
    181     {
    182         fprintf( stderr, "An error occured while using portaudio\n" );
    183         if( err == paUnanticipatedHostError )
    184         {
    185             fprintf( stderr, " unanticipated host error.\n");
    186             herr = Pa_GetLastHostErrorInfo();
    187             if (herr)
    188             {
    189                 fprintf( stderr, " Error number: %ld\n", herr->errorCode );
    190                 if (herr->errorText)
    191                     fprintf( stderr, " Error text: %s\n", herr->errorText );
    192             }
    193             else
    194                 fprintf( stderr, " Pa_GetLastHostErrorInfo() failed!\n" );
    195         }
    196         else
    197         {
    198             fprintf( stderr, " Error number: %d\n", err );
    199             fprintf( stderr, " Error text: %s\n", Pa_GetErrorText( err ) );
    200         }
    201 
    202         err = 1;          /* Always return 0 or 1, but no other return codes. */
    203     }
    204 
    205     printf( "bye\n" );
    206 
    207     return err;
    208 }