# How to add release time when stopping a sound

**URL:** <https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199>\
**Category:** General\
**Created:** [April 29, 2023, 11:46am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199 "2023-04-29T11:46:00Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![KANIYO](https://forum.csound.com/user_avatar/forum.csound.com/kaniyo/32/386_2.png) [@KANIYO](https://forum.csound.com/u/KANIYO)\
**Post date:** [April 29, 2023, 11:46am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/1 "2023-04-29T11:46:00Z")

</div>

I am trying to make a simple keyboard that plays SoundFont sounds on unity.  
Since Csound is new to me, I created the following as a test.

```csound
instr PlayOrStop
    kStart chnget "start"
    if changed(kStart) == 1 then
        event "i", "SoundFont", 0, -1, gkNote, gkVel
    endif
    
    kStop chnget "stop"
    if changed(kStop) == 1 then
        event "i", "SoundFont", 0, 0, gkNote, 0
    endif
endin

instr SoundFont
    mididefault 60, p3
    midinoteonkey	p4, p5
    inum	init	p4
    ivel	init	p5
    ivel	init	ivel/127	
    kamp	linsegr	1, 1, 1, 0.5, 0 ;envelope
    kamp	= kamp/3000	
    kfreq	init	1
    a1,a2	sfplay3	ivel, inum, kamp*ivel, kfreq, 0 
    outs	a1, a2
endin

```

When you press PlaySound, the sound is played, StopSound will stop the sound.

The problem is that when I press StopSound, the sound immediately disappears and the release time that I should have set in SoundFont does not work. (Of course, this is because the vericity is zero.)

For example, what event should I issue to make the sound release over 0.5 seconds?  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [April 30, 2023, 12:18am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/2 "2023-04-30T00:18:35Z")

</div>

Hi @KANIYO and welcome to the forum.  
Try having a look at one of the ‘r’ opcodes like:  
[https://csound.com/docs/manual/linenr.html](https://csound.com/docs/manual/linenr.html)  
or  
[https://csound.com/docs/manual/linsegr.html](https://csound.com/docs/manual/linsegr.html)

---

<div class="post-metadata">

**Author:** ![KANIYO](https://forum.csound.com/user_avatar/forum.csound.com/kaniyo/32/386_2.png) [@KANIYO](https://forum.csound.com/u/KANIYO)\
**Post date:** [April 30, 2023, 1:26pm UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/3 "2023-04-30T13:26:08Z")

</div>

Thanks for the quick answer!!!

I read the page you linked to. I am trying to avoid issuing midi events as I am planning to eventually import them into Unity (iOS). This is because I do not know how to loop back Midi events on Unity (iOS).

What I want to build is a simple piano. It should play a sound when a finger touches the keys, and the sound disappears when the finger leaves the keys.

I have already included `linsegr`. I have tried changing some values but it doesn’t seem to work…

```csound
kamp linsegr 1, 1, 1, 0.5, 0 ;envelope

```

I would be very happy if you could give me some more details.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [April 30, 2023, 4:00pm UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/4 "2023-04-30T16:00:44Z")

</div>

Hehe sorry it was late and I didn’t notice you were already using _linsegr_.  
I think the issue lies in the _event_ opcode, that doesn’t check for release segments.  
Try this (using _turnoff2_):

```csound
instr PlayOrStop

    kStart, kval cabbageGetValue "start"
    insno nstrnum "SoundFont"
     
    if changed(kStart) == 1 then
        event "i", "SoundFont", 0, -1, gkNote, gkVel
    endif
    
    kStop chnget "stop"
    if changed(kStop) == 1 then
       
        turnoff2 k(insno), 0, 1
        ;event "i", "SoundFont", 0, 0, gkNote, 0
    endif
endin

```

---

<div class="post-metadata">

**Author:** ![KANIYO](https://forum.csound.com/user_avatar/forum.csound.com/kaniyo/32/386_2.png) [@KANIYO](https://forum.csound.com/u/KANIYO)\
**Post date:** [May 1, 2023, 7:56am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/5 "2023-05-01T07:56:05Z")

</div>

Thanks for your cooperation. But I just can’t get it to work. The sound didn’t stop.  
Here is my code. What am I doing wrong …

```csound
<Cabbage>
    form caption("TITLE") size(200,200), guiMode("queue") pluginId("def1")
    button bounds(30, 20, 80, 40) channel("start") text("PlaySound")
    button bounds(30, 100, 80, 40) channel("stop") text("StopSound")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

;load a soundfont
prints "------------LOAD SF--------------\n"
strset 1, "88KeysMultiTonesPiano.sf2"
isf sfload 1
sfplist isf
sfpassign 0, isf

gkNote init 60
gkVel init 100

instr PlayOrStop

    kStart, kval cabbageGetValue "start"
    insno nstrnum "SoundFont"
     
    if changed(kStart) == 1 then
        event "i", "SoundFont", 0, -1, gkNote, gkVel
    endif
    
    kStop chnget "stop"
    if changed(kStop) == 1 then
        turnoff2 k(insno), 0, 1
        ;event "i", "SoundFont", 0, 0, gkNote, 0
    endif
endin

instr SoundFont
    prints "HELLO\n" 
    mididefault	60, p3
    midinoteonkey	p4, p5
    inum	init	p4
    ivel	init	p5
    ivel	init	ivel/127 ;make velocity dependen
    kamp	linsegr	1, 1, 1, 17, 0 ;envelope
    kamp	= kamp/3000 ;scale amplitude
    kfreq	init	1 ;do not change freq from sf
    a1,a2	sfplay3	ivel, inum, kamp*ivel, kfreq, 0	;preset index = 0
    outs	a1, a2
endin

	
</CsInstruments>
<CsScore>
f0 z
i "PlayOrStop" 0 z
</CsScore>
</CsoundSynthesizer>

```

---

<div class="post-metadata">

**Author:** ![tarmo](https://forum.csound.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@tarmo](https://forum.csound.com/u/tarmo)\
**Post date:** [May 1, 2023, 9:09am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/6 "2023-05-01T09:09:04Z")

</div>

Hi !

Try to add  
printk2 kStop  
to see how it actually behaves.

Tarmo

E, 1. mai 2023 11:06 KANIYO via The Csound Community \<[noreply@forum.csound.com](mailto:noreply@forum.csound.com)\> kirjutas:

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [May 1, 2023, 10:27am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/7 "2023-05-01T10:27:52Z")

</div>

Inside the SoundFont instrument, the time it takes for the sound to fade out after the release is set to 17 seconds!  
Try this:

```
;kres linsegr starting value, duration of first segment, value after first segment, duration of release segment, ending value
kamp	linsegr	1, 1, 1, 0.3, 0 ;envelope

```

So here the sound starts with an amplitude of 1, after 1 second amplitude is still 1, and when released, after 0.3 seconds, amplitude is 0.

Also I noticed I made you use _cabbageGetValue_, which won’t work on Unity. Sorry for that, my brain is on holiday!! 🤦‍♂️  
Use chnget as you were doing already:

```
instr PlayOrStop

    kStart chnget "start"
    insno nstrnum "SoundFont"
 
    if changed(kStart) == 1 then
        event "i", "SoundFont", 0, -1, gkNote, gkVel
    endif

    kStop chnget "stop"
    if changed(kStop) == 1 then
        turnoff2 k(insno), 0, 1
    endif
endin

```

Another little note is about loading sf files in Unity: probably Csound won’t be able to find the file at runtime, import the _Environment Vars_ sample from the CsoundUnity section of the Unity package manager and have a look at the _SFDIR_ scene. You will have to place your sf file under a _Resources_ folder, and rename it to _\< name of the file \>.bytes_

---

<div class="post-metadata">

**Author:** ![KANIYO](https://forum.csound.com/user_avatar/forum.csound.com/kaniyo/32/386_2.png) [@KANIYO](https://forum.csound.com/u/KANIYO)\
**Post date:** [May 1, 2023, 10:39am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/8 "2023-05-01T10:39:33Z")

</div>

I was able to do it! 17 seconds was clearly a mistake!  
Now that the test is complete, I’m going to let this thread be complete for now, although the integration into Unity may cause problems again.

Thank you so much!

---

<div class="post-metadata">

**Author:** ![KANIYO](https://forum.csound.com/user_avatar/forum.csound.com/kaniyo/32/386_2.png) [@KANIYO](https://forum.csound.com/u/KANIYO)\
**Post date:** [May 8, 2023, 7:59am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/9 "2023-05-08T07:59:25Z")

</div>

If I use the turnoff2 method, all sounds will be lost when multiple sounds are played on the same inst.  
Is there any opcode or method to release only certain notes?  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![stevenyi](https://forum.csound.com/user_avatar/forum.csound.com/stevenyi/32/4_2.png) [@stevenyi](https://forum.csound.com/u/stevenyi)\
**Post date:** [May 8, 2023, 7:50pm UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/10 "2023-05-08T19:50:42Z")

</div>

If you use fractional instrument numbers I think you can turn off just those matching the fraction.

---

<div class="post-metadata">

**Author:** ![KANIYO](https://forum.csound.com/user_avatar/forum.csound.com/kaniyo/32/386_2.png) [@KANIYO](https://forum.csound.com/u/KANIYO)\
**Post date:** [May 9, 2023, 4:07am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/11 "2023-05-09T04:07:31Z")

</div>

Thank you for your help!!  
I am trying to build a keyboard with 88 keys. The sound source is SoundFont.  
Sorry for my lack of understanding. Does that mean that I have to define 88 insts?

---

<div class="post-metadata">

**Author:** ![g\_b](https://forum.csound.com/user_avatar/forum.csound.com/g_b/32/208_2.png) [@g\_b](https://forum.csound.com/u/g_b)\
**Post date:** [May 9, 2023, 7:54am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/12 "2023-05-09T07:54:18Z")

</div>

Have a look here:

> **[The Csound FLOSS Manual](https://flossmanual.csound.com/midi/triggering-instrument-instances#using-multiple-triggering)**
>
> Csound FLOSS

Section

> Using Multiple Triggering

---

<div class="post-metadata">

**Author:** ![tarmo](https://forum.csound.com/letter_avatar_proxy/v4/letter/t/c37758/32.png) [@tarmo](https://forum.csound.com/u/tarmo)\
**Post date:** [May 9, 2023, 8:28am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/13 "2023-05-09T08:28:28Z")

</div>

Hi!

Here is a snippet from one instrument that I found:

---

<div class="post-metadata">

**Author:** ![KANIYO](https://forum.csound.com/user_avatar/forum.csound.com/kaniyo/32/386_2.png) [@KANIYO](https://forum.csound.com/u/KANIYO)\
**Post date:** [May 9, 2023, 10:08am UTC](https://forum.csound.com/t/how-to-add-release-time-when-stopping-a-sound/1199/14 "2023-05-09T10:08:48Z")

</div>

I was able to do this by using fractional instrument. Thank you very much.  
I don’t know if anyone will need this, but I will leave the code.

```csound
<Cabbage>
    form caption("TITLE") size(300,200), guiMode("queue") pluginId("def1")
    button bounds(30, 20, 80, 40) channel("start1") text("Start1")
    button bounds(150, 20, 80, 40) channel("start2") text("Start2")
    button bounds(30, 100, 80, 40) channel("stop1") text("Stop1")
    button bounds(150, 100, 80, 40) channel("stop2") text("Stop2")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

;load a soundfont
prints "------------LOAD SF--------------\n"
isf sfload "soundfont.sf2"
sfplist isf
sfpassign 0, isf

instr PlayOrStop    
    kpreset init 1
    kNote1 init 60
    kNote2 init 64
    kVel init 100
    krelease init 0.2
 
    kStart1 chnget "start1"
    if changed(kStart1) == 1 then
        instrnum1 = 2 + i(kpreset)/100 + i(kNote1)/100000
        event "i", instrnum1, 0, -1, kNote1, kVel, krelease, kpreset
    endif

    kStart2 chnget "start2"
    if changed(kStart2) == 1 then
        instrnum2 = 2 + i(kpreset)/100 + i(kNote2)/100000 
        event "i", instrnum2, 0, -1, kNote2, kVel, krelease, kpreset
    endif

    kStop1 chnget "stop1"
    if changed(kStop1) == 1 then
        instrnum1 = 2 + i(kpreset)/100 + i(kNote1)/100000
        turnoff2 k(instrnum1), 4, 1
    endif

    kStop2 chnget "stop2"
    if changed(kStop2) == 1 then
        instrnum2 = 2 + i(kpreset)/100 + i(kNote2)/100000 
        turnoff2 k(instrnum2), 4, 1
    endif

endin

instr 2
    inum init	p4
    ivel init	p5
    irelease init p6
    ipreset init p7
    ivel init	ivel/127 ;make velocity dependen
    kamp linsegr	1, 1, 1, irelease, 0 ;envelope
    kamp = kamp/3000 ;scale amplitude
    kfreq init	1 ;do not change freq from sf
    a1,a2 sfplay3	ivel, inum, kamp*ivel, kfreq, ipreset
    outs	a1, a2
endin
	
</CsInstruments>
<CsScore>
f0 z
i "PlayOrStop" 0 z
</CsScore>
</CsoundSynthesizer>

```
