# Choose MIDI Chord

**URL:** <https://in-thread.sonic-pi.net/t/choose-midi-chord/3673>\
**Category:** Support, Help & Resources\
**Created:** [April 20, 2020, 12:02am UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673 "2020-04-20T00:02:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 12:02am UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/1 "2020-04-20T00:02:31Z")

</div>

Hi, everyone.

Why does the following not work?

```ruby
chords = [(chord :C, :minor7), (chord :Ab, :major7)].ring

thisChord1 = chords.choose

live_loop :midich do
  midi thisChord1, release: 0.6
  sleep 0.5
end

```

I was hoping to swap the “play” in my existing code with “midi”, but no dice.

I can get midi chords to play, but only if I choose those chords ahead of time. How to I play randomly selected midi chords the way I am able to with the built in synths?

Thank you so much,  
Jack

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 20, 2020, 7:16am UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/2 "2020-04-20T07:16:11Z")

</div>

Hi @jackdire,

before answering, would you please tell us if you only want to play this chords inside sonic pi or if you really want to send chords to another midi device, software ?

For play them into sonic pi, just try this :

```ruby
chords = [(chord :C, :minor7), (chord :Ab, :major7)].ring

live_loop :without_midi do
  play chords.choose, release: 0.6
  sleep 0.5
end

```

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 20, 2020, 7:41am UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/3 "2020-04-20T07:41:13Z")

</div>

After some research

Sonic Pi does not support chords to be played via midi natively. But we can create a function to achieve that.

@robin.newman proposed a solution in 2017 [https://github.com/samaaron/sonic-pi/issues/1691](https://github.com/samaaron/sonic-pi/issues/1691)

so you can do that

```ruby
 use_midi_defaults port: "midi_through_port-0", channel: 1

define :midi_chord do |notes, *args|
  notes.each do |note|
    midi note, *args
  end
end

live_loop :play_chords_via_midi do
  midi_chord chords.choose, release: 0.6
  sleep 0.5
end

```

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/b/b17e2aa10072edb4c23fc5f2b81a57650620195c.png)

If you are on windows, you have to create a midi loop via [https://www.tobias-erichsen.de/software/loopmidi.html](https://www.tobias-erichsen.de/software/loopmidi.html)

au plaisir !

---

<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:** [April 20, 2020, 10:03am UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/4 "2020-04-20T10:03:14Z")

</div>

Thanks @nlb I was just about to dig out that routine and post it!

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 20, 2020, 10:05am UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/5 "2020-04-20T10:05:22Z")

</div>

you’re welcome “rendre à César ce qui appartient à César”.

but why this feature is not natively supported @samaaron ?

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 2:17pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/6 "2020-04-20T14:17:51Z")

</div>

Thank you so much for the responses. I am using this to fire off some Native Instruments in Ableton as well as a hardware Minitaur, Sirin, SH-01A, and Erebus through Ableton. I hope.

I couldn’t get your code to send midi as written, so I messed around and made these changes, which work now:

```ruby
chords = [(chord :C, :minor7), (chord :Ab, :major7)].ring

define :midi_chord do |notes, *args|
  notes.each do |note|
    midi note, *args
  end
end

live_loop :play_chords_via_midi do
  midi_chord chords.choose, release: 0.6, channel: 16
  sleep 0.5
end

```

What will I beak by nuking the use\_midi\_defaults line? I’m not assigning a port, and the IAC is picking it all up. Will this break something later?

And where can I find information on what is happening in this block:

```ruby
define :midi_chord do |notes, *args|
  notes.each do |note|
    midi note, *args
  end
end

```

A few terms and principles in there are new to me. I finally got my head around the non-midi stuff somewhat yesterday, but the \*args and |note| stuff is new to me now.

Thanks again. I really appreciate it.

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 3:26pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/7 "2020-04-20T15:26:23Z")

</div>

To further illustrate my total lack of understanding in that mystery block, I can’t see what is causing the following to play consistently random chords with each loop when the chord is chosen before the loop (a technique that worked for me with non-midi code yesterday).

```ruby
chords = [(chord :C, :minor7), (chord :Ab, :major7), (chord :Eb, :major7), (chord :Bb, "7")].ring

thisChord1 = chords.choose

define :midi_chord do |notes, *args|
  notes.each do |note|
    midi note, *args
  end
end

live_loop :channel16 do
  midi_chord thisChord1, release: 0.6, channel: 16
  sleep 0.5
end

```

What in that \*arg |note| sorcery block is changing all of this?

Thanks!

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 3:29pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/8 "2020-04-20T15:29:47Z")

</div>

To make all of this even weirder, I deleted the \*args block and it is still firing off midi in Ableton:

```ruby
chords = [(chord :C, :minor7), (chord :Ab, :major7), (chord :Eb, :major7), (chord :Bb, "7")].ring

thisChord1 = chords.choose

live_loop :channel16 do
  midi_chord thisChord1, release: 0.6, channel: 16
  sleep 0.5
end

```

