Music now stops unexpectedly (possibly due to updating from v2.11.1 to v3.1?)

I’ve been trying to recreate a song in Sonic Pi, I remember it working OK in v2.11.1. But when I updated from v2.11.1 to v3.1, my code started only playing music up to a certain point, before stopping, when it shouldn’t. I’m not sure whether it’s due to updating from v2.11.1 to v3.1, or a silly typo or something I made when I last opened it in 2.11.1. It would be great if I could get this working again. :slight_smile:

Here’s the code (ignore the top comments, I made the playChord function when I didn’t know you could play chords using play):

# Welcome to Sonic Pi v2.11
# Plays a chord at an octave
#define :playChord do |chord,type|
  # Major Chord: Root + Major Third (+4 steps) + Perfect Fifth (+7 steps)
  # Minor Chord: Root + Minor Third (+3 steps) + Perfect Fifth (+7 steps)
  # Augmented Chord: Root + Major Third (+4 steps) + Augmented Fifth (+8 steps)
  # Diminished Chord Chord: Root + Minor Third (+3 steps) + Diminished Fifth (+6 steps)
#  if (type = 0) # Major Chord
#   play_chord [chord, chord + 4, chord + 7],sustain:10
#  end
#  if (type = 1) # Minor Chord
#    play_chord [chord, chord + 3, chord + 7],sustain:10
#  end
#  if (type = 2) # Augmented Chord
#    play_chord [chord, chord + 4, chord + 8],sustain:10
#  end
#  if (type = 3) # Diminished Chord
#    play_chord [chord, chord + 4, chord + 6],sustain:10
#  end
#end

# A A# B C C# D D# E F F# G G#

# 'Shelter' by Porter Robinson and Madeon
# Recreated in Sonic Pi by SunderB
# Chords and notes from:
# - kaylaser's ukelele tutorial on YouTube: https://www.youtube.com/watch?v=AJBmmvBRprQ
# - HuntroxicMusic's piano tutorial/midi YouTube video: https://www.youtube.com/watch?v=0iDNPiBNHrU
# Feel free to tinker with parts of the code. But please don't just copy and paste it and say it's your work; please give me some credit. Thanks :)

song_ended = 0

define :background_beat do
  in_thread do
    while (song_ended == 0) do
      sample :drum_snare_soft
      sleep 1
    end
  end
end


define :melody do |play_first_note,play_final_notes|
  in_thread do
    with_fx :vowel, voice:1, mix:0.1 do
      volume = 3
      pitch = 12
      use_synth :piano
      #sync :run
      # First runthrough
      if play_first_note == 1
        play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      else
        play :c,amp:0
      end
      sleep 1
      play :a4,pitch:pitch,amp:volume
      sleep 0.3
      play :g,pitch:pitch,amp:volume
      sleep 0.4
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1.8
      play :e,pitch:pitch,amp:volume
      sleep 0.5
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1
      play :a4,pitch:pitch,amp:volume
      sleep 0.3
      play :g,pitch:pitch,amp:volume
      sleep 0.4
      play :d,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1.3
      
      play :d,pitch:pitch,amp:volume
      sleep 0.4
      play :c,pitch:pitch,amp:volume
      sleep 0.6
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1
      play :a4,pitch:pitch,amp:volume
      sleep 0.3
      play :g,pitch:pitch,amp:volume
      sleep 0.4
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1.8
      
      play :d,pitch:pitch,amp:volume
      sleep 0.5
      play :e,pitch:pitch,amp:volume
      sleep 0.33333
      play :c,pitch:pitch,amp:volume
      sleep 0.33333
      play :d,pitch:pitch,amp:volume
      sleep 0.33333
      play :e,pitch:pitch,amp:volume
      sleep 0.33333
      play :c,pitch:pitch,amp:volume
      sleep 0.33333
      play :d,pitch:pitch,amp:volume
      sleep 0.33333
      play :e,pitch:pitch,amp:volume
      sleep 0.33333
      play :c,pitch:pitch,amp:volume
      sleep 0.33333
      play :d,pitch:pitch,amp:volume
      sleep 0.33333
      play :e,pitch:pitch,amp:volume
      sleep 0.33333
      play :c,pitch:pitch,amp:volume
      sleep 0.33333
      play :d,pitch:pitch,amp:volume
      sleep 0.33334
      # Second runthrough
      cue :melody_half
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1
      play :a4,pitch:pitch,amp:volume
      sleep 0.3
      play :g,pitch:pitch,amp:volume
      sleep 0.4
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1.8
      play :e,pitch:pitch,amp:volume
      sleep 0.5
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1
      play :a4,pitch:pitch,amp:volume
      sleep 0.3
      play :g,pitch:pitch,amp:volume
      sleep 0.4
      play :e,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1.3
      
      play :g,pitch:pitch,amp:volume
      sleep 0.4
      play :g,pitch:pitch,amp:volume
      sleep 0.6
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1
      play :a4,pitch:pitch,amp:volume
      sleep 0.3
      play :g,pitch:pitch,amp:volume
      sleep 0.4
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      sleep 1.8
      
      play :d,pitch:pitch,amp:volume
      sleep 0.5
      play :e,pitch:pitch,amp:volume
      sleep 0.33333
      play :c,pitch:pitch,amp:volume
      sleep 0.33333
      play :d,pitch:pitch,amp:volume
      sleep 0.33333
      play :e,pitch:pitch,amp:volume
      sleep 0.33333
      play :c,pitch:pitch,amp:volume
      sleep 0.33333
      play :d,pitch:pitch,amp:volume
      sleep 0.33333
      play :e,pitch:pitch,amp:volume
      sleep 0.33333
      play :c,pitch:pitch,amp:volume
      sleep 0.33333
      play :d,pitch:pitch,amp:volume
      sleep 0.33333
			if (play_final_notes == 1)
				play :e,pitch:pitch,amp:volume
				sleep 0.33333
				play :c,pitch:pitch,amp:volume
				sleep 0.33333
				play :d,pitch:pitch,amp:volume
				sleep 0.33334
				play :c,pitch:pitch,amp:volume
			end
			cue :melody_end
		end
  end
