Csound Csound-dev Csound-tekno Search About

[Csnd] Problems With If-Then Statements

Date2012-09-20 20:51
Fromthe_nacho
Subject[Csnd] Problems With If-Then Statements
So I am writing this thing that I want to be a sampler-type thing. You press
a button and it records sound from the microphone and saves it to a file on
the computer, and then stops recording when you turn the button off. Then
when a key on the attached synthesizer is pressed, it should play back the
previously recorded sound. However, whenever a key is pressed on the
synthesizer (and, additionally, right when the file is run) all the if-then
statements get executed no matter if the condition is met or not. I tried
typecasting the k-rate variables to i-rate variables, but that doesn't help.

If all the variables are left as k-rate, you can record a sample by pressing
the button and then a key, although the sample will be replaced by a blank
file if you press any other keys.

I looked around on here and found that (for some reason) when k-rate
variables are involved, if statements are ALWAYS run, even if the condition
is not met. This seems silly to me, and if there is a way to somehow disable
that, that would be great. But I don't expect the solution will be that
easy. Here is the code I am using, perhaps I am making some horrible simple
mistake:






sr = 44100
ksmps = 64
nchnls = 1
0dbfs  = 1

initc7 1, 7,0.5

instr record ;Controls recording of samples
ginote notnum
krecord1 ctrl7 1,65,0,1
krecord2 ctrl7 1,66,0,1
krecord3 ctrl7 1,67,0,1
krecord4 ctrl7 1,68,0,1

irecord3 = i(krecord3)
irecord4 = i(krecord4)

gamic inch 1

printk2 krecord1

if (krecord1 == 1) then
	turnon "sample1"
elseif (krecord1 == 0) then
	turnoff2 "sample1",0,0
endif   

if (i(krecord2) == 1) then
	turnon "sample2"
elseif (i(krecord2) == 0) then
	turnoff2 "sample2",0,0
endif

if (irecord3 == 1) then
	turnon "sample3"
elseif (irecord3 == 0) then
	turnoff2 "sample3",0,0
endif

if (irecord4 == 1) then
	turnon "sample4"
elseif (irecord4 == 0) then
	turnoff2 "sample4",0,0
endif

endin

instr sample1 ;Records Sample 1
prints "Recording sample 1\\r"
fout "sample1.wav", 4, gamic
gkdur1 line 0,1,1
printk2 gkdur1
endin

instr sample2 ;Records sample 2
prints "Recording sample 2\\r"
fout "sample2.wav", 4, gamic
gkdur2 line 0,1,1
printk2 gkdur2
endin

instr sample3 ;Records sample 3
prints "Recording sample 3\\r"
fout "sample3.wav", 4, gamic
gkdur3 line 0,1,1
printk2 gkdur3
endin

instr sample4 ;Records sample 4
prints "Recording sample 4\\r"
fout "sample4.wav", 4, gamic
gkdur4 line 0,1,1
printk2 gkdur4
endin

instr effects ;Applies effects to the samples being played back
;stereo panning
;pitch
;playback speed

endin

instr playback ;Figures out which sample to play
print ginote
inote1 init 65
inote2 init 67
inote3 init 69
inote4 init 71
prints "Playback instrument in da house\\r"
if (ginote == inote1) then
	prints "Playing back sample 1\\r"
	aout diskin2 "sample1.wav",1
	out aout
elseif (ginote == inote2) then
	prints "Playing back sample 2\\r"
	aout diskin2 "sample2.wav",1
	out aout
elseif (ginote == inote3) then
	prints "Playing back sample 3\\r"
	aout diskin2 "sample3.wav",1
	out aout
elseif (ginote == inote4) then
	prints "Playing back sample 4\\r"
	aout diskin2 "sample4.wav",1
	out aout
else
	prints "Wrong key, doofus\\r"
endif
endin



f 0 3600
i"record" 0 36000
i"playback" 0 36000



Help would be much appreciated, thanks!



