Cross-fade two samples

My first little snippet, wanted to create a cross-fade effect to be used in a Pubquiz (using two songs as separate samples, cross-fade in between them and people need to guess the second song ;)).

Hope you guys like it and can do something with it. If not, that’s also ok. Feedback is welcome!

speed_factor=0.25

sample1= :ambi_lunar_land
sample2= :ambi_dark_woosh

duration_sample1=sample_duration(sample1)
duration_sample2=sample_duration(sample2)

#Reference duration is the smallest duration of the two samples, to make sure
#the samples end at the same time, so we have a correct cross-fade
reference_duration=duration_sample1
if duration_sample1 > duration_sample2
  reference_duration = duration_sample2
end

#Lets alter the speed by a factor
reference_duration/=speed_factor
loop do
  
  #Cross-fade happens here, pretty self-explanatory I would say
  sample sample1,beat_stretch: reference_duration, release: reference_duration
  sample sample2,beat_stretch: reference_duration, attack: reference_duration
  sleep reference_duration
end

Thanks for this - nice use of the built-in samples.

One thing to look at next (if you don’t mind a bit of a tip) is to check out live_loop. It’s actually pretty rare to find loop in typical Sonic Pi code these days as live_loop is actually a lot more useful (and fun!).

Have fun on your live coding journey!