Page 180 - Hacking Roomba
P. 180

Chapter 8 — Making Roomba Sing                161




                               Listing 8-4 Continued

                               roombacomm.pause(50);

                               ArrayList notelist = RTTTLParser.parse(rtttl);
                               int songsize = notelist.size();
                               // if within the size of a roomba song,  make the song, then
                               play
                               if( songsize <= 16 ) {
                                 println(“creating a song with createSong()”);
                                 int notearray[] = new int[songsize*2];
                                 int j=0;
                                 for( int i=0; i< songsize; i++ ) {
                                   Note note = (Note) notelist.get(i);
                                   int sec64ths = note.duration * 64/1000;
                                   notearray[j++] = note.notenum;
                                   notearray[j++] = sec64ths;
                                 }
                                 roombacomm.createSong(1, notearray);
                                 roombacomm.playSong(1);
                               }
                               // otherwise, try to play it in realtime
                               else {
                                 println(“playing song in realtime with playNote()”);
                                 int fudge = 20;
                                 for( int i=0; i< songsize; i++ ) {
                                   Note note = (Note) notelist.get(i);
                                   int duration = note.duration;
                                   int sec64ths = duration*64/1000;
                                   if( sec64ths < 5 ) sec64ths = 5;
                                   if( note.notenum != 0 )
                                     roombacomm.playNote(note.notenum, sec64ths);
                                   roombacomm.pause(duration + fudge);
                                 }
                               }
                               System.out.println(“Disconnecting”);
                               roombacomm.disconnect();





                     RoombaMidi: Roomba as MIDI Instrument

                             If you have a MIDI keyboard or a MIDI sequencer and you want to use Roomba as a MIDI
                             instrument, that’s possible too. As mentioned earlier, Java provides a basic MIDI API, but it
                             isn’t well-developed across platforms. Fortunately Mac OS X does provide Java wrappers to its
                             entire Core MIDI C language API, making programming MIDI applications in Java possible.
                             Because of Core MIDI, these Java applications appear as just another MIDI device to other
   175   176   177   178   179   180   181   182   183   184   185