Hi! I’m new to this, I’m starting to program music with Sonic Pi, and I’m having a lot of fun! I have a few questions, if anyone here knows the software, I need some help with something.
I’m trying to create a bassline that follows the note of the chord that’s currently playing. I’d like to do this without having to write each note individually. I know there has to be a much simpler way.
I’ve attached a screenshot so you can see how the code is written for now.
I initially thought I could do it with “if” statements, but I couldn’t find a way to program it correctly. Any ideas? Thanks!
Hi
Welcome to Sonic Pi! Because a chord is a ring array, we can target individual chord tones by index (to derive a bassline from a given chord); 0 = root, 1 = 3rd, 2 = 5th etc:
live_loop :tonic do
play chord(:e, :minor)[0], release: 0.1
sleep 0.5
end
HTH
PD-Pi
[EDIT]
You can get the bassline to follow the chord progression like this (apologies if a bit clunky, haven’t used Sonic Pi in a while):
triads = [":e3",":a3","b3",":e3"].ring
use_synth :fm
live_loop :chord do
tick
play chord(triads.look, :minor)+12
sleep 2
end
use_synth :bass_foundation
use_synth_defaults pitch: -12
live_loop :root do
tick
note = chord(triads.look, :minor)[0]
4.times do
play note, release: 0.6
sleep 0.5
end
end
PD-Pi
1 Like
Okay! That works, thank you so much! I’ve been very busy these past few days and haven’t had time to reply quickly. I have a few questions about what you wrote.
Why do you put a “tick” command before each “play”? What does it do?
Does putting “.ring” at the end of the square brackets turn the content into a ring? Is it the same as using “(ring)”?
Hi
tick is a thread/loop-independent counter, which reads through the ring array of named chords called ‘triads’ via .look. This generates a chord sequence (the second loop simply targets the root note of each chord, see: Help/Lang/tick). Without the tick, each loop would only target the first element in that ring array. And yes, .ring is exactly the same as (ring).
PD-Pi
Just to clarify, I think what Brendan means here by ‘thread/loop-independent’ is ‘isolated per-thread/loop’, rather than ‘isolated from any thread/loop’.
(I think the word Sam often used was ‘Thread-local’ (i.e. local to the thread/loop).
1 Like
Thanks for refining my clumsy syntax 
Also, remember that notes are just numbers, and you can do math. If you want the note to be 2 octaves below the first note of the chord, just do
live_loop :tonic do
play chord(:e, :minor)[0] - 24, release: 0.1
sleep 0.5
end
Have fun exploring sonic pi! It’s a blast!