end

define :melody_background_hum do |volume,fade_out|
  in_thread do
    time = 0
    2.times do
      time = time + 1
      volume
      pitch = -12
      delay_1 = 4
      delay_2 = 1.5
      delay_3 = 2.5
      use_synth :prophet
			with_fx :band_eq, freq:30, mix: 0.1  do
				with_fx :distortion, mix:0.5, distort:0.1 do
					with_fx :reverb,room:1,damp:1 do
						with_fx :lpf,cutoff:65,amp: volume  do
							with_fx :normaliser, level: 2 do
								with_fx :compressor do
									with_fx :flanger do
										play chord(:f, :sus2),pitch:pitch,amp:volume,attack:delay_1/4,decay:delay_1/4,sustain:delay_1/4,release:delay_1/4,sustain_level:0.5
										sleep 4
										play chord(:c5, :major),pitch:pitch,amp:volume,attack:delay_2/4,decay:delay_2/4,sustain:delay_2/4,release:delay_2/4,sustain_level:0.5
										sleep 1.5
										play chord(:e, :minor7),pitch:pitch,amp:volume,attack:delay_3/4,decay:delay_3/4,sustain:delay_3/4,release:delay_3/4,sustain_level:0.5
										sleep 2.5
										play chord(:g, :sus4),pitch:pitch,amp:volume,attack:delay_2/4,decay:delay_2/4,sustain:delay_2/4,release:delay_2/4,sustain_level:0.5
										sleep 1.5
										play chord(:e, :minor),pitch:pitch,amp:volume,attack:delay_3/4,decay:delay_3/4,sustain:delay_3/4,release:delay_3/4,sustain_level:0.5
										sleep 2.5
										if (time == 1 || fade_out == 0)
											play chord(:a, :minor7),pitch:pitch,amp:volume,attack:delay_1/4,decay:delay_1/4,sustain:delay_1/4,release:delay_1/4,sustain_level:0.5
											sleep 3
											play chord(:d, :minor7),pitch:pitch,amp:volume,attack:0.25,decay:0.25,sustain:0.25,release:0.25,sustain_level:0.5
											sleep 1
										end
										if (time == 2 && fade_out == 1)
											play chord(:a, :minor7),pitch:pitch,amp:volume,attack:delay_1/4,decay:delay_1/4,sustain:delay_1/8,release:delay_1/4,sustain_level:0.5
											sleep 4
										end
									end
								end
							end
						end
					end
				end
			end
		end
  end
end

