Csound Csound-dev Csound-tekno Search About

[Csnd] Sending a different random number with successive triggers

Date2020-03-09 21:34
FromRyan Jeffares
Subject[Csnd] Sending a different random number with successive triggers
Hey all,

For a current project I am taking inputs from sensors on an Arduino in to Csound over serial. One of the means of interaction is pressing a tactile button connected to the Arduino which schedules an event for another instrument. The even is scheduled with at least one random k-rate variable, which is rounded to an integer. What I need to know is, is it possible to ensure that a different integer is sent each time? There's a lot of repetition currently because the range of random numbers isn't very big. Here's some very basic example code of what I mean:

<CsoundSynthesizer>
<CsOptions>
-n -d
</CsOptions>
<CsInstruments>
; Initialize the global variables.
ksmps = 32
nchnls = 2
0dbfs = 1

instr 1

iPort serialBegin "/dev/cu.usbmodem14301", 9600 ;connect to the arduino with baudrate = 9600
serialWrite iPort, 1 ;Triggering the Arduino (k-rate)
kbutton serialRead iPort
                       
ktrig trigger kbutton, 0, 0

if (ktrig = 1) then
    knote random 60, 63
    knote = int(knote)
    event "i", "plucky", 0, 0.5, knote
endif
     
endin

instr plucky

kfreq = p4
kfreq = cpsmidinn(kfreq)

asig pluck 1, kfreq, i(kfreq), 0, 1
outs asig, asig

endin

</CsInstruments>
<CsScore>
i1 0 z
</CsScore>

Thanks!
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2020-03-09 22:02
From"Jeanette C."
SubjectRe: [Csnd] Sending a different random number with successive triggers
Mar 9 2020, Ryan Jeffares has written:
...
> What I need to know is, is it possible to
> ensure that a different integer is sent each time? There's a lot of
> repetition currently because the range of random numbers isn't very big.
...
> if (ktrig = 1) then
>    knote random 60, 63
>    knote = int(knote)
>    event "i", "plucky", 0, 0.5, knote
> endif
...
If you can spare the cycles: I'd create two varialbes, one koldnote and
knote. In a loop generate random numbers until knote != koldnote. Set
koldnote to the current knote then and continue with your event. Would
that do?

Best wishes,

Jeanette

-- 
  * Website: http://juliencoder.de - for summer is a state of sound
  * Youtube: https://www.youtube.com/channel/UCMS4rfGrTwz8W7jhC1Jnv7g
  * SoundCloud: https://soundcloud.com/jeanette_c
  * Twitter: https://twitter.com/jeanette_c_s
  * Audiobombs: https://www.audiobombs.com/users/jeanette_c
  * GitHub: https://github.com/jeanette-c

There's no time to loose
And next week,
You might not see me here <3
(Britney Spears)

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2020-03-09 23:08
FromPete Goodeve
SubjectRe: [Csnd] Sending a different random number with successive triggers
AttachmentsNone  

Date2020-03-09 23:32
FromPete Goodeve
SubjectRe: [Csnd] Sending a different random number with successive triggers
AttachmentsNone  

Date2020-03-10 13:24
FromRyan Jeffares
SubjectRe: [Csnd] Sending a different random number with successive triggers
Thanks a million for the input guys. I have it working now using the following:

koldnote init 0

if(ktrig==1) then

    knote random 60, 63
    knote = int(knote)
   
    if(knote != koldnote) then              
        event "i", "plucky", 0, 1, knote
    else
        knote += 1
        event "i", "plucky", 0, 1, knote
    endif
       
    koldnote = knote
   
endif

Simply adding 1 to the note after the 'else' feels a little arbitrary but I can make it work for the purpose of the project. I realise that the int opcode returns the integer part of the k-rate decimal, so doing the above actually makes 63 (the top of the given random range) more likely. 64 would only be given in the extremely unlikely event that 63.00000 is generated randomly twice in a row haha. Thanks!

Kind regards,
Ryan

On Mon, 9 Mar 2020 at 23:33, Pete Goodeve <pete.goodeve@computer.org> wrote:
On Mon, Mar 09, 2020 at 04:08:37PM -0700, I wrote:
> > Mar 9 2020, Ryan Jeffares has written:
> > ...
> > >What I need to know is, is it possible to
> > >ensure that a different integer is sent each time? There's a lot of
> > >repetition currently because the range of random numbers isn't very big.

> > ...
> ...Or expand the random range and use a 'while' instead of 'if'...
>
On second thoughts, that wouldn't do what you want, as a random
number a mutiple of 4 would get a repeated note.

        -- Pete --

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2020-03-10 13:39
Fromjoachim heintz
SubjectRe: [Csnd] Sending a different random number with successive triggers
if you like you may try:

koldnote init 0
if(ktrig==1) then
     calculate:
     knote random 60, 63.9999
     knote = int(knote)
     if(knote != koldnote) then
         event "i", "plucky", 0, 1, knote
     else
         kgoto calculate
     endif
     koldnote = knote
endif

not tested, so perhaps it hangs ...

best -
	joachim


On 10/03/2020 14:24, Ryan Jeffares wrote:
> koldnote init 0
> 
> if(ktrig==1) then
> 
>      knote random 60, 63
>      knote = int(knote)
> 
>      if(knote != koldnote) then
>          event "i", "plucky", 0, 1, knote
>      else
>          knote += 1
>          event "i", "plucky", 0, 1, knote
>      endif
> 
>      koldnote = knote
> 
> endif

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2020-03-10 13:43
FromRyan Jeffares
SubjectRe: [Csnd] Sending a different random number with successive triggers
Hi Joachim,

That makes perfect sense, very clever. Thanks a mill

Kind regards,
Ryan

On Tue, 10 Mar 2020 at 13:39, joachim heintz <jh@joachimheintz.de> wrote:
if you like you may try:

koldnote init 0
if(ktrig==1) then
     calculate:
     knote random 60, 63.9999
     knote = int(knote)
     if(knote != koldnote) then
         event "i", "plucky", 0, 1, knote
     else
         kgoto calculate
     endif
     koldnote = knote
endif

not tested, so perhaps it hangs ...

best -
        joachim


On 10/03/2020 14:24, Ryan Jeffares wrote:
> koldnote init 0
>
> if(ktrig==1) then
>
>      knote random 60, 63
>      knote = int(knote)
>
>      if(knote != koldnote) then
>          event "i", "plucky", 0, 1, knote
>      else
>          knote += 1
>          event "i", "plucky", 0, 1, knote
>      endif
>
>      koldnote = knote
>
> endif

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here
Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here