home/ atoms/ sc-sc-object-modeling

SuperCollider classes and events both support object modeling: classes via inheritance, events via prototype chains

Chapter 8 shows two complementary approaches to object modeling in SC. First: define a proper Class with instance variables (var <>myfreq), constructor (*new), and methods (blip). Second: use an Event as a prototype object — set pseudo-instance variables with .myfreq_(50) and pseudo-methods with .blip_({ |ev| … }). The Event approach is more dynamic but less type-safe. The Puppet/Shout examples show progressive refinement: start with a sketch in an Event, then graduate to a Class when the design stabilises. ProtoObject and Environment underlie both approaches.

Examples

// Class approach: Puppet { var <>myfreq; *new { |f=50| ^super.new.myfreq_(f) } blip { { Blip.ar(myfreq, 11) * XLine.kr(1, 0.01, 0.6, doneAction: 2) }.play } } // Event approach: m = (); m.myfreq_(50); m.blip_({ |ev| { Blip.ar(ev.myfreq, 11) * XLine.kr(1, 0.01, 0.6, doneAction: 2) }.play }); m.blip;

Assessment

Implement a simple drum machine as an Event prototype with methods for addPattern, removePattern, and play. Then convert it to a proper Class. Identify what the Event prototype approach cannot do that the class can.

“Puppet { var <>myfreq; // an instance variable with a getter and a setter method // a method for creating a new object of this kind *new { |myfreq=50| ^super.new.myfreq_(myfreq) }”
corpus · the-supercollider-book-official-code-examples-scbookcode-gpl · chunk 151