1 /*
2  *  CoreAudioDevice.cpp
3  *  iGoom
4  *
5  *  Created by Guillaume Borios on 14/01/05.
6  *  Copyright 2005 iOS Software. All rights reserved.
7  *
8  */
9 
10 #include "CoreAudioDevice.h"
11 
12 
CoreAudioDevice(AudioDeviceID devId)13 CoreAudioDevice::CoreAudioDevice(AudioDeviceID devId):deviceID(devId) {}
14 
~CoreAudioDevice()15 CoreAudioDevice::~CoreAudioDevice() {}
16 
17 
name() const18 CFStringRef CoreAudioDevice::name() const
19 {
20     CFStringRef nom;
21     try
22     {
23         UInt32 size = sizeof(CFStringRef);
24         propertyData(0, kAudioDeviceSectionGlobal, kAudioDevicePropertyDeviceNameCFString, size, &nom);
25     }
26     catch(...)
27     {
28         nom = CFSTR("");
29     }
30     return nom;
31 }
32 
propertyDataSize(UInt32 channel,CoreAudioDeviceSection section,AudioHardwarePropertyID property) const33 UInt32 CoreAudioDevice::propertyDataSize(UInt32 channel, CoreAudioDeviceSection section, AudioHardwarePropertyID property) const
34 {
35     UInt32 size = 0;
36     if (AudioDeviceGetPropertyInfo(deviceID, channel, section, property, &size, NULL) != 0)
37     {
38         fprintf(stderr,"Error while fetching audio device property size. Exiting.");
39         exit(0);
40     }
41     return size;
42 }
43 
propertyData(UInt32 channel,CoreAudioDeviceSection section,AudioHardwarePropertyID property,UInt32 & size,void * data) const44 void CoreAudioDevice::propertyData(UInt32 channel, CoreAudioDeviceSection section, AudioHardwarePropertyID property, UInt32 &size, void* data) const
45 {
46     AudioDeviceGetProperty(deviceID, channel, section, property, &size, data) != 0;
47 }
48 
setPropertyData(UInt32 channel,CoreAudioDeviceSection section,AudioHardwarePropertyID property,UInt32 inDataSize,const void * data)49 void CoreAudioDevice::setPropertyData(UInt32 channel, CoreAudioDeviceSection section, AudioHardwarePropertyID property, UInt32 inDataSize, const void* data)
50 {
51     OSStatus theError = AudioDeviceSetProperty(deviceID, NULL, channel, section, property, inDataSize, data);
52     //if (theError) fprintf(stderr,"Error");
53 }
54 
numberOfChannels(CoreAudioDeviceSection section) const55 UInt32 CoreAudioDevice::numberOfChannels(CoreAudioDeviceSection section) const
56 {
57     UInt32 n = 0;
58     UInt32 size = propertyDataSize(0, section, kAudioDevicePropertyStreamConfiguration);
59     AudioBufferList* bufList=(AudioBufferList*)malloc(size);
60 
61     propertyData(0, section, kAudioDevicePropertyStreamConfiguration, size, bufList);
62     for(UInt32 i = 0; i < bufList->mNumberBuffers; ++i)
63     {
64         n += bufList->mBuffers[i].mNumberChannels;
65     }
66     free(bufList);
67     return n;
68 }
69 
hogModeOwner() const70 pid_t	CoreAudioDevice::hogModeOwner() const
71 {
72     pid_t retour = 0;
73     UInt32 size = sizeof(pid_t);
74     propertyData(0, kAudioDeviceSectionInput, kAudioDevicePropertyHogMode, size, &retour);
75     return retour;
76 }
77 
78 
AddPropertyListener(UInt32 inChannel,CoreAudioDeviceSection inSection,AudioHardwarePropertyID inPropertyID,AudioDevicePropertyListenerProc inListenerProc,void * inClientData)79 void	CoreAudioDevice::AddPropertyListener(UInt32 inChannel, CoreAudioDeviceSection inSection, AudioHardwarePropertyID inPropertyID, AudioDevicePropertyListenerProc inListenerProc, void* inClientData)
80 {
81     if (AudioDeviceAddPropertyListener(deviceID, inChannel, inSection, inPropertyID, inListenerProc, inClientData) != 0)
82     {
83         fprintf(stderr,"Error while Installing device notifications listener. Exiting.");
84         exit(0);
85     }
86 }
87 
RemovePropertyListener(UInt32 inChannel,CoreAudioDeviceSection inSection,AudioHardwarePropertyID inPropertyID,AudioDevicePropertyListenerProc inListenerProc)88 void	CoreAudioDevice::RemovePropertyListener(UInt32 inChannel, CoreAudioDeviceSection inSection, AudioHardwarePropertyID inPropertyID, AudioDevicePropertyListenerProc inListenerProc)
89 {
90     if (AudioDeviceRemovePropertyListener(deviceID, inChannel, inSection, inPropertyID, inListenerProc) !=0)
91     {
92         //fprintf(stderr,"Error while Removing device notifications listener. Exiting.");
93         //exit(0);
94     }
95 }
96 
97 // *************************************** VOLUME CONTROL ***************************************
98 
HasVolumeControl(UInt32 channel,CoreAudioDeviceSection section) const99 bool CoreAudioDevice::HasVolumeControl(UInt32 channel, CoreAudioDeviceSection section) const
100 {
101     OSStatus theError = AudioDeviceGetPropertyInfo(deviceID, channel, section, kAudioDevicePropertyVolumeScalar, NULL, NULL);
102     return (theError == 0);
103 }
104 
VolumeControlIsSettable(UInt32 channel,CoreAudioDeviceSection section) const105 bool CoreAudioDevice::VolumeControlIsSettable(UInt32 channel, CoreAudioDeviceSection section) const
106 {
107         Boolean isWritable = false;
108         OSStatus theError = AudioDeviceGetPropertyInfo(deviceID, channel, section, kAudioDevicePropertyVolumeScalar, NULL, &isWritable);
109         return (isWritable != 0);
110 }
111 
GetVolumeControlScalarValue(UInt32 channel,CoreAudioDeviceSection section) const112 Float32 CoreAudioDevice::GetVolumeControlScalarValue(UInt32 channel, CoreAudioDeviceSection section) const
113 {
114     Float32 value = 0.0;
115     UInt32 size = sizeof(Float32);
116     propertyData(channel, section, kAudioDevicePropertyVolumeScalar, size, &value);
117     return value;
118 }
119 
SetVolumeControlScalarValue(UInt32 channel,CoreAudioDeviceSection section,Float32 value)120 void CoreAudioDevice::SetVolumeControlScalarValue(UInt32 channel, CoreAudioDeviceSection section, Float32 value)
121 {
122     UInt32 size = sizeof(Float32);
123     setPropertyData(channel, section, kAudioDevicePropertyVolumeScalar, size, &value);
124 }
125 
126 
127