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

Add optional normalization parameter to createPeriodicWave #545

Merged
merged 4 commits into from
Jul 22, 2015

Conversation

rtoy
Copy link
Member

@rtoy rtoy commented Jun 15, 2015

Fix #91

This adds the optional normalization parameter to createPeriodicWave, defines how normalization is done and how the time-domain waveform can be created from the Fourier coefficients.

Raymond Toy added 2 commits June 12, 2015 10:39
This fixes WebAudio#91 by adding the optional normalization parameter to
createPeriodicWave.  We also describe the basic waveform generation
and the normalization process.
…icWave

o Add optional normalization parameter to createPeriodicWave.
o Define how normalization is done.
o Define the waveform generation better.
@domenic
Copy link
Contributor

domenic commented Jun 16, 2015

This seems like it exhibits two antipatterns:

  • The boolean trap: context-less boolean arguments
  • Incorrect defaults: defaults for booleans should always be false.

May I suggest createPeriodicWave(..., { denormalized: true }) instead of createPeriodicWave(..., false)?

@hoch
Copy link
Member

hoch commented Jun 16, 2015

That's a good point. However, we might have to consider the different name than dernomaized. What we are trying to do here is to avoid the internal normalization. I think what denormalizing means does not quite fit here.

Perhaps we should do createPeriodicWave(..., { normalize: true }) and change the default behavior of the method since the internal normalization was known to be problematic in some cases. Some people will not like it though.

@domenic
Copy link
Contributor

domenic commented Jun 16, 2015

Yeah, I thought denormalized might not be right. And yeah, it would be cleanest to change the default. Otherwise you have to come up with an inverted name, which can be awkward for words without natural antonyms.

@rtoy
Copy link
Member Author

rtoy commented Jun 16, 2015

I am opposed to changing the default normalization.

@domenic
Copy link
Contributor

domenic commented Jun 16, 2015

Right, I didn't meant to imply you should actually make a backward-incompatible change, just that that would be "easiest" from a naming perspective. Coming up with an inverted name seems most straightforward.

@rtoy
Copy link
Member Author

rtoy commented Jun 16, 2015

Ok. I don't have any strong opinions either way on the other suggestions. I'd even be ok with createUnnormalizedPeriodicWave(r, i).

@foolip
Copy link
Contributor

foolip commented Jun 17, 2015

Incorrect defaults: defaults for booleans should always be false.

I like it when this is the case, but does it matter if the default is true in cases where the inverted name would be awkward?

@domenic
Copy link
Contributor

domenic commented Jun 17, 2015

I like it when this is the case, but does it matter if the default is true in cases where the inverted name would be awkward?

Yes, I think it's better to have a slightly-awkward name than to violate the expectation that a boolean defaults to false.

@foolip
Copy link
Contributor

foolip commented Jun 17, 2015

FWIW, it wouldn't be the first dictionary member to default to true, and the existing cases in canvas don't seem terrible to me:
https://html.spec.whatwg.org/#canvasrenderingcontext2dsettings
https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.2

Of course, it would depend on what the actual suggestion is, and if there are actual usability problems with boolean dictionary members defaulting to true, not just a style issue.

@rtoy
Copy link
Member Author

rtoy commented Jun 18, 2015

If we write the Fourier series in complex form, I think this basically works out to be the same as solving for the roots of an order N polynomial with complex coefficients. Doing the inverse transform and finding the max is much easier to do and doesn't require finding and writing a good polynomial root solver, especially since the order of the polynomial could be as high as 2048 or more.

@karlt
Copy link
Contributor

karlt commented Jun 18, 2015 via email

@hoch
Copy link
Member

hoch commented Jun 19, 2015

I can second the idea of having PeriodicWave detached from the context, but:

var p1 = new PeriodicWave({ normalization: false });
var p2 = context.createPeriodicWave({ normalization: true });

This doesn't look great in my opinion. Probably we really need to try hard to come up with a clever name.

@rtoy
Copy link
Member Author

rtoy commented Jul 17, 2015

How about we just name the parameter disableNormalization?

@hoch
Copy link
Member

hoch commented Jul 17, 2015

+1

A bit verbose, but I can live with that.

@padenot
Copy link
Member

padenot commented Jul 17, 2015

The key of the dict, you mean ? Or just the parameter in the prototype ?

@rtoy
Copy link
Member Author

rtoy commented Jul 17, 2015

Just the parameter. I don't foresee other items we would want to add to the dict, so a dict seems like overkill.

@domenic
Copy link
Contributor

domenic commented Jul 17, 2015

No. Use a dict. The Boolean trap is real.

@rtoy
Copy link
Member Author

rtoy commented Jul 17, 2015

Ugh. Ok.

@karlt
Copy link
Contributor

karlt commented Jul 19, 2015 via email

@rtoy
Copy link
Member Author

rtoy commented Jul 20, 2015

I don't quite follow.

But in any case, the waveforms for oscillators are always normalized, and to be consistent with PeriodicWaves, the normalization must be consistent with the normalization of PeriodicWaves. We are not allowing oscillators the option of not being normalized. If a user wants that, they can use a periodic wave without normalization.

@rtoy
Copy link
Member Author

rtoy commented Jul 20, 2015

Please take another look. I've updated the spec to use a dictionary.

the <code>real</code> and <code>imag</code> parameters represent
<em>relative</em> values.
<em>relative</em> values. If normalization is disabled via the <code>normalize</code>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/normalize/disableNormalization?

@domenic
Copy link
Contributor

domenic commented Jul 20, 2015

LGTM, thank you so much for taking the time to make this API ergonomic :)

padenot added a commit that referenced this pull request Jul 22, 2015
Add optional normalization parameter to createPeriodicWave
@padenot padenot merged commit 7782b88 into WebAudio:gh-pages Jul 22, 2015
@padenot
Copy link
Member

padenot commented Jul 22, 2015

Yep, looks good.

@hoch
Copy link
Member

hoch commented Jul 23, 2015

@domenic Is there any method in the other Web APIs that uses a JavaScript object (dict) as an optional descriptor? What if new Float32Array(1) is passed as an argument? For example:

context.createPeriodicWave(new Float32Array(5), new Float32Array(5), new Float32Array(5));

How should we handle the third argument?

@domenic
Copy link
Contributor

domenic commented Jul 23, 2015

Yes, I suggest searching for the keyword "dictionary" in your favorite spec (e.g. DOM or HTML) and you will find plenty of examples.

From: Hongchan Choi notifications@github.com
Sent: Jul 22, 2015 5:51 PM
To: WebAudio/web-audio-api
Cc: Domenic Denicola
Subject: Re: [web-audio-api] Add optional normalization parameter to createPeriodicWave (#545)

@domenichttps://github.com/domenic Is there any method in the other Web APIs that uses a JavaScript object (dict) as an optional descriptor? What if new Float32Array(1) is passed as an argument? For example:

context.createPeriodicWave(new Float32Array(5), new Float32Array(5), new Float32Array(5));

How should we handle the third argument?


Reply to this email directly or view it on GitHubhttps://github.com//pull/545#issuecomment-123918885.

@hoch
Copy link
Member

hoch commented Jul 23, 2015

@domenic Thanks! It helped.

@karlt
Copy link
Contributor

karlt commented Aug 26, 2019

However, it wouldn't solve the problem with built-in oscillator types if they are going to be based on a similar normalization using a magnitude from an arbitrary Fourier truncation of the specified waveform. If that is the intention, then a solution is required on OscillatorNode instead of PeriodicWave.

#2046 is reconsidering this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants