belangeo / pyo

Python DSP module
GNU Lesser General Public License v3.0
1.32k stars 132 forks source link

Issue with Linseg output #264

Closed smojef closed 10 months ago

smojef commented 1 year ago

Hello, I am having a hard time with Linseg because it outputs values out of the supplied range. For instance, the following code:

from pyo import *
s=Server(sr=8000,nchnls=2,buffersize=100,duplex=0)
s.boot()
lin = Linseg([(0,0), (2,0), (2.1,0.001), (3.9, 0.01), (4,0)], loop=True).play()  # Linseg is not accurate
p = Print(lin, method=0)
s.gui(locals())

Outputs:

0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000001
0.001751
0.003001
0.004251
0.005501
0.006751
0.008001
0.009251
-0.000013
-0.000013
-0.000013
-0.000013
-0.000013
-0.000013
-0.000013
-0.000013
-0.000011
0.001738
0.002988
0.004238
0.005488
0.006738
0.007988
0.009238
-0.000013
-0.000013
-0.000013
-0.000013
-0.000013
-0.000013
-0.000013
-0.000013

The first loop is ok, but then it outputs negative values which are clearly not intended.

I was expecting a linear interpolation between segments. Did I missed something? Should I use another object instead?

Thanks,

belangeo commented 1 year ago

I can repro... Looks clearly like a bug, I'll investigate.

belangeo commented 1 year ago

Ok, floating-point rounding error when computing the increment for the linear interpolation... Fix will be up in 1.0.6 (I upload 1.0.5 wheels today). Simple workaround for the time being is to clip the output of Linseg to the intended range:

lin = Expseg([(0,0), (2,0), (2.1,0.001), (3.9, 0.01), (4,0)], loop=True).play()
linc = Clip(lin, min=0.0, max=0.1)
smojef commented 1 year ago

Thanks @belangeo for looking into this. Here is another test case where I see no obvious workaround. Can you confirm that your fix covers this case?

from pyo import *
sr = 8000
s=Server(sr=sr,nchnls=2,buffersize=100,duplex=0)
s.boot()
segments = [
        (0,     100),
        (0.99,  100),
        (1,     440),
        (1.99,  440),
        (2,     200),
        (2.99,  200),
        (3,     820),
        (3.99,  820),
        (4.0,   100),
    ]
# Is this the way to have exactly 32000 samples per loop?
ls = Linseg(segments, loop=True).play()     # Wrong values
modulo = ls % 100                           # Even worse after some math
p = Print(modulo, method=0)
s.start()
s.gui(locals())

Output:

0.000000
0.000000
0.000000
40.000343
40.000343
40.000343
40.000343
0.000119
0.000119
0.000119
0.000119
20.000744
20.000744
20.000744
20.000744
90.999992
90.999992
90.999992
90.999992
31.000328
31.000328
31.000328
31.000328
91.000107
91.000107
91.000107
91.000107
11.000729
11.000729
11.000729
11.000729
90.999992
90.999992

Expected output:

0.000000
0.000000
0.000000
40.000000
40.000000
40.000000
40.000000
0.000000
0.000000
0.000000
0.000000
20.000000
20.000000
20.000000
20.000000
0.000000
0.000000
0.000000
0.000000
40.000000
40.000000
...

Best,

belangeo commented 10 months ago

Hi @smojef , it's now fixed in master. Just a note about your last snippet, don't forget that there are a lot of samples in 10 ms (i.e. between 0.99 and 1 sec), it's likely that your Print will output some intermediary values. If you want to print the stable part of the Linseg segments, you can slightly delay the input of Print, like:

p = Print(SDelay(modulo, 0.011), method=0)

With this, I get your expected output.