Csound Csound-dev Csound-tekno Search About

[Csnd] It took too much time until I understood how printf works!

Date2019-11-02 22:55
FromBernard Geyer
Subject[Csnd] It took too much time until I understood how printf works!
Some stupid example:
instr 1 ; generate some overtones
  kTrig metro 8
  if(kTrig == 1) then
    kRnd rand 0.5
    kVal = round((kRnd+0.5)*(8-1)) + 1
    printf "%f\n", kTrig, kVal
    schedkwhen kTrig, 0, 0, 2, 0, 0.25, 261 * kVal
  endif
endin

instr 2 ; play each note
  aEnv linseg 0, 0.01, 1, p3 - 0.11, 1, 0.1, 0
  aOut1 oscils 0.2, p4, 2
  out aOut1*aEnv, aOut1*aEnv
endin

I was surprised that printf prints only the first time, until I was reading
carefully the documentation again, who says:
ktrig -- if greater than zero and different from the value on the previous
control cycle the opcode performs the requested printing. Initially this
previous value is taken as zero. 

So, when I took the printf line outside the if(kTrig == 1) condition, it
worked:
instr 1
  kTrig metro 8
  if(kTrig == 1) then
    kRnd rand 0.5
    kVal = round((kRnd+0.5)*(8-1)) + 1
    schedkwhen kTrig, 0, 0, 2, 0, 0.25, 261 * kVal
  endif
  printf "%f\n", kTrig, kVal
endin

What I find strange is that printf behaves differently than schedkwhen in
this situation.
Should I also put schedkwhen out of the condition to make the program more
clear ?




--
Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html

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

Date2019-11-03 08:28
Fromjoachim heintz
SubjectRe: [Csnd] It took too much time until I understood how printf works!
hi bernard -

what you describe is consistent for me:

- in the if-condition the kTrig is always 1, so printf will only print 
the first time.
- if you have the printf external to the if-condition, you will have 
zeros in all k-cycles between the ones in which kTrig is 1.

the point is that the if-condition is only performed when the condition 
is true; so "different from the value on the previous control cycle" is 
in this case "different from the value on the previous control cycle in 
which this block of code is being performed".

in schedkwhen the behaviour is different, as the event is triggered 
whenever is not zero.  as the manual states:
" ktrigger -- triggers a new score event. If ktrigger = 0, no new event 
is triggered. "

to your question: yes i think the code would be clearer either having 
the if-condition with event instead of schedkwhen:

instr 1
   kTrigPrint init 0
   kTrig metro 8
   if(kTrig == 1) then
     kTrigPrint += 1
     kRnd rand 0.5
     kVal = round((kRnd+0.5)*(8-1)) + 1
     printf "%f\n", kTrigPrint, kVal
     event "i", 2, 0, 0.25, 261 * kVal
   endif
endin

or you use schedkwhen as abbreviation for an if-condition:

instr 1
   kTrig metro 8
   kRnd rand 0.5
   kVal = round((kRnd+0.5)*(8-1)) + 1
   printf "%f\n", kTrig, kVal
   schedkwhen kTrig, 0, 0, 2, 0, 0.25, 261 * kVal
endin

i would very much prefer the first solution, because the code tells much 
better what is happening, and kRnd as well as kVal are only calculated 
when needed.

best -
	joachim


On 02/11/19 23:55, Bernard Geyer wrote:
> Some stupid example:
> instr 1 ; generate some overtones
>   kTrig metro 8
>   if(kTrig == 1) then
>     kRnd rand 0.5
>     kVal = round((kRnd+0.5)*(8-1)) + 1
>     printf "%f\n", kTrig, kVal
>     schedkwhen kTrig, 0, 0, 2, 0, 0.25, 261 * kVal
>   endif
> endin
>
> instr 2 ; play each note
>   aEnv linseg 0, 0.01, 1, p3 - 0.11, 1, 0.1, 0
>   aOut1 oscils 0.2, p4, 2
>   out aOut1*aEnv, aOut1*aEnv
> endin
>
> I was surprised that printf prints only the first time, until I was reading
> carefully the documentation again, who says:
> ktrig -- if greater than zero and different from the value on the previous
> control cycle the opcode performs the requested printing. Initially this
> previous value is taken as zero.
> 
So, when I took the printf line outside the if(kTrig == 1) condition, it
> worked:
> instr 1
>   kTrig metro 8
>   if(kTrig == 1) then
>     kRnd rand 0.5
>     kVal = round((kRnd+0.5)*(8-1)) + 1
>     schedkwhen kTrig, 0, 0, 2, 0, 0.25, 261 * kVal
>   endif
>   printf "%f\n", kTrig, kVal
> endin
> 
What I find strange is that printf behaves differently than schedkwhen in
> this situation.
> Should I also put schedkwhen out of the condition to make the program more
> clear ?
>
>
>
>
> --
> Sent from: http://csound.1045644.n5.nabble.com/Csound-General-f1093014.html
>
> 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