aboutsummaryrefslogtreecommitdiff
path: root/site/udo/chop.udo
blob: cf8d2f31204930702226771b67ae53a928d79e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
#ifndef UDO_CHOP
#define UDO_CHOP ##
/*
	chop: init-time table editing

	This file is part of the SONICS UDO collection by Richard Knight 2023, 2024
		License: GPL-2.0-or-later
		http://1bpm.net
*/

#include "/uniqueid.udo"

opcode chop_mktemp, ii, ij
	isize, imono xin
	ifnL ftgen 0, 0, isize, 2, 0
	if (imono < 1) then
		ifnR ftgen 0, 0, isize, 2, 0
	endif
	xout ifnL, ifnR 
endop

opcode chop_mktemp, i, i
	isize xin
	ifn ftgen 0, 0, -isize, -2, 0
	xout ifn
endop



opcode chop_zerostartend, ii, i
	ifn xin
	ilen = ftlen(ifn)
	istart = -1
	iend = -1
	iprevsample = tab_i(0, ifn)
	index = 1
	while (index < ilen) do
		isample = tab_i(index, ifn)
		if ((isample > 0 && iprevsample < 0) || (isample < 0 && iprevsample > 0)) then
			istart = index
			goto foundstart
		endif
		iprevsample = isample
		index += 1
	od

foundstart:
	index = ilen - 2
	iprevsample = tab_i(ilen - 1, ifn)
	while (index >= 0) do
		isample = tab_i(index, ifn)
		if ((isample > 0 && iprevsample < 0) || (isample < 0 && iprevsample > 0)) then
			iend = index
			goto foundend
		endif
		iprevsample = isample
		index -= 1
	od

foundend:
	xout istart, iend
endop


opcode _chop_assamples, iii, iii
	ichopstart, ichoplen, iassamples xin
	if (iassamples == 1) then
		ichopstartsamp = ichopstart
		ichopendsamp = ichopstart + ichoplen
		ichoplensamp = ichoplen
	else
		ichopstartsamp = round(ichopstart * sr)
		ichopendsamp = round((ichopstart + ichoplen) * sr)
		ichoplensamp = round(ichoplen * sr)
	endif
	xout ichopstartsamp, ichopendsamp, ichoplensamp
endop




;ftslice better?
opcode chop_cut, iiii, iiiij
	ichopstart, ichoplen, ifnL, ifnR, iassamples xin
	ichopstartsamp, ichopendsamp, ichoplensamp _chop_assamples ichopstart, ichoplen, iassamples

	ilen = ftlen(ifnL)
	inewlen = ilen - ichoplensamp
	if (ifnR == -1) then
		icutL chop_mktemp round(ichoplensamp)
		ifntempL chop_mktemp round(inewlen)
	else
		icutL, icutR chop_mktemp round(ichoplensamp)
		ifntempL, ifntempR chop_mktemp round(inewlen)
	endif

	ireadpos = 0
	iwritepos = 0
	ichopwritepos = 0
	while (ireadpos + 1 < ilen) do
		ivalL = tab_i(ireadpos, ifnL)
		if (ifnR != -1) then
			ivalR = tab_i(ireadpos, ifnR)
		endif

		if (ireadpos >= ichopstartsamp && ireadpos < ichopendsamp) then
			tabw_i ivalL, ichopwritepos, icutL
			if (ifnR != 1) then
				tabw_i ivalR, ichopwritepos, icutR
			endif
			ichopwritepos += 1
		else
			tabw_i ivalL, iwritepos, ifntempL
			if (ifnR != 1) then
				tabw_i ivalR, iwritepos, ifntempR
			endif
			iwritepos += 1
		endif
		ireadpos += 1
	od

	ftfree ifnL, 0
	if (ifnR != -1) then
		ftfree ifnR, 0
	endif

	xout icutL, icutR, ifntempL, ifntempR
endop


opcode chop_cut, ii, iiij
	ichopstart, ichoplen, ifn, iassamples xin
	ifnCut, i_, ifnNew, i_ chop_cut ichopstart, ichoplen, ifn, -1, iassamples
	xout ifnCut, ifnNew
endop


