Resources: Drumball implementation
Here you can find out a little about the about the implementation of Mousepickle’s Drumball application for iPhone®, iPad®, and iPod touch®.
Try the other resources...
The underlying implementation of Mousepickle’s Drumball application might be of interest to other developers, so some details are given below.
The application shares a great deal of code with Tinkerball, but with a simpler physics model, and using OpenAL to play sampled sounds instead of creating waveforms on the fly.
A separate page is provided in case you’d prefer just to learn how to use Drumball.
The small spherelets that represent each of the beats are created from the same icosahedron model, or from one of a small number of hardcoded models approximating platonic solids.
Rotation of the model as a whole is constrained to axes that lie in the plane of the device display; by contrast the sphere in Tinkerball can be rotated about any axis.
Bulk rotation of the model is done entirely via OpenGL calls. Similarly the beats that sit on the sphere surface. There is no separate 'internal model' of the combined Drumball object that gives, for example, XYZ coordinates of any element.
Drumball builds on the OpenGL work undertaken for Tinkerball, with an additional tweaks:
Two off screen buffers are used to render low-resolution duplicates of the currently-drawn sphere and beats. One paints a 'bloated' version of each beat with a flat unique colour, and that allows us to identify when a beat was been touched (ready to drag or change it). The bloating, simple a change to the scale factor applied in OpenGL, makes the beats easier to touch.
The second one paints a 'shrunken' vearsion of each beat, again painted in a flat colour uniquely identifying the beat object; that allows us to play the sound file associated with each beat as it passes the ‘play line’ in the middle of the screen. The shrinking makes the beats play at a more regular interval.
A collection of 22.05 kHz audio clips provide the sounds for Drumball.
Whenever the rotational speed of the model is changed (and this is always in response to a user action, touching or dragging the surface of the sphere), the pitch of all currently-playing sounds is adjusted; this means the sound can be sped up, or slowed down, to match the rotation of the sphere.
Some sounds, like the metronomes, have a natural loop or beat to them in their own right. The ‘native’ BPM of the audio clips, and the pitch that we apply is calculated such that each clip fits a neat number of times into a whole number of rotations of the sphere. In this way the metronome loop stays in lock-step with the beats on the sphere surface, and any sound clips that have an inherent beat are always in time with the overall rhythm.
When a beat (or metronome) would be played too fast, at too high a pitch, or too slowly, at too low a pitch, we simply shift it an octave back towards it's natural rate by doubling or halving its OpenAL pitch setting.