Previous: A Bestiary of Kiwi Objects Up: A Bestiary of Kiwi Objects Next: Note and ScoreList

Sound

Figure 2.2. Sound class hierarchy

Most Kiwi classes have a single instance and no subclasses (Scheduler). Others may have multiple instances (Note, Processor). The Sound class has subclasses but no instances. This is known as an abstract class. Class Sound is the root of a hierarchy of classes, each of which inherits from its parent (see Figure 2.2). Class hierarchies are an essential structure of object-oriented programming. The Sound class hierarchy is a library of building blocks which can be combined to specify complex timbres. Each subclass of Sound defines the function next_sample. This consistent interface allows objects to pass samples while hiding the algorithms used to generate them. In some cases the next sample can be computed from the instance variables in the object itself.

Samp Lookup::next_sample()
{
  phase += phaseinc;
  if ((phase + 0.5) >= tlength) phase -= tlength;
  return table->entry((int) phase);
}

Figure 2.3. Lookup next_sample method

A lookup table oscillator uses only its waveform table, phase pointer, and phase increment to compute its next sample (Figure 2.3).

Samp BandPass::next_sample()
{
  Samp new_in = source->next_sample();
  Samp kv1 = new_in + parm3 * state0 + parm4 * state1;
  Samp kv2 = state1;
  state1 = state0;
  state0 = kv1;
  return parm9 * (kv1 - kv2);
}

Figure 2.4. Bandpass next_sample method

On the other hand, some objects use pointers to other objects to compute their next sample. For instance, a band pass filter has a pointer called source which points to the Sound object being filtered (Figure 2.4). When objects like the band pass filter are assembled into timbres they form an instance tree. Objects which compute their samples from instance variables alone are the leaves of these instance trees.


[Bill's Home Page] Comments to walker@shout.net