-odac /* LIVE GRANULAR SYNTHESIS */ ;example for CsoundQt ;written by joachim heintz ;jan 2010 / june 2011 ;please send bug reports and suggestions ;to jh at joachimheintz.de sr = 44100 ksmps = 32 nchnls = 1 0dbfs = 1 giWin1 ftgen 1, 0, 4096, 20, 1, 1 ; Hamming giWin2 ftgen 2, 0, 4096, 20, 2, 1 ; von Hann giWin3 ftgen 3, 0, 4096, 20, 3, 1 ; Triangle (Bartlett) giWin4 ftgen 4, 0, 4096, 20, 4, 1 ; Blackman (3-term) giWin5 ftgen 5, 0, 4096, 20, 5, 1 ; Blackman-Harris (4-term) giWin6 ftgen 6, 0, 4096, 20, 6, 1 ; Gauss giWin7 ftgen 7, 0, 4096, 20, 7, 1, 6 ; Kaiser giWin8 ftgen 8, 0, 4096, 20, 8, 1 ; Rectangle giWin9 ftgen 9, 0, 4096, 20, 9, 1 ; Sync giLiveBuf ftgen 0, 0, 16384, 2, 0 ; buffer for writing and reading live input giDisttab ftgen 0, 0, 32768, 7, 0, 32768, 1 ; for kdistribution giCosine ftgen 0, 0, 8193, 9, 1, 1, 90 ; cosine giMaxGrLen = 0.1 ; maximaum grain length in seconds opcode PartikkelSimpB, a, iakkkkkkkiii /* Simplified Version of the partikkel opcode, with a time pointer input. ifiltab: function table to read apnter: pointer in the function table (0-1) kgrainamp: multiplier of the grain amplitude (the overall amplitude depends also on grainrate and grainsize) kgrainrate: number of grains per seconds kgrainsize: grain duration in ms kdist: 0 = periodic (synchronous), 1 = scattered (asynchronous) kcent: transposition in cent kposrand: random deviation (offset) of the pointer in ms kcentrand: random transposition in cents (up and down) icosintab: function table with cosine (e.g. giCosine ftgen 0, 0, 8193, 9, 1, 1, 90) idisttab: function table with distribution (e.g. giDisttab ftgen 0, 0, 32768, 7, 0, 32768, 1) iwin: function table with window shape (e.g. giWin ftgen 0, 0, 4096, 20, 9, 1) */ ifiltab, apnter, kgrainamp, kgrainrate, kgrainsize, kdist, kcent, kposrand, kcentrand, icosintab, idisttab, iwin xin /*amplitude*/ kamp = kgrainamp * 0dbfs /*transposition*/ kcentrand rand kcentrand; random transposition iorig = 1 / (ftlen(ifiltab)/sr); original pitch kwavfreq = iorig * cent(kcent + kcentrand) /*pointer*/ kposrand = (kposrand/1000) * (sr/ftlen(ifiltab)); random offset converted from ms to phase (0-1) arndpos linrand kposrand; random offset in phase values asamplepos = apnter + arndpos /* other parameters */ imax_grains = 1000; maximum number of grains per k-period async = 0; no sync input awavfm = 0; no audio input for fm aout partikkel kgrainrate, kdist, idisttab, async, 1, iwin, \ -1, -1, 0, 0, kgrainsize, kamp, -1, \ kwavfreq, 0, -1, -1, awavfm, \ -1, -1, icosintab, kgrainrate, 1, \ 1, -1, 0, ifiltab, ifiltab, ifiltab, ifiltab, \ -1, asamplepos, asamplepos, asamplepos, asamplepos, \ 1, 1, 1, 1, imax_grains xout aout endop opcode ShowLED_a, 0, Sakkk ;shows an audiosignal in an outvalue channel, in dB or raw amplitudes ;Soutchan: string as name of the outvalue channel ;asig: audio signal to be shown ;kdispfreq: refresh frequency of the display (Hz) ;kdb: 1 = show as db, 0 = show as raw amplitudes (both in the range 0-1) ;kdbrange: if idb=1: which dB range is shown Soutchan, asig, ktrig, kdb, kdbrange xin kdispval max_k asig, ktrig, 1 if kdb != 0 then kdb = dbfsamp(kdispval) kval = (kdbrange + kdb) / kdbrange else kval = kdispval endif if ktrig == 1 then outvalue Soutchan, kval endif endop opcode ShowOver_a, 0, Sakk ;shows if asig has been larger than 1 and stays khold seconds ;Soutchan: string as name of the outvalue channel ;kdispfreq: refresh frequency of the display (Hz) Soutchan, asig, ktrig, khold xin kon init 0 ktim times kstart init 0 kend init 0 khold = (khold < .01 ? .01 : khold); avoiding too short hold times kmax max_k asig, ktrig, 1 if kon == 0 && kmax > 1 && ktrig == 1 then kstart = ktim kend = kstart + khold outvalue Soutchan, kmax kon = 1 endif if kon == 1 && ktim > kend && ktrig == 1 then outvalue Soutchan, 0 kon = 0 endif endop instr 1; master instrument ;;get live input and write to buffer (ftable) giLiveBuf aLiveInPre inch 1 kingaindb invalue "ingaindb" kLiveInGain = ampdb(kingaindb) aLiveInPost = aLiveInPre * kLiveInGain iphasfreq = 1 / (ftlen(giLiveBuf) / sr); phasor frequency kfreeze invalue "freeze"; if checked, freeze writing (and reading) of the buffer kphasfreq = (kfreeze == 1 ? 0 : iphasfreq) awritpnt phasor kphasfreq tablew aLiveInPost, awritpnt, giLiveBuf, 1, 0, 1 ;;create an instrument for a readpointer which is giMaxGrLen seconds later than the writepointer event_i "i", 2, giMaxGrLen, -1, iphasfreq ;;show live input gkTrigDisp metro 10 gkshowdb invalue "showdb" gkdbrange invalue "dbrange" ShowLED_a "LiveInPre", aLiveInPre, gkTrigDisp, gkshowdb, gkdbrange ShowOver_a "LiveInPreOver", aLiveInPre, gkTrigDisp, 2 ShowLED_a "LiveInPost", aLiveInPost, gkTrigDisp, gkshowdb, gkdbrange ShowOver_a "LiveInPostOver", aLiveInPost, gkTrigDisp, 2 ;;select shape of the grain envelope and show it kwinshape invalue "winshape"; 0=Hamming, 1=von Hann, 2=Bartlett, 3=Triangle, 4=Blackman-Harris, ;5=Gauss, 6=Kaiser, 7=Rectangle, 8=Sync event_i "i", 10, 0, -1, i(kwinshape)+1 outvalue "ftab", -(kwinshape+1); graph widget shows selected window shape ;;triggers i 10 at the beginning and whenever the grain envelope has changed kchanged changed kwinshape; sends 1 if the windowshape has changed if kchanged == 1 then event "i", -10, 0, -1; turn off previous instance of i10 event "i", 10, 0, -1, kwinshape+1; turn on new instance endif endin instr 2; creates the pointer for reading the buffer giLiveBuf iphasfreq = p4; usual phasor frequency (calculated and sent by instr 1) kfreeze invalue "freeze"; if checked, freeze reading of the buffer kphasfreq = (kfreeze == 1 ? 0 : iphasfreq); otherwise use kphasfreq areadpnt phasor kphasfreq chnset areadpnt, "readpnt"; sends areadpnt to the channel 'readpnt' endin instr 10; performs granular synthesis ;;parameters for the partikkel opcode iwin = p4; shape of the grain window ifiltab = giLiveBuf; buffer to read apnt init 0; initializing the read pointer kgrainrate invalue "grainrate"; grains per second kgrainsize invalue "grainsize"; length of the grains in ms kcent invalue "transp"; pitch transposition in cent kgraindb invalue "outgaindb"; volume kgrainamp = ampdb(kgraindb) kdist invalue "dist"; distribution (0=periodic, 1=scattered) kposrand invalue "posrand"; time position randomness (offset) of the read pointer in ms kcentrand invalue "centrand"; transposition randomness in cents (up and down) icosintab = giCosine; ftable with a cosine waveform idisttab = giDisttab; ftable with values for scattered distribution apnt chnget "readpnt"; pointer generated ;;granular synthesis agrain PartikkelSimpB ifiltab, apnt, kgrainamp, kgrainrate, kgrainsize,\ kdist, kcent, kposrand, kcentrand, icosintab, idisttab, iwin out agrain ;;show output ShowLED_a "out", agrain, gkTrigDisp, gkshowdb, gkdbrange ShowOver_a "outover", agrain, gkTrigDisp, 2 endin i 1 0 3600 e 146 65 903 657 true 236 236 236 9 9 886 44 {2a56f609-3f3a-49a2-8ef2-04e1041ed49f} true 0 -3 center top Lucida Grande 26 3 0 0 0 255 255 255 noborder 1 0 10 58 886 66 {49b99e1e-2b4b-4aef-b413-95171c64b350} true 0 -3 left top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 11 132 484 148 {e39c91d6-a0bd-4681-8dbc-e384493e578c} true 0 -3 left top Lucida Grande 18 3 0 0 0 255 255 255 border 1 1 LiveInPre 120 164 336 22 {7286a833-c0b0-4f55-8ecc-837453106e15} true 0 -3 out1_post 0.00000000 1.00000000 0.00000000 1.00000000 0.26302832 0.73076900 fill 1 0.00000000 jump noborder #00FF00 0 234 0 false 0 0 0 true LiveInPreOver 454 164 27 22 {42e31476-6cb3-42b4-a1fd-8f48e76892ac} true 0 -3 outLover 0.00000000 1.00000000 0.00000000 1.00000000 0.00000000 0.00000000 fill 1 0.00000000 jump noborder #00FF00 196 14 12 false 0 0 0 true LiveInPost 116 240 336 22 {77fcb44e-b9a2-423d-9764-52f01754d96d} true 0 -3 out2_post 0.00000000 1.00000000 0.00000000 1.00000000 0.11348984 0.59090900 fill 1 0.00000000 jump noborder #00FF00 0 234 0 false 0 0 0 true LiveInPostOver 450 240 27 22 {3e4c2001-fd4f-4b10-bd75-1766b5d1bbde} true 0 -3 outRover 0.00000000 1.00000000 0.00000000 1.00000000 0.00000000 0.00000000 fill 1 0.00000000 jump noborder #00FF00 196 14 12 false 0 0 0 true 21 164 93 28 {9dd7688d-d7d4-4863-bbe8-44c1e622f2bd} true 0 -3 left top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 19 240 97 29 {a6c9d0da-7821-4528-b193-9ee9af3d441f} true 0 -3 left top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 20 202 93 31 {bdebe2b5-07c1-4f1f-b7cb-d2f29a26a5f8} true 0 -3 left top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 ingaindb 120 199 260 26 {64de8264-0345-4faf-b16c-4af284a95268} true 0 0 -12.00000000 12.00000000 -7.47692308 lin continuous -1.00000000 false 518 131 380 215 {e748e0c8-197c-4d4d-b893-9501e50ae02b} true 0 -3 left top Lucida Grande 18 3 0 0 0 255 255 255 border 1 1 out 528 197 338 24 {133bd5e9-655f-4ba7-895d-45c9c8e54eb3} true 0 -3 out 0.00000000 1.00000000 0.00000000 1.00000000 0.68301880 0.68301880 fill 1 0.00000000 jump noborder #00FF00 0 234 0 false 0 0 0 true outover 863 197 26 24 {dc5f0c73-a680-4e93-9855-6706fbd5180d} true 0 -3 outRover 0.00000000 1.00000000 0.00000000 1.00000000 0.00000000 0.00000000 fill 1 0.00000000 jump noborder #00FF00 196 14 12 false 0 0 0 true 525 228 368 105 {c385ef49-3efa-4ec3-806a-ff883343cb9c} true 0 -3 -255.00000000 scope 2.00000000 1.00000000 1.00000000 1.00000000 0.00000000 NoTrigger 11 354 882 281 {f470cb16-0510-44e6-b6e0-c532a68e3409} true 0 -3 left top Lucida Grande 18 3 0 0 0 255 255 255 border 1 1 529 164 95 27 {dcc76929-4dec-4912-b228-fcab4a27e75f} true 0 -3 left top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 outgaindb 625 164 155 24 {2735c656-a8f6-429d-acc3-2fe0db7e078a} true 0 0 -12.00000000 12.00000000 -2.55483871 lin continuous -1.00000000 false 44 390 143 27 {d7297eb9-b0d9-43c7-a5cb-5c71793c04d0} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 17 562 147 25 {ad4914bd-492d-45c0-bb8c-86e98247e37e} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 transp 18 586 222 32 {2a0c5abd-8f05-4f19-8c6e-5ceb8d4572dc} true 0 -3 -1200.00000000 1200.00000000 0.00000000 lin continuous -1.00000000 false 34 481 101 25 {078aa6dc-ef57-4db7-949b-c11e729c837a} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 grainsize 31 506 177 26 {8b59f109-6fc3-4138-bb2e-93113fe13a40} true 0 -3 1.00000000 100.00000000 1.00000000 lin continuous -1.00000000 false grainrate 21 418 252 25 {5df30f9c-f3dd-4ac9-8088-9cf169cd31f5} true 0 0 1.00000000 200.00000000 1.00000000 lin continuous -1.00000000 false 374 394 143 27 {cc73b249-baad-47fd-9842-b921037d1a6a} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 dist 369 420 152 25 {a142fe2c-da20-4aa8-a13b-0432a4cb6352} true 0 -3 0.00000000 1.00000000 1.00000000 lin continuous -1.00000000 false 238 481 191 27 {175842e1-79f7-4518-882a-4555834acf3a} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 posrand 239 509 265 29 {bd0a56e7-6873-484a-be88-67c57c0a66f4} true 0 -3 0.00000000 100.00000000 1.88679245 lin continuous -1.00000000 false centrand 275 589 306 28 {d2da0179-913a-4308-8b92-65f69293814b} true 0 -3 0.00000000 100.00000000 0.00000000 lin continuous -1.00000000 false 275 562 234 25 {558f5942-1622-49a1-aeac-5faede2c8d51} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 298 419 71 29 {ac7d4a98-e00c-4c47-9bb6-9bf8591999ce} true 0 0 right top DejaVu Sans 14 3 0 0 0 255 255 255 noborder 1 0 520 418 90 28 {f41b248e-80fb-4cb5-a210-eded60c8c325} true 0 0 left top DejaVu Sans 14 3 0 0 0 255 255 255 noborder 1 0 694 363 115 28 {5d0f4113-a2a8-4f15-872d-d10b174dcf71} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 winshape 682 414 140 26 {35980021-56da-4027-841b-abf660393ab2} true 0 0 Hamming 0 von Hann 1 Triangle 2 Blackman 3 Blackman-Harris 4 Gauss 5 Kaiser 6 Rectangle 7 Sync 8 8 false ftab 620 476 256 145 {dcb84ff3-5289-475e-813f-db94e34bc296} true 0 -3 0 1.00000000 1.00000000 1.00000000 1.00000000 auto auto true true true true true true true 667 451 168 27 {2d0fa256-93bd-4690-911d-437222bc7428} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 652 389 185 27 {fbbd39e2-e473-4455-8e7c-a57724ae5010} true 0 -3 center top Lucida Grande 14 3 0 0 0 255 255 255 noborder 1 0 458 198 30 31 {eecbf48f-983f-4ce5-bdcc-e6b1ae657b20} true 0 0 right top DejaVu Sans 14 3 0 0 0 255 255 255 noborder 1 0 ingaindb 380 200 76 27 {a44b45c8-8c22-4c53-9442-75339608f708} true 0 0 left Liberation Sans 14 0 0 0 255 255 255 0.00100000 -1e+12 1e+12 false 0 861 162 30 31 {a5947f34-1e11-490f-bd5d-c543d6e4494e} true 0 0 right top DejaVu Sans 14 3 0 0 0 255 255 255 noborder 1 0 outgaindb 781 163 76 27 {52bb78bb-8e63-44a1-a86a-5def867d5311} true 0 0 left Liberation Sans 14 0 0 0 255 255 255 0.00100000 -1e+12 1e+12 false 0 grainrate 186 390 76 27 {c8857f63-c4d2-456d-92df-cf078b7c6525} true 0 0 left Liberation Sans 14 0 0 0 255 255 255 1.00000000 1 1000 false 152 freeze 513 480 87 59 {209e708e-b0c9-4794-b610-fec1096126c8} true 0 0 value 1.00000000 Freeze! / i1 0 10 true false false 10 grainsize 131 481 76 27 {9047dd54-fb61-4009-9867-802809da63cd} true 0 0 left Liberation Sans 14 0 0 0 255 255 255 1.00000000 1 1000 false 100 posrand 429 480 76 27 {d9780cbd-e79a-4741-b575-2a2a49790067} true 0 0 left Liberation Sans 14 0 0 0 255 255 255 0.00100000 0 1000 false 0 transp 164 562 76 27 {20a7f015-fe29-40d1-ac20-589371f9685d} true 0 0 left Liberation Sans 14 0 0 0 255 255 255 1.00000000 -10000 10000 false 0 centrand 506 561 76 27 {0cc8b907-1cb3-425d-b6f5-994a39abb647} true 0 0 left Liberation Sans 14 0 0 0 255 255 255 1.00000000 0 1000 false 0 dbrange 421 289 79 27 {81528aba-64e8-4dc7-b127-daa1434195a2} true 0 0 left Noto Sans 14 0 0 0 255 255 255 0.10000000 1 1e+12 false 0 334 288 90 29 {cc0e0187-e5c9-4b44-b8c0-28d00d45cb89} true 0 0 right top Nimbus Sans L 14 3 0 0 0 255 255 255 noborder 1 0 145 289 110 27 {df57c2f7-4a74-460f-9f7f-b3efb8106306} true 0 0 right top Nimbus Sans L 14 3 0 0 0 255 255 255 noborder 1 0 showdb 254 289 80 28 {d09ab184-f4b1-4340-8730-e73b870bcf9a} true 0 0 raw amps 0 dB 1 1 false inchnl 105 290 47 27 {2fce6993-35f8-4dd7-bedd-f809c0803989} true 0 0 right DejaVu Sans 14 0 0 0 255 255 255 1.00000000 1 1e+12 false 1 5 289 102 29 {df02308f-b978-4275-817e-1ad38c5b30d5} true 0 0 right top Nimbus Sans L 14 3 0 0 0 255 255 255 noborder 1 0