int selectDevice() { int i, numDevices, defaultDisplayed; const PaDeviceInfo *deviceInfo; PaStreamParameters inputParameters, outputParameters; PaStream* stream; PaError err; paTestData data; int totalFrames; int numSamples; int numBytes; char devSelection[4]; int devSelectInt; err = Pa_Initialize(); if( err != paNoError ) { printf( "ERROR: Pa_Initialize returned 0x%x\n", err ); goto error; } printf( "PortAudio version: 0x%08X\n", Pa_GetVersion()); printf( "Version text: '%s'\n", Pa_GetVersionInfo()->versionText ); numDevices = Pa_GetDeviceCount(); if( numDevices < 0 ) { printf( "ERROR: Pa_GetDeviceCount returned 0x%x\n", numDevices ); err = numDevices; goto error; } printf( "Number of devices = %d\n", numDevices ); for( i=0; i<numDevices; i++ ) { deviceInfo = Pa_GetDeviceInfo( i ); printf( "--------------------------------------- device #%d\n", i ); /* Mark global and API specific default devices */ defaultDisplayed = 0; if( i == Pa_GetDefaultInputDevice() ) { printf( "[ Default Input" ); defaultDisplayed = 1; } else if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultInputDevice ) { const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); printf( "[ Default %s Input", hostInfo->name ); defaultDisplayed = 1; } if( i == Pa_GetDefaultOutputDevice() ) { printf( (defaultDisplayed ? "," : "[") ); printf( " Default Output" ); defaultDisplayed = 1; } else if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultOutputDevice ) { const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); printf( (defaultDisplayed ? "," : "[") ); printf( " Default %s Output", hostInfo->name ); defaultDisplayed = 1; } if( defaultDisplayed ) printf( " ]\n" ); /* print device info fields */ #ifdef WIN32 { /* Use wide char on windows, so we can show UTF-8 encoded device names */ wchar_t wideName[MAX_PATH]; MultiByteToWideChar(CP_UTF8, 0, deviceInfo->name, -1, wideName, MAX_PATH-1); wprintf( L"Name = %s\n", wideName ); } #else printf( "Name = %s\n", deviceInfo->name ); #endif printf( "Host API = %s\n", Pa_GetHostApiInfo( deviceInfo->hostApi )->name ); printf( "Max inputs = %d", deviceInfo->maxInputChannels ); printf( ", Max outputs = %d\n", deviceInfo->maxOutputChannels ); printf( "Default low input latency = %8.4f\n", deviceInfo->defaultLowInputLatency ); printf( "Default low output latency = %8.4f\n", deviceInfo->defaultLowOutputLatency ); printf( "Default high input latency = %8.4f\n", deviceInfo->defaultHighInputLatency ); printf( "Default high output latency = %8.4f\n", deviceInfo->defaultHighOutputLatency ); #ifdef WIN32 #if PA_USE_ASIO /* ASIO specific latency information */ if( Pa_GetHostApiInfo( deviceInfo->hostApi )->type == paASIO ){ long minLatency, maxLatency, preferredLatency, granularity; err = PaAsio_GetAvailableLatencyValues( i, &minLatency, &maxLatency, &preferredLatency, &granularity ); printf( "ASIO minimum buffer size = %ld\n", minLatency ); printf( "ASIO maximum buffer size = %ld\n", maxLatency ); printf( "ASIO preferred buffer size = %ld\n", preferredLatency ); if( granularity == -1 ) printf( "ASIO buffer granularity = power of 2\n" ); else printf( "ASIO buffer granularity = %ld\n", granularity ); } #endif /* PA_USE_ASIO */ #endif /* WIN32 */ printf( "Default sample rate = %8.2f\n", deviceInfo->defaultSampleRate ); /* poll for standard sample rates */ inputParameters.device = i; inputParameters.channelCount = deviceInfo->maxInputChannels; inputParameters.sampleFormat = paInt16; inputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */ inputParameters.hostApiSpecificStreamInfo = NULL; outputParameters.device = i; outputParameters.channelCount = deviceInfo->maxOutputChannels; outputParameters.sampleFormat = paInt16; outputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */ outputParameters.hostApiSpecificStreamInfo = NULL; if( inputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for half-duplex 16 bit %d channel input = \n", inputParameters.channelCount ); PrintSupportedStandardSampleRates( &inputParameters, NULL ); } if( outputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for half-duplex 16 bit %d channel output = \n", outputParameters.channelCount ); PrintSupportedStandardSampleRates( NULL, &outputParameters ); } if( inputParameters.channelCount > 0 && outputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for full-duplex 16 bit %d channel input, %d channel output = \n", inputParameters.channelCount, outputParameters.channelCount ); PrintSupportedStandardSampleRates( &inputParameters, &outputParameters ); } } /* for numDevices */ printf("What input device would you like to use? "); fgets(devSelection, 4, stdin); devSelectInt = atoi(devSelection); if(devSelectInt < numDevices) { printf("Hooray! You chose device %s, which is within bounds.\n", devSelection); inputParameters.device = devSelectInt; if(inputParameters.device == paNoDevice) { fprintf(stderr,"Error: input device at that device number.\n"); goto error; } // if - no device data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */ data.frameIndex = 0; numSamples = totalFrames * NUM_CHANNELS; numBytes = numSamples * sizeof(SAMPLE); data.recordedSamples = (SAMPLE *) malloc( numBytes ); /* From now on, recordedSamples is initialised. */ if( data.recordedSamples == NULL ) { printf("Could not allocate record array.\n"); goto error; } for( i=0; i<numSamples; i++ ) data.recordedSamples[i] = 0; inputParameters.channelCount = 2; /* stereo input */ inputParameters.sampleFormat = PA_SAMPLE_TYPE; inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; inputParameters.hostApiSpecificStreamInfo = NULL; /* Record some audio. -------------------------------------------- */ err = Pa_OpenStream( &stream, &inputParameters, NULL, /* &outputParameters, */ SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, /* we won't output out of range samples so don't bother clipping them */ recordCallback, &data ); if( err != paNoError ) goto error; err = Pa_StartStream( stream ); if( err != paNoError ) goto error; printf("\n=== Now recording!! Please speak into the microphone. ===\n"); fflush(stdout); while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) { Pa_Sleep(100); printf("index = %d; amplitude = %f\n", data.frameIndex, data.recordedSamples[data.frameIndex] ); fflush(stdout); } if( err < 0 ) goto error; err = Pa_CloseStream( stream ); if( err != paNoError ) goto error; } // if - device number within bounds Pa_Terminate(); printf("----------------------------------------------\n"); return 0; error: Pa_Terminate(); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; } /* selectDevice */
int main(void) { int i, numDevices, defaultDisplayed; const PaDeviceInfo *deviceInfo; PaStreamParameters inputParameters, outputParameters; PaError err; Pa_Initialize(); printf( "PortAudio version number = %d\nPortAudio version text = '%s'\n", Pa_GetVersion(), Pa_GetVersionText() ); numDevices = Pa_GetDeviceCount(); if( numDevices < 0 ) { printf( "ERROR: Pa_GetDeviceCount returned 0x%x\n", numDevices ); err = numDevices; goto error; } printf( "Number of devices = %d\n", numDevices ); for( i=0; i<numDevices; i++ ) { deviceInfo = Pa_GetDeviceInfo( i ); printf( "--------------------------------------- device #%d\n", i ); /* Mark global and API specific default devices */ defaultDisplayed = 0; if( i == Pa_GetDefaultInputDevice() ) { printf( "[ Default Input" ); defaultDisplayed = 1; } else if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultInputDevice ) { const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); printf( "[ Default %s Input", hostInfo->name ); defaultDisplayed = 1; } if( i == Pa_GetDefaultOutputDevice() ) { printf( (defaultDisplayed ? "," : "[") ); printf( " Default Output" ); defaultDisplayed = 1; } else if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultOutputDevice ) { const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); printf( (defaultDisplayed ? "," : "[") ); printf( " Default %s Output", hostInfo->name ); defaultDisplayed = 1; } if( defaultDisplayed ) printf( " ]\n" ); /* print device info fields */ printf( "Name = %s\n", deviceInfo->name ); printf( "Host API = %s\n", Pa_GetHostApiInfo( deviceInfo->hostApi )->name ); printf( "Max inputs = %d", deviceInfo->maxInputChannels ); printf( ", Max outputs = %d\n", deviceInfo->maxOutputChannels ); printf( "Default low input latency = %8.4f\n", deviceInfo->defaultLowInputLatency ); printf( "Default low output latency = %8.4f\n", deviceInfo->defaultLowOutputLatency ); printf( "Default high input latency = %8.4f\n", deviceInfo->defaultHighInputLatency ); printf( "Default high output latency = %8.4f\n", deviceInfo->defaultHighOutputLatency ); #ifdef WIN32 #if PA_USE_ASIO /* ASIO specific latency information */ if( Pa_GetHostApiInfo( deviceInfo->hostApi )->type == paASIO ){ long minLatency, maxLatency, preferredLatency, granularity; err = PaAsio_GetAvailableLatencyValues( i, &minLatency, &maxLatency, &preferredLatency, &granularity ); printf( "ASIO minimum buffer size = %ld\n", minLatency ); printf( "ASIO maximum buffer size = %ld\n", maxLatency ); printf( "ASIO preferred buffer size = %ld\n", preferredLatency ); if( granularity == -1 ) printf( "ASIO buffer granularity = power of 2\n" ); else printf( "ASIO buffer granularity = %ld\n", granularity ); } #endif /* PA_USE_ASIO */ #endif /* WIN32 */ printf( "Default sample rate = %8.2f\n", deviceInfo->defaultSampleRate ); /* poll for standard sample rates */ inputParameters.device = i; inputParameters.channelCount = deviceInfo->maxInputChannels; inputParameters.sampleFormat = paInt16; inputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */ inputParameters.hostApiSpecificStreamInfo = NULL; outputParameters.device = i; outputParameters.channelCount = deviceInfo->maxOutputChannels; outputParameters.sampleFormat = paInt16; outputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */ outputParameters.hostApiSpecificStreamInfo = NULL; if( inputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for half-duplex 16 bit %d channel input = \n", inputParameters.channelCount ); PrintSupportedStandardSampleRates( &inputParameters, NULL ); } if( outputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for half-duplex 16 bit %d channel output = \n", outputParameters.channelCount ); PrintSupportedStandardSampleRates( NULL, &outputParameters ); } if( inputParameters.channelCount > 0 && outputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for full-duplex 16 bit %d channel input, %d channel output = \n", inputParameters.channelCount, outputParameters.channelCount ); PrintSupportedStandardSampleRates( &inputParameters, &outputParameters ); } } Pa_Terminate(); printf("----------------------------------------------\n"); return 0; error: Pa_Terminate(); fprintf( stderr, "An error occured while using the portaudio stream\n" ); fprintf( stderr, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; }
int selectDevice() { int i, numDevices, defaultDisplayed; const PaDeviceInfo *deviceInfo; PaStreamParameters inputParameters, outputParameters; PaStream* stream; PaError err; paTestData data; int totalFrames; int numSamples; int numBytes; char devSelection[4]; int devSelectInt; err = Pa_Initialize(); if( err != paNoError ) { printf( "ERROR: Pa_Initialize returned 0x%x\n", err ); return -1; } printf( "PortAudio version: 0x%08X\n", Pa_GetVersion()); printf( "Version text: '%s'\n", Pa_GetVersionInfo()->versionText ); numDevices = Pa_GetDeviceCount(); if( numDevices < 0 ) { printf( "ERROR: Pa_GetDeviceCount returned 0x%x\n", numDevices ); err = numDevices; return -1; } printf( "Number of devices = %d\n", numDevices ); for( i=0; i<numDevices; i++ ) { deviceInfo = Pa_GetDeviceInfo( i ); printf( "--------------------------------------- device #%d\n", i ); /* Mark global and API specific default devices */ defaultDisplayed = 0; if( i == Pa_GetDefaultInputDevice() ) { printf( "[ Default Input" ); defaultDisplayed = 1; } else if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultInputDevice ) { const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); printf( "[ Default %s Input", hostInfo->name ); defaultDisplayed = 1; } if( i == Pa_GetDefaultOutputDevice() ) { printf( (defaultDisplayed ? "," : "[") ); printf( " Default Output" ); defaultDisplayed = 1; } else if( i == Pa_GetHostApiInfo( deviceInfo->hostApi )->defaultOutputDevice ) { const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo( deviceInfo->hostApi ); printf( (defaultDisplayed ? "," : "[") ); printf( " Default %s Output", hostInfo->name ); defaultDisplayed = 1; } if( defaultDisplayed ) printf( " ]\n" ); /* print device info fields */ #ifdef WIN32 { /* Use wide char on windows, so we can show UTF-8 encoded device names */ wchar_t wideName[MAX_PATH]; MultiByteToWideChar(CP_UTF8, 0, deviceInfo->name, -1, wideName, MAX_PATH-1); wprintf( L"Name = %s\n", wideName ); } #else printf( "Name = %s\n", deviceInfo->name ); #endif printf( "Host API = %s\n", Pa_GetHostApiInfo( deviceInfo->hostApi )->name ); printf( "Max inputs = %d", deviceInfo->maxInputChannels ); printf( ", Max outputs = %d\n", deviceInfo->maxOutputChannels ); printf( "Default low input latency = %8.4f\n", deviceInfo->defaultLowInputLatency ); printf( "Default low output latency = %8.4f\n", deviceInfo->defaultLowOutputLatency ); printf( "Default high input latency = %8.4f\n", deviceInfo->defaultHighInputLatency ); printf( "Default high output latency = %8.4f\n", deviceInfo->defaultHighOutputLatency ); #ifdef WIN32 #if PA_USE_ASIO /* ASIO specific latency information */ if( Pa_GetHostApiInfo( deviceInfo->hostApi )->type == paASIO ){ long minLatency, maxLatency, preferredLatency, granularity; err = PaAsio_GetAvailableLatencyValues( i, &minLatency, &maxLatency, &preferredLatency, &granularity ); printf( "ASIO minimum buffer size = %ld\n", minLatency ); printf( "ASIO maximum buffer size = %ld\n", maxLatency ); printf( "ASIO preferred buffer size = %ld\n", preferredLatency ); if( granularity == -1 ) printf( "ASIO buffer granularity = power of 2\n" ); else printf( "ASIO buffer granularity = %ld\n", granularity ); } #endif /* PA_USE_ASIO */ #endif /* WIN32 */ printf( "Default sample rate = %8.2f\n", deviceInfo->defaultSampleRate ); /* poll for standard sample rates */ inputParameters.device = i; inputParameters.channelCount = deviceInfo->maxInputChannels; inputParameters.sampleFormat = paInt16; inputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */ inputParameters.hostApiSpecificStreamInfo = NULL; outputParameters.device = i; outputParameters.channelCount = deviceInfo->maxOutputChannels; outputParameters.sampleFormat = paInt16; outputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */ outputParameters.hostApiSpecificStreamInfo = NULL; if( inputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for half-duplex 16 bit %d channel input = \n", inputParameters.channelCount ); PrintSupportedStandardSampleRates( &inputParameters, NULL ); } if( outputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for half-duplex 16 bit %d channel output = \n", outputParameters.channelCount ); PrintSupportedStandardSampleRates( NULL, &outputParameters ); } if( inputParameters.channelCount > 0 && outputParameters.channelCount > 0 ) { printf("Supported standard sample rates\n for full-duplex 16 bit %d channel input, %d channel output = \n", inputParameters.channelCount, outputParameters.channelCount ); PrintSupportedStandardSampleRates( &inputParameters, &outputParameters ); } } /* for numDevices */ printf("What input device would you like to use? "); fgets(devSelection, 4, stdin); devSelectInt = atoi(devSelection); if(devSelectInt >= numDevices) { printf("Sorry; that number is out of the parameters; must be less than %d.", numDevices); return -1; } // if return devSelectInt; } // selectDevice