patest_wmme_low_level_latency_params.c (6419B)
1 /* 2 * $Id: $ 3 * Portable Audio I/O Library 4 * Windows MME low level buffer parameters test 5 * 6 * Copyright (c) 2007 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 <windows.h> /* required when using pa_win_wmme.h */ 43 #include <mmsystem.h> /* required when using pa_win_wmme.h */ 44 45 #include "portaudio.h" 46 #include "pa_win_wmme.h" 47 48 #define NUM_SECONDS (6) 49 #define SAMPLE_RATE (44100) 50 51 #define WMME_FRAMES_PER_BUFFER (440) 52 #define WMME_BUFFER_COUNT (6) 53 54 #define FRAMES_PER_BUFFER WMME_FRAMES_PER_BUFFER /* hardwire portaudio callback buffer size to WMME buffer size for this test */ 55 56 #ifndef M_PI 57 #define M_PI (3.14159265) 58 #endif 59 60 #define TABLE_SIZE (2048) 61 62 #define CHANNEL_COUNT (2) 63 64 65 typedef struct 66 { 67 float sine[TABLE_SIZE]; 68 double phase; 69 } 70 paTestData; 71 72 /* This routine will be called by the PortAudio engine when audio is needed. 73 ** It may called at interrupt level on some machines so don't do anything 74 ** that could mess up the system like calling malloc() or free(). 75 */ 76 static int patestCallback( const void *inputBuffer, void *outputBuffer, 77 unsigned long framesPerBuffer, 78 const PaStreamCallbackTimeInfo* timeInfo, 79 PaStreamCallbackFlags statusFlags, 80 void *userData ) 81 { 82 paTestData *data = (paTestData*)userData; 83 float *out = (float*)outputBuffer; 84 unsigned long i,j; 85 86 (void) timeInfo; /* Prevent unused variable warnings. */ 87 (void) statusFlags; 88 (void) inputBuffer; 89 90 for( i=0; i<framesPerBuffer; i++ ) 91 { 92 float x = data->sine[(int)data->phase]; 93 data->phase += 20; 94 if( data->phase >= TABLE_SIZE ){ 95 data->phase -= TABLE_SIZE; 96 } 97 98 for( j = 0; j < CHANNEL_COUNT; ++j ){ 99 *out++ = x; 100 } 101 } 102 103 return paContinue; 104 } 105 106 /*******************************************************************/ 107 int main(int argc, char* argv[]) 108 { 109 PaStreamParameters outputParameters; 110 PaWinMmeStreamInfo wmmeStreamInfo; 111 PaStream *stream; 112 PaError err; 113 paTestData data; 114 int i; 115 int deviceIndex; 116 117 printf("PortAudio Test: output a sine blip on each channel. SR = %d, BufSize = %d, Chans = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT); 118 119 err = Pa_Initialize(); 120 if( err != paNoError ) goto error; 121 122 deviceIndex = Pa_GetHostApiInfo( Pa_HostApiTypeIdToHostApiIndex( paMME ) )->defaultOutputDevice; 123 if( argc == 2 ){ 124 sscanf( argv[1], "%d", &deviceIndex ); 125 } 126 127 printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name ); 128 129 /* initialise sinusoidal wavetable */ 130 for( i=0; i<TABLE_SIZE; i++ ) 131 { 132 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); 133 } 134 135 data.phase = 0; 136 137 outputParameters.device = deviceIndex; 138 outputParameters.channelCount = CHANNEL_COUNT; 139 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point processing */ 140 outputParameters.suggestedLatency = 0; /*Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;*/ 141 outputParameters.hostApiSpecificStreamInfo = NULL; 142 143 wmmeStreamInfo.size = sizeof(PaWinMmeStreamInfo); 144 wmmeStreamInfo.hostApiType = paMME; 145 wmmeStreamInfo.version = 1; 146 wmmeStreamInfo.flags = paWinMmeUseLowLevelLatencyParameters | paWinMmeDontThrottleOverloadedProcessingThread; 147 wmmeStreamInfo.framesPerBuffer = WMME_FRAMES_PER_BUFFER; 148 wmmeStreamInfo.bufferCount = WMME_BUFFER_COUNT; 149 outputParameters.hostApiSpecificStreamInfo = &wmmeStreamInfo; 150 151 152 if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported ){ 153 printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT ); 154 }else{ 155 printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT ); 156 } 157 158 err = Pa_OpenStream( 159 &stream, 160 NULL, /* no input */ 161 &outputParameters, 162 SAMPLE_RATE, 163 FRAMES_PER_BUFFER, 164 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 165 patestCallback, 166 &data ); 167 if( err != paNoError ) goto error; 168 169 err = Pa_StartStream( stream ); 170 if( err != paNoError ) goto error; 171 172 printf("Play for %d seconds.\n", NUM_SECONDS ); 173 Pa_Sleep( NUM_SECONDS * 1000 ); 174 175 err = Pa_StopStream( stream ); 176 if( err != paNoError ) goto error; 177 178 err = Pa_CloseStream( stream ); 179 if( err != paNoError ) goto error; 180 181 Pa_Terminate(); 182 printf("Test finished.\n"); 183 184 return err; 185 error: 186 Pa_Terminate(); 187 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 188 fprintf( stderr, "Error number: %d\n", err ); 189 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 190 return err; 191 } 192