Important: Inside Macintosh: Sound is deprecated as of Mac OS X v10.5. For new audio development in Mac OS X, use Core Audio. See the Audio page in the ADC Reference Library.
Registering and Opening a Sound Component
The Component Manager sends your component thekComponentRegisterSelect
selector, usually at system startup time, to allow your component to determine whether it wants to register itself with the Component Manager. Utility components should always register themselves, so that the capabilities they provide will be available when needed. Sound output device components, however, should first check to see whether any necessary hardware is available before registering themselves. If the hardware they drive isn't available, there is no point in registering with the Component Manager.The Component Manager sends your component the
kComponentOpenSelect
selector whenever the Sound Manager wants to open a connection to your component. In general, a sound output device component has only one connection made to it. A utility component, however, might have several instances, if the capabilities it provides are needed by more than one sound component chain. Your component should do as little as possible when opening up. It should allocate whatever global storage it needs to manage the connection and callSetComponentInstanceStorage
so that the Component Manager can remember the location of that storage and pass it to all other component-defined routines.As noted in the previous section, your component is probably loaded into the system heap. If so, you should also allocate any global storage in the system heap. If memory is tight, however, your component might be loaded into an application's heap (namely, the heap of the first application that plays sound). In that case, you should allocate any global variables you need in that heap. The Sound Manager ensures that other applications will not try to play sound while the component is in this application heap.
The Sound Manager sends the
- IMPORTANT
- Your component is always sent the
kComponentOpenSelect
component selector before it is sent thekComponentRegisterSelect
selector. As a result, you should not attempt to initialize or configure any associated hardware in response tokComponentOpenSelect
.kSoundComponentInitOutputDeviceSelect
selector specifically to allow a sound output device component to perform any hardware-related operations. Your component should initialize the hardware to some reasonable default values, create the Apple Mixer, and allocate any other memory that might be needed. Listing 5-4 shows one way to respond to thekSoundComponentInitOutputDeviceSelect
selector.Listing 5-4 Initializing an output device
static pascal ComponentResult MySoundComponentInitOutputDevice (SoundComponentGlobalsPtr globals, long actions) { #pragma unused (actions) ComponentResult myResult; /*Make sure we got our globals.*/ if (globals->hwGlobals == nil) return (notEnoughHardwareErr); /*Set up the hardware.*/ myResult = MySetupHardware(globals); if (myResult != noErr) return (myResult); /*Create an Apple Mixer.*/ myResult = OpenMixerSoundComponent(&globals->thisComp, 0, &globals->sourceComponent); return (myResult); }TheMySoundComponentInitOutputDevice
function defined in Listing 5-4 simply retrieves the location of its global variables, configures the hardware by calling theMySetupHardware
function, and then callsOpenMixerSoundComponent
to create an instance of the Apple Mixer.