-
Notifications
You must be signed in to change notification settings - Fork 757
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
Added a new 'snap' function to SimpleNumber that quantizes values #3160
Conversation
It would be good to add a comparison between I could also imagine a |
Well quantize will change any values bigger than (quantum+-tolerance) while snap will change any value smaller than (quantum+-tolerance). So I can certainly add that. There was talk of deprecating quantize just because it's horribly named (my proposed function 'snap' is what quantize should have been). Of course in an ideal world my new function would be 'quantize', and the current 'quantize' function would probably be called 'snap'. Backwards compatibility is annoying sometimes. I'd also like to change the parameter name for quantize. I proposed 'grid' for snap, as it relates to the idea of a musical grid of beats (e.g. a sequencer/DAW). I really don't love that, but quantum is quite mathy and scary to non-math people (which is probably most users of SuperCollider, and certainly the ones most intimidated by it in the first place). |
Quantize should be deprecated, for the reasons given in #2826:
A possible alternative is to deprecate quantize and add a second function which does the same thing but is more appropriately named. However, I think you'll have a hard time coming up with a name for it, or even an example showing its utility. IMO there is none. I would rather just deprecate it.
I think grid is fine, although it isn't immediately intuitive to me. Technically speaking the value isn't a grid, it's the resolution of the grid; EnvelopeView has the same naming problem. I offer 'resolution' for consideration. A longer name is OK here since it's very unlikely someone would type out the parameter name when calling it - in other words, write
Since quantize will be deprecated, changing the parameter name doesn't do much. I think it should be left as-is. |
If we can imagine another way for a number to snap, the name probably should be |
These are in some sense variants of round, yes? And possibly useful for more than the normal quantising stuff? Maybe they should be named roundSomething?
snapToGrid is nice, but grids need not be regular. A real snapToGrid might be useful, but would require a Grid object capable of representing more than equal steps.
… On 4 Sep 2017, at 17:27, Julian Rohrhuber ***@***.***> wrote:
If we can imagine another way for a number to snap, the name probably should be snapToGrid. But otherwise I'm fine with snap. I hope nobody complains that a float snapped at him.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
I don't have a strong opinion on the name, but let's try to suggest concrete alternatives so the discussion doesn't derail. |
roundWithTolerance?
… On 4 Sep 2017, at 18:11, Brian Heim ***@***.***> wrote:
I don't have a strong opinion on the name, but let's try to suggest concrete alternatives so the discussion doesn't derail.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
we have On the other hand, I can imagine many other rounding to grid functions, but they can almost always better be written directly as a function. |
... sorry couldn't help myself |
(I also needed to hold myself back!) |
BTW, I never published this extension, but I use it all the time for FM modulator ratios:
Or a panner with an indent in the center: |
This seems like a no-brainer. Resolution it is. Nice one. |
roundToward isn't bad. roundWithTolerance would be perfect if it wasn't so long. gradualRound? There are two hard problems in Computer Science - naming things and cache invalidation. I'd be inclined if we went down that route to replicate the old quantize function as snap (with better named parameters and help). |
I won't hold my breath for graphics programs to start to |
my thought after seeing these suggestions is |
I like softRound. How about I create a copy of quantize called 'snap' which has the behavior that James described (snap to grid lines). And my function is softRound. And I'll add better documentation for 'snap' than the current quantize function. And quantize is deprecated. |
|
naming shouldn't be justified by current autocomplete functionality imho. i'd rather have code that makes sense and fits with naming conventions. |
Well there's currently a round and a roundUp (though not a roundDown - what's 'up' with that). So I guess it would fit into an already existing scheme if it was roundSoftly. |
yes, that's what I actually wanted to say. But I'm fine with most variants. |
So do we think we can achieve some kind of consensus on naming? I'd like to call it roundSoft, or roundSoftly, just because it would fit into the already existing naming convention for round. And use snap for 'snap' in the sense that James referenced it above. But I'm open to persuasion. And I'll start a new pull request to add roundDown... Seriously, why's that not in there? |
if you're looking for a convention, the other things in SC that have "soft" in their names are:
similarly, there are
|
So you had to bring facts to this discussion @brianlheim... Okay I'll rename the function to softRound, move the help up so that it's in the round section. And I'll create a copy of quantize called snap. And I'll update help appropriately. Is that cool? Edited because I wrote the wrong function name (duh). |
new function name softRound, not roundSoftly. Dammit. |
…Number. Help was also updated
…Number. Help was also updated
I still need to rewrite the tests. Everything else should be done. |
snap { arg resolution = 1.0, margin = 0.05, strength = 1.0; | ||
var round = round(this, resolution); | ||
var diff = round - this; | ||
if ( abs(diff) < margin ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are there suddenly spaces in the parens now? also FWIW i think our semiformal style is to not add a space after if
since it is also a method and not a true control structure as in many other languages:
if(abs(diff) < margin)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea how those spaces got there. Wasn't deliberate.
this.assertEquals(testF.(1,0.2,0.5), [ 0, 0.2, 0.2, 0.8, 0.8, 1, 1.2, 1.2, 1.8, 1.8, 2 ], "Snap test 5 failed."); | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing an EOL here. we should have a low-priority issue that ensures the IDE always inserts a \n after the last line in the editor, we have way too many faulty commits due to this.
I think this should address all the issues. The data now covers all the edge cases (plus a little extra for my OCD paranoia), including negative values. Also have tests for values that should never be entered. Apparently I don't find user tests soothing :-/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for all the tests, there are still issues though
|
||
} | ||
|
||
test_Snap { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be lowercase "snap"
var testF = {|vals, g, t, s| vals.collect({|num| num.softRound(g, t, s)})}; | ||
|
||
val = [-1.3, -1, -0.7, -0.5, -0.3, 0, 0.2, 0.5, 0.7, 1, 1.2, 1.8, 2]; | ||
this.assertEquals(testF.(val, 1,0,1), [ -1, -1, -1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2 ], "softRound test 1 failed."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep in mind that message
is posted even on a successful verbose run. The message should describe the test; these currently read like error messages
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and again, testing way too many values IMO, it's very hard to tell what's going on in this method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry used to unit test libraries that only show the message when it fails. I will modify. I see why people complain about the verbosity of the messages.
Each of the values is testing a very specific edge case where a bug (imo) could creep in. I could split these onto multiple lines, but I'm hesitant to reduce coverage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you!
|
||
// Testing weird edge cases that are probably not typical use cases for backwards compatability purposes. | ||
val = [-1, -0.5, 0, 1, 0.5, 1]; | ||
this.assertEquals(testF.(val,0,0,1), val, "softRound test 5 failed."); // resolution value of 0 leave the original array unchanged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please put these end of line comments on the line above instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this looks great, thank you so much for putting in the effort!
as an aside, this has definitely given me some good ideas for what to include in a unit testing guide. i'm sorry that you didn't have anything to go off of for this PR, that shouldn't happen in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, didn't notice this before, but you've still got a submodule link at external_libraries/link. Just remove that and we're good to merge.
That submodule keeps creeping back in. This should be fixed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
NB - I realized at the last moment that this PR hadn't actually deprecated quantize yet, so I went ahead and pushed that change to your branch, @cianoc |
See:
#2826
Added a new snap function which 'quantizes' values. It's really just the existing quantize function, but which does the expected thing.
As part of this PR I added help for snap, added some clarification to quantize and also added a unit test for this new function.
I welcome discussion on the best name for this function, as well as what to do about the old 'quantize' function.