Random number per Note (code)

DSP related issues, mathematics, processing and techniques
User avatar
TrojakEW
Posts: 111
Joined: Sat Dec 25, 2010 10:12 am
Location: Slovakia

Random number per Note (code)

Post 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.
Attachments
random_per_note.fsm
(1.5 KiB) Downloaded 864 times
deraudrl
Posts: 239
Joined: Thu Nov 28, 2019 9:12 pm
Location: SoCal

Re: Random number per Note (code)

Post 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.
I keep a pair of oven mitts next to my computer so I don't get a concussion from slapping my forehead while I'm reading the responses to my questions.
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: Random number per Note (code)

Post 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!
Attachments
random_per_note spogged.fsm
3.06
(10.23 KiB) Downloaded 887 times
User avatar
TrojakEW
Posts: 111
Joined: Sat Dec 25, 2010 10:12 am
Location: Slovakia

Re: Random number per Note (code)

Post by TrojakEW »

This looks good. Going to test it more and adjust it to my needs. Thank you Spogg and also thanks to Adam.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Random number per Note (code)

Post by tulamide »

Why so complicated and CPU intensive?
Just use Random.rand() in Ruby, or the green random number generator prim.
"There lies the dog buried" (German saying translated literally)
User avatar
TrojakEW
Posts: 111
Joined: Sat Dec 25, 2010 10:12 am
Location: Slovakia

Re: Random number per Note (code)

Post 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.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Random number per Note (code)

Post 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".
"There lies the dog buried" (German saying translated literally)
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: Random number per Note (code)

Post 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.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Random number per Note (code)

Post 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?
"There lies the dog buried" (German saying translated literally)
adamszabo
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: Random number per Note (code)

Post 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.
Post Reply