opcode _chop_copy, ii, iiiij
	ichopstart, ichoplen, ifnL, ifnR, iassamples xin
	ichopstartsamp, ichopendsamp, ichoplensamp _chop_assamples ichopstart, ichoplen, iassamples
	if (ifnL > 0) then
		ifntempL chop_mktemp round(ichoplensamp)
		ftslicei ifnL, ifntempL, ichopstartsamp, ichopendsamp
	endif
	if (ifnR > 0) then
		ifntempR chop_mktemp round(ichoplensamp)
		ftslicei ifnR, ifntempR, ichopstartsamp, ichopendsamp
	endif
	xout ifntempL, ifntempR
endop

opcode chop_copy, i, iiij
	ichopstart, ichoplen, ifn, iassamples xin
	ifn, i_ _chop_copy ichopstart, ichoplen, ifn, -1, iassamples
	xout ifn
endop

opcode chop_copy, ii, iiiij
	ichopstart, ichoplen, ifnL, ifnR, iassamples xin
	ifntempL, ifntempR _chop_copy ichopstart, ichoplen, ifnL, ifnR, iassamples
	xout ifntempL, ifntempR
endop



opcode _chop_operation, 0, iiiiipoop ; should only be run in one k-cycle
	ifnSource, ifnTarget, isourceStart, isourceEnd, itargetStart, iterations, itargetIncrement, imix, iteration xin
	ilen = (isourceEnd - isourceStart)
	ilens = ilen / sr
	ikcycles = ilen / ksmps
	kcount = 0
	while (kcount < ikcycles) do
		aposSource linseg isourceStart, ilens, isourceEnd
		aposTarget linseg itargetStart, ilens, (itargetStart + ilen) - 1
		asig table3 aposSource, ifnSource
		if (imix == 1) then
			asig += table3:a(aposTarget, ifnTarget)
		endif
		tablew asig, aposTarget, ifnTarget
		kcount += 1
	od
	if (iteration < iterations) then
		_chop_operation ifnSource, ifnTarget, isourceStart, isourceEnd, itargetStart + itargetIncrement, iterations, itargetIncrement, imix, iteration + 1
	endif
endop


opcode chop_pastek, kiii, ijijipo
	ifnSrcL, ifnSrcR, ifnDestL, ifnDestR, istart, inumber, imix xin	
	inumber = round(inumber)
	kdone init 0	
	ifnanalysis = (ifnDestL != -1) ? ifnDestL : ifnDestR
	idestlen = ftlen(ifnanalysis)
	ipastelen = ftlen((ifnSrcL != -1) ? ifnSrcL : ifnSrcR)
	itotalpastelen = ipastelen * inumber
	iextended = 1
	if (imix == 0) then
		inewlen = round(idestlen + itotalpastelen)
	else
		if (itotalpastelen > idestlen - istart) then
			inewlen = round(istart + itotalpastelen)
		else
			inewlen = idestlen
			iextended = 0
		endif
	endif
	ktimek timeinstk
	if (ktimek == 1) then
		if (ifnDestL != -1) then
			if (iextended == 1) then
				ifnNewL chop_mktemp inewlen
				ftfree ifnDestL, 1
			else
				ifnNewL = ifnDestL
			endif
			if (imix == 0) then
				_chop_operation ifnDestL, ifnNewL, 0, istart, 0
				_chop_operation ifnSrcL, ifnNewL, 0, ipastelen, istart, inumber, ipastelen
				_chop_operation ifnDestL, ifnNewL, istart + 1, idestlen, istart + itotalpastelen + 1
			else
				if (iextended == 1) then
					_chop_operation ifnDestL, ifnNewL, 0, idestlen, 0
				endif
				_chop_operation ifnSrcL, ifnNewL, 0, ipastelen, istart, inumber, ipastelen, imix
			endif
		else
			ifnNewL = -1
		endif

		if (ifnDestR != -1) then
			if (iextended == 1) then
				ifnNewR chop_mktemp inewlen
				ftfree ifnDestR, 1
			else
				ifnNewR = ifnDestR
			endif
			if (imix == 0) then
				_chop_operation ifnDestR, ifnNewR, 0, istart, 0
				_chop_operation ifnSrcR, ifnNewR, 0, ipastelen, istart, inumber, ipastelen
				_chop_operation ifnDestR, ifnNewR, istart + 1, idestlen, istart + itotalpastelen + 1
			else
				if (iextended == 1) then
					_chop_operation ifnDestR, ifnNewR, 0, idestlen, 0
				endif
				_chop_operation ifnSrcR, ifnNewR, 0, ipastelen, istart, inumber, ipastelen, imix
			endif
		else
			ifnNewR = -1
		endif
	else
		kdone = 1
	endif
	
	xout kdone, ifnNewL, ifnNewR, itotalpastelen
