Controlling Master volume throught MIDI

Hi! I was thinking if it is possible to control the master volume with a knob of my MIDI controller. I’ve found out that I can use set_volume! to choose the initial master volume, but I don’t know how to control it with the knob. Thank you!

Here you are. I used a controller knob which sent midi cc messages to control 17 on my keyboard

live_loop :vcontrol do
  use_real_time
  control_num,val = sync "/midi/*/*/*/control_change"
  if control_num==17
    puts "controller number 17 detected: setting volume to #{val.to_f/127} "
    set_volume! (val.to_f/127)
  end
end

live_loop :pl do #to play some notes so you can hear the volume change
  play scale(:c4,:major,num_octaves: 2).choose, release: 0.2
  sleep 0.2
end

Remember to reset the volume back to 1 at the end as the last value will persist.
I used wild cards for the sync stgring so it will work with any connected controller on the specified ccontroll 17

1 Like

Hey Robin,
so this is a general method to assign a midi controller? I could use this also to change the random seed or the octave range, right?
I´m struggling with this topic but I hope this is the solution :slight_smile:

Hi all.
I’m trying to learn to map a knob. It doesn’t have to be volume.
What do I miss as a newbie?
YouTube of my failed try to map a knob
Kind regards
Relaxnow

#https://in-thread.sonic-pi.net/t/controlling-master-volume-throught-midi/1438
# How do I midi map knob to Sonic pi
# https://youtu.be/dbGlHmcxDzg

live_loop :vcontrol do
  use_real_time
  #control_num,val = sync "/midi/*/*/*/control_change"
  control_num,val = sync "/midi/2-_launch_control_1:11/control_change"
  #midi/2-_launch_control_1  #my novation launchcontrol
  if control_num==21#17
    puts "controller number 17 detected: setting volume to #{val.to_f/127} "
    set_volume! (val.to_f/127)
  end
end

live_loop :pl do #to play some notes so you can hear the volume change
  play scale(:c4,:major,num_octaves: 2).choose, release: 0.2
  sleep 0.2
end

Hi @Relaxnow,

Assuming you are using Sonic Pi >= v3.2 - you need to use a colon : to separate the midi label from the rest of the address, not a forward slash. (In v3.2, the addressing scheme of incoming MIDI and OSC events changed).
You can always use an asterisk * as a wildcard to match the addresses of incoming events as well - so /midi*/control_change should work too, assuming you’ve only got one device connected.
See also tutorial chapter 11.1 for details of receiving incoming MIDI events:

Thank you @ethancrawford
It works now :smiley:


# https://in-thread.sonic-pi.net/t/controlling-master-volume-throught-midi/1438
# Working: How to midimap a knob to Sonic pi?
# Remember to use ":" in "/midi:" after version 3.2


live_loop :vcontrol do
  use_real_time
    control_num,val = sync "/midi:2-_launch_control_1:11/control_change" 
    if control_num==21
    puts "controller number 21 detected: setting volume to #{val.to_f/127*4} "
    set_volume! (val.to_f/127*4)  # added *4
  end
end

live_loop :pl do #to play some notes so you can hear the volume change
  play scale(:c4,:major,num_octaves: 2).choose, release: 0.2
  sleep 0.2
end
1 Like

There are a code example in “Removing Latency” in chapter 11.1 that are missing “midi:” then

1 Like

Oh yes, it seems that part of the online version is slightly out of date. Thanks for that, hopefully we can correct it soon :slight_smile: