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

Mallets - Add random knob function #6466

Merged
merged 9 commits into from
Sep 29, 2023
Merged

Conversation

zonkmachine
Copy link
Member

@zonkmachine zonkmachine commented Jul 20, 2022

Add a random knob to liven up the sound a bit. Originally introduced as a part of #2671.

The values of Position and Hardness in the first nine instruments and the values of Modulator and Crossfeed in Tubular Bells are randomly nudged by the value of Random.

@Rossmaxx

This comment was marked as off-topic.

Copy link
Contributor

@sakertooth sakertooth left a comment

Choose a reason for hiding this comment

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

I suggest using <random> provided by the C++ standard library instead of rand. It has far more control in terms of PRNG as you can specify the distribution explicitly as well as the random number engine, and it's just more idiomatic to C++ in general. See https://en.cppreference.com/w/cpp/numeric/random for details.

plugins/Stk/Mallets/Mallets.h Show resolved Hide resolved
plugins/Stk/Mallets/Mallets.cpp Show resolved Hide resolved
plugins/Stk/Mallets/Mallets.cpp Show resolved Hide resolved
@zonkmachine
Copy link
Member Author

zonkmachine commented Aug 31, 2022

I suggest using <random> provided by the C++ standard library instead of rand. It has far more control in terms of PRNG as you can specify the distribution explicitly as well as the random number engin

Thanks! I've been ruminating over random engines quite a lot. Looking at <random> was actually the inspiration to the random distribution hacks in the arpeggiator but I ended up going the fast_rand way with them and I was actually thinking of switching to fast_rand() here also. The reason is that <random>, tries to solve problems that we don't really need. Predictability, quality of randomness, etc. Speed is only one of the variables in there, but in our case it may be the only one we really should care about. In this PR we only run rand() on note on so that is not going to be a problem really, but the other qualities from more advanced random engines isn't going to help us either. The quality of good random distribution is eaten up by the low resolution here. %128 and %64 makes 7 and 6 bits resolution respectively. Maybe random distribution would matter a bit actually as Mallets seem to be quite sensitive to position. In theory that is. I wouldn't bother.

Out of curiosity I stitched the fast_rand() function from lmms into the test in the article below to see how it compares. mersenne and mersenne 64 are the ones form <random>. I omitted some tests from the article that didn't compile or otherwise acted up.
How competitive are C++ standard random number generators

fast_rand      0.957325 ns per rand
rand           13.1842 ns per rand
mersenne       6.91625 ns per rand
mersenne 64    5.71177 ns per rand
fast_rand      0.955309 ns per rand
rand           13.1856 ns per rand
mersenne       6.94733 ns per rand
mersenne 64    5.71553 ns per rand
fast_rand      0.956868 ns per rand
rand           13.1891 ns per rand
mersenne       6.94192 ns per rand
mersenne 64    5.71617 ns per rand

This makes fast_rand() some 13 times faster than rand(). In the case of this PR I can't see how we could benefit from a better random engine but I'm definitely keeping that library in the back of my mind. Here are some other random engines that look interesting: splitmix, xorshift and pcg. https://arvid.io/2018/07/02/better-cxx-prng/

@zonkmachine
Copy link
Member Author

  • Rebased on latest master.
  • rand() changed to fast_rand(). No advanced rand needed here.

@LMMS LMMS deleted a comment from LmmsBot Sep 28, 2023
@zonkmachine
Copy link
Member Author

zonkmachine commented Sep 28, 2023

As for the gui, that isn't my cup of tea. You could add some visual indication of how the random knob affects the synth. Like an arrow drawn from the random knob to the affected knobs (updated artwork). And/Or reorganize the knobs so the random knob is situated by the affected knobs. This is a later project.

Copy link
Contributor

@sakertooth sakertooth left a comment

Choose a reason for hiding this comment

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

Just some format updates. The overall code looks okay.

plugins/Stk/Mallets/Mallets.cpp Outdated Show resolved Hide resolved
plugins/Stk/Mallets/Mallets.cpp Outdated Show resolved Hide resolved
plugins/Stk/Mallets/Mallets.cpp Outdated Show resolved Hide resolved
plugins/Stk/Mallets/Mallets.cpp Outdated Show resolved Hide resolved
@zonkmachine zonkmachine merged commit 7dc0052 into LMMS:master Sep 29, 2023
9 checks passed
@zonkmachine zonkmachine deleted the malletrandom branch September 29, 2023 18:45
@zonkmachine zonkmachine restored the malletrandom branch September 29, 2023 18:45
@zonkmachine zonkmachine deleted the malletrandom branch September 29, 2023 18:47
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.

4 participants