float CAPlayThrough::GetAverageVolume(){ float amps[2]; amps[0] = 0.0; amps[1] = 0.0; AudioUnitGetParameter(mMixerUnit, kMatrixMixerParam_PostAveragePower, kAudioUnitScope_Input, 0, &s[0]); AudioUnitGetParameter(mMixerUnit, kMatrixMixerParam_PostPeakHoldLevel, kAudioUnitScope_Input, 0, &s[1]); return amps[0]; }
Float32 CAAUParameter::GetValue() const { Float32 value = 0.; //OSStatus err = AudioUnitGetParameter(mAudioUnit, mParameterID, mScope, mElement, &value); return value; }
OSStatus GetParameter(AudioUnitParameterID inID, AudioUnitScope scope, AudioUnitElement element, Float32 &outValue) const { if (mGetParamProc != NULL) { return reinterpret_cast<AudioUnitGetParameterProc>(mGetParamProc) (mConnInstanceStorage, inID, scope, element, &outValue); } return AudioUnitGetParameter(mUnit, inID, scope, element, &outValue); }
float OutputImplAudioUnit::getVolume() const { float aValue = 0.0; OSStatus err = AudioUnitGetParameter( mMixerUnit, kMultiChannelMixerParam_Volume, kAudioUnitScope_Output, 0, &aValue ); if( err ) { //throw } return aValue; }
static int control(int cmd,void *arg){ ao_control_vol_t *control_vol; OSStatus err; Float32 vol; switch (cmd) { case AOCONTROL_GET_VOLUME: control_vol = (ao_control_vol_t*)arg; if (ao->b_digital) { // Digital output has no volume adjust. return CONTROL_FALSE; } err = AudioUnitGetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &vol); if(err==0) { // printf("GET VOL=%f\n", vol); control_vol->left=control_vol->right=vol*100.0/4.0; return CONTROL_TRUE; } else { ao_msg(MSGT_AO, MSGL_WARN, "could not get HAL output volume: [%4.4s]\n", (char *)&err); return CONTROL_FALSE; } case AOCONTROL_SET_VOLUME: control_vol = (ao_control_vol_t*)arg; if (ao->b_digital) { // Digital output can not set volume. Here we have to return true // to make mixer forget it. Else mixer will add a soft filter, // that's not we expected and the filter not support ac3 stream // will cause mplayer die. // Although not support set volume, but at least we support mute. // MPlayer set mute by set volume to zero, we handle it. if (control_vol->left == 0 && control_vol->right == 0) ao->b_muted = 1; else ao->b_muted = 0; return CONTROL_TRUE; } vol=(control_vol->left+control_vol->right)*4.0/200.0; err = AudioUnitSetParameter(ao->theOutputUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, vol, 0); if(err==0) { // printf("SET VOL=%f\n", vol); return CONTROL_TRUE; } else { ao_msg(MSGT_AO, MSGL_WARN, "could not set HAL output volume: [%4.4s]\n", (char *)&err); return CONTROL_FALSE; } /* Everything is currently unimplemented */ default: return CONTROL_FALSE; } }
float AudioMixerAudioNode::volume(int bus) { if (!m_audioUnit) return 0; Float32 db; OSStatus err = AudioUnitGetParameter(m_audioUnit, kStereoMixerParam_Volume, kAudioUnitScope_Input, bus, &db); BACKEND_ASSERT3(err == noErr, "Could not get volume on audio mixer node.", NORMAL_ERROR, 0) return float(db); }
// ---------------------------------------------------------- float ofxAudioUnitMixer::getOutputLevel() // ---------------------------------------------------------- { AudioUnitParameterValue level; ERR_CHK(AudioUnitGetParameter(*_unit, kMultiChannelMixerParam_PreAveragePower, kAudioUnitScope_Output, 0, &level), "getting mixer output level"); return level; }
static int get_volume(struct ao *ao, struct ao_control_vol *vol) { struct priv *p = ao->priv; float auvol; OSStatus err = AudioUnitGetParameter(p->audio_unit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &auvol); CHECK_CA_ERROR("could not get HAL output volume"); vol->left = vol->right = auvol * 100.0; return CONTROL_TRUE; coreaudio_error: return CONTROL_ERROR; }
Float32 CCoreAudioUnit::GetCurrentVolume() { if (!m_Component) return 0.0f; Float32 volPct = 0.0f; OSStatus ret = AudioUnitGetParameter(m_Component, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &volPct); if (ret) { CLog::Log(LOGERROR, "CCoreAudioUnit::GetCurrentVolume: Unable to get AudioUnit volume. Error = 0x%08x (%4.4s)", ret, CONVERT_OSSTATUS(ret)); return 0.0f; } return volPct; }
int sa_stream_get_volume_abs(sa_stream_t *s, float *vol) { if (s == NULL || s->output_unit == NULL) { return SA_ERROR_NO_INIT; } pthread_mutex_lock(&s->mutex); Float32 local_vol = 0; AudioUnitGetParameter(s->output_unit, kHALOutputParam_Volume, kAudioUnitParameterFlag_Output, 0, &local_vol); *vol = local_vol; pthread_mutex_unlock(&s->mutex); return SA_SUCCESS; }
Float32 CAUMatrixMixer::GetGlobalVolume() { if (!m_audioUnit) return 0.0f; Float32 vol = 0.0f; OSStatus ret = AudioUnitGetParameter(m_audioUnit, kMatrixMixerParam_Volume, kAudioUnitScope_Global, 0xFFFFFFFF, &vol); if (ret) { CLog::Log(LOGERROR, "CAUMatrixMixer::GetGlobalVolume: " "Unable to get global volume. Error = %s", GetError(ret).c_str()); return 0.0f; } return vol; }
Float32 CAUMatrixMixer::GetOutputVolume(UInt32 element) { if (!m_audioUnit) return 0.0f; Float32 vol = 0.0f; OSStatus ret = AudioUnitGetParameter(m_audioUnit, kMatrixMixerParam_Volume, kAudioUnitScope_Output, element, &vol); if (ret) { CLog::Log(LOGERROR, "CAUMatrixMixer::GetOutputVolume: " "Unable to get output volume. Error = %s", GetError(ret).c_str()); return 0.0f; } return vol; }
Float32 CAUOutputDevice::GetCurrentVolume() { if (!m_audioUnit) return 0.0f; Float32 volPct = 0.0f; OSStatus ret = AudioUnitGetParameter(m_audioUnit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &volPct); if (ret) { CLog::Log(LOGERROR, "CCoreAudioUnit::GetCurrentVolume: " "Unable to get AudioUnit volume. Error = %s", GetError(ret).c_str()); return 0.0f; } return volPct; }
Float32 CAUMultiChannelMixer::GetCurrentVolume() { if (!m_audioUnit) return false; Float32 volPct = 0.0f; OSStatus ret = AudioUnitGetParameter(m_audioUnit, kMultiChannelMixerParam_Volume, kAudioUnitScope_Input, kInputBus, &volPct); if (ret) { CLog::Log(LOGERROR, "CAUMultiChannelMixer::GetCurrentVolume: Unable to get Mixer volume. Error = %s", GetError(ret).c_str()); return 0.0f; } return volPct; }
/*---------------------------------------------------------------------- | OsxAudioUnitsOutput_GetVolume +---------------------------------------------------------------------*/ BLT_METHOD OsxAudioUnitsOutput_GetVolume(BLT_VolumeControl* _self, float* volume) { OsxAudioUnitsOutput* self = ATX_SELF(OsxAudioUnitsOutput, BLT_VolumeControl); ComponentResult result; Float32 au_volume = 1.0f; result = AudioUnitGetParameter(self->audio_unit, kHALOutputParam_Volume, kAudioUnitScope_Global, 0, &au_volume); if (result == noErr) { *volume = au_volume; return BLT_SUCCESS; } else { *volume = 1.0f; return BLT_FAILURE; } }
OSStatus PrintBuses (FILE* file, const char* str, AudioUnit au, AudioUnitScope inScope) { OSStatus result; UInt32 busCount; UInt32 theSize = sizeof(busCount); ca_require_noerr (result = AudioUnitGetProperty (au, kAudioUnitProperty_ElementCount, inScope, 0, &busCount, &theSize), home); fprintf (file, "\t%s Elements:\n\t\t", str); for (UInt32 i = 0; i < busCount; ++i) { Float32 val; ca_require_noerr (result = AudioUnitGetParameter (au, kMatrixMixerParam_Enable, inScope, i, &val), home); UInt32 numChans; ca_require_noerr (result = NumberChannels (au, inScope, i, numChans), home); char frameCharStart = (val != 0 ? '[' : '{'); char frameCharEnd = (val != 0 ? ']' : '}'); fprintf (file, "%d:%c%d, %c%c ", (int)i, frameCharStart, (int)numChans, (val != 0 ? 'T' : 'F'), frameCharEnd); } fprintf (file, "\n"); home: return result; }
JNIEXPORT jint JNICALL Java_com_apple_audio_units_AudioUnit_AudioUnitGetParameter (JNIEnv *, jclass, jint ci, jint inID, jint inScope, jint inElement, jint outValue) { return (jint)AudioUnitGetParameter((AudioUnit)ci, (AudioUnitParameterID)inID, (AudioUnitScope)inScope, (AudioUnitElement)inElement, (Float32 *)outValue); }
const AudioUnitParameterValue AudioUnitNode::getParameter(AudioUnitParameterID pid, AudioUnitScope scope, AudioUnitElement bus) const { AudioUnitParameterValue value; checkError(AudioUnitGetParameter(mUnit, pid, scope, bus, &value), "AudioUnitGetParameter"); return value; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // MixerTimerProc // //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void MixerTimerProc(EventLoopTimerRef inTimer, void *inUserData) { //printf("MixerTimerProc() : %x, %x\n", inUserData, gAUMixer3DView ); AUMixer3DView *This = (AUMixer3DView*)gAUMixer3DView; CGrafPtr gp = GetWindowPort(This->GetCarbonWindow() ); //printf("gp = %x\n", gp ); #if 1 CGrafPtr save; GetPort(&save); SetPort(gp ); // output metering for(int i = 0; i < 5; i++ ) { Float32 avePower; Float32 peak; AudioUnitGetParameter(This->GetEditAudioUnit(), 1000+i /* k3DMixerParam_PreAveragePower */, kAudioUnitScope_Output, 0, &avePower ); AudioUnitGetParameter(This->GetEditAudioUnit(), 2000+i /* peak */, kAudioUnitScope_Output, 0, &peak ); DrawPowerMeter(avePower, peak, 470 + i*10 ); } // input metering for(int i = 0; i < 3; i++ ) { Float32 avePower; Float32 peak; AudioUnitGetParameter(This->GetEditAudioUnit(), 3000, kAudioUnitScope_Input, i, &avePower ); AudioUnitGetParameter(This->GetEditAudioUnit(), 4000, kAudioUnitScope_Input, i, &peak ); DrawPowerMeter(avePower, peak, 420 + i*10 ); } SetPort(save ); #endif }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // MyEventHandler // //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OSStatus AUMixer3DView::MyEventHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent) { OSStatus result = noErr; UInt32 eventClass = GetEventClass(inEvent); UInt32 eventKind = GetEventKind(inEvent); // draw event if(eventClass == kEventClassWindow) { if(eventKind == kEventWindowDrawContent ) { Rect r = {mOffsetY, mOffsetX, mOffsetY + 400, mOffsetX + 400}; RGBForeColor(&mBackgroundColor ); PaintRect(&r); for(int i = 0; i < kNTrackers; i++) { gTrackers[i]->Draw(); } gListener->Draw(); return noErr; } } CGrafPtr gp = GetWindowPort(GetCarbonWindow()); CGrafPtr save; GetPort(&save); SetPort(gp ); const float kDistanceScale = 0.1 /*0.3*/; /* k3DMixerRenderingFlags_InterAuralDelay = (1L << 0), k3DMixerRenderingFlags_DopplerShift = (1L << 1), k3DMixerRenderingFlags_DistanceAttenuation = (1L << 2), k3DMixerRenderingFlags_DistanceFilter = (1L << 3), k3DMixerRenderingFlags_DistanceDiffusion = (1L << 4) */ if(eventClass == kEventClassCommand ) { HICommand command; GetEventParameter (inEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof(HICommand), NULL, &command); SInt16 value = ::GetControlValue((ControlRef)command.menu.menuRef ); //char *p = ((char*)&command.commandID ); //printf("%x command.commandID = %d : %d : %c%c%c%c\n", command.menu.menuRef, command.commandID, int(value), p[0],p[1],p[2],p[3] ); if(command.commandID == 'algo' ) { // set rendering algorithm UInt32 algo = value - 1; for(int i = 0; i < kNTrackers; i++) { AudioUnitSetProperty( GetEditAudioUnit(), kAudioUnitProperty_SpatializationAlgorithm, kAudioUnitScope_Input, i, &algo, sizeof(algo) ); } // rendering flags have changed with setting of rendering algorithm SetRenderingFlagsCheckboxes(); } else if(command.commandID == 'volm' ) { Float32 s = float(value) / 100.0; s = s*s; if(s == 0.0) { s = -96.0; } else { s = 20.0 * log10(s); } //printf("volume = %f dB\n", s); // set master gain in dB AudioUnitSetParameter(GetEditAudioUnit(), k3DMixerParam_Gain, kAudioUnitScope_Output, 0, s, 0 ); } else if(command.commandID == 'attn' ) { // volume attenuation curve Float64 s = float(value) / 100.0; //printf("volume atten curve= %f\n", s); // volume attenuation curve follows the law: 1.0 / (1.0 + s *(distance - 1.0) ) // // where distance is in meters, and s is generally between 0.1 and 1.0 (0.3 is good default) // // there is no attenuation if distance <= 1.0 meter for(int i = 0; i < kNTrackers; i++) { result = AudioUnitSetProperty( GetEditAudioUnit(), kAudioUnitProperty_3DMixerDistanceAtten, kAudioUnitScope_Input, i, &s, sizeof(s) ); } } else { UInt32 mask = 0; switch(command.commandID ) { case 'atr0': mask = k3DMixerRenderingFlags_InterAuralDelay; break; case 'atr1': mask = k3DMixerRenderingFlags_DopplerShift; break; case 'atr2': mask = k3DMixerRenderingFlags_DistanceAttenuation; break; case 'atr3': mask = k3DMixerRenderingFlags_DistanceFilter; break; case 'atr4': mask = k3DMixerRenderingFlags_DistanceDiffusion; break; case 'rvrb': { UInt32 usesReverb = value; AudioUnitSetProperty( GetEditAudioUnit(), kAudioUnitProperty_UsesInternalReverb, kAudioUnitScope_Input, 0, &usesReverb, sizeof(usesReverb) ); break; } } if(mask != 0) { for(int i = 0; i < kNTrackers; i++) { UInt32 flags; UInt32 size = sizeof(flags); AudioUnitGetProperty( GetEditAudioUnit(), kAudioUnitProperty_3DMixerRenderingFlags, kAudioUnitScope_Input, i, &flags, &size ); if(value > 0) { flags |= mask; } else { flags &= ~mask; } AudioUnitSetProperty( GetEditAudioUnit(), kAudioUnitProperty_3DMixerRenderingFlags, kAudioUnitScope_Input, i, &flags, size ); } return noErr; } } return eventNotHandledErr; } else if(eventClass == kEventClassMouse ) { HIPoint mousePos; result = GetEventParameter( inEvent, kEventParamMouseLocation, typeHIPoint, NULL, /* can be NULL */ sizeof(HIPoint), NULL, /* can be NULL */ &mousePos); UInt32 modifiers; result = GetEventParameter( inEvent, kEventParamKeyModifiers, typeUInt32, NULL, /* can be NULL */ sizeof(UInt32), NULL, /* can be NULL */ &modifiers); Point pt; pt.h = short(mousePos.x); pt.v = short(mousePos.y); Rect r2 = GetPortBitMapForCopyBits(gp)->bounds; GlobalToLocal(&pt); pt.h -= mOffsetX; pt.v -= mOffsetY; if(!mIsTrackingMouse && (pt.h < 0 || pt.v < 0) ) return eventNotHandledErr; // check for title bar if(pt.v < 0 && pt.v > -30 ) { return eventNotHandledErr; } Rect r1; GetPortBounds(gp, &r1 ); //int width = r1.right-r1.left; //int height = r1.bottom-r1.top; //!!@ hardcoded int width = 400; int height = 400; int centerx = int(0.5*width); int centery = int(0.5*height); static int currentLine = 0; float x = pt.h - centerx; float y = -(pt.v - centery); float rangle = atan2(x,y); float angle = 180.0 * rangle / 3.14159; static int hitx = 0; static int hity = 0; static float hitAngle = 0.0; switch(eventKind) { case kEventMouseDown: { mIsTrackingMouse = true; // determine the closest source float bestDistance = 100000.0; int bestIndex = 0; for(int i = 0; i < kNTrackers; i++) { if(gTrackers[i]->Distance(pt.h, pt.v) < bestDistance ) { bestIndex = i; bestDistance = gTrackers[i]->Distance(pt.h, pt.v); } } if(modifiers & (1L << shiftKeyBit)) { Float32 gain; AudioUnitGetParameter(GetEditAudioUnit(), k3DMixerParam_Gain, kAudioUnitScope_Input, bestIndex, &gain); if (gain > -120) gain = -120; else gain = 0; AudioUnitSetParameter(GetEditAudioUnit(), k3DMixerParam_Gain, kAudioUnitScope_Input, bestIndex, gain, 0); } currentLine = bestIndex; SetInput(currentLine); for(int i = 0; i < kNTrackers; i++) { if(i != currentLine) gTrackers[i]->Draw(); gTrackers[i]->Anchor(); } hitx = (int)x; hity = (int)y; hitAngle = angle; } case kEventMouseDragged: { if(modifiers & (1L << optionKeyBit) ) { for(int i = 0; i < kNTrackers; i++) { float newAngle = gTrackers[i]->Rotate(angle - hitAngle ); AudioUnitSetParameter(GetEditAudioUnit(), 0 /* azimuth */, kAudioUnitScope_Input, i, newAngle, 0 ); } gListener->Draw(); } else if(modifiers & (1L << cmdKeyBit) ) { for(int i = 0; i < kNTrackers; i++) { float angle; float pixelDistance; gTrackers[i]->Offset(x-hitx, -(y-hity), angle, pixelDistance ); float distance = kDistanceScale * pixelDistance; AudioUnitSetParameter(GetEditAudioUnit(), 0 /* azimuth */, kAudioUnitScope_Input, i, angle, 0 ); AudioUnitSetParameter(GetEditAudioUnit(), 2 /* distance */, kAudioUnitScope_Input, i, distance, 0 ); } gListener->Draw(); } else { TrackingLine *trackingLine = gTrackers[currentLine]; if(trackingLine) { trackingLine->Track(pt.h, pt.v); for(int i = 0; i < kNTrackers; i++) { if(i != currentLine) gTrackers[i]->Draw(); gTrackers[i]->Anchor(); } gListener->Draw(); } AudioUnitSetParameter(GetEditAudioUnit(), 0 /* azimuth */, kAudioUnitScope_Input, currentLine, angle, 0 ); float distance = kDistanceScale * sqrt( x*x + y*y); AudioUnitSetParameter(GetEditAudioUnit(), 2 /* distance */, kAudioUnitScope_Input, currentLine, distance, 0 ); } break; } case kEventMouseUp: mIsTrackingMouse = false; break; } SetPort(save); return noErr; } else { UInt8 keyCode; result = GetEventParameter( inEvent, kEventParamKeyMacCharCodes, typeChar, NULL, /* can be NULL */ sizeof(UInt8), NULL, /* can be NULL */ &keyCode); #if 1 switch(keyCode) { case 'q': SetFileInput(0); break; case 'w': SetFileInput(1); break; case 'e': SetFileInput(2); break; case 'r': SetFileInput(3); break; case 't': SetFileInput(4); break; case 'y': SetFileInput(5); break; case 'u': SetFileInput(6); break; case 'i': SetFileInput(7); break; case 'o': SetFileInput(8); break; case 'p': SetFileInput(9); break; case 'a': SetFileInput(10); break; case 's': SetFileInput(11); break; case 'd': SetFileInput(12); break; default: break; } #endif SetPort(save); return noErr; } return eventNotHandledErr; }