SimpleDrumMachine -- a simple Cocoa application This is an application which implements a simple toy drum machine using Cocoa classes. Essentially it contains several (in this case, 7) drum samples, and 16 note events ('ticks') per bar, and allows the user to toggle on which ticks a sample is played. The samples are stored among the resources, and are Sun/NeXT-style sound files (more often seen in the wild with the '.au' extension, but NeXT, and thus MacOS X, uses the '.snd' extension). (The samples are from a Roland TR-606 kit floating around the Net, in case you were wondering, but could just as easily be from something else.) SimpleDrumMachine was created using InterfaceBuilder and ProjectBuilder, much in the way that the CurrencyConverter tutorial was. It consists of two classes: DrumMachineController, a controller class which connects to the interface in the Nib file, and DrumMachine, the machine itself, which contains the patterns and does all the work. DrumMachine uses a NSTimer as a clock source. This is not perfect, but as this is only a toy, it will serve. The samples are NSSound objects, which are a rather dinky and not particularly useful sound object (i.e., you cannot look into the actual sample data it contains); however, the alternative is delving around in arcane I/O APIs, which for this example would have been overkill. One trick to note is in DrumMachineController's awakeFromNib method. InterfaceBuilder doesn't let you link a number of interface elements to elements of an array, which, given that we use 16 checkboxes for the sequence, would have meant a number of ugly and laborious switch statements dealing with 16 individual variables. To get around this, awakeFromNib pokes around the main window's top-level NSView and goes through its subviews. We have given all the checkboxes, and only them, tags from 0 to 15, and thus we can find NSButtons with those tags and put them in an array, giving us a convenient array of checkboxes. Future possibilities: - Put the actual drum machine patterns into a separate data class. - Perhaps make this a document-based application, with a document being either a pattern, a set of patterns, or a set of patterns with a sequence (not unlike ReBirth). - Splitting the DrumMachine class into a StepSequencer class and a SoundPlayer class, the former sending messages to the latter - some strobing lights chasing the current position would be nice - In the long term, doing away with NSSound and using a more advanced, sample-based audio API of some sort. This could allow soft-synth sound generators, effects and the like to be added. -- acb 31/10/2001