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_sine_srate.c (6396B)


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