--
View this message in context: http://csound.1045644.n5.nabble.com/Problems-With-If-Then-Statements-tp5715978.html
Sent from the Csound - General mailing list archive at Nabble.com.

Date2012-09-22 18:24
FromAidan Collins
SubjectRe: [Csnd] Problems With If-Then Statements
Hi,

I was hoping someone with a little more experience would reply... but
I do recall from seeing messages on this list about if/then statements
that they do work a little weirdly in Csound sometimes.

I think the usual recommendation is that you use kgoto statements
instead of 'then'
That will make your code a little messier to read, but I think the
behavior with krate variables is more predictable.

hope that helps,

A

On Thu, Sep 20, 2012 at 3:51 PM, the_nacho  wrote:
> So I am writing this thing that I want to be a sampler-type thing. You press
> a button and it records sound from the microphone and saves it to a file on
> the computer, and then stops recording when you turn the button off. Then
> when a key on the attached synthesizer is pressed, it should play back the
> previously recorded sound. However, whenever a key is pressed on the
> synthesizer (and, additionally, right when the file is run) all the if-then
> statements get executed no matter if the condition is met or not. I tried
> typecasting the k-rate variables to i-rate variables, but that doesn't help.
>
> If all the variables are left as k-rate, you can record a sample by pressing
> the button and then a key, although the sample will be replaced by a blank
> file if you press any other keys.
>
> I looked around on here and found that (for some reason) when k-rate
> variables are involved, if statements are ALWAYS run, even if the condition
> is not met. This seems silly to me, and if there is a way to somehow disable
> that, that would be great. But I don't expect the solution will be that
> easy. Here is the code I am using, perhaps I am making some horrible simple
> mistake:
>
> 
> 
> 
> 
>
> sr = 44100
> ksmps = 64
> nchnls = 1
> 0dbfs  = 1
>
> initc7 1, 7,0.5
>
> instr record ;Controls recording of samples
> ginote notnum
> krecord1 ctrl7 1,65,0,1
> krecord2 ctrl7 1,66,0,1
> krecord3 ctrl7 1,67,0,1
> krecord4 ctrl7 1,68,0,1
>
> irecord3 = i(krecord3)
> irecord4 = i(krecord4)
>
> gamic inch 1
>
> printk2 krecord1
>
> if (krecord1 == 1) then
>         turnon "sample1"
> elseif (krecord1 == 0) then
>         turnoff2 "sample1",0,0
> endif
>
> if (i(krecord2) == 1) then
>         turnon "sample2"
> elseif (i(krecord2) == 0) then
>         turnoff2 "sample2",0,0
> endif
>
> if (irecord3 == 1) then
>         turnon "sample3"
> elseif (irecord3 == 0) then
>         turnoff2 "sample3",0,0
> endif
>
> if (irecord4 == 1) then
>         turnon "sample4"
> elseif (irecord4 == 0) then
>         turnoff2 "sample4",0,0
> endif
>
> endin
>
> instr sample1 ;Records Sample 1
> prints "Recording sample 1\\r"
> fout "sample1.wav", 4, gamic
> gkdur1 line 0,1,1
> printk2 gkdur1
> endin
>
> instr sample2 ;Records sample 2
> prints "Recording sample 2\\r"
> fout "sample2.wav", 4, gamic
> gkdur2 line 0,1,1
> printk2 gkdur2
> endin
>
> instr sample3 ;Records sample 3
> prints "Recording sample 3\\r"
> fout "sample3.wav", 4, gamic
> gkdur3 line 0,1,1
> printk2 gkdur3
> endin
>
> instr sample4 ;Records sample 4
> prints "Recording sample 4\\r"
> fout "sample4.wav", 4, gamic
> gkdur4 line 0,1,1
> printk2 gkdur4
> endin
>
> instr effects ;Applies effects to the samples being played back
> ;stereo panning
> ;pitch
> ;playback speed
>
> endin
>
> instr playback ;Figures out which sample to play
> print ginote
> inote1 init 65
> inote2 init 67
> inote3 init 69
> inote4 init 71
> prints "Playback instrument in da house\\r"
> if (ginote == inote1) then
>         prints "Playing back sample 1\\r"
>         aout diskin2 "sample1.wav",1
>         out aout
> elseif (ginote == inote2) then
>         prints "Playing back sample 2\\r"
>         aout diskin2 "sample2.wav",1
>         out aout
> elseif (ginote == inote3) then
>         prints "Playing back sample 3\\r"
>         aout diskin2 "sample3.wav",1
>         out aout
> elseif (ginote == inote4) then
>         prints "Playing back sample 4\\r"
>         aout diskin2 "sample4.wav",1
>         out aout
> else
>         prints "Wrong key, doofus\\r"
> endif
> endin
>
> 
> 
> f 0 3600
> i"record" 0 36000
> i"playback" 0 36000
> 
> 
>
> Help would be much appreciated, thanks!
>
>
>
> --
> View this message in context: http://csound.1045644.n5.nabble.com/Problems-With-If-Then-Statements-tp5715978.html
> Sent from the Csound - General mailing list archive at Nabble.com.
>
>
> Send bugs reports to the Sourceforge bug tracker
>             https://sourceforge.net/tracker/?group_id=81968&atid=564599
> Discussions of bugs and features can be posted here
> To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"
>

