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_dsound_low_level_latency_params.c (6141B)


      1 /*
      2  * $Id: $
      3  * Portable Audio I/O Library
      4  * Windows DirectSound low level buffer parameters test
      5  *
      6  * Copyright (c) 2011 Ross Bencina
      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 #include <stdio.h>
     40 #include <math.h>
     41 
     42 #include "portaudio.h"
     43 #include "pa_win_ds.h"
     44 
     45 #define NUM_SECONDS         (6)
     46 #define SAMPLE_RATE         (44100)
     47 
     48 #define DSOUND_FRAMES_PER_HOST_BUFFER  (256*2) //(440*10)
     49 
     50 #define FRAMES_PER_BUFFER   256
     51 
     52 #ifndef M_PI
     53 #define M_PI  (3.14159265)
     54 #endif
     55 
     56 #define TABLE_SIZE          (2048)
     57 
     58 #define CHANNEL_COUNT       (2)
     59 
     60 
     61 typedef struct
     62 {
     63     float sine[TABLE_SIZE];
     64 	double phase;
     65 }
     66 paTestData;
     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 patestCallback( 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     float *out = (float*)outputBuffer;
     80     unsigned long i,j;
     81 
     82     (void) timeInfo; /* Prevent unused variable warnings. */
     83     (void) statusFlags;
     84     (void) inputBuffer;
     85     
     86     for( i=0; i<framesPerBuffer; i++ )
     87     {
     88         float x = data->sine[(int)data->phase];
     89         data->phase += 20;
     90         if( data->phase >= TABLE_SIZE ){
     91 			data->phase -= TABLE_SIZE;
     92 		}
     93 
     94 		for( j = 0; j < CHANNEL_COUNT; ++j ){
     95             *out++ = x;
     96 		}
     97 	}
     98     
     99     return paContinue;
    100 }
    101 
    102 /*******************************************************************/
    103 int main(int argc, char* argv[])
    104 {
    105     PaStreamParameters outputParameters;
    106     PaWinDirectSoundStreamInfo dsoundStreamInfo;
    107     PaStream *stream;
    108     PaError err;
    109     paTestData data;
    110     int i;
    111     int deviceIndex;
    112 
    113     printf("PortAudio Test: output a sine blip on each channel. SR = %d, BufSize = %d, Chans = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT);
    114 
    115     err = Pa_Initialize();
    116     if( err != paNoError ) goto error;
    117 
    118 	deviceIndex = Pa_GetHostApiInfo( Pa_HostApiTypeIdToHostApiIndex( paDirectSound ) )->defaultOutputDevice;
    119 	if( argc == 2 ){
    120 		sscanf( argv[1], "%d", &deviceIndex );
    121 	}
    122 
    123 	printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name );
    124 
    125     /* initialise sinusoidal wavetable */
    126     for( i=0; i<TABLE_SIZE; i++ )
    127     {
    128         data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
    129     }
    130 
    131 	data.phase = 0;
    132 
    133     outputParameters.device = deviceIndex;
    134     outputParameters.channelCount = CHANNEL_COUNT;
    135     outputParameters.sampleFormat = paFloat32; /* 32 bit floating point processing */
    136     outputParameters.suggestedLatency = 0; /*Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;*/
    137     outputParameters.hostApiSpecificStreamInfo = NULL;
    138 
    139     dsoundStreamInfo.size = sizeof(PaWinDirectSoundStreamInfo);
    140     dsoundStreamInfo.hostApiType = paDirectSound; 
    141     dsoundStreamInfo.version = 2;
    142     dsoundStreamInfo.flags = paWinDirectSoundUseLowLevelLatencyParameters;
    143     dsoundStreamInfo.framesPerBuffer = DSOUND_FRAMES_PER_HOST_BUFFER;
    144     outputParameters.hostApiSpecificStreamInfo = &dsoundStreamInfo;
    145    
    146 
    147 	if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported  ){
    148 		printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT );
    149 	}else{
    150 		printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT );
    151 	}
    152 
    153     err = Pa_OpenStream(
    154               &stream,
    155               NULL, /* no input */
    156               &outputParameters,
    157               SAMPLE_RATE,
    158               FRAMES_PER_BUFFER,
    159               paClipOff,      /* we won't output out of range samples so don't bother clipping them */
    160               patestCallback,
    161               &data );
    162     if( err != paNoError ) goto error;
    163 
    164     err = Pa_StartStream( stream );
    165     if( err != paNoError ) goto error;
    166 
    167     printf("Play for %d seconds.\n", NUM_SECONDS );
    168     Pa_Sleep( NUM_SECONDS * 1000 );
    169 
    170     err = Pa_StopStream( stream );
    171     if( err != paNoError ) goto error;
    172 
    173     err = Pa_CloseStream( stream );
    174     if( err != paNoError ) goto error;
    175 
    176     Pa_Terminate();
    177     printf("Test finished.\n");
    178     
    179     return err;
    180 error:
    181     Pa_Terminate();
    182     fprintf( stderr, "An error occured while using the portaudio stream\n" );
    183     fprintf( stderr, "Error number: %d\n", err );
    184     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
    185     return err;
    186 }
    187