Page 176 - Hacking Roomba
P. 176

Chapter 8 — Making Roomba Sing                157




                               Listing 8-2: RoombaComm playNote()

                               public void playNote(int note, int duration) {
                                 byte cmd[] = {(byte)SONG, 15, 1,
                                                (byte)note, (byte)duration,  // define “song”
                                                (byte)PLAY, 15 };            // play it back
                                 send(cmd);
                               }






                     Playing Roomba as a Live Instrument

                             Now that you can play single musical notes on Roomba, it should be easy to turn it into a
                             musical instrument that can be played live. Although Java offers some amount of MIDI sup-
                             port, it doesn’t offer the feature needed to turn the Roomba into a MIDI instrument: the
                             creation of virtual MIDI destinations. Also, not everyone has a MIDI keyboard and a MIDI
                             interface, but everyone should definitely be allowed to use their robotic vacuum cleaners to jam
                             along with their favorite MIDI tunes.
                             The computer keyboard offers a workable replacement to a musical keyboard and because of
                             the Roomba’s limitations, using it doesn’t affect the articulation of the Roomba as an instru-
                             ment. The computer keyboard’s simplicity of input mirrors the Roomba’s simplicity of output.
                             Figure 8-4 shows the computer keyboard to musical keyboard mapping to be used in a
                             Processing sketch called RoombaTune, shown in Listing 8-3. The a key becomes a C note,
                             the w key becomes a C# note, the s key becomes a D note, and so on. The mapping allows
                             only a little more than an octave, so the z and x keys are used to move the keyboard up and
                             down an octave.



                               `    1   2    3    4    5    6   7    8    9    0    -   =    Del

                               Tab   q    w    e    r    t   y    u    i    o    p   [    ]    \

                               Caps   a    s    d    f    g    h    j   k    l    ;    ‘   Return

                                Shift    z   x    c    v    b    n   m    ,    .    /      Shift

                                         –   +
                                         Octave
                             FIGURE 8-4: The RoombaTune computer keyboard mapping of musical keyboard
   171   172   173   174   175   176   177   178   179   180   181