Release of envelope spoils loop

;
; "Tied Note Example"
; by Steven Yi
;
; Example instruments and score written for "Exploring Tied Notes in Csound" article in The Csound Journal, Volume 1, Issue 1.
;
; Generated by blue 0.99.7 (http://csounds.com/stevenyi/blue/index.html)
;

<CsoundSynthesizer>
<CsOptions>
	-odac --env:SSDIR="/Users/now/Music/drum kits/808s/" --default-paths
</CsOptions>
<CsInstruments>
sr=96000  
ksmps=24
nchnls=2
0dbfs=1


	opcode tieStatus,i,0

itie tival

if (itie == 0 && p3 < 0) ithen
    ; this is an initial note within a group of tied notes
    itiestatus = 0
    
       
elseif (p3 < 0 && itie == 1) ithen
    ; this is a middle note within a group of tied notes 
    itiestatus = 1

			
elseif (p3 > 0 && itie == 1) ithen
    ; this is an end note out of a group of tied notes
    itiestatus = 2


elseif (p3 > 0 && itie == 0) ithen
    ; this note is a standalone note
    itiestatus = -1

endif  

	xout	itiestatus

	endop 


	instr 1	;Normal Instrument
	idur = abs(p3)
	icps	 init cpspch(p4)
	iskip tival
	itie1 tieStatus
	itab = ftgenonce(1, 0 ,2^16, -10 , 1)
                     ; Init pitch for untied note
                     tigoto skipInit
  kcps init icps
  kcps     lag      icps,0.5      ; Drift towards
; ioldpch init ipch
indx = 1
imax = 5
 asig vco ampdb(-18), kcps,1				
 loop:
	print(indx)
	kL lfo 0.1+indx*0.05,0.6
	asigd vdelay asig,kL,0.3 
	asigdR vdelay asig,kL,0.3
	
	asigL = asig
	asigR = asig
	asigLd,asigRd pan2 asigd, indx*.05
	asigL += asigLd
	asigR += asigRd 
	asigLd,asigRd pan2 asigdR, indx*.050221+0.5
	asigL += asigLd 
	asigR += asigRd 
	    
;	 asig = asig*kenv
	
	loop_le indx, 1, imax, loop
	asigL lowpass2 asigL, 5000, 2
	asigR lowpass2 asigR, 5000, 2
	outs asigL*(1/(imax+1)),asigR*(1/(imax+1))
skipInit:


;inewpch 	= ipch
;kpchline	linseg	ioldpch, .2, inewpch, idur - .05,  inewpch
;ioldpch	= inewpch
;it1 = 0
;if (itie1 == -1) then
;    kenv	adsr	.05, .05, .9, .05
;		it1 =0
;elseif (itie1 == 0) then	
;    kenv	linseg	0, .05, 1,  .2, 1
;    it1=0
;
;elseif (itie1 == 1) then	
;    kenv init 1
;    it1 = -1 
;
;elseif (itie1 == 2) then
;    kenv linseg	1, idur - .05, 1, .5, 0
;    it1 = -1 
;     
;    endif
;    prints("Im a tied note!")
	endin 
	gi1 ftgen 0,0,2^10,9,  1,3,0,   3,1,0, 9,0.333,180	
instr 2
	kcps init cpspch(p4)
	ke = 0 
	if p5 == 1 then 
	ke=1
	endif
	ke linsegr 1,0.1,0
	skipke:
	as oscil ke*0.1,kcps 
	kor = 0.2  
	as distort as,0.2,gi1
;	as1 distort as1,0.22222,gi1
	out as, as
endin
</CsInstruments>

<CsScore>

r8 
t 0 130
i2 0 1.5 6.0 0
i2 3 1.5 5.07
i2 4.5 .5 5.08
i2 5 1.5 5.03
i2 6.5 1.5 5.03
s



</CsScore>

</CsoundSynthesizer>

In this code release of linseg spoils loop, i can change envelope with if that connected to p5 but it makes it without release and clicks, and its, redundant i think?
if i do it with r or n in score it waits note off of instrument i dont want it i want it like with retyping the same values with i statements that continue loop. or with { } and p2 is like [0+$index.]. Seems like unresolvable but mb someone knows what can be done other than deleting release.
Need release of last note to fade into start of the loop

Hi, took a quick look & there appears to be a few issues. I don’t use r in a score often, never tried with an (r) release env. The release does appear to be applied, but the the r score section does not appear to restart until the release envelope finishes. So then there is no overlap & also each loop is no longer the correct length of time - the loop time is adding the release time - which will go out of sync very quickly (the tempo will not be as expected).

For example, if you set
t 0 60
in the score, each loop will last 8.1 seconds, not 8 seconds. And with:
t 0 120
each loop will last 4.05 seconds.

You can somewhat compensate for that by reducing the last note in the loop by .1 but that won’t solve having the env release overlap (there will be no fade from the last to the first note of the loop).

This code will work but it’s a bit more complicated. The benefit is it can be saved as a template and easily modified for different situations without having to tweak the score as much.

<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>
sr=96000  
ksmps=24
nchnls=2
0dbfs=1

gi1 ftgen 0,0,2^10,9,  1,3,0,   3,1,0, 9,0.333,180	
gkIdx init -1
giIdx init 0

instr trig
    iBPM  = 130 ; beats per minute
    iBeat = 60/iBPM ; length of beat in seconds
    ; array of note durations, necessary to add 1
    ; extra duration equal to first duration
    iDur[]  fillarray 1.5, 1.5, 1.5, .5, 1.5, 1.5, 1.5
    iLen  = lenarray(iDur)
    kTrig = metro(1/iBeat/iDur[gkIdx])
    if  kTrig == 1 then
       schedulek "play", 0, iBeat * iDur[gkIdx + 1]
       gkIdx += 1
    endif
    ; if statement to loop thru note durations
    gkIdx = (gkIdx == iLen - 1 ? 0 : gkIdx)
endin

instr play
    iNote[] fillarray 6.0, 0, 5.07, 5.08, 5.03, 5.03
    iAmp[]  fillarray 1, 0, 1, 1, 1, 1
    iVol = iAmp[giIdx]
    iLen init lenarray(iNote)
	icps init cpspch(iNote[giIdx])
    ke linsegr .1, p3, .1, .1, 0
	as oscil ke*iVol,icps 
	as distort as,0.2,gi1
	out as, as

    giIdx += 1	
	if giIdx == iLen then
	   giIdx = 0
	endif
endin

</CsInstruments>
<CsScore>
i"trig"  0  30
</CsScore>
</CsoundSynthesizer>

Not sure this is exactly what you’re looking for though. Others here are more knowledgeable so maybe someone will have an easier solution.

Best,
Scott