# Instrument time gate

**URL:** <https://forum.csound.com/t/instrument-time-gate/971>\
**Category:** General\
**Created:** [November 24, 2022, 4:10pm UTC](https://forum.csound.com/t/instrument-time-gate/971 "2022-11-24T16:10:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cordelia](https://forum.csound.com/user_avatar/forum.csound.com/cordelia/32/279_2.png) [@Cordelia](https://forum.csound.com/u/Cordelia)\
**Post date:** [November 24, 2022, 4:10pm UTC](https://forum.csound.com/t/instrument-time-gate/971/1 "2022-11-24T16:10:41Z")

</div>

how could I make a time gate for a csound instrument?

e.g. I want that my instrument 1 if it receives 2 “i” in less than 1second, the second “i” is discarded.

---

<div class="post-metadata">

**Author:** ![joachim](https://forum.csound.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@joachim](https://forum.csound.com/u/joachim)\
**Post date:** [November 24, 2022, 4:44pm UTC](https://forum.csound.com/t/instrument-time-gate/971/2 "2022-11-24T16:44:23Z")

</div>

what is this “i”? can you provide a bit of code to look at?  
(because there is not only one solution, but it depends on the situation  
you are in.)

---

<div class="post-metadata">

**Author:** ![Cordelia](https://forum.csound.com/user_avatar/forum.csound.com/cordelia/32/279_2.png) [@Cordelia](https://forum.csound.com/u/Cordelia)\
**Post date:** [November 24, 2022, 4:59pm UTC](https://forum.csound.com/t/instrument-time-gate/971/3 "2022-11-24T16:59:44Z")

</div>

For exemple:

```csound
<CsInstruments>

instr 1
print 1
endin

</CsInstruments>
<CsScore>
i 1 0 1
i 1 .25 1
i 1 .35 1
e
</CsScore>

```

I want to call the first instruction (_i 1 0 1_) and automatically discard all the others (_i 1 .25 1, i 1 .35 1_) of the score because they’re called within 1 seconds the first call.

I want to use this for livecoding, so this merely an idea (i’m sorry, it’s almost dumb), but I want to control too many accumulation in time of certain instrument instruction through UDP.

---

<div class="post-metadata">

**Author:** ![joachim](https://forum.csound.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@joachim](https://forum.csound.com/u/joachim)\
**Post date:** [November 24, 2022, 5:34pm UTC](https://forum.csound.com/t/instrument-time-gate/971/4 "2022-11-24T17:34:46Z")

</div>

ok, got it.  
you can set a variable which holds the previous time, and compare with it.  
for instance (ksmps=64 here):

//set a variable for the last time instr 2 was playing  
giPlayed init -1

instr 1  
//trigger instr 2 irregularily  
if rnd:k(100) \< 1 then  
schedulek(2,0,1)  
endif  
endin  
schedule(1,0,100)

instr 2  
//calculate time difference to last activation  
iDiff = times:i() - giPlayed  
//activate this instrument depending on this  
if iDiff \>= 1 then  
//do something  
outall(poscil:a(transeg:a(.2,p3,-3,0),random:i(400,800)))  
//set variable to current time  
giPlayed = times:i()  
//print  
prints(“time = .3f, note activated\n",times:i()) else //print what has been skipped prints(" time = .3f, note NOT activated\n”,times:i())  
endif  
endin

---

<div class="post-metadata">

**Author:** ![Cordelia](https://forum.csound.com/user_avatar/forum.csound.com/cordelia/32/279_2.png) [@Cordelia](https://forum.csound.com/u/Cordelia)\
**Post date:** [November 24, 2022, 11:20pm UTC](https://forum.csound.com/t/instrument-time-gate/971/5 "2022-11-24T23:20:23Z")

</div>

that’s wonderful and precise.  
Thank you Joachim.

---

<div class="post-metadata">

**Author:** ![Cordelia](https://forum.csound.com/user_avatar/forum.csound.com/cordelia/32/279_2.png) [@Cordelia](https://forum.csound.com/u/Cordelia)\
**Post date:** [November 25, 2022, 12:22am UTC](https://forum.csound.com/t/instrument-time-gate/971/6 "2022-11-25T00:22:36Z")

</div>

> [@joachim](#):
>
> giPlayed init -1

I make `if iDiff >= gigatems then` so that at the declaration of the variables, i put `giPlayed init gigatems`. In this way I can change the time and at the beginning I can start livecoding immediately if `gigatems` is very long!

---

<div class="post-metadata">

**Author:** ![joachim](https://forum.csound.com/letter_avatar_proxy/v4/letter/j/a87d85/32.png) [@joachim](https://forum.csound.com/u/joachim)\
**Post date:** [November 25, 2022, 4:23pm UTC](https://forum.csound.com/t/instrument-time-gate/971/7 "2022-11-25T16:23:58Z")

</div>

glad to know it’s working for you.  
it is also possible to use chnset/chnget, by the way. it is perhaps even  
more flexible, and the channel might be used to display the value, for  
instance. this is the code:

chnset(-1,“prevTime”)  
instr 1  
if rnd:k(100) \< 1 then  
schedulek(2,0,1)  
endif  
endin  
schedule(1,0,100)  
instr 2  
iDiff = times:i() - chnget:i(“prevTime”)  
if iDiff \>= 1 then  
chnset(times:i(),“prevTime”)  
prints(“time = .3f, note activated\n",times:i()) else prints(" time = .3f, note NOT activated\n”,times:i())  
endif  
endin
