wrote the following code to test the behavior of “buttX”
it seems like i can set the default value to that ain’t 0.0 nor 1.0
but once i touch the button it goes back to off=0.0 on=1.0
is there any way to set on and off to arbitural value?
In that example the chnset line is used to send the button’s k-rate values to a global channel. It essentially serves the same purpose as your gkb01 so isn’t really necessary unless you want to learn to use channels.
What do you want the button to do - what behavior? Yes, you can set default values for on/off or even multiple presses (ex. initialize at 0, press once = .5, press again = .9, press 3rd time = back to 0).
Here’s an example that allows to button to be used as a toggle switch instead of the default (momentary):
instr 1
kamp init 0
kamp chnget “butt1”
kOnOff init 0
ktrig trigger kamp, .5, 0
if ktrig == 1 then
kOnOff += 1
endif
if kOnOff == 1 then
kamp =.3
else
kamp = 0
kOnOff = 0
endif
printk2 kOnOff
asig lfo port(kamp, .005), 400
out asig
endin
In this case the init value is 0 (no sound), when pressed once the value is .3, press again and the value is back to 0. It helps to use the port opcode when applying the value to avoid clicking.
ktrig trigger gkb01,0.99,0
if ktrig == 1 then
kMelo += 1
endif
if kMelo == 7 then
kMelo = 0
endif
if kMelo == 0 then
kfrq = 110.0
endif
if kMelo == 1 then
kfrq = 133.0
endif
if kMelo == 2 then
kfrq = 104.0
endif
if kMelo == 3 then
kfrq = 157.0
endif
if kMelo == 4 then
kfrq = 222.0
endif
if kMelo == 5 then
kfrq = 210.0
endif
if kMelo == 6 then
kfrq = 191.0
endif
kenv1 oscil 0.7,0.1+9.9*port(kfrq/230.0,.0
05),giftc,0
asigc oscil 0.3+kenv1,port(kfrq,.005),giftc,0
outs asigc,asigc
endin
Glad to see that it works ok for you. A few small ideas. For one you don’t need the chnset line, in this case it serves no purpose.
Perhaps you could consider using an array for the frequencies, then you don’t need as many if statements. Also, you can see in the below code that the array length can be used to cycle kMelo back to 0, so you can add or delete as many frequencies as you like without altering the rest of the code.
It also makes the code a little easier to read and possibly more efficient. Just a thought, of course it depends on what you’re more comfortable with.