How to pass variable values from one block to another

Hi experts, wondering if there is a way to pass variable values from one block to another. For instance, I am trying to build a intro and a verse1. For the verse1 block, I would like it to sleep the duration of the intro block before it starts playing. The code below doesn’t work. I assume the variable introBeats I created is local only to the intro block and it wouldn’t pass the value externally. So how am I supposed to make it public (global?)
Thanks as always for your help

in_thread(name: :intro) do
  32.times do
    sample :loop_amen, onset: choose
    sleep 0.125
  end
  introBeats = beat
end

in_thread(name: :verse1) do
  sleep introBeats
  1.times do
    sample :ambi_choir
  end
end

Hey…

I’m a newbie myself, so I might have understood your question wrong… But if I didn’t, then one way to do it is to use functions…

Doesn’t directly answer the variable question, but it will let you play your “sections” one after the other, in the order you want them…
Something like this:

define :intro do
#body of your intro goes here, with how
#many times your want it to repeat
end
define :section_01 do
#body of your first section goes here
end

# And then, once your melodies are captured inside functions,
#you just call them with their names, like so:
intro
section_01 
#These will play the sections of your track in the order
#you want them to, without having to use a "jumping variable" for sleep times

You use “in_thread” when you want to play things at the same time, not one after the other…
You can combine the two approaches, of course…
For example, your intro itself might be constituted of two things that play at the same time, and then stop to go to section_01… In this case, you just put your in_thread blocks inside your first function definition, they will play together when it is called, and stop together to make way for the next section of the track…
Hope this helps!
Cheers!

Correctaroonie. @minetestist rightly points out that the most useful characteristic of in_thread is to allow you to play things at the same time, and shows that you can indeed use functions as one way to package up and run code in neat little sections in whatever order, possibly avoiding threads if you don’t need them. The challenge as you have seen is when you want to use specific variables between sections of code, including between different threads or live_loops.

The recommended way to communicate between threads/live_loops is by using get and set, as part of accessing Sonic Pi’s global Time-State memory store. (See Tutorial chapter 10 for details).

For your above code, you can actually use set and sync, since sync is also built on top of the Time-State system, and would allow part of your code to wait until another unblocks it. Here’s an example:

in_thread(name: :intro) do
  32.times do
    sample :loop_amen, onset: choose
    sleep 1
  end
  set(:introbeats, true)
end

in_thread(name: :verse1) do
  sync(:introbeats)
  1.times do
    sample :ambi_choir
  end
end

This of course works for our situation in this very specific case because :intro and :verse1 are not live_loops (i.e. they execute once). If they were live_loops then we’d need to make sure using sync in this way would still give us the result we want.

1 Like

Thanks. I haven’t got that far yet on the tutorial about functions. But it definitely looks like something I can use to build songs. The codes looks more tidier. Appreciate your answer!

1 Like

Thanks Ethan. I will definitely read the chapter 10 about the Time-State memory store part of the tutorials. If I don’t intend to do live coding, is there an advantage of using live_loop vs in_thread? also, what advantages does set and get have over other methods of structuring song sections such as using functions or passing beat duration variables as an value for sleep argument? As always, thank you!

1 Like

is there an advantage of using live_loop vs in_thread?

The only difference is that one loops and the other doesn’t.

what advantages does set and get have over other methods of structuring song sections

Well, tutorial chapter 10 describes their main purpose/advantage :wink: deterministic behaviour in cross-thread communication, which using ordinary variables does not have. If you do not need to send data between threads/live_loops or manipulate shared data from them, then there’s less need for get and set.
Functions are still useful to call anywhere, as long as you use get and set in them when needed :slightly_smiling_face: