# MIDI input experiment

**URL:** <https://in-thread.sonic-pi.net/t/midi-input-experiment/2050>\
**Category:** Creations & Ideas\
**Created:** [February 24, 2019, 9:41pm UTC](https://in-thread.sonic-pi.net/t/midi-input-experiment/2050 "2019-02-24T21:41:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zmatt](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/zmatt/32/673_2.png) [@zmatt](https://in-thread.sonic-pi.net/u/zmatt)\
**Post date:** [February 24, 2019, 9:41pm UTC](https://in-thread.sonic-pi.net/t/midi-input-experiment/2050/1 "2019-02-24T21:41:33Z")

</div>

First test at trying to control the music with MIDI. This controls the cutoff with a knob on the controller:

```ruby
    use_bpm 100

    co = 0 #cutoff

    live_loop "starp" do
      with_synth :dpulse do
        with_fx :reverb, room: 0.8 do
          play_pattern_timed [:c4, :e4, :g4, :b4, :c5, :b4, :g4, :e4], [0.25],
            attack: 0.1,
            release: 0.25,
            cutoff: co
        end
      end
    end

    loop do
      ctrl, co = sync "/midi/mpkmini2/0/1/control_change"
    end

```

The first thing I’ve noticed is that the cutoff value doesn’t change until the next iteration of the play\_pattern\_timed, which isn’t exactly what I want, suppose that I could change it to 8 individual plays and sleeps.

Any other pointers?

EDIT: oops, should point out that this is not my composition. Y’all probably recognize it right off.

---

<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:** [February 24, 2019, 10:54pm UTC](https://in-thread.sonic-pi.net/t/midi-input-experiment/2050/2 "2019-02-24T22:54:42Z")

</div>

EDIT Shouldn’t try and do something quickly just before going to bed 🙂  
Some errors and unnecessary bits in the original now corrected.  
The program below will work (based on yours)

```ruby
use_bpm 100

co = 0 #cutoff

live_loop "starp" do
  n=(ring :c4, :e4, :g4, :b4, :c5, :b4, :g4, :e4)
  d=knit 0.25,8)
  with_synth :dpulse do
    with_fx :reverb, room: 0.8 do
      play n.tick,
        attack: 0.1,
        release: 0.25,
        cutoff: co
      sleep d.look
    end
  end
end

live_loop :cl do
  ctrl,co = sync "/midi/*/*/*/control_change"
  puts co
end

```

Notes:  
I have used wild cards in the midi sync function.  
I have removed the play pattern as this introduces a long delay before you can change the cutoff value, and instead used separate rings for the note and durations so that you can alter the cutoff for each note. {The knit function here produces (ring 0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25) }  
It would be better to use set and get to pass the co value between the loops ie

`set :c0,0` at the beginning and then  
get(:co) wherever you want to retrieve the co value and  
`set :co,co`  
in the second loop :cl to store the co value read in the second loop.  
You could set up individual values in d for more complex rhythms instead of all the same values.

---

<div class="post-metadata">

**Author:** ![Eli](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/eli/32/116_2.png) [@Eli](https://in-thread.sonic-pi.net/u/Eli)\
**Post date:** [February 25, 2019, 1:28am UTC](https://in-thread.sonic-pi.net/t/midi-input-experiment/2050/3 "2019-02-25T01:28:51Z")

</div>

Robin’s done the technical ‘fixy’ bit… so my turn… 🙂

I dont know where you got your example from, but if you expand  
it just a little, it becomes really nice.

Eli…

```ruby
use_bpm 100

co = 0 #cutoff

with_fx :reverb, room: 0.8 do
  live_loop "starp2" do
    with_synth :dpulse do
      
      with_transpose -32 do
        play_pattern_timed [:c4, :e4, :g4, :b4, :c5, :b4, :g4, :e4], [2],
          attack: 0.1,
          release: 2,
          cutoff: co
      end
    end
  end
  
  sleep 8
  
  live_loop "starp1" do
    with_synth :dpulse do
      with_transpose -12 do
        if rand() < 0.65
          sleep 4
        else
          play_pattern_timed [:c4, :e4, :g4, :b4, :c5, :b4, :g4, :e4], [0.5],
            attack: 0.1,
            release: 0.5,
            cutoff: co
        end
      end
    end
  end
  
  sleep 4
  
  live_loop "starp" do
    with_synth :dpulse do
      if rand() > 0.65
        sleep 4
      else
        play_pattern_timed [:c4, :e4, :g4, :b4, :c5, :b4, :g4, :e4], [0.25],
          attack: 0.1,
          release: 0.25,
          cutoff: co
      end
    end
  end
  
end
```
