How to play sample with notes?

hi everyone thanks for your help so far :slight_smile: below is a small simple riff played with the square synth:
“”"
live_loop :example do
use_bpm 140
with_swing 0.6 do
use_synth :square
riff = (ring :e , :r , :g , :f , :r , :e)
play riff.tick
end
sleep 1
end

is it possible to play this riff with a sample instead of the square synth and please could you explain to me how to do this? thanks for all your help :slight_smile:

Hi @soundwarrior,

There is no built-in command to directly say ‘play note X with sample Y’. However, there are ways to change the pitch or the rate that a sample plays at. (Using opts for the sample command such as pitch:, rpitch:, and rate:, etc).
It’s definitely possible to write a function that takes into account your sample and a desired note to play at, which then calculates how much to adjust the pitch of the sample to match the desired note; but this would also depend on knowing beforehand (or being able to figure out, even if only approximately) the basic ‘note’ of the un-modified sample.

There is a short discussion at the topic http://in-thread.sonic-pi.net/t/tune-samples-with-a-note-input/1703 which may be of interest, with a function that @robin.newman once demonstrated for playing specific notes with a sample. Does any of it make any sense? - there’s comments in the code, and I’m sure we can explain the details as well :slight_smile:

1 Like

hi that makes sense thanks :slight_smile: could I say for example using pitch or repitch set notes for the sample to play at?

hi everyone thanks for your help :slight_smile:

Yes, you can use pitch: or rpitch: when calling sample in your own function so that you can pass the function a note, and it works out the right pitch for the sample to play at. (The other topic I linked above - Tune samples with a note input shows an example: Robin’s :plAmbiChoir function there takes a note to play at, a note length, and a number representing the ratio between the note’s sustain and release values, for a basic sound envelope, and plays the sample at the correct pitch). I’m sure you could use a similar function with your own samples - you’d just need to work out the basic pitch of your un-modified sample, as Robin describes in his thread.
Feel free to have a go at adapting Robin’s example if you like - I’m happy to help you if you want.

1 Like