Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program with dynamic knob names makes knobs flicker #19

Open
rillig opened this issue Oct 31, 2018 · 3 comments
Open

Program with dynamic knob names makes knobs flicker #19

rillig opened this issue Oct 31, 2018 · 3 comments
Labels
bug Something isn't working

Comments

@rillig
Copy link
Contributor

rillig commented Oct 31, 2018

Describe the bug
When I paste the following code, it produces an ugly noise and shows the expected knobs on the right side.
After waiting for a few seconds, maybe hovering over the knobs, they disappear and are replaced by the "Tip: add knobs" message. They start to flicker, quickly switching between the "Tip" message and the knobs. At this point, it is not possible to use the knobs.

// Harmonic sine
//
// See https://en.wikipedia.org/wiki/Harmonic

const persistent = this

function logScale(knob, min, max) {
    return min * Math.exp(knob * Math.log(max / min))
}

function linearScale(knob, min, max) {
    return min + (max - min) * knob
}

var out = 0

const baseFreq = logScale(knobs['Base frequency'], 220, 880)

function addFreq(name, freq) {
    const volume = knobs['' + name + ' volume ']
    const shift = knobs['' + name + ' phase shift']
    const id = 'phase.' + name
    const phase = (persistent[id] + freq / 44100) % 1
    persistent[id] = phase
    out += volume * Math.sin(2 * Math.PI * ((phase + shift) % 1))
}

for (let i = 3; i <= 11; i += 2) {
    addFreq('' + i, i * baseFreq)
}

return out * knobs['Master volume']

Expected behavior
The knobs are generated, even though the knob names cannot be parsed from the source code.
Using the knobs it is possible to control the phase shift and volume of each of the harmonics.
Since the knobs don't change effectively, the right half of the screen should not be repainted.

Environment (please complete the following information):

  • OS: Windows 10
  • Browser: Chrome
  • Version 70.0.3538.77 (Offizieller Build) (64-Bit)

Additional context

  • CPU usage goes up to 30%, which on my 4-core system is probably 1 CPU busy with processing events and another one with garbage collection.
  • The event queue in the browser seems to fill up. When changing the program, it takes up to 30 seconds for the sound to adjust to the changed program.
@cfstras
Copy link

cfstras commented Nov 1, 2018

One thing about the knobs is that they aren't parsed from the source code, but rather there is detection code on the "knobs" object. IIRC, if a knob isn't accessed in every call of your script, it will behave strangely. So you have to make sure to do that.

@rillig
Copy link
Contributor Author

rillig commented Nov 3, 2018

As far as I understand the code, the knobs have to be used at least once every 1.5 seconds to stay on the screen. The keyword is lower_usage.

I added some debugging code (console.log in the events) and found out that a knob is displayed when its knobUsages is larger than 0.25. When it is actually accessed, its usage is increased by a bit. When it is not used for a time of 100 ms, its usage gets decremented.

Now the thing is that a knob can always be around the magic value 0.25 and then the screen gets nervous. Whether a knob is active or not should work more like a Schmitt trigger, which provides more stability.

An alternative were to change the rule set:

  • A knob that is used is shown immediately, knobUsage[key] = 2.
  • Each knob usage goes down by 0.1 every 100 ms.
  • Knobs whose usage is below 0 are deleted.

I tried this, but the above program still creates lots of noise, and even when I put a return 0 in the first line to get immediate silence, the noise still continues for minutes. I didn't wait until it would finally stop. The knobUsage decreased a little bit, by about 0.1 every five minutes. That was totally unexpected.

@anuejn
Copy link
Owner

anuejn commented Nov 4, 2018

yup that code stinks a bit ;). you are very welcome to improve / rewrite it.

however, i am not a big fan of displaying knobs immediately, as this would spawn many knobs during the writing of long knob names.

the last error you described sounds like the noise would never stop, because one of the roules for roll back kicked in.

@anuejn anuejn added the bug Something isn't working label Dec 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants