If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Random number per Note (code)
12 posts
• Page 1 of 2 • 1, 2
Random number per Note (code)
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.
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 725 times
-
TrojakEW - Posts: 111
- Joined: Sat Dec 25, 2010 10:12 am
- Location: Slovakia
Re: Random number per Note (code)
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 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.
- deraudrl
- Posts: 239
- Joined: Thu Nov 28, 2019 9:12 pm
- Location: SoCal
Re: Random number per Note (code)
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!
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 739 times
-
Spogg - Posts: 3358
- Joined: Thu Nov 20, 2014 4:24 pm
- Location: Birmingham, England
Re: Random number per Note (code)
This looks good. Going to test it more and adjust it to my needs. Thank you Spogg and also thanks to Adam.
-
TrojakEW - Posts: 111
- Joined: Sat Dec 25, 2010 10:12 am
- Location: Slovakia
Re: Random number per Note (code)
Why so complicated and CPU intensive?
Just use Random.rand() in Ruby, or the green random number generator prim.
Just use Random.rand() in Ruby, or the green random number generator prim.
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Random number per Note (code)
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.
-
TrojakEW - Posts: 111
- Joined: Sat Dec 25, 2010 10:12 am
- Location: Slovakia
Re: Random number per Note (code)
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)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Random number per Note (code)
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.
-
Spogg - Posts: 3358
- Joined: Thu Nov 20, 2014 4:24 pm
- Location: Birmingham, England
Re: Random number per Note (code)
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?
For that you don't need to deal with polystream. Get the new value, feed a modulator, done. Or what is the purpose?What I want to do is to get different number each time new note is triggered in range from 0 to 1 for modulation
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Random number per Note (code)
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:
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.
- 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.
- adamszabo
- Posts: 667
- Joined: Sun Jul 11, 2010 7:21 am
12 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 53 guests