endop


opcode chop_pastek, kiii, ijijip
	ifnSrcL, ifnSrcR, ifnDestL, ifnDestR, istart, inumber xin	
	inumber = round(inumber)
	kdone init 0	
	ifnanalysis = (ifnDestL != -1) ? ifnDestL : ifnDestR
	ipastelen = ftlen((ifnSrcL != -1) ? ifnSrcL : ifnSrcR)
	itotalpastelen = ipastelen * inumber
	inewlen = round(ftlen(ifnanalysis) + itotalpastelen)
	ktimek timeinstk
	if (ktimek == 1) then
		if (ifnDestL != -1) then
			ifnNewL chop_mktemp inewlen
			_chop_operation ifnDestL, ifnNewL, 0, istart, 0
			_chop_operation ifnSrcL, ifnNewL, 0, ipastelen, istart, inumber, ipastelen
			_chop_operation ifnDestL, ifnNewL, istart + 1, ftlen(ifnDestL), istart + itotalpastelen + 1
			ftfree ifnDestL, 1
		else
			ifnNewL = -1
		endif
		if (ifnDestR != -1) then
			ifnNewR chop_mktemp inewlen
			_chop_operation ifnDestR, ifnNewR, 0, istart, 0
			_chop_operation ifnSrcR, ifnNewR, 0, ipastelen, istart, inumber
			_chop_operation ifnDestR, ifnNewR, istart + 1, ftlen(ifnDestR), istart + itotalpastelen + 1
			ftfree ifnDestR, 1
		else
			ifnNewR = -1
		endif
	else
		kdone = 1
	endif
	
	xout kdone, ifnNewL, ifnNewR, itotalpastelen
endop


opcode chop_setsilencek, k, iijjJ
	istart, idellen, ifnL, ifnR, ktrig xin
	kdone init 0	
	ktimek timeinstk	

	if (kdone == 0 && (ktrig == -1 && ktimek == 1 || ktrig == 1)) then
		ilens = idellen / sr
		ikcycles = idellen / ksmps
		kcount = 0
		while (kcount < ikcycles) do
			apos linseg istart, ilens, istart + idellen
			anull init 0
			if (ifnL != -1) then
				tablew dcblock(anull), apos, ifnL
			endif
			if (ifnR != -1) then
				tablew dcblock(anull), apos, ifnR
			endif
			kcount += 1
		od
		kdone = 1
	endif
	
	xout kdone
endop


opcode chop_deletek, kii, iijj
	istart, idellen, ifnL, ifnR xin
	kdone init 0		

	ktimek timeinstk

	if (ktimek == 1) then
		if (ifnL != -1) then
			ifnNewL chop_mktemp round(ftlen(ifnL) - idellen)
			_chop_operation ifnL, ifnNewL, 0, istart, 0
			_chop_operation ifnL, ifnNewL, istart + idellen, ftlen(ifnL), istart
			ftfree ifnL, 1
		else
			ifnNewL = -1
			ifnCutL = -1
		endif
		if (ifnR != -1) then
			ifnNewR chop_mktemp round(ftlen(ifnR) - idellen)
			_chop_operation ifnR, ifnNewR, 0, istart, 0
			_chop_operation ifnR, ifnNewR, istart + idellen, ftlen(ifnR), istart
			ftfree ifnR, 1
		else
			ifnNewR = -1
			ifnCutR = -1
		endif
	else
		kdone = 1
	endif
	
	xout kdone, ifnNewL, ifnNewR
endop


opcode chop_trimk, kii, iijj
	istart, itrimlen, ifnL, ifnR xin
	kdone init 0
	ktimek timeinstk
	if (ktimek == 1) then
		if (ifnL != -1) then
			ifnNewL chop_mktemp round(itrimlen)
			_chop_operation ifnL, ifnNewL, istart, istart + itrimlen, 0
			ftfree ifnL, 1
		else
			ifnNewL = -1
		endif
		if (ifnR != -1) then
			ifnNewR chop_mktemp round(itrimlen)
			_chop_operation ifnR, ifnNewR, istart, istart + itrimlen, 0
			ftfree ifnR, 1
		else
			ifnNewR = -1
		endif
	else
		kdone = 1
	endif
	xout kdone, ifnNewL, ifnNewR