So strange.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 20, 2020, 5:53pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/9 "2020-04-20T17:53:22Z")

</div>

chords = [(chord :C, :minor7), (chord :Ab, :major7), (chord :Eb, :major7), (chord :Bb, “7”)].ring

- the chosen is chord is memorised into a variable

```ruby
thisChord1 = chords.choose 

```

there the chord is chosen and is memorized into the variable `thisChord1`

- a personal function midi\_chord

```ruby
define :midi_chord do |notes, *args|
  notes.each do |note|
    midi note, *args
  end
end

```

then we create a “personal” function or method call it as you want.  
we name it `midi_chord` pretty name no ?  
and this function needs `arguments`, parameters if you prefer.

so the `notes` included into the chord chosen  
and then optional parameters, indeed we can give zero or more parameters so we use the `*`

- each : to loop every note of the chord

```ruby
notes.each do |note|
    midi note, *args
  end

```

But how can i guess the parameters the midi instruction can receive ?  
I encourage you to to read some docs :-). The sonic pi Help is a “merveille”

To do that place your cursor in the word `midi` and type `F1` then you will reach the good page given you an explanation about the midi instruction used into `midi_chord` function.

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/d/d7de0d3eb04d5df0617ca20c560843f20683f3cf.png)

- the midi\_chord function is called

```ruby
live_loop :channel16 do
  midi_chord thisChord1, release: 0.6, channel: 16 
 sleep 0.5
end

```

there the midi\_chord function is called and the variable `thisChord1` is given as first argument, then `release: 0.6, channel:16 ` also as following arguments.

![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/b/b67edcbecfed59be67566a5c461ab4b2f972225f.png)

Hope this can help you !

By the way, do you manage to get Ableton play something or not at all. Which os do you use ? Have you made a midi loop ?

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 6:02pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/10 "2020-04-20T18:02:34Z")

</div>

Thank you for all of this! That f1 trick for help was a big missing link for me.

But why is the chord randomly changing with every loop when the thisChord1 variable memorizes a chord outside of the live loop? That does not happen with the internal synths.

I have gotten Ableton on OSX to receive all of the midi being generated, thanks to some digging I did on here last night. The software synth in Ableton is working great. I just need the live loop to not change the chord played by the thisChord1 variable each time.

Thanks again!

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 20, 2020, 6:27pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/11 "2020-04-20T18:27:21Z")

</div>

> [@jackdire](#):
>
> But why is the chord randomly changing with every loop when the thisChord1 variable memorizes a chord outside of the live loop? That does not happen with the internal synths.

euh ? to be sure you don’t press the run button once your script is running ?  
is it your whole code ? Give us the code causing issue ?

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 6:46pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/12 "2020-04-20T18:46:49Z")

</div>

Sorry, it’s this code from a few posts back. I think I sent it while you were replying, so you may not have seen it:

```ruby
chords = [(chord :C, :minor7), (chord :Ab, :major7), (chord :Eb, :major7), (chord :Bb, "7")].ring

thisChord1 = chords.choose

define :midi_chord do |notes, *args|
  notes.each do |note|
    midi note, *args
  end
end

live_loop :channel16 do
  midi_chord thisChord1, release: 0.6, channel: 16
  sleep 0.5
end

```

When I run this, the chord changes with each loop. I can’t see what would cause that since thisChord1 is locked in before the loop.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 20, 2020, 7:03pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/13 "2020-04-20T19:03:42Z")

</div>

as you say it’s strange 🙂 . I test your code and the same chord is played with sp3.2.2 on ubuntu 18.04.04.  
So what sonic pi version do you use ?  
Is there somebody on mac, a mac u$er 🙂 ?

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 7:05pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/14 "2020-04-20T19:05:12Z")

</div>

UPDATE: I dropped a print thisChord1 inside the loop, and discovered the chord isn’t actually changing. Which is good. It just sounds like it is.

So I threw a print count into the loop as well to see what was happening in each loop. Check it out:

