Piano Composition

Hi, i am currently working on a project in my school where i have to create a music with Sonic Pi. Unfortunately, i can’t seem to go past this problem. I created 2 different piano part, one that play first and the second one later however, once i put my second part in my program it dosent play it??? Like it wasnt there. I though there was problem with the program so i played it by itself however it work fine. Please help :frowning:

This is my program

use_bpm 88

#Introduction
# Our chord sequence
vol=1000
p1 = [chord(:C3,"minor"),
      chord(:G2,"7"),
      chord(:Bb2,"major"),
      chord(:G2,"7"),
      ]


# the main piano
with_fx :level, amp: 2 do
  with_fx :reverb do
    with_synth :piano do
      p1.each {
        |c|
        cc = c
        play c, decay: 1
        play c+12
        sleep 2.7
        
        
      }
    end
  end
end


#PART 1
# Our chord sequence
vol=1000
p1 = [chord(:C3,"minor"),
      chord(:G2,"7"),
      chord(:Bb2,"major"),
      chord(:G2,"7"),
      ]


# the main piano
with_fx :level, amp: 1 do
  with_fx :reverb do
    with_synth :piano do
      live_loop :piano do
        p1.each {
          |c|
          cc = c
          play c, decay: 1
          play c+12
          sleep 4
          
          
          
          
        }
      end
    end
  end
end



use_synth :piano

use_bpm 88

define :play_phrase do
  play_pattern_timed [48, 70, 68, 67, 65, 63, 65, 67, 65, 60, 55, 58, 60, 58, 60, 63, 65, 65, 65, 60, 60],
    [1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 0.5, 3.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 0.5, 0.5]
end



# this thread plays the phrase consistently
with_fx :pan, pan: -0.5 do
  in_thread(name: :steady) do
    1.times do
      play_phrase
      sleep 5
    end
  end
end


#PART 2
p2 = [chord(:F3,"major7"),
      chord(:E3,"minor7"),
      chord(:D3,"minor7"),
      chord(:D3,"m7-5"),
      chord(:C3,"major7"),
      ]

# the main piano
with_fx :level, amp: 3 do
  with_fx :reverb do
    with_synth :piano do
      live_loop :piano do
        p2.each {
          |c|
          cc = c
          play c, decay: 1
          play c+12
          sleep 5
          play c+12
          sleep 2
          play c+12
          sleep 1
          
        }
      end
    end
  end
end

use_synth :piano

use_bpm 88

define :play_phrase do
  play_pattern_timed [64, 60, 57, 64, 60, 57, 64, 60, 57, 64, 60, 57, 62, 59, 55, 62, 59, 55, 62, 59, 55, 62, 59, 55, 60, 57, 53, 60, 57, 53, 60, 57, 53, 60, 57, 53, 59, 55, 52, 59, 55, 52, 59, 55, 52, 59, 55, 52, 59, 55, 52],
    [0.3, 0.3, 0.3, 0.3, 0.3, 0.3]
end


# this thread plays the phrase consistently
with_fx :pan, pan: -0.5 do
  in_thread(name: :steady) do
    1.times do
      play_phrase
      sleep 5
    end
  end
end

# First part in piano without chords

use_synth :piano

use_bpm 88

define :play_phrase do
  play_pattern_timed [48, 70, 68, 67, 65, 63, 65, 67, 65, 60, 55, 58, 60, 58, 60, 63, 65, 65, 65, 60, 60],
    [1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 0.5, 3.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 0.5, 0.5]
end



# this thread plays the phrase consistently
with_fx :pan, pan: -0.5 do
  in_thread(name: :steady) do
    1.times do
      play_phrase
      sleep 5
    end
  end
end

# Second Part in piano without cords

use_synth :piano

use_bpm 88

define :play_phrase do
  play_pattern_timed [64, 60, 57, 64, 60, 57, 64, 60, 57, 64, 60, 57, 62, 59, 55, 62, 59, 55, 62, 59, 55, 62, 59, 55, 60, 57, 53, 60, 57, 53, 60, 57, 53, 60, 57, 53, 59, 55, 52, 59, 55, 52, 59, 55, 52, 59, 55, 52, 59, 55, 52],
    [0.3, 0.3, 0.3, 0.3, 0.3, 0.3]
end


# this thread plays the phrase consistently
with_fx :pan, pan: -0.5 do
  in_thread(name: :steady) do
    1.times do
      play_phrase
      sleep 5
    end
  end
end

Hi @Sharonne,

