# [PD] How does vline~ work under the hood?

Matt Barber brbrofsvl at gmail.com
Fri Oct 2 18:06:59 CEST 2015

```Post some code. :)

In both [line~] and [vline~] I believe the increment is calculated, set,
and unset set once per event, since all the info you need for the ramp is
given by the event definition. Unsetting it is the fun part -- it doesn't
unset once it's reached its target, but rather once the number of blocks or
samples has passed. Check out these lines of code, though:

from [line~]:
if (x->x_ticksleft)
{
t_sample f = x->x_value;
while (n--) *out++ = f, f += x->x_inc;
x->x_value += x->x_biginc;
x->x_ticksleft--;
}
else
{
t_sample g = x->x_value = x->x_target;
while (n--)
*out++ = g;
}

from [vline~]:
if (x->x_targettime <= timenext)
f = x->x_target, inc = x->x_inc = 0, x->x_targettime = 1e20;
*out++ = f;
f = f + inc;

In [line~]: if there are still blocks to ramp over, get the value set from
the last block, increment all the samples in the new block by the
increment, store what will be the value for the next block, and decrement
the number of blocks left. If there are no blocks left, then manually set a
variable and the next block value to the target, and write that to all the
points in the block.

In [vline~]: if the target time has elapsed, set the variable f to the
target, the increment to zero, and put the next target time WAY off in the
future. Then continue to increment successive values in the block by 0.
Since it's checking every sample here instead of every block, a conditional
here would be more expensive than just adding 0 every time.

Anyway, manually setting the output value to the target at the end of the
ramp ensures that you'll make your target. That way you don't have to worry
about trying to time exactly when to unset the increment right when it's
finally incremented to the target, which is a great way to get an
off-by-one error.

Matt

On Fri, Oct 2, 2015 at 11:36 AM, i go bananas <hard.off at gmail.com> wrote:

> Hi, me again.
>
> Thanks for the discussion.  It has really opened my eyes.
>
> So, i got my naive c++ implementation of line~ basically working.
>
> And of course, just running a for loop incrementing by ticks, i run into
> the exact precision error that this block quantizing seems to avoid.  My
> line from 0 to 100 over 44100 samples only gets to 99.93
>
> So, i also need to add something like pd's block quantization to make sure
> my line goes all the way to the specified value.
>
> My questions then are 2:
>
> Is pd's method the way i should do it?  Or is there a better alternative?
>
> And, if i do it the pd way, how does that work?  Does the increment get
> updated every block?   Or is it just the last block that is stretched?
>
> _______________________________________________
> Pd-list at lists.iem.at mailing list
> UNSUBSCRIBE and account-management ->
> http://lists.puredata.info/listinfo/pd-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20151002/f7e78a26/attachment.html>
```