Csound Csound-dev Csound-tekno Search About

Re: [Csnd] Problems With If-Then Statements: Switch

Date2012-09-22 19:10
FromDavid Mooney
SubjectRe: [Csnd] Problems With If-Then Statements: Switch
This doesn't really address your problem, but when using multiple
conditionals as you show I've often thought it would be handy if
Csound had a switch structure, as in C++. Given the variable kval (or
ival), and possible numerical values 1-n for kval, a switch looks like
this:

switch(kval) {
  case 1:
    
  case 2:
    
  case 3:
    
.
.
.
  case n:
    
  break
}

 All of the  codes could be kgoto."break" at the end stops
the switch, which is generally used inside of a while loop. In C++,
this is done using a blank case (case ' ') or a newline escape
sequence (case '\n').

Just a thought.

--David Mooney

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"
>



-- 
Opaque Melodies
http://opaquemelodies.com

Date2012-09-22 20:58
From"Dr. Richard Boulanger"
SubjectRe: [Csnd] Problems With If-Then Statements: Switch
This would be nice.

Sent from my iPhone.

On Sep 22, 2012, at 2:10 PM, David Mooney  wrote:

> This doesn't really address your problem, but when using multiple
> conditionals as you show I've often thought it would be handy if
> Csound had a switch structure, as in C++. Given the variable kval (or
> ival), and possible numerical values 1-n for kval, a switch looks like
> this:
> 
> switch(kval) {
>  case 1:
>    
>  case 2:
>    
>  case 3:
>    
> .
> .
> .
>  case n:
>    
>  break
> }
> 
> All of the  codes could be kgoto."break" at the end stops
> the switch, which is generally used inside of a while loop. In C++,
> this is done using a blank case (case ' ') or a newline escape
> sequence (case '\n').
> 
> Just a thought.
> 
> --David Mooney
> 
> 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"
> 
> 
> 
> -- 
> Opaque Melodies
> http://opaquemelodies.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 23:18
FromJim Aikin
Subject[Csnd] Re: Problems With If-Then Statements: Switch
Possibly I'm off-base here. (I didn't see the original message, only the
response from David.) But it appears that this line:

irecord3 = i(krecord3) 

is going to run (during the init-pass) BEFORE krecord3 has been assigned a
value, because krecord3 has not been given an initial value by using the
init opcode.

That may have nothing to do with the problem, but I'm suspicious of it.



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

Date2012-09-23 00:19
FromRoger Kelly
SubjectRe: [Csnd] Problems With If-Then Statements: Switch
I am trying to do the exact same thing at this moment.  What is the best way to turn on and off this instrument so that it can record and playback using diskin.

Someone mentioned using schedwhen instead of turnon/off.  Is that the best solution?


instr 500
krec chnget "record_500"
kstop chnget "stop_500"
kplay chnget "play_500"
karm_1 chnget "arm1_500"
karm_2 chnget "arm2_500"
karm_3 chnget "arm3_500"
karm_4 chnget "arm4_500"
kmonitor_1 chnget "monitor1_500"
kmonitor_2 chnget "monitor2_500"
kmonitor_3 chnget "monitor3_500"
kmonitor_4 chnget "monitor4_500"
imonitor_1 = i(kmonitor_1)
iarm_1 = 0
iarm_2 = 0
iarm_3 = 0
iarm_4 = 0 
;trac1, trac2, trac3, trac4
a1_l zar 1
a1_r zar 2
a2_l zar 3
a2_r zar 4
a3_l zar 5
a3_r zar 6
a4_l zar 7
a4_r zar 8
; al,ar diskin2  "/mnt/sdcard/testaudio/track1.wav", 1.01
printks "Monitor 1 %f\n", .3, kmonitor_1
if (krec == 1.0 ) then
;all_l,all_r monitor
;fout "/mnt/sdcard/testaudio/master.wav", 14, all_l, all_r
; printks "kmon %f,  %f,  %f\n", .2, imonitor_1, karm_1, iarm_1
if (karm_1 == 1.0 && iarm_1 == 0.0) then
printks "Writig track 1", .2
fout "/mnt/sdcard/testaudio/track1.wav", 14, a1_l, a1_r
iarm_1 = 1
endif
if (karm_2 == 1.0 && iarm_2 == 0.0) then
printks "Writig track 2", .2
fout "/mnt/sdcard/testaudio/track2.wav", 14, a2_l, a2_r
iarm_2 = 1
endif
if (karm_3 == 1.0 && iarm_3 == 0.0) then
printks "Writig track 3", .2
fout "/mnt/sdcard/testaudio/track3.wav", 14, a3_l, a3_r
iarm_3 = 1
endif
if (karm_4 == 1.0  && iarm_4 == 0.0) then
printks "Writig track 4", .2
fout "/mnt/sdcard/testaudio/track4.wav", 14, a4_l, a4_r
iarm_4 = 1
endif
if (kmonitor_1 == 1.0) then
printks "MONITOR 1 ", .7
al,ar diskin2  "/mnt/sdcard/testaudio/track1.wav", 1.01
outs al, ar
endif
if (kmonitor_2 == 1.0) then
printks "MONITOR ", .2
al,ar diskin2  "/mnt/sdcard/testaudio/track2.wav", 1.01
outs al, ar
endif
if (kmonitor_3 == 1.0) then
printks "MONITOR ", .2
al,ar diskin2  "/mnt/sdcard/testaudio/track3.wav", 1.01
outs al, ar
endif
if (kmonitor_4 == 1.0) then
printks "MONITOR ", .2
al,ar diskin2  "/mnt/sdcard/testaudio/track4.wav", 1.01
outs al, ar
endif
endif
if (kstop == 1 ) then
krec = 0
iarm_1 = 0
;copy current file to the "listen file"          
chnset 0,"record_500"
;turnoff 
endif
endin

