# Are there variables such as global "time" , or sample "duration"

**URL:** <https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477>\
**Category:** Support, Help & Resources\
**Created:** [February 2, 2022, 10:42pm UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477 "2022-02-02T22:42:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![lron](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@lron](https://in-thread.sonic-pi.net/u/lron)\
**Post date:** [February 2, 2022, 10:42pm UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/1 "2022-02-02T22:42:52Z")

</div>

Are there variables such as global “time” , or sample “duration”

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [February 2, 2022, 11:57pm UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/2 "2022-02-02T23:57:01Z")

</div>

If you’d like to get the total number of seconds since the Epoch, or 1970-01-01 00:00:00 UTC, you can use `Time.new.to_i`, which comes from Ruby.

> **[Class: Time (Ruby 3.1.0)](https://ruby-doc.org/core-3.1.0/Time.html)**
>
> Class : Time - Ruby 3.1.0

To get the duration of a sample in beats however, you don’t need to use `Time`. Instead, you can use `sample_duration`, which is built right into Sonic-Pi. You can find the details of this function and many others in the Lang tab of the Sonic-Pi Help documentation. Note the note: “avoid using `sample_duration` to set the sleep time in `live_loop`s, prefer stretching the sample with the `beat_stretch:` opt or changing the BPM instead. See the examples below for details.”

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/0/0c6bffecac2b943b949e6a026b9cb69d49537d8f.png)

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [February 3, 2022, 12:15am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/3 "2022-02-03T00:15:37Z")

</div>

@d0lfyn happy in\_thread anniversary! 🎂

@lron - as also seen in the Lang reference tab, there are functions like `vt`, which give the virtual time of the current thread, and `current_time`, which behaves similar to Ruby `Time.now` with the exception that if used repeatedly with no sleeps or syncs in between, it returns the same value each time - and it is also affected by `time_warp`. What are you thinking of using a ‘global’ time value for? 🙂

---

<div class="post-metadata">

**Author:** ![lron](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@lron](https://in-thread.sonic-pi.net/u/lron)\
**Post date:** [February 3, 2022, 12:44am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/4 "2022-02-03T00:44:46Z")

</div>

Thanks for the info! I’m getting back into SonicPi after a long break. I want the time value to give me something I can hang on to while learning SP. I’m not a musician, thinking in beats and measures isn’t natural. I can relate to minutes and seconds. I find myself mentally converting beats into seconds.

It’s a crutch, but that’s how I think about time.

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [February 3, 2022, 2:00am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/5 "2022-02-03T02:00:18Z")

</div>

Anytime! I’m also revisiting Sonic-Pi, which I want to integrate in my compositions. Anyways, you can use the `bt` function to convert beats to seconds.

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/3/35f223197683ec6a3b38b555bab9f39494f6ff1d.png)

---

<div class="post-metadata">

**Author:** ![lron](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@lron](https://in-thread.sonic-pi.net/u/lron)\
**Post date:** [February 3, 2022, 2:05am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/6 "2022-02-03T02:05:52Z")

</div>

That actually helps, a lot!  
Thanks

---

<div class="post-metadata">

**Author:** ![lron](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@lron](https://in-thread.sonic-pi.net/u/lron)\
**Post date:** [February 3, 2022, 2:07am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/7 "2022-02-03T02:07:35Z")

</div>

Is there a function to convert a chord to intervals?

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [February 3, 2022, 2:47am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/8 "2022-02-03T02:47:09Z")

</div>

The closest thing I can think of is to use the `chord` function which, given a tonic note and a chord name, returns a ring of MIDI notes. You can count the difference between each MIDI note to determine its interval in semitones.

For instance, if we run `puts chord :A, :maj`, we get the following output:

`(ring <SonicPi::Chord :A :maj [69, 73, 76])`

73 - 69 = 4 semitones, AKA a major third  
76 - 73 = 3 semitones, AKA a minor third

Hope this helps!

---

<div class="post-metadata">

**Author:** ![lron](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@lron](https://in-thread.sonic-pi.net/u/lron)\
**Post date:** [February 3, 2022, 3:36pm UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/9 "2022-02-03T15:36:45Z")

</div>

Thanks,  
That worked fine.

```ruby
intervals = chord( :A, :major )
intervals = intervals - intervals[0]

```

---

<div class="post-metadata">

**Author:** ![datagrazer](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/datagrazer/32/2186_2.png) [@datagrazer](https://in-thread.sonic-pi.net/u/datagrazer)\
**Post date:** [February 10, 2022, 1:14am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/10 "2022-02-10T01:14:41Z")

</div>

Sorry but could you link to this documentation? I’m having a hard time locating it.

Would it be possible to use one of these time functions to set a duration that a certain portion should take? For example, play just one slow beat if a value is low, or play a lot of fast beats until the end of the time frame if a value is high? I hope that’s not too abstract to understand!

---

<div class="post-metadata">

**Author:** ![d0lfyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/d0lfyn/32/2254_2.png) [@d0lfyn](https://in-thread.sonic-pi.net/u/d0lfyn)\
**Post date:** [February 10, 2022, 4:46am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/11 "2022-02-10T04:46:49Z")

</div>

Hi there!

The documentation is built right into the Sonic Pi editor. To my knowledge, it’s the only place you can find the most up-to-date documentation at the moment. Anyways, to get there, first click on the “help” icon amongst the upper-right icons of the editor.

![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/4/412de6b38122d4522b19b4195b672b2b5ae52035.png)

Then, in the container that opens up at the bottom of the editor, select the “Lang” tab.

![image](https://europe1.discourse-cdn.com/flex017/uploads/sonic_pi/original/2X/5/54d3f20d86e6f7d1f5d40c97372f9515829ce75f.png)

And to address your second question: my understanding of the problem based on your example is that your value is directly proportional to the number of beats to fit into the timeframe. I would therefore approach the problem like this:

```ruby
# n.b. timeframe is in seconds
define :play_in_timeframe do |timeframe, num_beats|
  beat_duration = timeframe.to_f / num_beats
  
  num_beats.times do
    play :c4, attack: 0, decay: 0, sustain: beat_duration, release: 0
    sleep beat_duration
  end
end

play_in_timeframe(2, 8)

```

I could write a similar function for samples, by supplying a value for `rate:`.

```ruby
# n.b. timeframe is in seconds
define :sample_in_timeframe do |sample_id, timeframe, num_beats|
  beat_duration = timeframe.to_f / num_beats
  new_rate = (sample_duration sample_id) / beat_duration
  
  num_beats.times do
    sample sample_id, rate: new_rate
    sleep beat_duration
  end
end

sample_in_timeframe(:bd_808, 2, 8)

```

I hope this helps!

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [February 10, 2022, 5:16am UTC](https://in-thread.sonic-pi.net/t/are-there-variables-such-as-global-time-or-sample-duration/6477/12 "2022-02-10T05:16:24Z")

</div>

@datagrazer, @d0lfyn - there’s also a handy built-in function that will be helpful in this case 🙂 - `density`.  
Try modifying the parameter (let’s call it `d`) for `density` here - the number causes the inner block to be repeated `d` times and the BPM of the block is also multiplied by `d`.

```ruby
# Play a note at 30 BPM, that lasts for 2 seconds, and sleep for 2 seconds
density(0.5) do
  play :c4, sustain: 1, release: 0
  sleep 1
end

# play two samples at 120 BPM, with a sleep of 0.5 seconds after each
density(2) do
  sample :bd_haus
  sleep 1
end

```

More details are in the help panel that d0lfyn refers to above, under the Lang category 👍