Date2012-09-22 18:38
FromJustin Smith
SubjectRe: [Csnd] Problems With If-Then Statements
from the code:

if (krecord1 == 1) then
        turnon "sample1"

turnon only activates when the instrument starts, and k variables
change throughout the duration of the instrument

The instrument running turnon starts once and lasts for the entire
duration of the performance, so what you want is not turnon but
schedkwhen.

turnoff is the same - it only runs when the instrument starts (if at
all). If you provide the instrument with a number, a negative argument
to schedkwhen can turn the instrument off.


On Sat, Sep 22, 2012 at 10:24 AM, Aidan Collins
 wrote:
> Hi,
>
> I was hoping someone with a little more experience would reply... but
> I do recall from seeing messages on this list about if/then statements
> that they do work a little weirdly in Csound sometimes.
>
> I think the usual recommendation is that you use kgoto statements
> instead of 'then'
> That will make your code a little messier to read, but I think the
> behavior with krate variables is more predictable.
>
> hope that helps,
>
> A
>
> On Thu, Sep 20, 2012 at 3:51 PM, the_nacho  wrote:
>> So I am writing this thing that I want to be a sampler-type thing. You press
>> a button and it records sound from the microphone and saves it to a file on
>> the computer, and then stops recording when you turn the button off. Then
>> when a key on the attached synthesizer is pressed, it should play back the
>> previously recorded sound. However, whenever a key is pressed on the
>> synthesizer (and, additionally, right when the file is run) all the if-then
>> statements get executed no matter if the condition is met or not. I tried
>> typecasting the k-rate variables to i-rate variables, but that doesn't help.
>>
>> If all the variables are left as k-rate, you can record a sample by pressing
>> the button and then a key, although the sample will be replaced by a blank
>> file if you press any other keys.
>>
>> I looked around on here and found that (for some reason) when k-rate
>> variables are involved, if statements are ALWAYS run, even if the condition
>> is not met. This seems silly to me, and if there is a way to somehow disable
>> that, that would be great. But I don't expect the solution will be that
>> easy. Here is the code I am using, perhaps I am making some horrible simple
>> mistake:
>>
>> 
>> 
>> 
>> 
>>
>> sr = 44100
>> ksmps = 64
>> nchnls = 1
>> 0dbfs  = 1
>>
>> initc7 1, 7,0.5
>>
>> instr record ;Controls recording of samples
>> ginote notnum
>> krecord1 ctrl7 1,65,0,1
>> krecord2 ctrl7 1,66,0,1
>> krecord3 ctrl7 1,67,0,1
>> krecord4 ctrl7 1,68,0,1
>>
>> irecord3 = i(krecord3)
>> irecord4 = i(krecord4)
>>
>> gamic inch 1
>>
>> printk2 krecord1
>>
>> if (krecord1 == 1) then
>>         turnon "sample1"
>> elseif (krecord1 == 0) then
>>         turnoff2 "sample1",0,0
>> endif
>>
>> if (i(krecord2) == 1) then
>>         turnon "sample2"
>> elseif (i(krecord2) == 0) then
>>         turnoff2 "sample2",0,0
>> endif
>>
>> if (irecord3 == 1) then
>>         turnon "sample3"
>> elseif (irecord3 == 0) then
>>         turnoff2 "sample3",0,0
>> endif
>>
>> if (irecord4 == 1) then
>>         turnon "sample4"
>> elseif (irecord4 == 0) then
>>         turnoff2 "sample4",0,0
>> endif
>>
>> endin
>>
>> instr sample1 ;Records Sample 1
>> prints "Recording sample 1\\r"
>> fout "sample1.wav", 4, gamic
>> gkdur1 line 0,1,1
>> printk2 gkdur1
>> endin
>>
>> instr sample2 ;Records sample 2
>> prints "Recording sample 2\\r"
>> fout "sample2.wav", 4, gamic
>> gkdur2 line 0,1,1
>> printk2 gkdur2
>> endin
>>
>> instr sample3 ;Records sample 3
>> prints "Recording sample 3\\r"
>> fout "sample3.wav", 4, gamic
>> gkdur3 line 0,1,1
>> printk2 gkdur3
>> endin
>>
>> instr sample4 ;Records sample 4
>> prints "Recording sample 4\\r"
>> fout "sample4.wav", 4, gamic
>> gkdur4 line 0,1,1
>> printk2 gkdur4
>> endin
>>
>> instr effects ;Applies effects to the samples being played back
>> ;stereo panning
>> ;pitch
>> ;playback speed
>>
>> endin
>>
>> instr playback ;Figures out which sample to play
>> print ginote
>> inote1 init 65
>> inote2 init 67
>> inote3 init 69
>> inote4 init 71
>> prints "Playback instrument in da house\\r"
>> if (ginote == inote1) then
>>         prints "Playing back sample 1\\r"
>>         aout diskin2 "sample1.wav",1
>>         out aout
>> elseif (ginote == inote2) then
>>         prints "Playing back sample 2\\r"
>>         aout diskin2 "sample2.wav",1
>>         out aout
>> elseif (ginote == inote3) then
>>         prints "Playing back sample 3\\r"
>>         aout diskin2 "sample3.wav",1
>>         out aout
>> elseif (ginote == inote4) then
>>         prints "Playing back sample 4\\r"
>>         aout diskin2 "sample4.wav",1
>>         out aout
>> else
>>         prints "Wrong key, doofus\\r"
>> endif
>> endin
>>
>> 
>> 
>> f 0 3600
>> i"record" 0 36000
>> i"playback" 0 36000
>> 
>> 
>>
>> Help would be much appreciated, thanks!
>>
>>
>>
>> --
>> View this message in context: http://csound.1045644.n5.nabble.com/Problems-With-If-Then-Statements-tp5715978.html
>> Sent from the Csound - General mailing list archive at Nabble.com.
>>
>>
>> Send bugs reports to the Sourceforge bug tracker
>>             https://sourceforge.net/tracker/?group_id=81968&atid=564599
>> Discussions of bugs and features can be posted here
>> To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"
>>
>
>
> Send bugs reports to the Sourceforge bug tracker
>             https://sourceforge.net/tracker/?group_id=81968&atid=564599
> Discussions of bugs and features can be posted here
> To unsubscribe, send email sympa@lists.bath.ac.uk with body "unsubscribe csound"
>

Date2012-09-26 23:18
Fromthe_nacho
Subject[Csnd] Re: Problems With If-Then Statements
Thank you so much! I tried kgoto and it didn't do what I wanted, but using
schedkwhen works perfectly!



--
View this message in context: http://csound.1045644.n5.nabble.com/Problems-With-If-Then-Statements-tp5715978p5716186.html
Sent from the Csound - General mailing list archive at Nabble.com.