On Sat, Sep 22, 2012 at 1:10 PM, David Mooney <dmooney023@gmail.com> wrote:
This doesn't really address your problem, but when using multiple
conditionals as you show I've often thought it would be handy if
Csound had a switch structure, as in C++. Given the variable kval (or
ival), and possible numerical values 1-n for kval, a switch looks like
this:

switch(kval) {
  case 1:
    <do this>
  case 2:
    <do this>
  case 3:
    <do this>
.
.
.
  case n:
    <do this>
  break
}

 All of the <do this> codes could be kgoto."break" at the end stops
the switch, which is generally used inside of a while loop. In C++,
this is done using a blank case (case ' ') or a newline escape
sequence (case '\n').

Just a thought.

--David Mooney

On Thu, Sep 20, 2012 at 3:51 PM, the_nacho <rotoclone@gmail.com> 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:
>
> <CsoundSynthesizer>
> <CsOptions>
> </CsOptions>
> <CsInstruments>
>
> 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
>
> </CsInstruments>
> <CsScore>
> f 0 3600
> i"record" 0 36000
> i"playback" 0 36000
> </CsScore>
> </CsoundSynthesizer>
>
> 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"
>



--
Opaque Melodies
http://opaquemelodies.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-24 17:27
Fromjoachim heintz
SubjectRe: [Csnd] Problems With If-Then Statements: Switch
sorry for coming late in this thread and not reading everything 
carefully (so maybe i will repeat something which has been written 
already), but i think the question here is not using sched(k)when or 
not, but to understand i- and k-rate conditions, and find out the best 
(or one working) architecture of the instruments.

if i understood correctly, you have a control input at k-rate, and you 
want to trigger an instrument once a specific control input has been 
reached. and you want to turn off this instrument in case another value 
has benn reached.

this code ...

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

...  is not far from working, but has two problems:

1. the conditions 'if (krecord1 == 1)' and 'if (krecord1 == 0)' will 
usually not be true once (= for one k-cycle), but for several k-cycles. 
so, if you then trigger instrument "sample1", you will trigger it 
multiple times, this means: you will create multiple instances.
to ensure you trigger it just once, in the moment krecord1 has entered 
the value 1 resp. 0, you must add a line like this:
knewval changed krecord1

2. as you are not at the beginning of the instrument invocation (= at 
i-rate), but during its performance (= at k-rate), to trigger an 
instrument, you must use a k-rate opcode to turn the subinstrument on. 
the manual states that the "turnon" opcode just works with an i-rate 
input. you must use "event" (or schedkwhen) instead ("turnoff2" is ok - 
it works at k-rate).

so this should be a working code:

knewval changed krecord1
if (krecord1 == 1 && knewval == 1) then
         event "i", "sample1", 0, 99999
elseif (krecord1 == 0 && knewval == 1) then
         turnoff2 "sample1",0,0
endif

does this make sense and help? for further reading, i'd suggest jim 
aikin's book, or these articles online:
http://en.flossmanuals.net/csound/a-initialization-and-performance-pass
http://en.flossmanuals.net/csound/e-triggering-instrument-events

	joachim




