# Polyphony and midi controllers

**URL:** <https://in-thread.sonic-pi.net/t/polyphony-and-midi-controllers/8420>\
**Category:** Support, Help & Resources\
**Created:** [December 3, 2023, 10:49pm UTC](https://in-thread.sonic-pi.net/t/polyphony-and-midi-controllers/8420 "2023-12-03T22:49:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rebelcello](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/rebelcello/32/2602_2.png) [@rebelcello](https://in-thread.sonic-pi.net/u/rebelcello)\
**Post date:** [December 3, 2023, 10:49pm UTC](https://in-thread.sonic-pi.net/t/polyphony-and-midi-controllers/8420/1 "2023-12-03T22:49:19Z")

</div>

Hello, apols if this has been covered, have searched but not found this topic :sweat\_smile …  
Is it possible to play out multiple midi channels, triggered by Sonic Pi?

I have Novation Circuit Tracks working with SP, would like to allocate different channels with different sounds. The code run is showing **channel 1** , though **midi out** is set to **all** and the code below has no channel 1.

Have been trying code like this (be warned, am a beginner!): Thanks for any help or suggestions you can give, not sure if this setup is possible.

> Blockquote  
> use\_midi\_logging port: “circuit\_tracks\_3”

use\_bpm 60

in\_thread do  
loop do  
midi channel: 2  
midi 75  
sleep 0.25  
midi 81  
sleep 0.25  
midi\_all\_notes\_off  
end  
end

in\_thread do  
loop do  
midi channel: 5  
midi\_note\_on 44  
sleep 0.3  
midi\_note\_on 50  
sleep 0.3  
midi\_all\_notes\_off  
end  
end

in\_thread do  
loop do  
midi channel: 6  
midi\_note\_on 62  
sleep 0.3  
midi\_all\_notes\_off  
end  
end

loop do  
midi channel: 5  
midi 44  
sleep 0.3  
midi\_note\_on 39  
sleep 0.3  
midi\_all\_notes\_off  
end

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [December 4, 2023, 11:30am UTC](https://in-thread.sonic-pi.net/t/polyphony-and-midi-controllers/8420/2 "2023-12-04T11:30:11Z")

</div>

Hi Jules  
There are a few things that need changing in your code. I came up with the version below, and I’ll discuss some of the changes. First, when you paste code on in\_thread it is a good idea to put three back ticks `on the line before the code starts, and a further three` on the line after the code ends. This formats it nicely and makes it easier to copy.

```ruby
use_bpm 60
use_midi_logging true
use_midi_defaults port: "circuit_tracks_3" #set the destination port

# midi_all_notes_off channel: "*" ; stop

live_loop :m1 do
  use_merged_midi_defaults channel: 2
  midi 75,sustain: 0.5
  sleep 0.25
  midi 81,sustain: 0.25
  sleep 0.25
end

live_loop :m2 do
  use_merged_midi_defaults channel: 5
  midi 44,sustain: 0.6
  sleep 0.3
  midi 50,sustain: 0.3
  sleep 0.3
end

live_loop :m3 do
  use_merged_midi_defaults channel: 6
  midi 62,sustain: 0.3
  sleep 0.3
end

live_loop :m4 do #this live loop gives example of using midi_note_on
  use_merged_midi_defaults channel: 5
  midi_note_on 44
  sleep 0.3
  midi_note_on 39
  sleep 0.3
  midi_all_notes_off #uses currently specified channel
end

```

The first change you’ll notice is how to specify the midi port you want to send to (I assume it is “circuit\_tracks\_3” If your novation device is connected it should display the port name in the IO menu Midi Outputs section on Sonic PI.

Secondly, it is much neater not to use loop do inside an in\_thread but instead to use a live\_loop as shown. The main difference is that you have to give each live loop a unique name. Here I arbitrarily used :m1, :m2 :m3…

Third lets look at the actual midi command. By itself it will automatically send a midi\_note\_on and then 1 beat later a midi\_note\_off. If youi want the note to last for a different duration you can use `midi 72,sustain: 0.3` for example which will last for 0.3 seconds at 60bpm  
You can also specify the channel :and/or port: directly in each midi command if you wish, but it is easier to use the `use_midi_defaults` or in this case `use_merged_midi_defaults` command which means that you can specify the channel just once in each live loop rather than for every note. The merged bit allows it to use the port: setting which then is only typed once art the top of the program and merged into the separete channel settings.

You can (as you did) uae seperate midi\_note\_on commands as shown in live\_loop :m4

Finally I would incorporate a paniic line  
`# midi_all_notes_off channel: "*" ; stop`  
at the start of the program. When you stop the preograms running it will be in the middle of live loops and some notes will still be sounding. You can then uncommnet this line and run again,a dn it will stop all the currently sounding notes. Note specifying the channel: “\*” is important to make sure it kills the notes on ALL channels.  
Alternatively, there may be a panic button on your novation to stop all notes.

Have fun, getting it going with your Novation Circuit Tracks

---

<div class="post-metadata">

**Author:** ![rebelcello](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/rebelcello/32/2602_2.png) [@rebelcello](https://in-thread.sonic-pi.net/u/rebelcello)\
**Post date:** [December 4, 2023, 8:30pm UTC](https://in-thread.sonic-pi.net/t/polyphony-and-midi-controllers/8420/3 "2023-12-04T20:30:28Z")

</div>

Fabulous - thanks so much, Robin. I wouldn’t have got there without your help!

It worked well and I’ve played around with it. The Circuit pots can be used for FX, distort etc, can be heard here in action, first attempt 😃

https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F1681978167&show_artwork=true  
Noted about pasting code - thanks.  
V exciting!  
Jules

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [December 4, 2023, 9:55pm UTC](https://in-thread.sonic-pi.net/t/polyphony-and-midi-controllers/8420/4 "2023-12-04T21:55:54Z")

</div>

Glad you got it going. Sounds good on SoundCloud. Have fun developing it further.  
Robin
