Specifying a Collection of MKUnitGenerators

To specify the collection, the MKSynthPatch subclass implements a single class method:

+ patchTemplateFor: aNote
      

This method creates the MKPatchTemplate used to represent the connections. The MKNote passed to the method may be used to choose between various “flavors”.

The MKPatchTemplate consists of a list of the MKUnitGenerator classes and MKSynthData requests needed to build an instance of the MKSynthPatch.

The MKOrchestra uses the MKPatchTemplate to build an instance of the MKSynthPatch. For each entry in the MKPatchTemplate, it allocates an appropriate MKUnitGenerator or MKSynthData instance. The collection of MKUnitGenerators appears in the MKSynthPatch instance as a List object in the instance variable synthElements. The instance can retrieve a particular MKUnitGenerator or MKSynthData instance by sending itself the message:

+ synthElementAt: (int) index
      

The MKUnitGenerators appear in the order they were specified in the MKPatchTemplate. For convenience, the MKPatchTemplate specification methods return the integer used to later access the particular element. By convention a MKSynthPatch stores this integer in a static int variable.

MKUnitGenerators may be specified as ordered or unordered. By default, they are ordered. Note that you must specify the particular MKUnitGenerator leaf class.

MKSynthData are specified by supplying a memory space and a length.

Example for simple MKSynthPatch:

static int  sc, patchPoint, out;

+ patchTemplateFor: aNote
/* We ignore aNote in this simple MKSynthPatch */
{	static PatchTemplate  *t = nil;

      if (!t)  {             /* Only create template the first time. */	
                t = [[PatchTemplate alloc]  init];  
                osc = [t addUnitGenerator: [OscgUGxy class]];
                patchPoint = [t addPatchpoint: MK_xPatch ];
 		out = [t addUnitGenerator: [Out2sumUGx class]]; 	
      } 	
      return t;	
}
      

Alternative to MKPatchTemplate:. Just allocate directly from MKOrchestra in -init method. The advantage of using a MKPatchTemplate is that the patch is stored as data and aborting on allocation failure is handled automatically.