Figure 18. A MIDIplay Script

-------------------------      Play Midi Note     -------------------------
-- These button scripts cause a random tone to be played on a MIDI       --
-- synthesizer, using MIDIplay external commands.                        --
---------------------------------------------------------------------------

-------------------------    mouseUp handler      -------------------------
-- Sets timbre to piano or violin tone (50% chance of each). Generates   --
-- midi note number randomly, and plays note for 1 second.  Note: midi   --
-- note numbers are integers, with 60 = middle C. Here we choose a       --
-- random number between 1 and 35, and add 47 to it, so we get a note    --
-- between 48 (C3) and 84 (C6).
---------------------------------------------------------------------------
on mouseUp
  if random(2) = 1
  then MIDIplay "xmit","t 192 0"       -- piano tone
  else MIDIplay "xmit","t 192 40"      -- violin tone
  put random(37) + 47 into note        -- get random number between 48 & 84
  mplay note, 60                       -- play note for 60 ticks (1 sec)
end mouseUp

-------------------------          mplay          -------------------------
-- Sends a "note on" signal, waits x ticks, and sends "note off".        --
---------------------------------------------------------------------------
on mplay note, x             -- note is midi note code (C4=60)
  MIDIplay "xmit", "t 144" && note && "100"     -- note on
  wait x ticks
  midiplay "xmit", "t 128" && note && "0"       -- note off
end mplay