# Midi sync vs. control

**URL:** <https://in-thread.sonic-pi.net/t/midi-sync-vs-control/4366>\
**Category:** Support, Help & Resources\
**Created:** [September 4, 2020, 8:58pm UTC](https://in-thread.sonic-pi.net/t/midi-sync-vs-control/4366 "2020-09-04T20:58:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwanab](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@bwanab](https://in-thread.sonic-pi.net/u/bwanab)\
**Post date:** [September 4, 2020, 8:58pm UTC](https://in-thread.sonic-pi.net/t/midi-sync-vs-control/4366/1 "2020-09-04T20:58:24Z")

</div>

This is probably an easy beginner error.

This code works fine:

```ruby
note = 0
use_synth :dsaw

live_loop :midi_piano do
  use_real_time
  note, velocity = sync "/midi:mio:0:1/note_on"
end

live_loop :midi_cc do
  use_real_time
  controller, value = sync "/midi:mio:0:1/control_change"
  play note, amp: value / 127.0 if controller == 2
end

```

But, this code doesn’t and I don’t understand why. The only difference is that it creates the play on the synth at the top as s, then uses control s to change the note and amp:

```ruby
note = 0
use_synth :dsaw
s = play note, attack: 10, attack_level: 0

live_loop :midi_piano do
  use_real_time
  note, velocity = sync "/midi:mio:0:1/note_on"
end

live_loop :midi_cc do
  use_real_time
  controller, value = sync "/midi:mio:0:1/control_change"
  control s, note: note, amp: value / 127.0 if controller == 2
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:** [September 5, 2020, 7:35am UTC](https://in-thread.sonic-pi.net/t/midi-sync-vs-control/4366/2 "2020-09-05T07:35:21Z")

</div>

> [@bwanab](#):
>
> ```ruby
> note, velocity = sync "/midi:mio:0:1/note_on"
> 
> ```

it seems a `play` is missing ` play note`

---

<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:** [September 5, 2020, 10:57am UTC](https://in-thread.sonic-pi.net/t/midi-sync-vs-control/4366/3 "2020-09-05T10:57:50Z")

</div>

I would tackle it like this:

```ruby
use_synth :dsaw

with_fx :level,amp: 0 do |v|
  set :v,v
  live_loop :midi_piano do
    use_real_time
    note, velocity = sync "/midi*/note_on"
    play note, amp: velocity/127.0 if velocity > 0
  end
end

live_loop :controlVol do #control level of fx envelope
  use_real_time
  controller, value = sync "/midi*/control_change"
  control get(:v), amp: value/127.0 if controller==2
end

```

I’ve placed the live loop midi\_piano inside an fx :level wrapper. The individual notes played in that live loop have their volume affectd in two ways. First the velocity with which the note is played on the keyboard, but secondly this is attenduated by the fx wrapper. The amp: setting of this is intially set to 0 and a reference to the =effect v is then stored in the time line using the command `set :v,v`  
A second live loop controlVol is then used to retrieve the value from the midi cc control number 2 and this is used to control the amplitude of the fx :level wrapper. It retrieves the pointer to the fx wrapper using get(:v) and controls the setting of the amp: for the wrapper, acting as an overall volume control.

When you start playing you will hear nothing until you adjust midi control 2 to raise the wrapper amplitude setting. If you prefer, you can set the inital value of the wrapper volume to 1 or 0.5 in the line  
`with_fx :level, amp: 0 do |v|` changing it to say  
`with_fx :level, amp: 0.5 do |v|`
