3 minutes to emulate PlayCommand agent X on (Mountain) Lion

Thierry Douez th.douez at gmail.com
Thu Oct 11 16:08:08 EDT 2012

3 minutes to emulate PlayCommand agent X on (Mountain) Lion !

Today I got the documentation of Shakobox which works
with PlayCommand Agent X. The API is quite short:

SBplay instrument,tempo,notestring
--	Plays a string of notes, formatted like the play command in HyperTalk.
--	Instrument can be a name or a number.

SBstartNote instrument,pitch,velocity
--	Starts playing a note. Instrument is a name or number. Pitch ranges
from 0 to 127
--	with 60 being middle C. Velocity ranges from 0 to 127. A velocity
of 0 will stop the
--	note (see below).

SBstopNote instrument,pitch
--	Stops a note being played by SBstartNote. Same as specifying 0 for
the velocity
--	in the above command.

In the documentation of sunnYmidi on http://sunny-tdz.com, you can find:

sunnymidi.HCplay channel, tempo, notestring
--  Plays a string of notes A la Hypercard

sunnYmidi.Play, channel, -1, velocity, pitch
--  Plays a note forever
sunnYmidi.Play, channel, 0, 0, pitch
--  Stops the note

Obviously, those 2 sets of commands are quite similar!

But with shakobox, the commands are associated with an instrument,
while with sunnYmidi they are associated with a channel.

So, another command from my documentation:

sunnYmidi.Set channel, "instrument", instrumentID

With this in mind, I write the 3 shakobox handlers like this:

on SBplay instrument, _tempo, notestring
   sunnYmidi.Set 1, "instrument", instrumentID( instrument)
   sunnymidi.HCplay 1, _tempo, notestring
end SBplay

on SBstartNote instrument, pitch, velocity
   sunnYmidi.Set 1, "instrument", instrumentID( instrument)
   sunnYmidi.Play, 1, -1, velocity, pitch
end SBstartNote

on SBstopNote instrument,pitch
   sunnYmidi.Set 1, "instrument", instrumentID( instrument)
   sunnYmidi.Play, 1, 0, 0, pitch
end SBstopNote

And to finish, I have to code this function:

private function  instrumentID instrument
   -- assuming the ID is correct
   if instrument is a number then return instrument
   -- custom prop containing on each line:  IDinstrument,InstrumentName
   get the GM_Instruments of this stack
   filter IT with "*," & instrument
   if IT is empty then return 1 -- piano
   return item 1 of IT -- the ID
end instrumentID

I put all the scripts mentioned above in a new stack script
and set the externals property of this stack.

Then, I create a button and copy-paste the following script from the
Shakobox documentation:

on mouseup
  -- 	"Nic's Russian Tune (Katyushka)" from "Knit Your Own Yoghurt"
  put "e4q. f#4e g4q. e4e gq f#e ee f#q b3q f#q. ge aq. f#e aq ge f#e
eq rq " & \
    "b4q e5q d5q e5e d5e c5q b4e a4e b4q e4q c5q. a4e b4q. g4e a4q g4e
f#4e e4h " & \
    "g4q. a4e b4q. g4e b4q a4e g4e f#4q a4q a4q. b4e c5q. b4e c5q b4e
a4e g4q rq " & \
    "e5q g5q b4q b4e d5e e5q d5e c5e e5q b4q e5q. c5e e4q. b4e d#5q
e5e f#5e e5h"  \
     into theSong
  sbplay 24,125,theSong
end mouseup

I save my stack, close and open it, click the button and ....

     It works !

     And all that in only 3 minutes !

It shows that Livecode is a powerful tool to code fast
which is also true for sunnYmidi !

This stack will be available on my site in a couple of days.

Happy coding,


More information about the Use-livecode mailing list