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; 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_GetAvailableBufferSizes( 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, "Error number: %d\n", err ); fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); return err; } // main
/*fa MTR_AudioInit yes */ MTR_DCLSPC bool MTR_CALL MTR_AudioInit(uint32_t sndDmSize, uint32_t sndReservedCount, uint32_t musDmSize, uint32_t musReservedCount, int freq, int channels, int chunkSize) { PaError error; int hostApiCount; const PaHostApiInfo* hostApiInfo; int deviceIndex; int devicesCount; const PaDeviceInfo* deviceInfo; int i; int j; MTR_LogWrite("Initializing audio manager", 0, MTR_LMT_INFO); MTR_LogWrite_s("Reporting PortAudio version:", 1, MTR_LMT_INFO, Pa_GetVersionInfo()->versionText); error = Pa_Initialize(); if (error != paNoError) { MTR_LogWrite("Unable to initialize PortAudio", 0, MTR_LMT_ERROR); MTR_LogWrite(Pa_GetErrorText(error), 0, MTR_LMT_ERROR); return false; } MTR_LogWrite("Reporting available devices:", 1, MTR_LMT_INFO); devicesCount = Pa_GetDeviceCount(); for (i = 0; i < devicesCount; i++) { deviceInfo = Pa_GetDeviceInfo(i); MTR_LogWrite(deviceInfo->name, 2, MTR_LMT_INFO); hostApiInfo = Pa_GetHostApiInfo(deviceInfo->hostApi); MTR_LogWrite_s("Host API:", 3, MTR_LMT_INFO, hostApiInfo->name); MTR_LogWrite_i("Max input channels:", 3, MTR_LMT_INFO, deviceInfo->maxInputChannels); MTR_LogWrite_i("Max output channels:", 3, MTR_LMT_INFO, deviceInfo->maxOutputChannels); MTR_LogWrite_d("Default low input latency (sec.):", 3, MTR_LMT_INFO, deviceInfo->defaultLowInputLatency); MTR_LogWrite_i("Default low output latency (sec.):", 3, MTR_LMT_INFO, deviceInfo->defaultLowOutputLatency); MTR_LogWrite_d("Default high input latency (sec.):", 3, MTR_LMT_INFO, deviceInfo->defaultHighInputLatency); MTR_LogWrite_i("Default high output latency (sec.):", 3, MTR_LMT_INFO, deviceInfo->defaultHighOutputLatency); MTR_LogWrite_i("Default sample rate:", 3, MTR_LMT_INFO, deviceInfo->defaultSampleRate); } MTR_LogWrite("Reporting available hosts APIs:", 1, MTR_LMT_INFO); hostApiCount = Pa_GetHostApiCount(); for (i = 0; i < hostApiCount; i++) { hostApiInfo = Pa_GetHostApiInfo(i); MTR_LogWrite(hostApiInfo->name, 2, MTR_LMT_INFO); MTR_LogWrite("Devices of this host API:", 3, MTR_LMT_INFO); devicesCount = hostApiInfo->deviceCount; for (j = 0; j < devicesCount; j++) { deviceIndex = Pa_HostApiDeviceIndexToDeviceIndex(i, j); deviceInfo = Pa_GetDeviceInfo(deviceIndex); MTR_LogWrite(deviceInfo->name, 4, MTR_LMT_INFO); } deviceInfo = Pa_GetDeviceInfo(hostApiInfo->defaultInputDevice); MTR_LogWrite_s("Default input device:", 3, MTR_LMT_INFO, deviceInfo->name); deviceInfo = Pa_GetDeviceInfo(hostApiInfo->defaultOutputDevice); MTR_LogWrite_s("Default output device:", 3, MTR_LMT_INFO, deviceInfo->name); } hostApiInfo = Pa_GetHostApiInfo(Pa_GetDefaultHostApi()); MTR_LogWrite_s("Default host API:", 1, MTR_LMT_INFO, hostApiInfo->name); // if (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0) // MTR_LogWrite("SDL audio subsystem initialized", 1, MTR_LMT_INFO); // else // MTR_LogWrite("Unable to initialize SDL audio subsystem", 1, // MTR_LMT_INFO); // // initFlags = MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MODPLUG | MIX_INIT_MP3 | // MIX_INIT_OGG | MIX_INIT_FLUIDSYNTH; // initResult = Mix_Init(initFlags); // if(initResult != initFlags) // { // if (initResult == 0) // { // MTR_Notify("Unable to load libraries needed by SDL_mixer", 1, // MTR_LMT_ERROR); // return false; // } // // MTR_LogWrite("Unable to initialize support of this formats:", 1, // MTR_LMT_WARNING); // if ((initResult & MIX_INIT_FLAC) == 0) // MTR_LogWrite("FLAC", 2, MTR_LMT_WARNING); // if ((initResult & MIX_INIT_MOD) == 0) // MTR_LogWrite("MOD", 2, MTR_LMT_WARNING); // if ((initResult & MIX_INIT_MODPLUG) == 0) // MTR_LogWrite("MODPlug", 2, MTR_LMT_WARNING); // if ((initResult & MIX_INIT_MP3) == 0) // MTR_LogWrite("MP3", 2, MTR_LMT_WARNING); // if ((initResult & MIX_INIT_OGG) == 0) // MTR_LogWrite("OGG", 2, MTR_LMT_WARNING); // if ((initResult & MIX_INIT_FLUIDSYNTH) == 0) // MTR_LogWrite("FluidSynth", 2, MTR_LMT_WARNING); // } // else // MTR_LogWrite("Support of the every SDL_mixer audio format initialized", // 1, MTR_LMT_INFO); // // MTR_LogWrite("Preparing parameters for initialize SDL_mixer", 1, // MTR_LMT_INFO); // // /* Choosing sampling frequency */ // if (freq == MTR_AU_FREQ_DEFAULT) // { // frequency = MIX_DEFAULT_FREQUENCY; // MTR_LogWrite_i("Choosed default sampling frequency (Hz): ", 2, // MTR_LMT_INFO, MIX_DEFAULT_FREQUENCY); // } // else if (freq < 11025) // { // frequency = 11025; // MTR_LogWrite_i("Choosed sampling frequency not supported. Using " // "minimal supported sampling frequency (Hz): ", 2, MTR_LMT_WARNING, // 11025); // } // else // { // frequency = freq; // MTR_LogWrite_i("Choosed sampling frequency (Hz): ", 2, MTR_LMT_INFO, // freq); // } // // /* Choosing channels count */ // if (channels == MTR_AU_CHANNELS_DEFAULT) // { // channelsCount = MIX_DEFAULT_CHANNELS; // MTR_LogWrite_s("Choosed default channels count: ", 2, MTR_LMT_INFO, // channelsConf[MIX_DEFAULT_CHANNELS - 1]); // } // else if (channels > 2) // { // channelsCount = 2; // MTR_LogWrite_s("Choosed channels count are not supported. Using max " // "supported channels count: ", 2, MTR_LMT_WARNING, channelsConf[2 - 1]); // } // else // { // channelsCount = channels; // MTR_LogWrite_s("Choosed channels count: ", 2, MTR_LMT_INFO, // channelsConf[channels - 1]); // } // // /* Choosing chunk size */ // if (chunkSize == MTR_AU_CHUNK_SIZE_DEFAULT) // { // finalChunkSize = 4096; // MTR_LogWrite_i("Choosed default chunk size (bytes): ", 2, MTR_LMT_INFO, // 4096); // } // else if (chunkSize > 8192) // { // finalChunkSize = 8192; // MTR_LogWrite_i("Choosed chunk size not supported. Using max supported " // "chunk size (bytes): ", 2, MTR_LMT_WARNING, 8192); // } // else if (chunkSize < 512) // { // finalChunkSize = 512; // MTR_LogWrite_i("Choosed chunk size not supported. Using minimal " // "supported chunk size (bytes): ", 2, MTR_LMT_WARNING, 512); // } // else // { // finalChunkSize = chunkSize; // MTR_LogWrite_i("Choosed chunk size (bytes): ", 2, MTR_LMT_INFO, // chunkSize); // } // // if (Mix_OpenAudio(frequency, MIX_DEFAULT_FORMAT, channelsCount, // finalChunkSize) == 0) // { // MTR_LogWrite("SDL_mixer initialized", 1, MTR_LMT_INFO); // // MTR_LogWrite("Reporting built-in SDL_mixer chunk decoders", 1, // MTR_LMT_INFO); // for (i = 0; i < Mix_GetNumChunkDecoders(); i++) // MTR_LogWrite_s("Chunk decoder found: ", 2, MTR_LMT_INFO, // Mix_GetChunkDecoder(i)); // MTR_LogWrite("Reporting built-in SDL_mixer music decoders", 1, // MTR_LMT_INFO); // for (i = 0; i < Mix_GetNumMusicDecoders(); i++) // MTR_LogWrite_s("Music decoder found: ", 2, MTR_LMT_INFO, // Mix_GetMusicDecoder(i)); // } // else // { // MTR_Notify("Unable to initialize SDL2_mixer", 1, MTR_LMT_ERROR); // MTR_LogWrite_s("SDL2_mixer error: ", 1, MTR_LMT_ERROR, Mix_GetError()); // Mix_Quit(); // return false; // } // Mix_AllocateChannels(64); // // mtrSoundKeeper = (mtrIndexkeeper_t *)MTR_IndexkeeperCreate(sndDmSize, // sndReservedCount, sizeof(mtrSound_t)); // if (mtrSoundKeeper == NULL) // { // MTR_Notify("Unable to initialize indexkeeper structure for sounds", 1, // MTR_LMT_ERROR); // Mix_CloseAudio(); // Mix_Quit(); // return false; // } // else // MTR_LogWrite("Indexkeeper structure for sounds initialized", 1, // MTR_LMT_INFO); // // mtrMusicKeeper = (mtrIndexkeeper_t *)MTR_IndexkeeperCreate(musDmSize, // musReservedCount, sizeof(mtrMusic_t)); // if (mtrMusicKeeper == NULL) // { // MTR_Notify("Unable to initialize indexkeeper structure for music", 1, // MTR_LMT_ERROR); // Mix_CloseAudio(); // Mix_Quit(); // return false; // } // else // MTR_LogWrite("Indexkeeper structure for music initialized", 1, // MTR_LMT_INFO); // // MTR_LogWrite("Audio manager initialized", 0, MTR_LMT_INFO); mtrCurrentMusic = 0U; mtrAudioInited = true; return true; }