Get with the name of a variable


#1

Hi !

Inspiring from the post of @robin.newman OSC + Loop machine + Makeymakey, I’m struggling with a new syntax question :slight_smile:

 controlPushName = ":c"+number
        puts controlPushName
        puts "----------- value of controlPushName : #{controlPushName}------------"
        foo = get(controlPushName) # it doesn't work
        foo = get(:c01) # it works
        puts "----------- value of foo : #{foo}------------"

i must miss something…
Help appreciated !
cheers


#2
#to store a value in the  time state the syntax is
value = 11
puts "value to store is #{value}"
set :controlPushName , value  #the name used to access the stored value must start with a :
#later in the program perhaps inside a live_loop you retrieve the value using this

retrievedValue = get(:controlPushName)

puts "retrieved value is #{retrievedValue}"

#3

Technically, it doesn’t exactly have to, but that’s probably the simplest way to do it!


#4

yes but how to do if we have the controlPushName to be create dynamically

this is the state of my code

tracks = {
  "track_01" => "loop_amen",
  "track_02" => "ambi_choir",
  "track_03" => "drum_roll"
}


define :play_track do | number |
  
  killBill = ":kill_" + number
  faderCurrent = ":fader_" + number
  
  
  set killBill , false
  puts "-------------@{killBill}----------"
  
  live_loop ":control_loop_"+number do
    trackToPlay = "track_"+number
    currentSample = tracks[trackToPlay]
    puts currentSample
    
    s = sample currentSample,
      amp: get(":fader_" + number.to_s+"_value")
    puts get(:fader_01_value)
    in_thread do
      loop do
        
        controlPushName = ":c"+number
        puts controlPushName
        puts "----------- value of controlPushName : #{controlPushName}------------"
        foo = get(controlPushName)
        puts "----------- value of controlPushName : #{foo}------------"
        
        foo = get(:c01)
        puts "----------- value of foo : #{foo}------------"
        
        if foo == 0
          
          kill s
          set killBill,true
          stop
        end
        sleep 0.01
      end
    end
    40.times do #do this to get a quicker response time
      sleep sample_duration(currentSample) / 40
      stop if get(killBill)
    end
  end
end


live_loop :p1 do
  b = sync "/osc/1/push_1"
  set :c01,b[0] #1 if pushed 0 when released
  play_track("01") if b[0]==1
end


#5

As mentioned above, while symbols are nice and short, you can use more than just symbols as the keys to store values against in the time state - you can also use a string, which then allows you to dynamically create the string. So, a small example:

set('c01', 42)
number = '01'
controlPushName = "c#{number}"
puts get(controlPushName)

outputs:
42

(So in a way, it is similar to the dynamic variables idea we discussed over in your previous thread)


#6

Ethan’s example may be the way you want to go. Personally I feel happier with using symbols. The bit missing in your code (which looks like it originates from code in the makey makey thread you refer to is the use of .to_sym This lets you convert a string to a symbol
So you can do something like this.

number = 3
killBill = ("kill_" + number.to_s).to_sym
puts killBill #it resturns a symbol name
set killBill,false

puts  get(killBill)

#setting name of live loop
lname = ("loop_"+number.to_s).to_sym
puts lname

live_loop lname do #live loop is called :loop_03 See name in log
  puts "hello there"
  sleep 1
end

If you fit this approach into your code it should work.


#7

Good evening !

Big thanks for your help : it works and i learn that symbol exist.
this is the state of the code. a whole version will be published soon or later

#
# 3 tracks in sonic pi controlled by osc signals using open stage control (named here as gui)
# nlb with big help from The Sonic PI Community !!


# set osc parameters
osc_server_ip = "127.0.0.1"
osc_server_port = 7777
use_osc osc_server_ip, osc_server_port

# under linux
prefix_osc = "/osc:127.0.0.1:7777"
# under windows
# prefix_osc = "/osc"


# values to be sent
init_fader = 0.25

# send values to the gui
osc "/1/toggle_1", 0

osc "/fader_1", init_fader
osc "/fader_2", init_fader
osc "/fader_3", init_fader
osc "/fader_4", init_fader

set :fader_1_value, init_fader
set :fader_2_value, init_fader
set :fader_3_value, init_fader
set :fader_4_value, init_fader

# live loop to read the fader value sent by gui
live_loop :_fader_1 do
  use_real_time
  f1 = sync (prefix_osc + "/fader_1")
  puts "fader 1 is ",f1[0]
  set :fader_1_value,f1[0]
end

live_loop :_fader_2 do
  use_real_time
  f2 = sync (prefix_osc + "/fader_2")
  puts "fader 2 is ",f2[0]
  set :fader_2_value,f2[0]
end

live_loop :_fader_3 do
  use_real_time
  f3 = sync (prefix_osc + "/fader_3")
  puts "fader 3 is ",f3[0]
  set :fader_3_value,f3[0]
end

# PUSHES
# set the sample for each push

tracks = {
  "track_1" => "loop_amen",
  "track_2" => "loop_3d_printer",
  "track_3" => "drum_roll"
}

i=1
tracks.length.times do
  controlPushName = "c#{i}"
  puts controlPushName
  set(controlPushName,0)
  i = i+1
end

define :play_track do | number |
  
  # different note to debug with ear
  # play number + 64
  
  killBill = ("kill_" + number.to_s).to_sym
  currentFaderValue = ("fader_" + number.to_s + "_value").to_sym
  loop_name = ("control_track_"+number.to_s).to_sym
  controlPushName = ("c#{number}").to_sym
  
  puts "-------------#{killBill}----------"
  puts "-------------#{get(currentFaderValue)}----------"
  puts "-------------#{loop_name}----------"
  puts "-------------#{controlPushName}----------"
  
  # say not to kill the loop let it sounds
  set killBill , false
  
  live_loop loop_name do
    trackToPlay = "track_"+number.to_s
    currentSample = tracks[trackToPlay]
    puts currentSample
    # play the sample
    s = sample currentSample,
      amp: get(currentFaderValue)
    in_thread do
      loop do
        
        if get(controlPushName) == 0
          kill s
          set(killBill,true)
          stop
        end
        sleep 0.01
      end
    end
    40.times do #do this to get a quicker response time
      sleep sample_duration(currentSample) / 40
      stop if get(killBill)
    end
  end
end

live_loop :p1 do
  b = sync(prefix_osc + "/1/push_1")
  set :c1,b[0] #1 if pushed 0 when released
  play_track(1) if b[0]==1
end

live_loop :p2 do
  b = sync(prefix_osc + "/1/push_2")
  set :c2,b[0] #1 if pushed 0 when released
  play_track(2) if b[0]==1
end

live_loop :p3 do
  b = sync(prefix_osc + "/1/push_3")
  set :c3,b[0] #1 if pushed 0 when released
  play_track(3) if b[0]==1
end

May it help.
cheers