Hi all, I am trying to write a program that remember the on/off state of a button. The question might not making sense, since I am using a physical button interacting with Csound on a microcontroller. What I am trying to do is press a button to start recording and press again to stop recording.Is there a way to achieve it?
What my thoughts on that now is :
gkRecord = kSwitch1
if changed(gkRecord) == 1 then
“trigger recording and keep the state of the button to on”
endif
Record and play
Hi Keyi, partially depends on whether the external button is latched (sends seperate On/Off messages) or operates as a momentary switch.
One option is to set up a seperate record instrument and turn it on/off with a control instr. You can use fout to record the audio to disk. You mentioned “keep the state of the switch to on” so I kind of assume it’s maybe a momentary switch.
This will do that for a momentary (non-latched) switch (press once it starts recording, press again it stops). The concept is fairly straightforward though so it should be relatively easy to modify for whatever type of switch you’re using.
<CsoundSynthesizer>
<CsOptions>
-odac -d
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1
instr Control
kRec init 0
kRec chnget "butt1"
kOnOff init 0
kTrig trigger kRec, .5, 0
if kTrig == 1 then
kOnOff += 1
endif
if changed(kOnOff) == 1 then
if kOnOff == 1 then
event "i", "Record", 0, 2^16
else
turnoff2 2, 1, 2
kOnOff = 0
endif
endif
printk2 kOnOff
endin
instr Record
aSig = oscil(.7, 400)
out(aSig)
fout "record.wav", 2, aSig
endin
</CsInstruments>
<CsScore>
i"Control" 0 z
e
</CsScore>
</CsoundSynthesizer>
@ST_Music Hi, thank you very much for the reply! I am using a non-latched switch. The code you provided is very helpful , and I successfully adapted it to remember the on.off state of the led. Really appreciate that.