Page 1 of 2

Random number per Note (code)

PostPosted: Wed Nov 03, 2021 7:00 pm
by TrojakEW
I need some help here.

I'm trying to get random number on every note/hit from noise source in DSP code, but what I get is same number every time. It only change with additional note but again same number every time. What I want to do is to get different number each time new note is triggered in range from 0 to 1 for modulation. Kind of sample & hold for each note.

What I'm doing wrong here? (schematic saved in 3.0.8.1)

Thanks for any help.

Re: Random number per Note (code)

PostPosted: Wed Nov 03, 2021 7:19 pm
by deraudrl
Sounds like a seeding problem in the RNG(s).

I ran into something similar helping someone debug a puzzle generator: worked fine in Linux, but in Windows it kept generating the same small sets of puzzles and then repeating. Turns out Windows has a separate RNG for each thread, all using the same seed (based on the process ID, IIRC).

Since I have no idea whether/how threading is used in FS, I don't know if this is the same issue, but possibly there's a way to reseed the RNG periodically using some high-resolution value like sample count.

Re: Random number per Note (code)

PostPosted: Thu Nov 04, 2021 8:18 am
by Spogg
I put 2 solutions in your schematic.

The issue with your noise-based system is that when a note is played a channel is opened and the white noise generator always starts at the same value. So the solution for that is to have the noise generator constantly running in blue and converting it to poly. That will always give a different random value.

The second solution uses code to initialise a random value when a channel opens and hold it until the channel is closed (released). This was given to me by Adam some years ago when I first needed a random poly value for each note played. This is my go-to random poly system because it’s much lighter on CPU than the noise system. You can set the limit range by just editing the values in the ASM code, which is nice!

Re: Random number per Note (code)

PostPosted: Thu Nov 04, 2021 9:59 am
by TrojakEW
This looks good. Going to test it more and adjust it to my needs. Thank you Spogg and also thanks to Adam.

Re: Random number per Note (code)

PostPosted: Thu Nov 04, 2021 2:15 pm
by tulamide
Why so complicated and CPU intensive?
Just use Random.rand() in Ruby, or the green random number generator prim.

Re: Random number per Note (code)

PostPosted: Thu Nov 04, 2021 3:08 pm
by TrojakEW
It needs to be in poly if you want to have different value for each note. What if you hit 3 notes at once while using green or ruby? It will set same value for all three notes. I need different value for each note.

Re: Random number per Note (code)

PostPosted: Thu Nov 04, 2021 9:56 pm
by tulamide
TrojakEW wrote:It needs to be in poly if you want to have different value for each note. What if you hit 3 notes at once while using green or ruby? It will set same value for all three notes. I need different value for each note.

No, it won't set the same value. MIDI is a serial protocol. All midi notes come in one after the other. There is no such thing as "at once". You will give each note its own random value, wether you hit 1 or 88 keys "at once".

Re: Random number per Note (code)

PostPosted: Fri Nov 05, 2021 7:28 am
by Spogg
tulamide wrote:Why so complicated and CPU intensive?
Just use Random.rand() in Ruby, or the green random number generator prim.


I agree that new midi notes can be made to generate new random values in Ruby. However, that random value would be output in the green world. The complexity would then be assigning the random green values to different open channels for poly. The same applies for the Random prim.

The beauty of Adam’s code is it only runs once when a channel is first opened, effectively just creating a randomised variable and nothing more thereafter. Plus of course ASM uses SSE so is already in the poly world. I now recall Martin showing how this could be done in DSP, but ASM is even better.

Re: Random number per Note (code)

PostPosted: Fri Nov 05, 2021 8:46 pm
by tulamide
Spogg wrote:
tulamide wrote:Why so complicated and CPU intensive?
Just use Random.rand() in Ruby, or the green random number generator prim.


I agree that new midi notes can be made to generate new random values in Ruby. However, that random value would be output in the green world. The complexity would then be assigning the random green values to different open channels for poly. The same applies for the Random prim.

The beauty of Adam’s code is it only runs once when a channel is first opened, effectively just creating a randomised variable and nothing more thereafter. Plus of course ASM uses SSE so is already in the poly world. I now recall Martin showing how this could be done in DSP, but ASM is even better.

I understand. But maybe I misunderstood the use case?
What I want to do is to get different number each time new note is triggered in range from 0 to 1 for modulation
For that you don't need to deal with polystream. Get the new value, feed a modulator, done. Or what is the purpose?

Re: Random number per Note (code)

PostPosted: Fri Nov 05, 2021 11:21 pm
by adamszabo
If one wants to use random value in the stream its always best to use assembly or the DSP code. Yes, they can be done with Ruby and midi but it might be overkill, and it can act funny in some DAWs. The simplest and easiest random code is this in assembly:

Code: Select all
streamout phase1;
float rand1[1] = rand(0,1);
stage0;
movaps xmm0,rand1;
movaps phase1,xmm0;


If you even write stage0; there, then it will be even more efficient since it will only be active for the very first sample and not the rest of the stream, so basically almost no CPU usage.