Mehackit tutorial - question about rings and ticks

I’m working my way through this tutorial (as a complete novice ) and have got a bit confused with this example from the section on rings and ticks What exactly is the ring function bringing to the party here, as it seems to make no difference if I remove it?

live_loop :keys do
use_synth :blade
play [(chord :Ab, :major7), (chord :Db, :major7), (chord :Bb, :minor7), (chord :Eb, ‘7’)].ring.tick, attack: 0.1, release: 1
sleep 1
end

If you’re using .tick, it doesn’t make a difference because .tick treats arrays as if they were rings. But if you try to access the elements by index, then you will see a difference. For example:

data = [(chord :Ab, :major7), (chord :Db, :major7), (chord :Bb, :minor7), (chord :Eb, '7')]

puts "Without ring"
range(0, 8).each do |i|
  puts i, data[i]
end

puts "With ring"
range(0, 8).each do |i|
  puts i, data.ring[i]
end

The first loop prints:

"Without ring"
0.0 (ring 68, 72, 75, 79)
1.0 (ring 61, 65, 68, 72)
2.0 (ring 70, 73, 77, 80)
3.0 (ring 63, 67, 70, 73)
4.0 nil
5.0 nil
6.0 nil
7.0 nil

But the second loop prints:

"With ring"
0.0 (ring 68, 72, 75, 79)
1.0 (ring 61, 65, 68, 72)
2.0 (ring 70, 73, 77, 80)
3.0 (ring 63, 67, 70, 73)
4.0 (ring 68, 72, 75, 79)
5.0 (ring 61, 65, 68, 72)
6.0 (ring 70, 73, 77, 80)
7.0 (ring 63, 67, 70, 73)

You can see in the first loop when it gets to the end of the array it just gets nil, but in the second loop in loops back around to the start, even when you get elements by index.

2 Likes

Thanks Emlyn,

I think I get the gist of that, but maybe I also need to be less literal in my learning and just accept that things just work the way they do for now;)

Hi @ChrisB

emlyn’s explanation is more precise than what follows, but in Sonic Pi I think of an array as ‘static’ or unidirectional, until we make it loop back to index zero and repeat, as emlyn says, by invoking a .ring/.tick/.look/.choose method on it.

a1 = [60, 62, 65]# 'static' array of 3 elements

use_synth :dpulse
play a1[3]# silence
puts "there is no 4th item:"
puts a1[3]
sleep 2

puts "now the array's a ring:"
4.times do
  tick
  play a1.look
  puts a1.look
  sleep 0.5
end

HTH

PD-Pi

2 Likes

Hi @brendanmac

Many thanks for this, it is all starting to make some sense in my aging brain now​:wink:

BR Chris

1 Like

Hi Chris,

I had the same questions about rings and ticks. As I understand it (limited understanding), arrays are a set of data addresses which are accessed via indexing the array/s. Each element of an array is numbered where the first element is number 0 (zero) and the 2nd element is number 1 (one) apropos.

So with an array (data set between square brackets/braces […] of 8 elements for example, the first element is index 0 and the last element is index 7. This allows you to “tick” through the indexed elements to access the assigned data at each element then do some stuff :slight_smile: . Well I think that’s how it works when using tick. It can be a very flexible way of working which is great musically. There is so much “under-the-bonnet” of Sonic Pi where some programming principles come in handy when going deeper into Sonic Pi’s offerings. GL with it.

I used a Ruby primer in my first experiments with Sonic Pi; with the proviso that not all Ruby classes/functions/methods are applicable in Sonic Pi. I used it just to get a flavour of the language, syntax and flow.

HTH

Ahh yes of course now that Sonic has evolved, the language conventions imply a fresh take. I initially started with learning Sonic Pi’s Ruby variation. I guess there is still a legacy in my mindset :slight_smile: and, I am loving the new releases.

Cheers.

1 Like

P.S.

Documents 8.4, 8.5 and 9.4 below are super simple and indispensable:

https://sonic-pi.net/tutorial-08.html#tut-08-4-rings

1 Like

Thanks Brendan… I just had a quick peak. The ‘rings’ tutorial is a useful refresher for me to look at how rings work with arrays and referencing the elements. I also wasn’t aware of the “inc” function for incrementing … previously I used the +1 or -1 (decrement) integers (I think C programs use ++ or --). It was good of you to point these out. Thank you. Happy coding..BTW I think your work is fantastic so thank you for sharing also.

Altan