Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Implement the Factory Presets Interface

Next, you define the audio unit factory presets, again by editing the source files for the audio unit custom subclass, TremoloUnit.h and TremoloUnit.cpp.

The steps, described in detail below, are:

Note: The work you do here does not make use of the factory presets, per se. It is the DSP code that you implement in “Implement Signal Processing” that makes use of the presets. Here, you are simply defining the presets so that they will appear in the audio unit generic view and so that they're ready to use when you implement the DSP code.

In this section:

Name the Factory Presets and Give Them Values
Add Method Declarations for Factory Presets
Set the Default Factory Preset
Implement the GetPresets Method
Define the Factory Presets


Name the Factory Presets and Give Them Values

Define the constants for the factory presets. A convenient location for this code in the TremoloUnit.h header file is below the parameter constants section.

Listing 5-5  Factory preset names and values (TremoloUnit.h)

#pragma mark ____TremoloUnit Factory Preset Constants
static const float kParameter_Preset_Frequency_Slow = 2.0;    // 1
static const float kParameter_Preset_Frequency_Fast = 20.0;   // 2
static const float kParameter_Preset_Depth_Slow     = 50.0;   // 3
static const float kParameter_Preset_Depth_Fast     = 90.0;   // 4
static const float kParameter_Preset_Waveform_Slow            // 5
    = kSineWave_Tremolo_Waveform;
static const float kParameter_Preset_Waveform_Fast            // 6
    = kSquareWave_Tremolo_Waveform;
enum {
    kPreset_Slow    = 0,                                      // 7
    kPreset_Fast    = 1,                                      // 8
    kNumberPresets  = 2                                       // 9
};
 
static AUPreset kPresets [kNumberPresets] = {                 // 10
    {kPreset_Slow, CFSTR ("Slow & Gentle")},
    {kPreset_Fast, CFSTR ("Fast & Hard")}
};
 
static const int kPreset_Default = kPreset_Slow;              // 11

Here’s how this code works:

  1. Defines a constant for the frequency value for the “Slow &amp; Gentle” factory preset.

  2. Defines a constant for the frequency value for the “Fast &amp; Hard” factory preset.

  3. Defines a constant for the depth value for the “Slow &amp; Gentle; Hard” factory preset.

  4. Defines a constant for the depth value for the “Fast &amp; Hard” factory preset.

  5. Defines a constant for the waveform value for the “Slow &amp; Gentle” factory preset.

  6. Defines a constant for the waveform value for the “Fast &amp; Hard” factory preset.

  7. Defines a constant for the “Slow &amp; Gentle” factory preset.

  8. Defines a constant for the “Fast &amp; Hard” factory preset.

  9. Defines a constant representing the total number of factory presets.

  10. Defines an array containing two Core Foundation string objects. The objects contain values for the menu items in the user interface corresponding to the factory presets.

  11. Defines a constant representing the default factory preset, in this case the “Slow & Gentle” preset.

Add Method Declarations for Factory Presets

Now, provide method declarations for overriding the GetPresets and NewFactoryPresetSet methods from the AUBase superclass. Add these method declarations to the public: portion of class declaration in the TremoloUnit.h header file. You implement these methods in a later step in this chapter.

Listing 5-6  Factory preset method declarations (TremoloUnit.h)

#pragma mark ____TremoloUnit
class TremoloUnit : public AUEffectBase {
 
public:
    TremoloUnit (AudioUnit component);
...
    virtual ComponentResult GetPresets (       // 1
        CFArrayRef        *outData
    ) const;
 
    virtual OSStatus NewFactoryPresetSet (     // 2
        const AUPreset    &inNewFactoryPreset
    );
protected:
...
};

Here’s how this code works:

  1. Declaration for the GetPresets method, overriding the method from the AUBase superclass.

  2. Declaration for the NewFactoryPresetSet method, overriding the method from the AUBase superclass.

Set the Default Factory Preset

Now you return to the TremoloUnit constructor method, in which you previously added code for setting the audio unit parameters. Here, you add a single statement to set the default factory preset, making use of the kTremoloPreset_Default constant.

Listing 5-7  Setting the default factory preset in the constructor (TremoloUnit.cpp)

TremoloUnit::TremoloUnit (AudioUnit component) : AUEffectBase (component) {
 
    CreateElements ();
    Globals () -> UseIndexedParameters (kNumberOfParameters);
    // code for setting default values for the audio unit parameters
    SetAFactoryPresetAsCurrent (                    // 1
        kPresets [kPreset_Default]
    );
    // boilerplate code for debug dispatcher
}

Here’s how this code works:

  1. Sets the default factory preset.

