How to access the 'previous sample' in an audio stream?


#1

Hi,
I am trying to create a zero-crossing detector, that is, as soon as the audio value crosses the 0 axis to output a value (e.g. 1) for 1 sample only. I could do this by comparing the previous audio sample with the current sample, but how do I access the previous value?
In a soft synth I used before there was a "z^-1" module that did exactly that (kept the previous sample value).
Any suggestions? Thanks.


#2

Store the current sample in a variable at some point in your code and then access it earlier in the code so that when it's accessed it's what was stored the last time round. I doubt there's a pre-defined global variable for 'previous sample', so just store it at some point and then check it next time round before it's overwritten.


#3

Thanks very much for your reply. Unfortunately the lack of documentation is killing me, so please bear with me.
I am unsure how this can be done in the S-rate Code of an object.
Each time I call (let's say) inlet_in1, do I get an updated value of the current audio stream every time it is called? Or better, how do I get an updated value of the stream?
Thanks.


#4

I'm not sure myself, I'm just trying to apply common sense here so I also need you to bear with me. :smile:

I would think that the s-rate code block is run in its entirety once per sample. So when you call inlet_in1 you get the sample currently being evaluated... so calling that several times within the block will get you the same sample several times until the s-rate code reaches its end at which point it starts all over again for the next sample. So to refer to the previous sample, you have to have stored it somewhere in the previous run of the s-rate code. So if right at the end of your s-rate code you store 'inlet_in1' in 'prev' or whatever, then I would think you can then check 'prev' earlier in the s-rate code to see what the previous sample was.

So to answer the question of how you get an updated value, the answer would be wait until the next pass of the s-rate code.

I hope that makes sense (and is in fact correct, I'm hoping someone will confirm this for both of our sakes).


#5

is it not possible to just use a delay line?
e.g.
table size 2
delay permanently set at 50% (32)

if your are writing custom objects, theres a few ways to do it... either in s-rate, or the k-rate code has access to an entire buffers (16 samples). also both have access to both inlet and (previous) outlet data.


#6

Thanks for the pointers! Will investigate these.


#7

Delay lines didn't occur to me as I assumed the OP was talking about within an object's code.


#8

Sorry to ask again, but I must be looking in the wrong place.
The delay/echo module allows me to delay by 33 samples (?)
How do I delay by 1 sample (@thetechnobear mentioned table size 2).


#9

sorry, mixing my tables and delays up... (they are pretty similar :wink: )
ok so with table write, you have a minimum of 256 samples, so one sample would be 64/256...

its a pity delay/echo doesnt go down to 1... only 32

my only 'fear' with all this, is working at this individual sample level is not very efficient, it would perhaps be worth considering doing this on the sample buffer (16 samples), and then incorporating your further logic.

the concern here is once you solve the Z-1, your then going to have to do a lot of other processing, potentially at sample level, so whilst the initial problem is solved, your just heading for bigger ones down the line e.g. you'll fine many objects that dont work at this level, for this reason.


#10

Thank you for the follow up! I learned something new about the tables.
In the mean time I found the filter/delta, which in combination with the relay object I managed to come up with, do exactly what I needed. Now I have a pitch follower driving a sine oscillator from the microphone input (takes 6% of the CPU load) It is very sensitive and difficult to tune, but if anybody is interested I can post it. Probably it has several questionable and inefficient choices, but this is the first thing I could do with the new board. :smile:


#11

yeah please post it, i'm interested!