Are there variables such as global “time” , or sample “duration”
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.
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.”
@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?
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.
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.
That actually helps, a lot!
Thanks
Is there a function to convert a chord to intervals?
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!
Thanks,
That worked fine.
intervals = chord( :A, :major )
intervals = intervals - intervals[0]
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!
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.
Then, in the container that opens up at the bottom of the editor, select the “Lang” tab.
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:
# 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:
.
# 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!
@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
.
# 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