Tune samples with a note input


I don’t know if this is even possible, but here we go:

Is there a way, in sonic pi, for tuning samples harmonically? I mean, if I have a bassline based on C chord, could I apply a function to a, for example, :ambi_choir sample to make it sound in C?
I know I can play with rate and pitch options, but what I want to know is if the pitch or rate of a sample can be changed, not directly with a number, but giving a note name parameter to a hypothetical function input.



Do you mean something like this?

#using sample :ambi_choir to accompany a tune

use_synth :tri

define :plAmbiChoir do |n,nt,sr|
  #use :rpitch to calculate pitch for required note frequency
  #Natural note is 75.2 found by experiment comparing
  #play 75.2   and     sample :ambi_choir      sounds
  sample :ambi_choir,rpitch: note(n)-75.2,sustain: nt*sr,release: nt*(1-sr),amp: 2

live_loop :test do
  #test live loops plays notes from scale accompanied by pitched sample :ambi_choir
  #nTime adjusts time between notes
  #srRatio adjusts % sustain and release times: eg 1 gives all sustain 0 release
  #0.5 gives equal sustain and release times
  #note you are restricted by duration of sample :ambi_choir (about 1.57s)
  #this duration will reduce when pitch is raised
  n=note(scale(:c4,:major,num_octaves: 2).choose)
  play n,sustain: nTime*srRatio,release: nTime*(1-srRatio),amp: 0.5
  sleep nTime

The sample this technique works best with is :ambi_glass_rub (which normally sounds as :Fs5) I first used it back in 2014 with a much earlier version of Sonic Pi before the :rpitch parameter was added to produce some Mozart.

I have edited the file to use :rpitch now and you can try it from here
(Copy and paste into Sonic Pi).


Yes Robin, that’s great! Thanks! I haven’t fully read it yet, but it sounds good when executed.

Just one more question: The natural 75.2 note from :ambi_choir (78 in rpitch :ambi_glass_rub version) is something that you’ve previously figured out, isn’t it? I mean, I can’t get the natural note of a sample right away. Is this correct?

Thank you again!


I just found tthe freq by playing the sample at the same time as a note and adjusting the note until they sounded the same. You need a frequency meter to do it otherwise. You could do that in a processiing script I think, but easiest to just do it manually. You only need to do it once for a given sample. Then just use :rpitch