Menu Close

My Midi

Download today the trial tutorial stack of sunnYmidi. Updated the 1st of October 2012.

This tutorial will show you how to connect 2 Midi sources (Input) and 2 Midi destinations (Output) devices.
Better have a look at the documentation Easy Fast Midi Player and Connecting Midi Devices before coding with sunnYmidi© API.
This tutorial focuses on Midi connexions and the last chapter shows you how you can work with Midi raw datas or Sysex messages.

My Midi context

Here is my Midi configuration for this tutorial.

What do I want to do?

As an exercise, and this was actually my Midi config I used for testing SunnYmidi©, I want to do these tasks within a single LiveCode stack:
Identify the 2 Midi source devices: my Yamaha piano is connected to my Mac with a Midi-USB cable and MidiKeys which is a virtual keyboard you can get for free.
Identify the 2 Midi destination devices: again my Yamaha piano plus the Midi Monitor which will act as a virtual player dumping all received Midi notes as text. This feature is very valuable for testing your LiveCode stack. (Midi Monitor is also a free Mac app)
Connect the 4 devices to LiveCode: that’s the only thing to do to be able to send or receive Midi events from or to physical or virtual devices.
Play drums on the Yamaha piano: LiveCode will play drums for each key pressed on my Yamaha.
Play different instruments with the Midi Keys app: by using your Mac keyboard or the mouse over the Midi Keys window notes will play on different instruments selected via the Midi Keys Channel menu.
LiveCode sends notes to the Yamaha: for instance, clicking a button in LiveCode will play a note on the Yamaha.
LiveCode sends notes to the Midi Monitor app: for instance, clicking a button in LiveCode will print a note on the Midi Monitor as text.
Setting a muti-timbre for my Yamaha piano: With the sunnYmidi.Connect and sunnYmidi.Set “sysex” commands, learn how to play few voices on different channels on your external device. This scenario is specific to my Yamaha Clavinoca piano, but you get the idea for any others devices.

Identify the 2 Midi source devices

on mouseUp
   put sunnYmidi.Get("numberOfMidiSources") into ns
   log "numberOfMidiSources ",ns
   repeat with i = 1 to ns
      get sunnYmidi.Get("midiSource",i,"dump")
      log  tab , i ,": " , IT
   end repeat
   putlog
end mouseUp

And the result of this script lets us know that the Yamaha piano (via Usb) is on the device 2 and MidiKeys app on device 3. We need these 2 values for the connection.

numberOfMidiSources 4
        1: Source Bus1 NotConnected
        2: Source USB Midi Cable NotConnected
        3: Source MidiKeys NotConnected
        4: Source SunnYmidi NotConnected

Identify the 2 Midi destination devices

on mouseUp
   put sunnYmidi.Get("numberOfMidiDestinations") into nd
   log "numberOfMidiSources ",ns
   repeat with i = 1 to ns
      get sunnYmidi.Get("midiDestination",i,"dump" )
      log  tab,i,": ",IT
   end repeat
   putlog
end mouseUp

And the result of this script lets us know that the Yamaha piano (via Usb) is on the device 2 and Midi Monitor app on device 3. We need these 2 values for the connection.

numberOfMidiDestinations 3
        1: Destination Bus1 NotConnected
        2: Destination USB Midi Cable NotConnected
        3: Destination MIDI Monitor (Untitled) NotConnected

Connect the 4 devices to LiveCode

Now that we have our device numbers, we can connect them to LiveCode and set a few properties at the same time . This is straightforward and simple.

on mouseUp
   put 3 into device_index              -- midi Keys
   put 0 into Remap2channel
   put "*4" into Volume
   sunnYmidi.Connect "Source",device_index,Remap2channel,Volume
   
   put 2 into device_index              -- Yamaha piano
   put 10 into Remap2channel
   put "+100" into Volume
   sunnYmidi.Connect "Source",device_index,Remap2channel,Volume
   
   put 2 into device_index              -- Yamaha piano
   put 3 into channel
   sunnYmidi.Connect "Destination",device_index,channel
   
   put 3 into device_index              -- Midi Monitor
   put 5 into channel
   sunnYmidi.Connect "Destination",device_index,channel
end mouseUp

Play drums on the Yamaha piano

Just sit in front of the piano and play ūüôā
Because we remap our Yamaha source to channel 10 above, this will play drums on the Mac. Not really much to say.

Play different instruments with the Midi Keys app

Use the mouse and click on the virtual Midi Keys keyboard or type on your normal keyboard.
First select an instrument previously with: sunnYmidi.Set 4,”Instrument”,33; then select the same channel (4) on the Midi Keys interface and you can start your band. You can do that on all Midi channels.

LiveCode sends notes to the Yamaha

As we have connected our Yamaha to channel 3, just script this line to play a note.

on mouseUp
  sunnYmidi.Play 3, 1000, 128, 53
end mouseUp

LiveCode sends notes to the Midi Monitor app

As we have connected Midi Monitor to channel 5, just script this line to send a note.

on mouseUp
  sunnYmidi.Play 5, 1000, 126, 53
end mouseUp

And you can see the result on the screen capture below.
On Midi Monitor screen you can see that we have selected only “Act as a destination”. You need to do that so you can see it in LiveCode with the others destination. This tool is a gem to debug your Midi creation when coding.

Setting a muti-timbre for my Yamaha piano

I have a Yamaha Clavinoca piano which has 8 timbres. I would like to associate each timbre to a specific channel.
First thing to do, is to connect my Yamaha as a destination. Then, I have to tell my Yamaha to play timbre 1 from channel 1, timbre 2 from channel 2 and so on. I also need to tell the Yamaha to be in a multi-timbre state ( by default, it is OFF ). For the purpose of this tutorial, I want also to set the volume of the first 4 channels or voices to a low value, the other 4 to a higher value. Here is the commented LiveCode script which does exactly that:

on mouseUp
   -- connect the Yamaha
   put 3 into index_destination
   sunnYmidi.Connect "destination",index_destination,1,2,3,4,5,6,7,8
   PerrorQuit  "Set Yamaha connection"
   
   -- Multitimbre ON
   put 1 into channel
   sunnYmidi.Set channel, "sysex", "F043733215F7", 1
   PerrorQuit  "Yamaha MultiTimbre ON"
   
   repeat with i=0 to 7
      -- Change of patch ( 8 instruments on my Yamaha )
      put "C" & i & "0" & i into RawData
      sunnYmidi.Set channel, "sysex", RawData, 1
      PerrorQuit  "Yamaha Set Patchs"
      -- Set volume for each channel
      if i < 4 then
         -- Channel 1 in LC means 0 in Midi
         sunnYmidi.Set i+1, "sysex", "B" & i & "0710" -- low
      else
         sunnYmidi.Set i+1, "sysex", "B" & i & "077f"  -- high
      end if
      PerrorQuit  "Yamaha volume"
   end repeat
   
   -- Try out every timbre of the Yamaha
   repeat with i=1 to 8
      sunnYmidi.Play i, 950, 70, 60
      wait 1 seconds
   end repeat
   
   -- Multitimbre OFF
   sunnYmidi.Set channel, "sysex", "F043733213F7"
   PerrorQuit  "Yamaha MultiTimbre OFF"
end mouseUp

on PerrorQuit
        -- Print Error and exit
        -- ...
end PerrorQuit

Conclusion

Hope this tutorial will help you to start with SunnYmidi©. For further understanding, you can download the SunnYmidi© demo which includes a LiveCode stack with a lot of small scripts describing most of the functionalities. Enjoy!

Thierry Douez. 2012-07-27