endop


opcode chop_cutk, kiiii, iijj
	istart, icutlen, ifnL, ifnR xin
	kdone init 0		

	ktimek timeinstk

	if (ktimek == 1) then
		if (ifnL != -1) then
			ifnNewL chop_mktemp round(ftlen(ifnL) - icutlen)
			ifnCutL chop_mktemp round(icutlen)
			_chop_operation ifnL, ifnNewL, 0, istart, 0
			_chop_operation ifnL, ifnCutL, istart, istart + icutlen, 0
			_chop_operation ifnL, ifnNewL, istart + icutlen, ftlen(ifnL), istart
			ftfree ifnL, 1 
		else
			ifnNewL = -1
			ifnCutL = -1
		endif
		if (ifnR != -1) then
			ifnNewR chop_mktemp round(ftlen(ifnR) - icutlen)
			ifnCutR chop_mktemp round(icutlen)
			_chop_operation ifnR, ifnNewR, 0, istart, 0
			_chop_operation ifnR, ifnCutR, istart, istart + icutlen, 0
			_chop_operation ifnR, ifnNewR, istart + icutlen, ftlen(ifnR), istart
			ftfree ifnR, 1
		else
			ifnNewR = -1
			ifnCutR = -1
		endif
	else
		kdone = 1
	endif
	
	xout kdone, ifnCutL, ifnCutR, ifnNewL, ifnNewR
endop



opcode chop_copyk, kii, iiij
	istart, ilen, ifnL, ifnR xin
	kdone init 0		

	ktimek timeinstk

	if (ktimek == 1) then
		if (ifnL != -1) then
			ifntempL chop_mktemp round(ilen)
			_chop_operation ifnL, ifntempL, istart, istart + ilen, 0
		else
			ifntempL = -1
		endif
		if (ifnR != -1) then
			ifntempR chop_mktemp round(ilen)
			_chop_operation ifnR, ifntempR, istart, istart + ilen, 0
		else
			ifntempR = -1
		endif
	else
		kdone = 1
	endif
	xout kdone, ifntempL, ifntempR
endop




opcode chop_move, ii, iiiii
	ichopstart, ichoplen, ichopdest, ifnL, ifnR xin
	ifnchopL, ifnchopR chop_copy ichopstart, ichoplen, ifnL, ifnR

	ichopdestsamp = round(ichopdest * sr)
	ichopendsamp = round((ichopdest + ichoplen) * sr)
	ilen = ftlen(ifnL)
	ifntempdestL, ifntempdestR chop_mktemp ilen

	ilastvalL = 0
	ilastvalR = 0
	iwritemode = 0
	ireadposmain = 0
	ireadposchop = 0
	iwritepos = 0
	while (iwritepos < ilen) do
		if (iwritepos >= ichopdestsamp && iwritepos + 1 < ichopendsamp) then
			ivalL = tab_i(ireadposchop, ifnchopL)
			ivalR = tab_i(ireadposchop, ifnchopR)
			if (iwritemode == 0) then
				if (ireadposchop + 1 <= ichopendsamp) then
					ivalR = (ilastvalR + tab_i(ireadposchop + 1, ifnchopL)) * 0.5
					ivalR = (ilastvalR + tab_i(ireadposchop + 1, ifnchopR)) * 0.5
				endif
				iwritemode = 1
			endif
			ireadposchop += 1
		else
			ivalL = tab_i(ireadposmain, ifnL)
			ivalR = tab_i(ireadposmain, ifnR)
			if (iwritemode == 1) then
				if (ireadposmain + 1 <= ilen) then
					ivalR = (ilastvalR + tab_i(ireadposmain + 1, ifnL)) * 0.5
					ivalR = (ilastvalR + tab_i(ireadposmain + 1, ifnR)) * 0.5
				endif
				iwritemode = 0
			endif			
			ireadposmain += 1
		endif
		ilastvalL = ivalL
		ilastvalR = ivalR
		tabw_i ivalL, iwritepos, ifntempdestL
		tabw_i ivalR, iwritepos, ifntempdestR
		iwritepos += 1
	od

	ftfree ifnchopL, 0
	ftfree ifnchopR, 0
	ftfree ifnL, 0
	ftfree ifnR, 0	
	xout ifntempdestL, ifntempdestR