I’ve taken the liberty of formatting your code so that it displays nicely. (You can do the same with any future code snippets by placing three backticks - eg ``` on blank lines before and after the snippet).
I can’t look at the issue you’re having right now, but I’ll try to come back to it a little later (unless someone beats me to it :slight_smile: )

oh thanks! will be sure to use it next time. Thank you :grin:

Hi Sharone,

I’m sorry nobodys got back to you before me. I guess lifes keeping
people pretty busy.

Looking at your code, I can think of several reasons why the second part
might not work, mostly down to having threads and loops and variables
all with the same name. This often confuses Sonic Pi… if it finds 2 loops
with the same name, it tries to redefine the original one.

So, I’ve cleaned up your code a little but kept your comments. Always good
to comment, it helps others see what you are trying to do. :slight_smile:

Out of habit, I try to put defines and strings at the beginning of my code… it
just feels neater to me… but that doesn’t mean you -have- to… there is no
right or wrong way to code… and often many different ways to do the same
thing. :slight_smile:

So here is my tidied up code. I’ve deliberately left it using ‘in_thread’ because
thats the way you wrote it… maybe later, we can discuss other ways such as
live_loops.

Tell me if it’s what you were trying to do, and if not, explain how it’s different, and
maybe we can sort things out more.

Eli…

use_bpm 88
use_synth :piano
vol=10

define :play_phrase1 do
  play_pattern_timed [48, 70, 68, 67, 65, 63, 65, 67, 65, 60, 55, 58, 60, 58, 60, 63, 65, 65, 65, 60, 60],
    [1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 0.5, 3.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 0.5, 0.5]
end

define :play_phrase2 do
  play_pattern_timed [64, 60, 57, 64, 60, 57, 64, 60, 57, 64, 60, 57, 62, 59, 55, 62, 59, 55, 62, 59, 55, 62, 59, 55, 60, 57, 53, 60, 57, 53, 60, 57, 53, 60, 57, 53, 59, 55, 52, 59, 55, 52, 59, 55, 52, 59, 55, 52, 59, 55, 52],
    [0.3, 0.3, 0.3, 0.3, 0.3, 0.3]
end

#Introduction chord sequence
intro = [chord(:C3,"minor"),chord(:G2,"7"),chord(:Bb2,"major"),chord(:G2,"7")]
# Part1 chord sequence
part1 = [chord(:C3,"minor"),chord(:G2,"7"),chord(:Bb2,"major"),chord(:G2,"7")]
# Part2 chord sequence
part2 = [chord(:F3,"major7"),chord(:E3,"minor7"),chord(:D3,"minor7"),chord(:D3,"m7-5"),chord(:C3,"major7")]


# Play the intro piano
with_fx :level, amp: 2 do
  with_fx :reverb do
    intro.each {
      |c|
      cc = c
      play c, decay: 1
      play c+12
      sleep 2.7
    }
  end
end

#PART 1

# Part1 main piano
with_fx :level, amp: 1 do
  with_fx :reverb do
    in_thread do
      part1.each {
        |c|
        cc = c
        play c, decay: 1
        play c+12
        sleep 4
      }
    end
    # this thread plays the phrase consistently
    with_fx :pan, pan: -0.5 do
      in_thread do
        play_phrase1
      end
    end
  end
end

sleep 20

#PART 2

# Part2 main piano
with_fx :level, amp: 1 do
  with_fx :reverb do
    in_thread do
      part2.each {
        |c|
        cc = c
        play c, decay: 1
        play c+12
        sleep 5
        play c+12
        sleep 2
        play c+12
        sleep 1
      }
    end
  end
  with_fx :pan, pan: -0.5 do
    in_thread do
      play_phrase2
      sleep 5
    end
  end
end
2 Likes

Hi Sharonne,

Sorry for not getting back - I’d actually downloaded your code and come to the same conclusions as Eli, but then got sidetracked by work before I got round to replying :slightly_frowning_face:

Eli’s done a lovely job tidying your code and I can now hear it sounding great. I always forget the ins and outs of threads if I haven’t used them for a while but I found this section of the online tutorial really helpful https://sonic-pi.net/tutorial.html#section-5-4

Another way of getting things to wait then come when you want them to after a period of time is the :cue :sync keywords: https://sonic-pi.net/tutorial.html#section-5-7

Hope that’s useful as well!

Claire

1 Like

Thank you! will certainly check it out :slight_smile:

1 Like

Thank you very much! i sincerely though nobody was going to reply haha but you did! And the upgrade you did is very amazing thank you again :slight_smile:

Shannon

1 Like