```ruby
{run: 1, time: 0.0, thread: :live_loop_channel16}
 └─ midi 74, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 0.0, thread: :live_loop_channel16}
 └─ midi 70, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 0.0, thread: :live_loop_channel16}
 └─ midi 80, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 0.0, thread: :live_loop_channel16}
 └─ midi 77, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 0.5, thread: :live_loop_channel16}
 └─ midi 77, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 0.5, thread: :live_loop_channel16}
 ├─ (ring <SonicPi::Chord :Bb :7 [70, 74, 77, 80])
 └─ 2
 
{run: 1, time: 0.5, thread: :live_loop_channel16}
 └─ midi 70, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 0.5, thread: :live_loop_channel16}
 └─ midi 74, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 0.5, thread: :live_loop_channel16}
 └─ midi 80, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 1.0, thread: :live_loop_channel16}
 └─ midi 77, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 1.0, thread: :live_loop_channel16}
 ├─ (ring <SonicPi::Chord :Bb :7 [70, 74, 77, 80])
 └─ 3
 
{run: 1, time: 1.0, thread: :live_loop_channel16}
 └─ midi 74, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 1.0, thread: :live_loop_channel16}
 └─ midi 80, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 1.0, thread: :live_loop_channel16}
 └─ midi 70, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 1.5, thread: :live_loop_channel16}
 ├─ (ring <SonicPi::Chord :Bb :7 [70, 74, 77, 80])
 └─ 4
 
{run: 1, time: 1.5, thread: :live_loop_channel16}
 └─ midi 80, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 1.5, thread: :live_loop_channel16}
 └─ midi 70, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 1.5, thread: :live_loop_channel16}
 └─ midi 77, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 1.5, thread: :live_loop_channel16}
 └─ midi 74, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.0, thread: :live_loop_channel16}
 └─ midi 74, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.0, thread: :live_loop_channel16}
 └─ midi 80, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.0, thread: :live_loop_channel16}
 └─ midi 70, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.0, thread: :live_loop_channel16}
 └─ midi 77, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.0, thread: :live_loop_channel16}
 ├─ (ring <SonicPi::Chord :Bb :7 [70, 74, 77, 80])
 └─ 5
 
=> Stopping all runs...

=> Stopping run 1

{run: 1, time: 2.5, thread: :live_loop_channel16}
 └─ midi 80, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.5, thread: :live_loop_channel16}
 └─ midi 74, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.5, thread: :live_loop_channel16}
 └─ midi 70, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.5, thread: :live_loop_channel16}
 └─ midi 77, 127, sustain: 1.0, port: "*", channel: 16
 
{run: 1, time: 2.5, thread: :live_loop_channel16}
 ├─ (ring <SonicPi::Chord :Bb :7 [70, 74, 77, 80])
 └─ 6

```

The chord sounds different each time because the midi\_chord function is handling/playing that chord’s notes so differently each time. It there something I can do to make it more consistent?

Here is the full code:

```ruby
chords = [(chord :C, :minor7), (chord :Ab, :major7), (chord :Eb, :major7), (chord :Bb, "7")].ring

thisChord1 = chords.choose
theCount = 1

define :midi_chord do |notes, *args|
  notes.each do |note|
    midi note, *args
  end
end

live_loop :channel16 do
  midi_chord thisChord1, release: 0.6, channel: 16
  sleep 0.5
  print thisChord1
  theCount = theCount+1
  print theCount
end

```

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 20, 2020, 7:10pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/15 "2020-04-20T19:10:23Z")

</div>

not sure you can rely on the order of output with `print` but i may be mistaken.

what if you change the instrument midi into Ableton ? and please give us a screenshot of midi note into ableton by record the track ?

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 7:20pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/16 "2020-04-20T19:20:29Z")

</div>

Ah, I had not considered that the output sequence may be inaccurate. Good idea with recording the midi notes. Here is what came in:

 ![Screen Shot 2020-04-20 at 12.16.36 PM](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/9/994cdf4aebf9ad327726fc0beff7e7dee89761f8.png)

I then thought maybe the timing of the notes was causing the chord sound to change each time, so I quantized the recorded midi and aligned all of the notes. Unfortunately, that didn’t help. The sound still changes.

I am testing with random built-in software synths in Ableton. I have tested it with about 5 or 6 now, and they all behave the same. The chord keeps changing sound.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 20, 2020, 7:23pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/17 "2020-04-20T19:23:49Z")

</div>

why other pink notes than the notes you sent via sonic pi ?  
otherwise i don’t see why and let it the question to others people.  
Cheers

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 7:27pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/18 "2020-04-20T19:27:11Z")

</div>

I just loaded up Hybrid Keys by Native Instruments, and it sounds great. The chord sounds the same each time. I will test with other plugins, but I have run it multiple times with multiple random seeds just now on Hybrid Keys, and it works beautifully. So it looks like this technique very much depends on the software synth being used.

Fingers crossed that the SH-01A, Reface DX, and Digitone hardware synths behave as well as Hybrid Keys. In typing this I realized this won’t be a problem on the monophonic Minitaur and Sirin, so at least I’m good there.

Thanks again for all of your help with this. It means a lot to me.

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 7:28pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/19 "2020-04-20T19:28:07Z")

</div>

And yeah I don’t know where those other droney notes are coming from. I’m about to test all of that out. 🙂

---

<div class="post-metadata">

**Author:** ![jackdire](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jackdire](https://in-thread.sonic-pi.net/u/jackdire)\
**Post date:** [April 20, 2020, 7:31pm UTC](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673/20 "2020-04-20T19:31:31Z")

</div>

I just recorded in Ableton without Sonic Pi running, and it is capturing those extra notes. So hey future people looking at this, the code is not making those extra notes happen. I have some kind of midi leak somewhere on my ship. I’ll start poking around to find it.

[Next page](https://in-thread.sonic-pi.net/t/choose-midi-chord/3673.md?page=2)