endop




opcode _chop_paste, iii, iiiiiii
	ifnSrcL, ifnSrcR, ifnDestL, ifnDestR, istartsamp, inumber, itimevarratio xin
	imonosrc = (ifnSrcR == -1) ? 1 : 0
	imonodest = (ifnDestR == -1) ? 1 : 0
	
	inumber = round(inumber)
	ipastelen = ftlen(ifnSrcL) * inumber
	inewlen = round(ftlen(ifnDestL) + ipastelen)
	ifnNewL, ifnNewR chop_mktemp inewlen, imonodest

	inum = 0
	inputsamps = ftlen(ifnDestL)
	ichoplensamps = ftlen(ifnSrcL)
	
	if (itimevarratio != 0) then
		itimevarratio = random(0.1, itimevarratio)
	endif
	ipastetime = istartsamp + (ichoplensamps * itimevarratio)
	ipastepos = int(ipastetime)
	ireadposorig = 0
	iwritepos = 0
	ireadchoppos = 0
	idonepaste = 0
print inumber
	while (iwritepos < inewlen) do
		if (idonepaste == 1 || iwritepos <= ipastepos) then
			if (ireadposorig < inputsamps) then
				ivalL = tab_i(ireadposorig, ifnDestL)
				if (imonosrc == 0) then
					ivalR = tab_i(ireadposorig, ifnDestR)
				else
					ivalL = ivalR
				endif
		
				if (imonodest == 1) then
					tabw_i ivalL, iwritepos, ifnNewL
				else
					tabw_i ivalL, iwritepos, ifnNewL
					tabw_i ivalR, iwritepos, ifnNewR
				endif
			endif
			ireadposorig += 1
		else
			ivalL = tab_i(ireadchoppos, ifnSrcL)
			if (imonosrc == 0) then
				ivalR = tab_i(ireadchoppos, ifnSrcR)
			else
				ivalL = ivalR
			endif
	
			if (imonodest == 1) then
				tabw_i ivalL, iwritepos, ifnNewL
			else
				tabw_i ivalL, iwritepos, ifnNewL
				tabw_i ivalR, iwritepos, ifnNewR
			endif

			if (ireadchoppos + 1 >= ichoplensamps) then
				ipastetime += (ichoplensamps * random(0, itimevarratio))
				ipastepos = ipastetime
print ipastepos
				if (inum + 1 < inumber) then
					inum += 1
print inum
				else
					idonepaste = 1
				endif
				ireadchoppos = 0
			else
				ireadchoppos += 1
			endif			
		endif

		iwritepos += 1
	od

	ftfree ifnDestL, 0
	ftfree ifnDestR, 0
	xout ifnNewL, ifnNewR, ipastelen
endop

opcode chop_paste, ii, iiiii
	ifnSrc, ifnDest, istartsamp, inumber, itimevarratio xin
	ifnNew, i_, ipastelen _chop_paste ifnSrc, -1, ifnDest, -1, istartsamp, inumber, itimevarratio
	xout ifnNew, ipastelen
endop

opcode chop_paste, iii, iiiiiii
	ifnSrcL, ifnSrcR, ifnDestL, ifnDestR, istartsamp, inumber, itimevarratio xin
	ifnNewL, ifnNewR, ipastelen _chop_paste ifnSrcL, ifnSrcR, ifnDestL, ifnDestR, istartsamp, inumber, itimevarratio
	xout ifnNewL, ifnNewR, ipastelen
endop




