Cabbage Logo
Back to Cabbage Site

Detect Samplerate

Hi everyone,

is there a way to detect the samplerate the vst host is using?

What I need is a way to determine it and then make it possible to choose the set of correct files in that samplerate.

thanks
//rootnote

Leave out the sr = assign in your header section and the host will set the sampling rate. Then simply query sr in your code to get the current sampling rather. If your loading files from disk a sample rate convertion should happen automatically afaik. If you are loading files to a function table, then I think you will need to do some converstion.

yes, I am loading files to a function table with ftload. What could be a smart way to convert those?

thanks
//rootnote

Use diskin2 to do the sample rate conversion for you. Here’s some sample code courtesy of Victor Lazzarini. It loads a file using diskin2, which does the sample rate conversion, and then writes to a function table. It’s a pretty neat trick :wink:

opcode ResampleAndLoad,k,S
kfun init 0
Sfile xin
if kfun == 0 then
andx init 0
kk init 0
ifl = filelen(Sfile)*sr
ifn ftgen 0,0,ifl,2,0
while kk < ksmps do
  andx[kk] = kk
  kk += 1
od
kk = 0
while kk < ifl do
  asig diskin2 Sfile
  tablew asig, andx+kk, ifn
  kk += ksmps
od 
kfun = ifn
endif
xout kfun
endop

instr 1
gkf ResampleAndLoad "fox.wav",1
endin
schedule(1,0,1/kr)

instr 2
a1 poscil 0.5,sr/ftlen(i(gkf)),i(gkf)
   out a1
endin
schedule(2,1/kr,4)
1 Like

Hi Rory. I’ve generally used a much simpler method, it seems to work fine regardless of the sr:

instr 1
  Sfile = "fox.wav"
  iLen  = filelen(Sfile)
  iFile = ftgen(0, 0, 0, 1, Sfile, 0, 0, 0)
  aL    = poscil(1, 1/iLen, iFile)
  aNdx  = phasor(1/iLen)
  aR    = table:a(aNdx, iFile, 1, 0, 1)
  outs(aL, aR)
endin 

I’m curious though, knowing Victor likely has reasons for such an approach. I threw in a phasor/table reading opcode for good measure. What is the benefit to actually resampling? Does it somehow offer better fidelity than just using interpolation when playing back the original waveform at a different sr?

I assume libsndfile does a pretty good of resampling, perhaps it uses some higher level of interpolation. But I can’t say for sure. The UDO Victor wote was specifically for resampling IRs, perhaps it was important in this instance to have the best interpolation possible. Again, I’m only speculating. Thanks for sharing your solution. :+1: It’s very elegant.

Thanks for the reply Rory.

One thing I missed (that ftload was mentioned) is that if one is loading ftables, even if they were created using audio files with different sr, the saved ftables have the sr embedded so it’s easy to deal with. For example, if they were created from audio/samples (not single cycle waveforms):

; table size not important but
; should be <= table being loaded
giLoad = ftgen(0, 0, 2, 2, 0)
ftload "file01.ft", 0, giLoad

instr 1
  iLen  = ftlen(giLoad)
  iSr   = ftsr(giLoad)
  iDur  = iLen/iSr
  aSig  = poscil(.8, 1/iDur, giLoad)
  out aSig
endin

If the ftables are a bunch of single cycle waveforms, e.g. for wavetable synthesis, I’m not sure conversion or sr compensation is really necessary.

No, I wouldn’t imagine it’s required. But if you’re using convolution opcodes you will need to do the conversion in advance as the opcode is only passed the table number. I don’t think it would take much work to update the convolution opcodes so they automatically resample. It would be a nice update.