MpAudioVolume MpPortAudioMixer::getInputVolume() const { OsLock lock(MpPortAudioDriver::ms_driverMutex); if (m_pxMixer) { return Px_GetInputVolume(m_pxMixer); } return 0.0; }
int main(int argc, char **argv) { int num_mixers; int i; PaError error; PortAudioStream *stream; int recDeviceNum; int playDeviceNum; int inputChannels; int outputChannels; int num_devices; int device; int opt; int opts=-1, optm=0; float optv=-2, opto=-2, opti=-2, opth=-2, optb=-2; printf("px_test: a program to demonstrate the capabilities of PortMixer\n"); printf("By Dominic Mazzoni\n"); printf("\n"); printf("Usage:\n"); printf(" -d [device number]\n"); printf(" -m [mixer number]\n"); printf(" -v [vol] (Master volume)\n"); printf(" -o [vol] (PCM output volume)\n"); printf(" -i [vol] (Input volume)\n"); printf(" -s [source number] (Input source)\n"); printf(" -h [vol] (Playthrough)\n"); printf(" -b [bal] (Balance: -1.0....1.0)\n"); printf("\n"); printf("All volumes are between 0.0 and 1.0.\n"); printf("\n"); error = Pa_Initialize(); if (error != 0) { printf("PortAudio error: %s\n", Pa_GetErrorText(error)); return -1; } num_devices = Pa_CountDevices(); device = Pa_GetDefaultInputDeviceID(); recDeviceNum = paNoDevice; playDeviceNum = paNoDevice; inputChannels = 0; outputChannels = 0; while(-1 != (opt=getopt(argc, argv, "d:m:v:o:i:s:h:b:"))) { switch(opt) { case 'd': device = atoi(optarg); printf("Set device to %d\n", device); break; case 'm': optm = atoi(optarg); printf("Set mixer number to %d\n", optm); break; case 'v': optv = getvolarg(optarg); break; case 'o': opto = getvolarg(optarg); break; case 'i': opti = getvolarg(optarg); break; case 'h': opth = getvolarg(optarg); break; case 'b': optb = atof(optarg); break; case 's': opts = atoi(optarg); break; } } printf("Devices:\n"); for(i=0; i<num_devices; i++) { const PaDeviceInfo* deviceInfo = Pa_GetDeviceInfo(i); if (i==device) { printf("* "); if (deviceInfo->maxInputChannels > 0) { recDeviceNum = device; inputChannels = deviceInfo->maxInputChannels; } if (deviceInfo->maxOutputChannels > 0) { playDeviceNum = device; outputChannels = deviceInfo->maxOutputChannels; } } else printf(" "); printf("Device %d: %s in=%d out=%d", i, deviceInfo->name, deviceInfo->maxInputChannels, deviceInfo->maxOutputChannels); if (i == Pa_GetDefaultInputDeviceID()) printf(" (default input)"); if (i == Pa_GetDefaultOutputDeviceID()) printf(" (default output)"); printf("\n"); } printf("\n"); error = Pa_OpenStream(&stream, recDeviceNum, inputChannels, paFloat32, NULL, playDeviceNum, outputChannels, paFloat32, NULL, 44101, 512, 1, paClipOff | paDitherOff, DummyCallbackFunc, NULL); if (error) { printf("PortAudio error %d: %s\n", error, Pa_GetErrorText(error)); return -1; } num_mixers = Px_GetNumMixers(stream); printf("Number of mixers for device %d: %d\n", device, num_mixers); for(i=0; i<num_mixers; i++) { PxMixer *mixer; int num; int j; printf("Mixer %d: %s\n", i, Px_GetMixerName(stream, i)); mixer = Px_OpenMixer(stream, i); if (!mixer) { printf(" Could not open mixer!\n"); continue; } if (i == optm) { if (optv!=-2) { Px_SetMasterVolume(mixer, optv); printf(" Set master volume\n"); } if (opto!=-2) { Px_SetPCMOutputVolume(mixer, opto); printf(" Set output volume\n"); } if (opti!=-2) { Px_SetInputVolume(mixer, opti); printf(" Set input volume\n"); } if (opth!=-2) { Px_SetPlaythrough(mixer, opth); printf(" Set playthrough volume\n"); } if (opts!=-2) { Px_SetCurrentInputSource(mixer, opts); printf(" Set input source\n"); } if (optb!=-2) { Px_SetOutputBalance(mixer, optb); printf(" Set balance\n"); } } printf(" Master volume: %.2f\n", Px_GetMasterVolume(mixer)); printf(" PCM output volume: %.2f\n", Px_GetPCMOutputVolume(mixer)); num = Px_GetNumOutputVolumes(mixer); printf(" Num outputs: %d\n", num); for(j=0; j<num; j++) { printf(" Output %d (%s): %.2f\n", j, Px_GetOutputVolumeName(mixer, j), Px_GetOutputVolume(mixer, j)); } num = Px_GetNumInputSources(mixer); printf(" Num input sources: %d\n", num); for(j=0; j<num; j++) { printf(" Input %d (%s) %s\n", j, Px_GetInputSourceName(mixer, j), (Px_GetCurrentInputSource(mixer)==j? "SELECTED": "")); } printf(" Input volume: %.2f\n", Px_GetInputVolume(mixer)); printf(" Playthrough:"); if (Px_SupportsPlaythrough(mixer)) printf(" %.2f\n", Px_GetPlaythrough(mixer)); else printf(" not supported.\n"); printf(" Output balance:"); if (Px_SupportsOutputBalance(mixer)) printf(" %.2f\n", Px_GetOutputBalance(mixer)); else printf(" not supported.\n"); Px_CloseMixer(mixer); } Pa_CloseStream(stream); Pa_Terminate(); return 0; }
RTC::ReturnCode_t PortAudioInput::onActivated(RTC::UniqueId ec_id) { RTC_DEBUG(("onActivated start")); PaStreamParameters inputParameters; // PaWasapiStreamInfo wasapiinfo; try { //m_pa_mutex.lock(); //by Irie Seisho m_format = getFormat(m_formatstr); m_totalframes = FRAMES_PER_BUFFER * m_channels; m_err = Pa_GetSampleSize(m_format); if( m_err > 0 ) { m_totalframes *= m_err; } #if 0 /* Find all WASAPI devices */ const PaDeviceInfo *device; for ( int i = 0; i < Pa_GetDeviceCount(); ++i ) { device = Pa_GetDeviceInfo(i); if ( Pa_GetDeviceInfo(i)->hostApi == Pa_HostApiTypeIdToHostApiIndex(paWASAPI) ) { std::cout << "Device Index " << i << " : " << device->name << ", inch " << device->maxInputChannels << ", outch " << device->maxOutputChannels << std::endl; } } //#if 0 PaDeviceIndex dnum = Pa_GetDeviceCount(); for (int i = 0; i < (int)dnum; i++) { std::cout << "Device Index " << i << " : " << Pa_GetDeviceInfo(i)->name << ", inch " << Pa_GetDeviceInfo(i)->maxInputChannels << ", outch " << Pa_GetDeviceInfo(i)->maxOutputChannels << std::endl; } //#endif void *pFormat; unsigned int nFormatSize; PaDeviceIndex nDevice; int r = PaWasapi_GetDeviceDefaultFormat(pFormat, nFormatSize, nDevice); #endif inputParameters.device = Pa_GetDefaultInputDevice(); //!< default input device if ( inputParameters.device < 0 ) { throw (paNotInitialized); } if ( m_channels > Pa_GetDeviceInfo(inputParameters.device)->maxInputChannels ) m_channels = Pa_GetDeviceInfo(inputParameters.device)->maxInputChannels; inputParameters.channelCount = m_channels; inputParameters.sampleFormat = m_format; inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; /* wasapiinfo.size = sizeof(PaWasapiStreamInfo); wasapiinfo.hostApiType = paWASAPI; wasapiinfo.version = 1; wasapiinfo.flags = paWasapiUseChannelSelectors; wasapiinfo.hostProcessorInput wasapiinfo.threadPriority wasapiinfo.channelMask = outputChannelSelectors; inputParameters.hostApiSpecificStreamInfo = wasapiinfo; */ inputParameters.hostApiSpecificStreamInfo = NULL; m_err = Pa_OpenStream( &m_stream, //!< PortAudioStream &inputParameters, //!< InputParameters NULL, //!< outputParameters m_samplerate, //!< sampleRate FRAMES_PER_BUFFER, //!< framesPerBuffer paClipOff, //!< streamFlags:we won't output out of range samples so don't bother clipping // StreamCB, //!< streamCallback // this ); //!< callback userData NULL, //!< streamCallback:no callback, use blocking API NULL ); //!< no callback, so no callback userData if( m_err != paNoError ) { throw m_err; } #ifdef HAVE_LIBPORTMIXER m_mixer = Px_OpenMixer( m_stream, 0 ); m_volume = Px_GetInputVolume( m_mixer ); #else #if defined(_WIN32) if ( InitMixer() == true ) { DWORD vol; GetMicrophoneLevel(&vol); } else { CloseMixer(); } #elif defined(__linux) const char* sound_device_names[] = SOUND_DEVICE_NAMES; m_device = -1; m_fd = -1; for ( int i = 0; i < SOUND_MIXER_NRDEVICES; i ++ ) { std::cout << " device name : " << sound_device_names[i] << std::endl; if ( strcmp( "mic", sound_device_names[i] ) == 0 ) { m_device = i; break; } } if ( ( m_fd = open( "/dev/mixer", O_RDONLY ) ) == -1 ) { perror( "open" ); } #endif #endif m_err = Pa_StartStream( m_stream ); if( m_err != paNoError ) { throw m_err; } //m_pa_mutex.unlock(); //by Irie Seisho } catch (...) { std::string error_str = Pa_GetErrorText(m_err); RTC_WARN(("PortAudio failed onActivated:%s", error_str.c_str())); return RTC::RTC_ERROR; } syncflg = true; is_active = true; RTC_DEBUG(("onActivated finish")); return RTC::RTC_OK; }