define :melody_background_beat do
  in_thread do
    16.times do
			#sample :drum_heavy_kick,amp:2
			sample :drum_heavy_kick,amp:2.5
			sleep 1
			sample :drum_snare_soft,amp:2.5
			sleep 1
    end
  end
end

define :first_melody_beat do
  in_thread do
    in_thread do
      volume = 2
      pitch = -12
      time = 0
      use_synth :chiplead
      with_fx :lpf, cutoff:100, amp:volume do
        with_fx :flanger,mix:0.1 do
          16.times do
            play chord(:f, :sus2),amp:volume,pitch:pitch
            sleep 0.25
          end
          6.times do
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:c5, :major),amp:volume,pitch:pitch
            sleep 0.25
          end
          10.times do
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:e, :minor7),amp:volume,pitch:pitch
            sleep 0.25
          end
          6.times do
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:g, :sus4),amp:volume,pitch:pitch
            sleep 0.25
          end
          10.times do
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:e, :minor),amp:volume,pitch:pitch
            sleep 0.25
          end
          12.times do
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:a, :minor7),amp:volume,pitch:pitch
            sleep 0.25
          end
		  4.times do
		    #sample :drum_snare_soft,onset:1,amp:1.5
		    play chord(:d, :minor7),amp:volume,pitch:pitch if (time == 1)
		    sleep 0.25
		  end
        end
      end
    end
  end
end

define :melody_beat do |pauses|
  in_thread do
    in_thread do
      volume = 2
      pitch = -12
      time = 0
      use_synth :chiplead
      with_fx :lpf, cutoff:100, amp:volume do
        with_fx :flanger,mix:0.1 do
          2.times do
            time = time + 1
            12.times do
              play chord(:f, :sus2),amp:volume,pitch:pitch
              sleep 0.25
            end
            # 3 beats
            sleep 1 if pauses == 1 || time == 2
			play_pattern_timed [chord(:f, :sus2),chord(:f, :sus2),chord(:f, :sus2),chord(:f, :sus2)], [0.25] if pauses == 0 && time == 1
            # 4 beats
            6.times do
              #sample :drum_snare_soft,onset:1,amp:1.5
              play chord(:c5, :major),amp:volume,pitch:pitch
              sleep 0.25
            end
            
            #sleep 0.25
            
            6.times do
              #sample :drum_snare_soft,onset:1,amp:1.5
              play chord(:e, :minor7),amp:volume,pitch:pitch
              sleep 0.25
            end
            
            sleep 1 if pauses == 1 || time == 2
            play_pattern_timed [chord(:e, :minor7),chord(:e, :minor7),chord(:e, :minor7),chord(:e, :minor7)], [0.25] if pauses == 0 && time == 1
			
            6.times do
              #sample :drum_snare_soft,onset:1,amp:1.5
              play chord(:g, :sus4),amp:volume,pitch:pitch
              sleep 0.25
            end
            10.times do
              #sample :drum_snare_soft,onset:1,amp:1.5
              play chord(:e, :minor),amp:volume,pitch:pitch
              sleep 0.25
            end
            12.times do
              #sample :drum_snare_soft,onset:1,amp:1.5
              play chord(:a, :minor7),amp:volume,pitch:pitch
              sleep 0.25
            end
            4.times do
              #sample :drum_snare_soft,onset:1,amp:1.5
              play chord(:d, :minor7),amp:volume,pitch:pitch if (time == 1)
              sleep 0.25
            end
          end
        end
      end
    end
  end
end

define :warped_melody_beat do
  in_thread do
    in_thread do
      volume = 2
      pitch = -12
      time = 0
      depth = 7.0
      wobble = 0.2
      use_synth :chiplead
      with_fx :lpf, cutoff:100, amp:volume do
        with_fx :flanger,mix:0.5, depth:7 do |f|
          16.times do
            play chord(:f, :sus2),amp:volume,pitch:pitch
            sleep 0.25
          end
          6.times do
            control f, mix: 0.6
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:c5, :major),amp:volume,pitch:pitch
            sleep 0.25
          end
          10.times do
            control f, mix: 0.7
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:e, :minor7),amp:volume,pitch:pitch
            sleep 0.25
          end
          6.times do
            control f, mix: 0.8
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:g, :sus4),amp:volume,pitch:pitch
            sleep 0.25
          end
          10.times do
            control f, mix: 1.0
            #sample :drum_snare_soft,onset:1,amp:1.5
            play chord(:e, :minor),amp:volume,pitch:pitch
            sleep 0.25
          end
          time = 1
          10.times do
            with_fx :wobble, mix:0.2 do |w|
              #sample :drum_snare_soft,onset:1,amp:1.5
              play chord(:a, :minor7),amp:volume,pitch:pitch
              cue :buildup_drums if time == 10
              sleep 1.0/3.0
              depth = depth + 0.5
              control f, depth: depth
              control w, mix: wobble + 0.1
            end
            time = time + 1
          end
        end
      end
    end
  end
