Stopping a sound without a click

Hi. I’m trying to interrupt a sound without having the final click, not with a midi command but from the keyboard. Thanks for the help!
n

instr 1
krel init 1
kenv linen 1, 0.01, p3 ,0.5
kKey init 0
kKey, kDown sensekey
if (kDown == 1 && kKey == 49) then
krel = 0
endif
aSound poscil 0.5, 400
outall aSoundkenvkrel
endin

You should use one of the “r” opcodes, like linsegr
Otherwise, try with xtratim

thank you, but they only work with midi events (if I understand correctly)
n

They should work in any case when they recognize a turnoff event (not necessarily coming from midi)
Btw you could try the same approach of the example in the sensekey manual page: sensekey

So you could have an always active instrument that detects keypresses and triggers a second instrument

no they always work, and extend the duration of a event. you can try
with this code:
instr Try
aSound = poscil:a(.2,400)
aOut = linenr:a(aSound,.1,3,.01)
outall(aOut)
endin
schedule(“Try”,0,1)

you will hear a tone of one second plus three seconds fade-out.

but i think the question in your code is about the architecture. if i
get it right, you want to be able to toggle a tone via the computer
keyboard. so linenr will not help, as it only adds one release segment
at the end of the whole note, but not during this event.

i think you have two options:

  1. you soften the transitions between the rapid changes of either 0 or 1
    by an interpolating opcode. for instance with lag:
    instr 1
    krel init 1
    kenv linen 1, 0.01, p3 ,0.5
    kKey, kDown sensekey
    if (kDown == 1 && kKey == 49) then
    krel = 0
    endif
    kinterp lag krel,0.1
    aSound poscil 0.5, 400
    outall aSoundkenvkinterp
    endin
    schedule(1,0,99)

you can also implement a constant toggle with this approach:
instr 1
ktoggle init 1
kenv linen 1, 0.01, p3 ,0.5
kKey, kDown sensekey
if (kDown == 1 && kKey == 49) then
ktoggle = 1-ktoggle
endif
kinterp lag ktoggle,0.1
aSound poscil 0.5, 400
outall aSoundkenvkinterp
endin
schedule(1,0,99)

  1. you separate both jobs in two instruments: one instrument is for
    turning on and off, the other one is producing the sound. in this
    approach you can then use linenr:
    instr Control
    ktoggle init 1
    kKey, kDown sensekey
    if (kDown == 1 && kKey == 49) then
    if (ktoggle == 1) then
    turnoff2 “Tone”,0,1
    else
    schedulek(“Tone”,0,99)
    endif
    ktoggle = 1-ktoggle
    endif
    endin
    schedule(“Control”,0,99)

instr Tone
kenv linenr 1, 0.01, .1 ,0.01
aSound poscil 0.5, 400
outall aSound*kenv
endin
schedule(“Tone”,0,99)

i think for your case solution 1 is better (more simple). but i think
it is good to know that we can always separate the jobs between two
instruments. in many cases this solves problems and the code reflects
more about what happens as interaction.

best -
joachim

dear Joachim,

Thank you very much. Very clear, I have already developed further. It works perfectly.
Thank you for your work with Csound!
n