/*
opcode chop_pastek, kiii, ijijip
	ifnSrcL, ifnSrcR, ifnDestL, ifnDestR, istart, inumber xin	
	inumber = round(inumber)
	iktime = 1 / kr
	if (ifnDestL != -1) then
		ipastelen = ftlen(ifnSrcL)  ;;;;;;;;;;;;;;;;;;;;;;;TODOTODOTODOTODO;TODO;;;;* inumber
		inewlen = round(ftlen(ifnDestL) + ipastelen)
		ifnNewL chop_mktemp inewlen

		; paste beginning from original
		Schannel = sprintf("choppaste%d", uniqueid())
		schedule("_chop_operation", 0, -1, ifnDestL, ifnNewL, 0, istart, 0, Schannel)
		kdone1L chnget Schannel

		; paste buffer
		Schannel = sprintf("choppaste%d", uniqueid())
		schedule("_chop_operation", iktime, -1, ifnSrcL, ifnNewL, 0, ipastelen, istart, Schannel)
		kdone2L chnget Schannel

		; paste remainder from original
		Schannel = sprintf("choppaste%d", uniqueid())
		ipasteend = istart + ipastelen
		schedule("_chop_operation", iktime * 2, -1, ifnDestL, ifnNewL, istart + 1, ftlen(ifnDestL), istart + ipastelen + 1, Schannel)
		kdone3L chnget Schannel

		ftfree ifnDestL, 1
	else
		kdone1L init 1
		kdone2L init 1
		kdone3L init 1
		ifnNewL = -1
	endif

	if (ifnDestR != -1) then
		ipastelen = ftlen(ifnSrcR)  ;;;;;;;;;;;;;;;;;;;;;;;TODOTODOTODOTODO;TODO;;;;* inumber
		inewlen = round(ftlen(ifnDestR) + ipastelen)
		ifnNewR chop_mktemp inewlen

		Schannel = sprintf("choppaste%d", uniqueid())
		schedule("_chop_operation", iktime * 3, -1, ifnDestR, ifnNewR, 0, istart, 0, Schannel)
		kdone1R chnget Schannel

		Schannel = sprintf("choppaste%d", uniqueid())
		schedule("_chop_operation", iktime * 4, -1, ifnSrcR, ifnNewR, 0, ipastelen, istart, Schannel)
		kdone2R chnget Schannel

		Schannel = sprintf("choppaste%d", uniqueid())
		schedule("_chop_operation", iktime * 5, -1, ifnDestR, ifnNewR, istart + 1, ftlen(ifnDestR), istart + ipastelen + 1, Schannel)
		kdone3R chnget Schannel

		ftfree ifnDestR, 1
	else
		kdone1R init 1
		kdone2R init 1
		kdone3R init 1
		ifnNewR = -1
	endif
	
	xout (kdone1L & kdone2L & kdone3L & kdone1R & kdone2R & kdone3R), ifnNewL, ifnNewR, ipastelen
endop
*/

opcode chop_copypaste, ii, iiiiii
	ichopstart, ichoplen, inumber, itimevarratio, ifnL, ifnR xin
	inumber = round(inumber)
	ifnchopL, ifnchopR chop_copy ichopstart, ichoplen, ifnL, ifnR

	inewlen = round(ftlen(ifnL) + (ichoplen * inumber * sr))
	ifntempL, ifntempR chop_mktemp inewlen

	inum = 0
	inputsamps = ftlen(ifnL)
	ichoplensamps = ftlen(ifnchopL)
	ipastetime = ichopstart + (ichoplen * random(0, itimevarratio))
	ipastepos = round(ipastetime * sr)
	ireadposorig = 0
	iwritepos = 0
	ireadchoppos = 0
	idonepaste = 0

	while (iwritepos < inewlen) do
		if (idonepaste == 1 || iwritepos <= ipastepos) then
			if (ireadposorig < inputsamps) then
				tabw_i tab_i(ireadposorig, ifnL), iwritepos, ifntempL
				tabw_i tab_i(ireadposorig, ifnR), iwritepos, ifntempR
			endif
			ireadposorig += 1
		else
			tabw_i tab_i(ireadchoppos, ifnchopL), iwritepos, ifntempL
			tabw_i tab_i(ireadchoppos, ifnchopR), iwritepos, ifntempR

			if (ireadchoppos + 1 >= ichoplensamps) then
				ipastetime += (ichoplen * random(0, itimevarratio))
				ipastepos = round(ipastetime * sr)
				if (inum + 1 < inumber) then
					inum += 1
				else
					idonepaste = 1
				endif
				ireadchoppos = 0
			else
				ireadchoppos += 1
			endif			
		endif

		iwritepos += 1
	od

	ftfree ifnchopL, 0
	ftfree ifnchopR, 0
	ftfree ifnL, 0
	ftfree ifnR, 0
	xout ifntempL, ifntempR
endop


#end