supercollider icon indicating copy to clipboard operation
supercollider copied to clipboard

Decay and Decay2 initial sample is double what's expected

Open jamshark70 opened this issue 1 year ago • 5 comments

Environment

  • SuperCollider version: current dev
  • Operating system: n/a
  • Other details (Qt version, audio driver, etc.): n/a

Steps to reproduce

{ Decay.ar(Impulse.ar(200), 1.5/200) }.plot

decay-bug

The second impulse performs as expected: there is a small amount of energy that has not decayed away, and the second impulse peak adds exactly 1.0.

The first impulse, however, causes Decay to peak at about 2.0.

I'm fairly sure this is an initial-sample issue. Recently there were some changes about UGen initialization, but blame view on FilterUGens.cpp shows clang reformatting four years ago, and before that, 11, 14 and 21 years ago (and nothing about initialization).

I'm puzzled because I used Decay and Decay2 before, and I don't remember this behavior... but the code has "always been like this" lol

It looks like I can work around it like this:

{ Decay.ar(Impulse.ar(200) - (0.5 * Impulse.ar(0)), 1.5/200) }.plot

... but this doesn't seem right. If, at every impulse, you're adding in exactly 1 sample of 1.0 (full-scale) energy, then the amount of increase at each impulse should be consistent.

jamshark70 avatar Nov 28 '23 03:11 jamshark70

Ohhh I see... it's because Impulse now sets its pre-sample to 1, if the initial phase is 0. So Decay sets y1 to 1, and then the first real calculation cycle adds to that.

(Decay2's initial sample is always expected to be 0 because it's the difference of two Decays -- but the double counting means that the envelope resulting from the first impulse is twice the expected amplitude.)

I could fix Decay2 by initializing unit->m_y1a and unit->m_y1b to 0., instead of the input sample.

But in Decay, replacing Decay_next(unit, 1); with ZOUT0(0) = ZIN0(0) at the end of Decay_Ctor doesn't work, because the coefficients don't get initialized. I'm out of time on that one. Probably need to set unit->m_b1 by formula in Ctor as well.

I think I'll just go with the workaround for now, for my code's compatibility with other systems.

jamshark70 avatar Nov 28 '23 05:11 jamshark70

Sorry I haven't yet gotten to this one yet, it's the list. Just finished up the LFUGens.cpp, FilterUGens.cpp is next, I'll bump the Decays toward the top of the list.

mtmccrea avatar Jan 07 '24 20:01 mtmccrea

Fixed by #6194

mtmccrea avatar Jan 13 '24 19:01 mtmccrea

Oh, nice! Any idea when the fix could be merged?

TatriX avatar May 03 '24 11:05 TatriX

Still waiting on a review :-/

mtmccrea avatar May 03 '24 21:05 mtmccrea

Oh, nice! Any idea when the fix could be merged?

@TatriX note this fix to Decay/2 has been merged into develop (no timeline yet for main release though). Thanks @joslloand!

mtmccrea avatar Jun 14 '24 06:06 mtmccrea