Am 23.09.2012 01:19, schrieb Roger Kelly:
> I am trying to do the exact same thing at this moment.  What is the best
> way to turn on and off this instrument so that it can record and
> playback using diskin.
>
> Someone mentioned using schedwhen instead of turnon/off.  Is that the
> best solution?
>
>
> instr 500
> krec chnget "record_500"
> kstop chnget "stop_500"
> kplay chnget "play_500"
> karm_1 chnget "arm1_500"
> karm_2 chnget "arm2_500"
> karm_3 chnget "arm3_500"
> karm_4 chnget "arm4_500"
> kmonitor_1 chnget "monitor1_500"
> kmonitor_2 chnget "monitor2_500"
> kmonitor_3 chnget "monitor3_500"
> kmonitor_4 chnget "monitor4_500"
> imonitor_1 = i(kmonitor_1)
> iarm_1 = 0
> iarm_2 = 0
> iarm_3 = 0
> iarm_4 = 0
> ;trac1, trac2, trac3, trac4
> a1_l zar 1
> a1_r zar 2
> a2_l zar 3
> a2_r zar 4
> a3_l zar 5
> a3_r zar 6
> a4_l zar 7
> a4_r zar 8
> ;al,ar diskin2  "/mnt/sdcard/testaudio/track1.wav", 1.01
> printks "Monitor 1 %f\n", .3, kmonitor_1
> if (krec == 1.0 ) then
> ;all_l,all_r monitor
> ;fout "/mnt/sdcard/testaudio/master.wav", 14, all_l, all_r
> ;printks "kmon %f,  %f,  %f\n", .2, imonitor_1, karm_1, iarm_1
> if (karm_1 == 1.0 && iarm_1 == 0.0) then
> printks "Writig track 1", .2
> fout "/mnt/sdcard/testaudio/track1.wav", 14, a1_l, a1_r
> iarm_1 = 1
> endif
> if (karm_2 == 1.0 && iarm_2 == 0.0) then
> printks "Writig track 2", .2
> fout "/mnt/sdcard/testaudio/track2.wav", 14, a2_l, a2_r
> iarm_2 = 1
> endif
> if (karm_3 == 1.0 && iarm_3 == 0.0) then
> printks "Writig track 3", .2
> fout "/mnt/sdcard/testaudio/track3.wav", 14, a3_l, a3_r
> iarm_3 = 1
> endif
> if (karm_4 == 1.0  && iarm_4 == 0.0) then
> printks "Writig track 4", .2
> fout "/mnt/sdcard/testaudio/track4.wav", 14, a4_l, a4_r
> iarm_4 = 1
> endif
> if (kmonitor_1 == 1.0) then
> printks "MONITOR 1 ", .7
> al,ar diskin2  "/mnt/sdcard/testaudio/track1.wav", 1.01
> outs al, ar
> endif
> if (kmonitor_2 == 1.0) then
> printks "MONITOR ", .2
> al,ar diskin2  "/mnt/sdcard/testaudio/track2.wav", 1.01
> outs al, ar
> endif
> if (kmonitor_3 == 1.0) then
> printks "MONITOR ", .2
> al,ar diskin2  "/mnt/sdcard/testaudio/track3.wav", 1.01
> outs al, ar
> endif
> if (kmonitor_4 == 1.0) then
> printks "MONITOR ", .2
> al,ar diskin2  "/mnt/sdcard/testaudio/track4.wav", 1.01
> outs al, ar
> endif
> endif
> if (kstop == 1 ) then
> krec = 0
> iarm_1 = 0
> ;copy current file to the "listen file"
> chnset 0,"record_500"
> ;turnoff
> endif
> endin
>
> On Sat, Sep 22, 2012 at 1:10 PM, David Mooney  > wrote:
>
>     This doesn't really address your problem, but when using multiple
>     conditionals as you show I've often thought it would be handy if
>     Csound had a switch structure, as in C++. Given the variable kval (or
>     ival), and possible numerical values 1-n for kval, a switch looks like
>     this:
>
>     switch(kval) {
>        case 1:
>          
>        case 2:
>          
>        case 3:
>          
>     .
>     .
>     .
>        case n:
>          
>        break
>     }
>
>       All of the  codes could be kgoto."break" at the end stops
>     the switch, which is generally used inside of a while loop. In C++,
>     this is done using a blank case (case ' ') or a newline escape
>     sequence (case '\n').
>
>     Just a thought.
>
>     --David Mooney
>
>     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 emailsympa@lists.bath.ac.uk  with body
>     "unsubscribe csound"
>     >
>
>
>
>     --
>     Opaque Melodies
>     http://opaquemelodies.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"
>
>