end

define :melody_buildup_drums do
  4.times do
    sample :elec_snare
    sample :drum_bass_hard
    sleep 0.25
  end
end

define :verse_hum do |volume|
  in_thread do
    time = 0
    2.times do
      pitch = -12
      use_synth :prophet
      with_fx :reverb,room:1,damp:1 do
        with_fx :lpf,cutoff:65  do
          with_fx :normaliser, level: 2 do
            with_fx :compressor do
              with_fx :flanger do
                # 0 beats
                play chord(:a, :minor7),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
                sleep 2
                # 1 beat
                play chord(:g, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
                sleep 2
                # 3 beats
                play chord(:f, :sus2),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
                sleep 2
                # 5 beats
                play chord(:c, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
                sleep 2
                # 7.5 beats
                play chord(:d, :minor7),pitch:pitch,amp:volume,attack:3.75/4,decay:3.75/4,sustain:3.75/4,release:3.75/4,sustain_level:0.5
                sleep 3.75
                # 10.75 beats
                play chord(:g, '7sus4'),pitch:pitch,amp:volume,attack:4.25/4,decay:4.25/4,sustain:4.25/4,release:4.25/4,sustain_level:0.5
                sleep 4.25
                # 16 beats
              end
            end
          end
        end
      end
    end
  end
end

define :verse_guitar do |play_first_part|
  in_thread do
    volume = 3
    pitch = -12
    use_synth :pluck
    with_fx :distortion do
      if (play_first_part == 1)
        8.times do
          play chord(:a, :minor7),pitch:pitch,amp:volume
          sleep 0.25
          print '1'
        end
        8.times do
          play chord(:g, :major),pitch:pitch,amp:volume
          sleep 0.25
          print '2'
        end
        8.times do
          play chord(:f, :sus2),pitch:pitch,amp:volume
          sleep 0.25
          print '3'
        end
        8.times do
          play chord(:c, :major),pitch:pitch,amp:volume
          sleep 0.25
          print '4'
        end
        15.times do
          play chord(:d, :minor7),pitch:pitch,amp:volume
          sleep 0.25
        end
        print '5'
        17.times do
          play chord(:g, '7sus4'),pitch:pitch,amp:volume
          sleep 0.25
        end
      else
        sleep 8
        15.times do
          play chord(:d, :minor7),pitch:pitch,amp:volume
          sleep 0.25
          print '1'
        end
        17.times do
          play chord(:g, '7sus4'),pitch:pitch,amp:volume
          sleep 0.25
          print '2'
        end
      end
    end
  end
end

define :verse_vocal_notes do
  in_thread do
    volume = 3
    pitch = 12
    use_synth :piano
    2.times do
      sleep 1
      # First chord - Am7
      play :c,pitch:pitch,amp:volume
      sleep 0.5
      play :g,pitch:pitch,amp:volume
      sleep 0.5
      # 1 beat
      # Second chord - G
      play :f,pitch:pitch,amp:volume
      sleep 0.5
      play :e,pitch:pitch,amp:volume
      sleep 0.5
      # 2 beats
      play :d,pitch:pitch,amp:volume
      sleep 0.5
      play :c,pitch:pitch,amp:volume
      sleep 0.5
      # Third chord - Fsus2
      # 3 beats
      play :d,pitch:pitch,amp:volume
      sleep 0.75
      # 3.75 beats
      play :d,pitch:pitch,amp:volume
      sleep 0.75
      # 4.5 beats
      play :c,pitch:pitch,amp:volume
      sleep 0.5
      # 5 beats
      # Fourth Chord - C
      play :d,pitch:pitch,amp:volume
      sleep 0.75
      # 5.75 beats
      play :d,pitch:pitch,amp:volume
      sleep 1.75
      # 7.5 beats
      # Fifth Chord - Dm7
      play :c,pitch:pitch,amp:volume
      sleep 0.5
      # 8 beats
      play :d,pitch:pitch,amp:volume,sustain:0.3,sustain_level:0.5
      sleep 0.45
      play :e,pitch:pitch,amp:volume
      sleep 0.55
      # 9 beats
      play :f,pitch:pitch,amp:volume
      sleep 0.25
      # 9.25 beats
      play :e,pitch:pitch,amp:volume
      sleep 0.5
      # 9.75 beats
      play :d,pitch:pitch,amp:volume
      sleep 0.5
      # 10.25 beats
      play :c,pitch:pitch,amp:volume
      sleep 0.5
      # 10.75 beats
      # Sixth Chord - G7sus4
      play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      cue :verse_half
      sleep 4.25
      # 16 beats
    end
  end
end

define :chorus_vocal_notes do
  in_thread do
    volume = 3
    pitch = 12
    use_synth :piano
	sleep 1
    play :a3,pitch:pitch,amp:volume
    sleep 0.5
    play :b3,pitch:pitch,amp:volume
    sleep 0.5
	# 2 beats
    play :c,pitch:pitch,amp:volume
    sleep 0.5
	# 2.5
	# First Chord
    play :g,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
    sleep 0.75
	# 3.25
    play :e,pitch:pitch,amp:volume
    sleep 0.75
	# 4
	# Second Chord
    play :d,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
    sleep 1
    play :e,pitch:pitch,amp:volume
	sleep 1
	# 6
	play :c,pitch:pitch,amp:volume
	sleep 0.5
	# Third Chord
	play :a,pitch:pitch,amp:volume
	sleep 0.75
	play :g,pitch:pitch,amp:volume
	sleep 0.75
	# 8
	# Fourth Chord
	play :e,pitch:pitch,amp:volume
	sleep 2
	# 10
	
    play :c,pitch:pitch,amp:volume
    sleep 0.5
    play :g,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
    sleep 0.75
    play :e,pitch:pitch,amp:volume
    sleep 0.75
	# 12
    play :d,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
    sleep 1
    play :e,pitch:pitch,amp:volume
	sleep 0.5
	play :c,pitch:pitch,amp:volume
	sleep 0.5
	# 14
	play :c,pitch:pitch,amp:volume
	sleep 0.5
	play :a,pitch:pitch,amp:volume
	sleep 0.75
	play :g,pitch:pitch,amp:volume
	sleep 0.75
	# 16
	play :d,pitch:pitch,amp:volume
	sleep 1
	# 17
	
	play :c,pitch:pitch,amp:volume
	sleep 0.5
	play :g,pitch:pitch,amp:volume
	sleep 0.5
	# 18
	# Chord1
	play :g,pitch:pitch,amp:volume
	sleep 1.5
	play :a,pitch:pitch,amp:volume
	sleep 0.5
	# 20
	# Chord2
	play :g,pitch:pitch,amp:volume
	sleep 1.5
	play :e,pitch:pitch,amp:volume
	sleep 0.5
	# 22
	# Chord3
	play :d,pitch:pitch,amp:volume
	sleep 1.5
	# 23.5
	
	play :d,pitch:pitch,amp:volume
	sleep 0.5
	# 24
	play :e,pitch:pitch,amp:volume
	sleep 0.35
	# Chord4
	play :f,pitch:pitch,amp:volume
	sleep 0.65
	# 25
	play :e,pitch:pitch,amp:volume
	sleep 0.35
	play :d,pitch:pitch,amp:volume
	sleep 0.65
	# 26
	play :d,pitch:pitch,amp:volume
	sleep 0.5
	# Chord5
	play :c,pitch:pitch,amp:volume
	sleep 1
	play :a,pitch:pitch,amp:volume
	sleep 0.5
	# 28
	# Chord6
	play :g,pitch:pitch,amp:volume
	sleep 1.5
	play :e,pitch:pitch,amp:volume
	sleep 0.5
	# 30
	# Chord7
	play :d,pitch:pitch,amp:volume
	sleep 2
	cue :chorus_end
	# 32
  end
end

define :chorus_hum do |volume, last_chorus|
	in_thread do
		time = 0
		pitch = -12
		use_synth :prophet
		with_fx :reverb,room:1,damp:1 do
			with_fx :lpf,cutoff:65  do
				with_fx :normaliser, level: 2 do
					with_fx :compressor do
						with_fx :flanger do
							# 0 beats
							sleep 2.5
							2.times do
								time = time + 1
								if (time == 2)
									#sleep 0.5
								end
								play chord(:d, :minor7),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
								print 'test1'
								sleep 2 #1.5
								# 4 beats
								play chord(:c, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
								print 'test2'
								sleep 2 #2.5
								# 6.5 beats
								play chord(:f, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
								print 'test3'
								sleep 2 #1.5
								# 8 beats
								play chord(:g, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
								print 'test4'
								sleep 2 #2
							end
							play chord(:f, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
							sleep 2
							play chord(:c, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
							sleep 2
							play chord(:g, 'sus4'),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
							sleep 2#2.35
							play chord(:a, :minor),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
							sleep 2#2.15
							play chord(:f, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
							sleep 2#1.5
							play chord(:c, :major),pitch:pitch,amp:volume,attack:2.0/4,decay:2.0/4,sustain:2.0/4,release:2.0/4,sustain_level:0.5
							sleep 2
							if (last_chorus == 0)
								play chord(:d, :minor7),pitch:pitch,amp:volume,attack:4.0/4,decay:4.0/4,sustain:4.0/4,release:4.0/4,sustain_level:0.5
								sleep 4
							end
							if (last_chorus == 1)
								play chord(:d, :minor7),pitch:pitch,amp:volume,attack:8.0/4,decay:8.0/4,sustain:8.0/4,release:8.0/4,sustain_level:0.5
								sleep 8
								cue :song_end
							end
						end
					end
				end
			end
		end
	end
end

define :chorus_ding do
	in_thread do
		
	end
end

define :chorus_arp do
	in_thread do
	end
end

#print "Get ready..."
#sleep 1
#print "Get set..."
#sleep 1
#print "Go!"
use_bpm 100
set :bpm_sync, true
#sample "D:/Sunder/Documents/Sonic Pi/Shelter.wav",amp:1.5#,start:0.177
#start:0.177 = first verse
#play_intro = 1
#play_first_melody = 1

melody_hum_volume = 1.25

# Intro
#if (play_intro == 1)
sleep 0.5
background_beat
melody 1, 1
melody_background_hum melody_hum_volume, 1
sync :melody_half, bpm_sync: true
first_melody_beat
sync :melody_end, bpm_sync: true
#end

# 1st Melody
#if (play_first_melody == 1)
melody 0, 1
melody_background_hum melody_hum_volume, 1
melody_beat 1
melody_background_beat
print "waiting for melody end"
# here's where it stops
sync :melody_end, bpm_sync: true
print "melody end"
#end

# 1st Verse
print "verse 1"
verse_vocal_notes
verse_hum 1
verse_guitar 0
sync :verse_half, bpm_sync: true
sleep 4.25
verse_guitar 1
sync :verse_half, bpm_sync: true

# 1st Chorus
print "chorus 1"
sleep 1.75
chorus_hum 1, 0
chorus_vocal_notes
sync :chorus_end, bpm_sync: true

# 2nd Melody
#use_bpm 100
sleep 2.5
melody 1, 1
melody_background_hum melody_hum_volume, 1
melody_beat 1
melody_background_beat
sync :melody_end, bpm_sync: true
melody 0, 1
melody_background_hum melody_hum_volume, 1
melody_beat 0
melody_background_beat
sync :melody_end, bpm_sync: true

# 2nd Verse
verse_vocal_notes
verse_hum 1
sync :verse_half, bpm_sync: true
sync :verse_half, bpm_sync: true

# 2nd Chorus
sleep 1.75
chorus_hum 1, 0
chorus_vocal_notes
sync :chorus_end, bpm_sync: true

# Melody Build Up
sleep 2.5
melody 1, 1
first_melody_beat
sync :melody_half, bpm_sync: true
warped_melody_beat
sync :buildup_drums, bpm_sync: true
melody_buildup_drums
sync :melody_end, bpm_sync: true

# 3rd Melody
melody 0, 0
melody_background_hum melody_hum_volume, 1
melody_beat 1
melody_background_beat
sync :melody_half, bpm_sync: true
sleep 13.5
#melody_background_hum melody_hum_volume, 1

print "Final Chorus"
# 3rd Chorus
chorus_hum 1, 1
chorus_vocal_notes
sleep 2.5
melody_background_beat
sync :chorus_end, bpm_sync: true
sync :song_end, bpm_sync: true
sample :drum_bass_soft
# Outro

song_ended = 1
1 Like

Hi Sun,

Wow… thats one monster-sized piece of code! By my count it’s
889 lines long.

I’m pretty sure it’s too big to fit in a single SPi tab/buffer, and is being
truncated about line 500-ish, which explains your problem.

I’m suprised it worked even in 2.11,

My own songs sometimes hit the size limit too, so I’m fairly confident on my reply.

Perhaps you could do a rewrite, using strings and rings as opposed to defining each
note pretty much on it’s own line… that might reduce the size considerably… I’m guessing
you will need to be under 500 lines.

The indenting also looks a bit wierd, but that may be due to copy/pasting.

There are methods of using 2 tabs/buffers at once, Robin Newman is
quite an expert on that, and I’m sure if he sees your post he can explain
how to do it.

I hope you can get it working, I’d love to hear the completed piece.

Regards,

Eli…

Rather than leave you to figure everything out on your own, I’ve
rewritten part of your song at the end of this post.

Let me say right now… there is no right or wrong way to write code
for SPi… but some ways are a bit more… compact, for want of a
better word coming to mind.

For example, many of your play commands use common values, which
can be set once, and then simply forgotten…

        play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
        sleep 1.8
        play :c,pitch:pitch,amp:volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5

That can become:

      volume = 3
      pitch = 12
      use_synth_defaults amp: volume,decay:0.5,sustain:0.3,release:0.2,sustain_level:0.5
      use_transpose pitch
      use_synth :piano

Here’s the little bit I converted… hope it helps you.

Eli…

song_ended = 0

live_loop :background_beat do
  while (song_ended == 0) do
      sample :drum_snare_soft
      sleep 1
    end
    stop
  end
  
  live_loop :melody_background_beat do
    cue :background_beat
    while song_ended == 0 do
        sample :drum_heavy_kick,amp:2.5
        sleep 1
        sample :drum_snare_soft,amp:2.5
        sleep 1
      end
      stop
    end
    
    live_loop :melody do
      with_fx :vowel, voice: 1, mix: 0.1 do
        volume = 3
        pitch = 12
        use_synth_defaults amp: volume,decay: 0.5,sustain: 0.3,release: 0.2,sustain_level: 0.5
        use_transpose pitch
        use_synth :piano
        notes = [:c, :a4, :g, :c, :e, :c, :a4, :g]
        sleeps = [1, 0.3, 0.4,1.8, 0.5, 1, 0.3, 1.3]
        8.times do |x|
          play notes[x]
          sleep sleeps[x]
        end
      end
      song_ended = 1
      stop
    end
    
    live_loop :first_melody_beat do
      notes=[:f,   :c5,   :e,     :g,   :e,     :a,      :d]
      key=[:sus2,:major,:minor7,:sus4,:minor, :minor7, :minor7]
      repeats=[16,    6,    10,     6,    10,     12,      4]
      volume = 1.5
      pitch = -12
      time = 0
      use_synth :chiplead
      while (song_ended == 0) do
          with_fx :lpf, cutoff:100, amp:volume do
            with_fx :flanger,mix:0.1 do
              7.times do |x|
                repeats[x].times do
                  play chord(notes[x], key[x]),amp:volume,pitch:pitch
                  sleep 0.25
                  if (song_ended == 1) then
                    stop
                  end
                  
                end
              end
            end
          end
        end
      end

Hi,
Thanks for your suggestions on how to condense the code, I will try those later. :slightly_smiling_face:
I forgot to mention that, to play the code, I use something like:
run_file “shelter.txt”
to try and get round the OSC message limit. It worked in v2.11.1, but maybe some of the changes relating to osc in v3.1 affected that.

Hi,

I would suggest reducing the size of the program until it starts working again - and then at that point you might start to get an idea of where it goes wrong.

If you’re able to create a short example of a bug I’d be very happy to look into fixing it.

Additionally, be aware that between v2 and v3 cue and sync were rewritten to make them fully deterministic. In v2 and before they had race conditions which meant that the behaviour could change with successive runs. It’s quite likely that this change has effected the behaviour of your program. If this is the case, please accept my apologies. However, things are much more likely to be stable with your code going forwards.

Hi, I thought that sync would wait for a cue. Is that right? If that’s not the case, could you please explain how they work differently? Thanks. :slightly_smiling_face:

HI @SunderB,

see

for an explanation.

1 Like