Creating a Pitch Tracking SynthDef


#1

Hi All,

I’ve been teaching an introductory computer music course for a couple of weeks now and some of the students are interested in pitch tracking audio input. It doesn’t appear that there is something similar to the fiddle~ object from Pure Data / Max in Sonic Pi, and so I was looking at creating a SuperCollider SynthDef to do pitch tracking. I’ve gotten as far as making a basic SynthDef in Supercollider that maps the detected pitch of audio input to a sine tone, and I’ve compiled the synth def and loaded it into sonic pi using load_synthdefs, but… there’s no output.

I think I’m hung up on how to get audio in piped into this synthDef possibly? I don’t have much experience in SuperCollider, so maybe I’m missing something else also, tbh. Any help with this would be hugely appreciated!

Here’s the SC code:

(

SynthDef(\pitchTracker,{

var env, in, freq, hasFreq;



// the audio input

in = AudioIn.ar(1); 



// the pitch variable and the hasFreq (Pitch.kr returns a list like this [freq, hasFreq])

# freq, hasFreq = Pitch.kr(in, ampThreshold: 0.2, median: 7);



// when the hasFreq is true (pitch is found) we generate a ADSR envelope that is open until

// the hasFreq is false again or the amplitude is below the ampThreshold of the Pitch.

env = EnvGen.ar(Env.adsr(0.51, 0.52, 1, 0.51, 1, -4), gate: hasFreq);



// we plug the envolope to the volume argument of the Sine

SinOsc.ar(freq, 0, env * 0.5) ! 2



}).writeDefFile("/Users/username/Desktop/synthDefs") ;

)

Ideally, this pitch tracker would be able to output the pitch data that’s accessible inside the SynthDef to Sonic Pi and not just play audio. It would be useful to have a function like this that you could route an audio stream into and get the fundamental frequency as output data that could be plugged in elsewhere in Sonic Pi.

Thanks for any help or thoughts on this!


#2

I’d love to get something like this working. My first thoughts are that it might make sense to model this as an “effect” e.g.

with_fx :pitch_tracker do
  foo
end

Then inside the synthdef, pass the audio through (unaffected) but then send an OSC message to a specific address each time the pitch changes using something like SendReply I don’t know about the specifics of how that would work yet but that’s how I’d try to implement it.

Then to get the result within Sonic Pi you could use the osc method to receive the message. If it works well we could look at wrapping it in a more convenient method for inclusion in the main app if needed.

Let me know what you think!