Csound Csound-dev Csound-tekno Search About

[Csnd] if statement question

Date2021-11-28 17:35
FromEnrico F
Subject[Csnd] if statement question
Hey there, 
i've a problem with the if statement, can anybody give me an insight, please?
Thanks, cheers!
Enrico






-odac





instr 1

ktrig init 0

	if (ktrig != 0) then
	schedule 2, 0, 1
	endif

endin


instr 2
prints "s***t" ; why is instr 2 scheduled? ... the condition of the if statement is not fulfilled
endin






i 1 0 1000





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

Date2021-11-28 17:47
FromRory Walsh
SubjectRe: [Csnd] if statement question
schedule is an i-time opcode. It will be triggered at i-time even if it is inside a k-rate loop. Use one of the k-rate opcodes instead. 

On Sun, 28 Nov 2021 at 17:45, Enrico F <enricomem@gmail.com> wrote:
Hey there,
i've a problem with the if statement, can anybody give me an insight, please?
Thanks, cheers!
Enrico



<CsoundSynthesizer>

<CsOptions>
-odac
</CsOptions>


<CsInstruments>

instr 1

ktrig init 0

        if (ktrig != 0) then
        schedule 2, 0, 1
        endif

endin


instr 2
prints "s***t" ; why is instr 2 scheduled? ... the condition of the if statement is not fulfilled
endin

</CsInstruments>


<CsScore>

i 1 0 1000
</CsScore>


</CsoundSynthesizer>

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

Date2021-11-28 18:03
Frommskala@ANSUZ.SOOKE.BC.CA
SubjectRe: [Csnd] if statement question
On Sun, 28 Nov 2021, Enrico F wrote:
> 	if (ktrig != 0) then
> 	schedule 2, 0, 1
> 	endif

> instr 2
> prints "s***t" ; why is instr 2 scheduled? ... the condition of the if statement is not fulfilled
> endin

The "schedule" opcode is an i-time opcode and your k-time if statement
does not affect it.

In more detail:  Csound code is not actually just a single program, it is
three different programs whose statements can be mixed together in the
file.  Some statements run at i-time (once when the note starts); some at
k-time (once per control cycle); and some at a-time (which functionally
means once per audio sample, though the actual implementation may fake
this by processing multiple samples at once).  These three programs are
separated out and run separately at the appropriate times.  You can
confuse yourself by writing k-time statements in the middle of i-time
code, but that won't change their relative order of execution.

So your code is really saying:

; AT I-TIME

ktrig init 0
schedule 2, 0, 1

; AT K-TIME

if (ktrig != 0) then
  ; nothing here!
endif

I think if you put a k-time opcode like "event" inside the k-time if
statement instead of "schedule", you'll find that it does get affected by
the conditional.  But you'll need to add a bit of further logic (resetting
a k-time variable as in the manual's example for "event") to prevent it
from triggering a new event on every single k-cycle.

-- 
Matthew Skala
mskala@ansuz.sooke.bc.ca                 People before tribes.
https://ansuz.sooke.bc.ca/

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

Date2021-11-28 18:04
FromEnrico Fiocco
SubjectRe: [Csnd] if statement question
I see, thanks!
scheduled fixed it!!!
kind regards
E



> On 28 Nov 2021, at 19:03, mskala@ansuz.sooke.bc.ca  wrote:
> 
> On Sun, 28 Nov 2021, Enrico F wrote:
>> 	if (ktrig != 0) then
>> 	schedule 2, 0, 1
>> 	endif
> 
>> instr 2
>> prints "s***t" ; why is instr 2 scheduled? ... the condition of the if statement is not fulfilled
>> endin
> 
> The "schedule" opcode is an i-time opcode and your k-time if statement
> does not affect it.
> 
> In more detail:  Csound code is not actually just a single program, it is
> three different programs whose statements can be mixed together in the
> file.  Some statements run at i-time (once when the note starts); some at
> k-time (once per control cycle); and some at a-time (which functionally
> means once per audio sample, though the actual implementation may fake
> this by processing multiple samples at once).  These three programs are
> separated out and run separately at the appropriate times.  You can
> confuse yourself by writing k-time statements in the middle of i-time
> code, but that won't change their relative order of execution.
> 
> So your code is really saying:
> 
> ; AT I-TIME
> 
> ktrig init 0
> schedule 2, 0, 1
> 
> ; AT K-TIME
> 
> if (ktrig != 0) then
>  ; nothing here!
> endif
> 
> I think if you put a k-time opcode like "event" inside the k-time if
> statement instead of "schedule", you'll find that it does get affected by
> the conditional.  But you'll need to add a bit of further logic (resetting
> a k-time variable as in the manual's example for "event") to prevent it
> from triggering a new event on every single k-cycle.
> 
> -- 
> Matthew Skala
> mskala@ansuz.sooke.bc.ca                 People before tribes.
> https://ansuz.sooke.bc.ca/
> 
> 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

Date2021-11-28 18:05
FromEnrico Fiocco
SubjectRe: [Csnd] if statement question
typo, i meant schedulek

thanks again
E


> On 28 Nov 2021, at 19:04, Enrico Fiocco  wrote:
> 
> I see, thanks!
> scheduled fixed it!!!
> kind regards
> E
> 
> 
> 
>> On 28 Nov 2021, at 19:03, mskala@ansuz.sooke.bc.ca  wrote:
>> 
>> On Sun, 28 Nov 2021, Enrico F wrote:
>>> 	if (ktrig != 0) then
>>> 	schedule 2, 0, 1
>>> 	endif
>> 
>>> instr 2
>>> prints "s***t" ; why is instr 2 scheduled? ... the condition of the if statement is not fulfilled
>>> endin
>> 
>> The "schedule" opcode is an i-time opcode and your k-time if statement
>> does not affect it.
>> 
>> In more detail:  Csound code is not actually just a single program, it is
>> three different programs whose statements can be mixed together in the
>> file.  Some statements run at i-time (once when the note starts); some at
>> k-time (once per control cycle); and some at a-time (which functionally
>> means once per audio sample, though the actual implementation may fake
>> this by processing multiple samples at once).  These three programs are
>> separated out and run separately at the appropriate times.  You can
>> confuse yourself by writing k-time statements in the middle of i-time
>> code, but that won't change their relative order of execution.
>> 
>> So your code is really saying:
>> 
>> ; AT I-TIME
>> 
>> ktrig init 0
>> schedule 2, 0, 1
>> 
>> ; AT K-TIME
>> 
>> if (ktrig != 0) then
>> ; nothing here!
>> endif
>> 
>> I think if you put a k-time opcode like "event" inside the k-time if
>> statement instead of "schedule", you'll find that it does get affected by
>> the conditional.  But you'll need to add a bit of further logic (resetting
>> a k-time variable as in the manual's example for "event") to prevent it
>> from triggering a new event on every single k-cycle.
>> 
>> -- 
>> Matthew Skala
>> mskala@ansuz.sooke.bc.ca                 People before tribes.
>> https://ansuz.sooke.bc.ca/
>> 
>> 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

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