Implement the GetPresets Method

For users to be able to use the factory presets you define, you must add a generic implementation of the GetPresets method. The following generic code works for any audio unit that can support factory presets.

A convenient location for this code in the TremoloUnit.cpp implementation file is after the GetPropertyInfo and GetProperty methods.

Note: You can refer to “Control Code: Parameters, Factory Presets, and Properties” for an architectural description of the GetPresets method, and how it fits into audio unit operation.

Listing 5-8  Implementing the GetPresets method (TremoloUnit.cpp)

#pragma mark ____Factory Presets
ComponentResult TremoloUnit::GetPresets (                     // 1
    CFArrayRef *outData
) const {
 
    if (outData == NULL) return noErr;                        // 2
 
    CFMutableArrayRef presetsArray = CFArrayCreateMutable (   // 3
        NULL,
        kNumberPresets,
        NULL
    );
 
    for (int i = 0; i < kNumberPresets; ++i) {                // 4
        CFArrayAppendValue (
            presetsArray,
            &kPresets [i]
        );
    }
 
    *outData = (CFArrayRef) presetsArray;                     // 5
    return noErr;
}

Here’s how this code works:

  1. The GetPresets method accepts a single parameter, a pointer to a CFArrayRef object. This object holds the factory presets array generated by this method.

  2. Checks whether factory presets are implemented for this audio unit.

  3. Instantiates a mutable Core Foundation array to hold the factory presets.

  4. Fills the factory presets array with values from the definitions in the TremoloUnit.h file.

  5. Stores the factory presets array at the outData location.

Define the Factory Presets

The NewFactoryPresetSet method defines all the factory presets for an audio unit. Basically, for each preset, it invokes a series of SetParameter calls.

A convenient location for this code in the TremoloUnit.cpp implementation file is after the implementation of the GetPresets method.

Listing 5-9  Defining factory presets in the NewFactoryPresetSet method (TremoloUnit.cpp)

OSStatus TremoloUnit::NewFactoryPresetSet (                         // 1
    const AUPreset &inNewFactoryPreset
) {
    SInt32 chosenPreset = inNewFactoryPreset.presetNumber;          // 2
 
    if (                                                            // 3
        chosenPreset == kPreset_Slow ||
        chosenPreset == kPreset_Fast
    ) {
        for (int i = 0; i < kNumberPresets; ++i) {                  // 4
            if (chosenPreset == kPresets[i].presetNumber) {
                switch (chosenPreset) {                             // 5
 
                    case kPreset_Slow:                              // 6
                        SetParameter (                              // 7
                            kParameter_Frequency,
                            kParameter_Preset_Frequency_Slow
                        );
                        SetParameter (                              // 8
                            kParameter_Depth,
                            kParameter_Preset_Depth_Slow
                        );
                        SetParameter (                              // 9
                            kParameter_Waveform,
                            kParameter_Preset_Waveform_Slow
                        );
                        break;
 
                    case kPreset_Fast:                             // 10
                        SetParameter (
                            kParameter_Frequency,
                           kParameter_Preset_Frequency_Fast
                        );
                        SetParameter (
                            kParameter_Depth,
                            kParameter_Preset_Depth_Fast
                        );
                        SetParameter (
                            kParameter_Waveform,
                            kParameter_Preset_Waveform_Fast
                        );
                        break;
                }
                SetAFactoryPresetAsCurrent (                        // 11
                    kPresets [i]
                );
                return noErr;                                       // 12
            }
        }
    }
    return kAudioUnitErr_InvalidProperty;                           // 13
}

Here’s how this code works:

  1. This method takes a single argument of type AUPreset, a structure containing a factory preset name and number.

  2. Gets the number of the desired factory preset.

  3. Tests whether the desired factory preset is defined.

  4. This for loop, and the if statement that follows it, allow for noncontiguous preset numbers.

  5. Selects the appropriate case statement based on the factory preset number.

  6. The settings for the “Slow & Gentle” factory preset.

  7. Sets the Frequency audio unit parameter for the “Slow & Gentle” factory preset.

  8. Sets the Depth audio unit parameter for the “Slow & Gentle” factory preset.

  9. Sets the Waveform audio unit parameter for the “Slow & Gentle” factory preset.

  10. The settings for the “Fast & Hard” factory preset. The three SetParameter statements that follow work the same way as for the other factory preset.

  11. Updates the preset menu in the generic view to display the new factory preset.

  12. On success, returns a value of noErr.

  13. If the host application attempted to set an undefined factory preset, return an error.



< Previous PageNext Page > Hide TOC


Last updated: 2007-10-31




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice