;Written by Iain McCurdy, 2009 ;Modified for QuteCsound by René, October 2010, updated Feb 2011 ;Tested on Ubuntu 10.04 with csound-float 5.13.0 and QuteCsound svn rev 817 ;An INIT preset is called at the start of csound (inst 2), a valid (mono or stereo) audio file have to be selected and preset 0 saved before playing. ; Notes on modifications from original csd: ; Add Browser for audio files, accept mono or stereo wav files ; Instrument 1 is activated by MIDI and by the GUI ; Removed Recording instrument, included in QuteCsound ; Removed "Instructions and Info Panel" for the gui to fit in a 1200x800 screen ; syncloop ; -------------------------------------------------------------------------------------------------------------------------- ; syncloop is a variation of syncgrain. For further information see the example for syncgrain first. ; syncloop adds start and end loop points which are k-rate and therefore editable in real-time. ; Some additional functions have been added that are not part of the original opcode: 'Grain Density' (frequency) and ; 'Grain Size' can be randomized. The 'Freeze' button sets the 'Progress Rate' slider to zero thereby effectively ; freezing progress through the sound file. The 'Loop Start & End' slider controls both the 'Loop Start' and 'Loop End' ; sliders simultaneously - the loop size will be zero so again progress through the sound file will be zero, the difference ; this time is that the user can freely define where within the sound file freezing is taking place. ; This instrument can also be activated via MIDI. MIDI pitch values can be mapped to grain density and/or to the pitch of ; the material contained within the grain. In the latter mapping option, middle C is the point at which no ; transposition occurs. Using MIDI activation, polyphony is possible. ; The attack and release times of an amplitude envelope applied to the entire grain cloud can be modulated by the user using ; two on screen sliders. These controls are probably most useful when triggering this example via MIDI. ;my flags on Ubuntu: -odac -b1024 -B2048 -+rtaudio=alsa -+rtmidi=alsa -Ma -m0 --midi-key-oct=4 --midi-velocity-amp=5 --env:SSDIR+=../../SourceMaterials sr = 44100 ;SAMPLE RATE ksmps = 100 ;NUMBER OF AUDIO SAMPLES IN EACH CONTROL CYCLE nchnls = 2 ;NUMBER OF CHANNELS (2=STEREO) 0dbfs = 1 ;MAXIMUM AMPLITUDE REGARDLESS OF BIT DEPTH ; WINDOWING FUNCTIONS USED TO DYNAMICALLY SHAPE THE GRAINS ; NUM | INIT_TIME | SIZE | GEN_ROUTINE | PARTIAL_NUM | STRENGTH | PHASE ;GRAIN ENVELOPE WINDOW FUNCTION TABLES: giwfn1 ftgen 0, 0, 131072, 9, .5, 1, 0 ; HALF SINE giwfn2 ftgen 0, 0, 131072, 7, 0, 3072, 1, 128000, 0 ; PERCUSSIVE - STRAIGHT SEGMENTS giwfn3 ftgen 0, 0, 131072, 5, .001, 3072, 1, 128000, .001 ; PERCUSSIVE - EXPONENTIAL SEGMENTS giwfn4 ftgen 0, 0, 131072, 7, 0, 1536, 1, 128000, 1, 1536, 0 ; GATE - WITH DE-CLICKING RAMP UP AND RAMP DOWN SEGMENTS giwfn5 ftgen 0, 0, 131072, 7, 0, 128000, 1, 3072, 0 ; REVERSE PERCUSSIVE - STRAIGHT SEGMENTS giwfn6 ftgen 0, 0, 131072, 5, .001, 128000, 1, 3072, .001 ; REVERSE PERCUSSIVE - EXPONENTIAL SEGMENTS instr 10 ;GUI ktrig metro 10 if (ktrig == 1) then gkMIDItoDensity invalue "MtoDensity" gkMIDItoPitch invalue "MtoPitch" gkwfn invalue "Grain_Envelope" kfreeze invalue "Freeze" gkamp invalue "Amplitude" gkfreq invalue "Density" gkpitch invalue "Pitch" gkprate invalue "Progress_Rate" if kfreeze=1 then outvalue "Progress_Rate",0 endif gkgrsize invalue "Grain_Size" gklstart invalue "Loop_Start" gklend invalue "Loop_End" knoloop invalue "Loop_Start_End" ktrigloop changed knoloop if ktrigloop=1 then outvalue "Loop_Start", knoloop outvalue "Loop_End", knoloop endif gkfreqOS invalue "Grain_Density_Offset" gksizeOS invalue "Grain_Size_Rnd_Offset" gkAtt invalue "Attack_Time" gkRel invalue "Release_Time" ;AUDIO FILE CHANGE / LOAD IN TABLES ****************************************************************************************************** ;Have put all this stuff in instr 10 to reduce the respons time when playing with midi Sfile_new strcpy "" ;INIT TO EMPTY STRING Sfile invalue "_Browse" Sfile_old strcpyk Sfile_new Sfile_new strcpyk Sfile kfile strcmpk Sfile_new, Sfile_old gkfile_new init 0 if kfile != 0 then ;IF A BANG HAD BEEN GENERATED IN THE LINE ABOVE gkfile_new = 1 ;Flag to inform instr 1 that a new file is loaded reinit NEW_FILE ;REINITIALIZE FROM LABEL 'NEW_FILE' endif NEW_FILE: ;FUNCTION TABLES NUMBERS OF THE SOUND FILE THAT WILL BE GRANULATED ichn filenchnls Sfile if ichn == 1 then giFileL ftgentmp 0, 0, 0, 1, Sfile, 0, 0, 1 ;READ MONO AUDIO FILE CHANNEL 1 giFileR = giFileL else giFileL ftgentmp 0, 0, 0, 1, Sfile, 0, 0, 1 ;READ STEREO AUDIO FILE CHANNEL 1 giFileR ftgentmp 0, 0, 0, 1, Sfile, 0, 0, 2 ;READ STEREO AUDIO FILE CHANNEL 2 endif ;******************************************************************************************************************************************* endif endin instr 1 ;SYNCLOOP INSTRUMENT if p4!=0 then ;MIDI ioct = p4 ;READ OCT VALUE FROM MIDI INPUT icps = cpsoct(ioct) ;CONVERT TO CPS ;PITCH BEND============================================================================================================ iSemitoneBendRange = 12 ;PITCH BEND RANGE IN SEMITONES imin = 0 ;EQUILIBRIUM POSITION imax = iSemitoneBendRange * .0833333 ;MAX PITCH DISPLACEMENT (IN oct FORMAT) kbend pchbend imin, imax ;PITCH BEND VARIABLE (IN oct FORMAT) kcps = cpsoct(ioct + kbend) ;SET FUNDAMENTAL ;======================================================================================================================= endif iporttime = 0.1 ;PORTAMENTO TIME kporttime linseg 0,0.01,iporttime,1,iporttime ;USE OF A RAMPING UP ENVELOPE PREVENTS GLIDING PARAMETERS EACH TIME A NOTE IS RESTARTED if p4!=0 && gkMIDItoPitch=1 then ;IF THIS IS A MIDI ACTIVATED NOTE AND MIDI-TO-PITCH SWITCH IS ON... kpitch = kcps/cpsoct(8) ;MAP TO MIDI NOTE VALUE TO PITCH (CONVERT TO RATIO: MIDDLE C IS POINT OF UNISON) else ;OTHERWISE... kpitch portk gkpitch, kporttime ;USE THE SLIDER VALUE endif ;END OF THIS CONDITIONAL BRANCH if p4!=0 && gkMIDItoDensity=1 then ;IF THIS IS A MIDI ACTIVATED NOTE AND MIDI-TO-PITCH SWITCH IS ON... kfreq = icps else ;OTHERWISE... kfreq portk gkfreq, kporttime ;USE THE GUI SLIDER VALUE endif ;END OF THIS CONDITIONAL BRANCH if gkfile_new = 1 then ;test if a new file is loaded by instr 10 gkfile_new = 0 ;flag to zero for next file change reinit UPDATE ;REINITIALIZE FROM LABEL 'UPDATE' endif kSwitch changed gkwfn ;GENERATE A MOMENTARY '1' PULSE IN OUTPUT 'kSwitch' IF ANY OF THE SCANNED INPUT VARIABLES CHANGE. (OUTPUT 'kSwitch' IS NORMALLY ZERO) if kSwitch=1 then ;IF I-RATE VARIABLE IS CHANGED... reinit UPDATE ;BEGIN A REINITIALISATION PASS IN ORDER TO EFFECT THIS CHANGE. BEGIN THIS PASS AT LABEL ENTITLED 'UPDATE' AND CONTINUE UNTIL rireturn OPCODE endif ;END OF CONDITIONAL BRANCHING UPDATE: ;LABEL iwfn = giwfn1 + i(gkwfn) ;FUNCTION TABLE NUMBER FOR WINDOWING FUNCTION IS DERIVED ilen = nsamp(giFileL)/sr ;LENGTH OF THE SOURCE SOUND FILE iolaps = 1000 ;NUMBER OF ALLOWED OVERLAPS - BEST JUST TO GIVE THIS A LARGE CONSTANT VALUE kfreqOS rand gkfreqOS ;kfreqOS IS A K-RATE RANDOM NUMBER WITHIN THE RANGE -gkfreqOS TO gkfreqOS kfreqOS = kfreqOS * gkfreq ;kfreqOS IS REDEFINED RELATIVE TO THE CURRENT VALUE OF gkfreq kgrsizeOS gauss gksizeOS ;kgrsizeOS IS A K-RATE GAUSSIAN RANDOM NUMBER WITHIN THE RANGE -gksizeOS TO gksizeOS kgrsizeOS = kgrsizeOS * gkgrsize ;kgrsizeOS IS REDEFINED RELATIVE TO THE CURRENT VALUE OF gkgrsize asigL syncloop gkamp, kfreq+kfreqOS, kpitch, gkgrsize+kgrsizeOS, gkprate, gklstart*ilen, gklend*ilen, giFileL, iwfn, iolaps ;LEFT CHANNEL GRANULAR SYNTHESIS asigR syncloop gkamp, kfreq+kfreqOS, kpitch, gkgrsize+kgrsizeOS, gkprate, gklstart*ilen, gklend*ilen, giFileR, iwfn, iolaps ;RIGHT CHANNEL GRANULAR SYNTHESIS aenv expsegr 0.0001,i(gkAtt),1,i(gkRel),0.0001 ;CLOUD AMPLITUDE ENVELOPE asigL = asigL * aenv ;APPLY AMPLITUDE ENVELOPE asigR = asigR * aenv ;APPLY AMPLITUDE ENVELOPE outs asigL, asigR ;SEND AUDIO TO OUTPUTS rireturn ;DENOTES THE END OF A REINITIALISATION PASS endin instr 2 ;INIT outvalue "_SetPresetIndex", 0 endin ;INSTR | START | DURATION i 10 0 3600 ;GUI i 2 0.1 0 ;INIT 332 339 1033 404 true 241 226 185 2 2 1028 402 {aa607456-d368-4d59-8497-d16d608404c3} true 0 0 center Liberation Sans 18 3 255 255 255 0 85 0 border 5 2 8 8 120 30 {55273d97-d39a-441c-8da6-87ea139493b6} true 0 0 event 1.00000000 On / Off (MIDI) / i1 0 -1 true false 547 117 144 30 {4e98b9a0-55c5-4aef-b473-1d0b761def5d} true 0 0 right Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 _Browse 521 62 170 30 {1757a18f-b418-4ef1-984d-bdee5e985805} true 0 0 value 1.00000000 ClassicalGuitar.wav Browse Audio File / false false _Browse 691 63 330 28 {804f4f24-03f1-4ac2-8ba2-697f15df06cf} true 0 0 left Liberation Sans 10 3 0 0 0 229 229 229 nobackground 521 41 120 30 {a918d231-4dd1-4893-81ef-bf453535bda6} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 8 106 180 30 {93f3e274-799d-49e8-9392-4d8c6ad43ae2} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Density 8 83 500 27 {89741b38-8333-4828-b8b8-656cff90d564} true 0 0 1.00000000 200.00000000 40.00400162 lin continuous -1.00000000 false Density 448 106 60 30 {d1569d45-d5fa-452c-b69f-0b3e0b47cd01} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 8 151 180 30 {9d200fd9-5a42-4f87-89ab-14ef5ac064ba} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Pitch 8 128 500 27 {ecd7a8b0-5bb3-4479-b692-e56294223499} true 0 0 -2.00000000 2.00000000 1.00000000 lin continuous -1.00000000 false Pitch 448 151 60 30 {f275c8fd-3605-49e8-9090-67ca5f21a9f6} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 8 64 200 30 {5cde19f3-b356-4945-9c8b-43dd67c604dd} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Amplitude 8 41 500 27 {d6d73a88-8d82-47de-a067-758f1917a3f2} true 0 0 0.00000000 1.00000000 0.50000000 lin continuous -1.00000000 false Amplitude 448 64 60 30 {073ad371-9227-46fa-a005-ac10a210db79} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 514 318 510 80 {b83b9228-3958-46d8-adf5-262b04a121c6} true 0 0 left Liberation Sans 14 3 255 255 255 255 255 255 border 1 1 519 364 180 30 {489dc531-f476-4ca3-b56d-f970ee2c49aa} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Attack_Time 519 341 250 27 {dbdbe7cb-a74d-45b2-9e47-e5c0594f3ea5} true 0 0 0.05000000 10.00000000 0.05000000 lin continuous -1.00000000 false Attack_Time 709 364 60 30 {2aea18ca-8a9a-457a-8de3-cb7c918a4e3b} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 Release_Time 959 364 60 30 {ea7f84c3-e2da-4b1c-a79e-61c2a264d20c} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 Release_Time 769 341 250 27 {cb664a0c-f84b-41f5-92e9-7deb5a672d56} true 0 0 0.05000000 10.00000000 0.05000000 lin continuous -1.00000000 false 769 364 180 30 {7a8b8cb8-12e8-415d-b11c-fb0d460c9e6f} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 519 192 200 30 {b6afbe1e-677c-44a6-ba84-4c32dc21b0a7} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Grain_Density_Offset 519 169 500 27 {859d1ded-b337-4ee7-ac9b-48a1b5f77d16} true 0 0 0.00000000 1.00000000 0.47200000 lin continuous -1.00000000 false Grain_Density_Offset 959 192 60 30 {95f6cb02-77ec-46fe-876b-a93efac3c5e5} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 8 239 180 30 {7fd47947-fd0d-4964-85e5-682fed1916c7} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Grain_Size 8 216 500 27 {475cdd64-a4ca-4ebc-a000-90448e932478} true 0 0 0.00100000 1.00000000 0.05095000 lin continuous -1.00000000 false Grain_Size 448 239 60 30 {dc75acc2-c015-4b8d-8fb2-467b9fb96d42} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 519 234 180 30 {1b53998a-9e9e-4067-8cee-83fc9f2f657b} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Grain_Size_Rnd_Offset 519 212 500 27 {4bea56a5-96d7-49d0-90a1-6d1dd17fc584} true 0 0 0.00000000 1.00000000 0.34400001 lin continuous -1.00000000 false Grain_Size_Rnd_Offset 959 234 60 30 {225c97bc-f9f0-4085-94c4-f8de353f00cc} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 8 193 200 30 {3ebc1139-1fdb-48dc-9150-49ee8dfdbd4a} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Progress_Rate 8 170 500 27 {2cf97843-5b49-438e-8034-62a459597e86} true 0 0 -2.00000000 2.00000000 1.21599996 lin continuous -1.00000000 false Progress_Rate 448 193 60 30 {e54486c5-f9aa-4d0e-9a67-6506dae3c790} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 Grain_Envelope 691 114 330 30 {5041c02a-c619-488c-8631-a3f9e1dab457} true 0 0 Half Sine 0 Percussive (straight segments) 1 Percussive (exponential segments) 2 Gate (with anti click ramp up and ramp down) 3 Reverse Percussive (straight segments) 4 Reverse Percussive (exponential segments) 5 0 false 514 266 510 50 {f083eb01-c7ae-40f6-9ac6-d7b32117fedd} true 0 0 left Liberation Sans 14 3 255 255 255 255 255 255 border 1 1 589 277 100 28 {9f6f0612-5f3c-4a18-9f40-6fb9ece10c64} true 0 0 right Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 721 280 100 28 {18bdb5ba-84f0-4561-8771-6792fe1b971f} true 0 0 right Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 MtoDensity 695 279 16 16 {c64b625e-c396-4f39-89fb-9c21b77affdd} true 0 0 value 1.00000000 / true false MtoPitch 828 281 16 16 {cdf66a23-91bd-4fe3-987f-dfc531c78348} true 0 0 value 1.00000000 / true false Freeze 325 192 80 20 {bae47aa6-1bde-4095-a40b-9bcfbcd66ed7} true 0 0 value 1.00000000 Freeze / true false 8 282 180 30 {c7f4893b-23ca-462a-9571-7ab00cc93df6} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Loop_Start 8 259 500 27 {b62d8847-6c0f-4256-860d-9821a6c4239c} true 0 0 0.00000000 1.00000000 0.00000000 lin continuous -1.00000000 false Loop_Start 448 282 60 30 {414ea7ab-a404-4ce7-87c2-d497d5418a48} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 8 370 180 30 {7c775281-dbc8-458d-8238-3e238a8be1af} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Loop_Start_End 8 347 500 27 {26b5fca1-223c-47a0-9e32-c13f75fbd052} true 0 0 0.00000000 1.00000000 0.00000000 lin continuous -1.00000000 false Loop_Start_End 448 370 60 30 {2b2822a5-8d03-4afc-865d-99d507e2d67d} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 8 324 200 30 {20da31e5-347c-42e8-a268-b138729678c9} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Loop_End 8 301 500 27 {4ac79c7a-2bc9-4f1f-9b11-43a9a76a621a} true 0 0 0.00000000 1.00000000 1.00000000 lin continuous -1.00000000 false Loop_End 448 324 60 30 {181add3e-7ccb-4b14-9b0a-575356b71721} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 0.00000000 0 ClassicalGuitar.wav ClassicalGuitar.wav 40.00400162 40.00400162 40.004 1.00000000 1.00000000 1.000 0.50000000 0.50000000 0.500 0.05000000 0.05000000 0.050 0.05000000 0.050 0.05000000 0.47200000 0.47200000 0.472 0.05095000 0.05100000 0.051 0.34400001 0.34400001 0.344 1.21599996 1.21599996 1.216 0.00000000 0.00000000 0 0.00000000 0 0.00000000 0 0.00000000 0.00000000 0.000 0.00000000 0.00000000 0.000 1.00000000 1.00000000 1.000