;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 3), a valid (mono or stereo) audio file have to be selected and preset 0 saved before playing. ;Notes on modifications from original csd: ; Add table(s) for exp slider ; Add Browser for audio files and power of two tables for fog opcode, accept mono or stereo wav files ; Instrument 1 is activated by MIDI and by the GUI, added pitchbend ; Removed Recording instr, included in QuteCsound ; Removed "Instructions and Info Panel" for the gui to fit in a 1200x800 screen ; fog ; ----------------------------------------------------------------------------------------------------------------------------- ; Fog performs granular synthesis upon sound material stored within a GEN 1 function table using a method that bears many ; simularities to the fof opcode. ; The amplitude envelope that is applied to each grain is controlled by a combination of the 'Grain Duration' (kdur), ; 'Grain Rise Time' (kris), 'Grain Decay Time' (kdec) and 'Bandwidth (Grain Exponential Decay)' (kband). Bandwidth ; controls how an exponential curve defined in a separate function is applied to the decay of each grain. ; 'Pointer' (aspd) defines the point from within the stored sound file (range 0-1) that grains will be read. ; 'Density' (kdens) controls the frequency of grain duration in grains per second. ; 'Transposition Factor' (ktrans) controls the transposition of each grain. 1=no transposition. 2=up one octave. 0.5=down one ; octave and so on... A minus sign indicates that grains will be played backwards, the numerical part functions in the same way. ; 'Transposition Mode' (itmode) is a 2-way switch defining whether transposition modulation is carried out on a grain by ; grain basis or continually, even within the duration of a grain. To appreciate this effect first create a texture with ; large sparse grains. ; 'Octaviation Index' (koct) is typically zero but as it tends to 1 every other grain is increasingly attenuated. When it is ; exactly 1 the grain density is effectively halved. From 1 to 2 the process is repeated and the density is halved again and ; so on from 2 to 3 and beyond. This effect is perceived quite differently for dense and sparse textures. ; In this example a controllable amount of portamento is applied to 'Pointer', 'Transposition Factor' and 'Density' in ; order to allow smoother changes of their values. A random factor can be multiplied to the density control to ; prevent the tone produced through synchronous reiteration of identical grains. Random factors can simlarly be multiplied ; to grain pointer location and bandwidth (the consequence of which is grain duration). ; 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 ; CURVE USED TO FORM ATTACK AND DECAY PORTIONS OF EACH GRAIN ; NUM | INIT_TIME | SIZE | GEN_ROUTINE | PARTIAL_NUMBER_1 | STRENGTH_1 | PHASE_1 | DC_OFFSET_1 giattdec ftgen 0, 0, 524288, 19, 0.5, 0.5, 270, 0.5 ; I.E. A RISING SIGMOID giExp400 ftgen 0, 0, 129, -25, 0, 0.5 , 128, 400.0 ;TABLE FOR EXP SLIDER giExp2 ftgen 0, 0, 129, -25, 0, 0.01, 128, 2.0 ;TABLE FOR EXP SLIDER instr 10 ;GUI ktrig metro 10 if (ktrig == 1) then gktransmode invalue "TransMode" gkMIDIToTrans invalue "MIDItoTrans" gkMIDIToDens invalue "MIDItoDens" gkoct invalue "Octaviation_Index" gktrans invalue "Transposition_Factor" gkTransRnd invalue "Trans_Random" kdur invalue "Grain_Duration" gkdur tablei kdur, giExp2, 1 outvalue "Grain_Duration_Value", gkdur gkris invalue "Grain_Rise_Time" gkdec invalue "Grain_Decay_Time" gkband invalue "Bandwidth" gkamp invalue "Amplitude" gkporttime invalue "Portamento_Amount" gkPtrRnd invalue "Pointer_Randomness" gkDensRnd invalue "Density_Randomness" gkBwRnd invalue "Bandwidth_Randomness" gkAtt invalue "Attack_Time" gkRel invalue "Release_Time" gkptr invalue "X_Pointer" kdens invalue "Y_Density" gkdens tablei kdens, giExp400, 1 outvalue "Y_Density_Value", gkdens ;AUDIO FILE CHANGE / LOAD IN POWER OF 2 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 "_Browse1" 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: ;fog accept only power of 2 table size ifnTemp ftgentmp 0, 0, 0, 1, Sfile, 0, 0, 1 ;Temporary table to get the audio file size iftlen = ftlen(ifnTemp) ;file size iftlen2 pow 2, ceil(log(iftlen)/log(2)) ;high nearest power of two table size ;FUNCTION TABLES NUMBERS OF THE SOUND FILE THAT WILL BE GRANULATED ichn filenchnls Sfile if ichn == 1 then gifnSoundFileL ftgentmp 0, 0, iftlen2, 1, Sfile, 0, 0, 1 ;READ MONO AUDIO FILE CHANNEL 1 gifnSoundFileR = gifnSoundFileL else gifnSoundFileL ftgentmp 0, 0, iftlen2, 1, Sfile, 0, 0, 1 ;READ STEREO AUDIO FILE CHANNEL 1 gifnSoundFileR ftgentmp 0, 0, iftlen2, 1, Sfile, 0, 0, 2 ;READ STEREO AUDIO FILE CHANNEL 2 endif ;******************************************************************************************************************************************* endif endin instr 1 ;FOG INSTRUMENT if p4!=0 then ;MIDI ioct = p4 ;READ OCT VALUE FROM MIDI INPUT ;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 FUNDEMENTAL ;======================================================================================================= endif kporttime linseg 0,0.001,1 ;CREATE A VARIABLE FUNCTION THAT RAPIDLY RAMPS UP TO A SET VALUE kporttime = kporttime * gkporttime ;SLIDER FOR PORTAMENTO TIME MULTIPLIED TO kporttime FUNCTION if p4!=0 && gkMIDIToTrans=1 then ;IF THIS IS A MIDI ACTIVATED NOTE AND MIDI-TO-PITCH SWITCH IS ON... ktrans = kcps/cpsoct(8) ;MAP TO MIDI NOTE VALUE TO PITCH (CONVERT TO RATIO: MIDDLE C IS POINT OF UNISON) else ;OTHERWISE... ktrans portk gktrans, kporttime ;USE THE SLIDER VALUE endif ;END OF THIS CONDITIONAL BRANCH if p4!=0 && gkMIDIToDens=1 then ;IF THIS IS A MIDI ACTIVATED NOTE AND MIDI-TO-PITCH SWITCH IS ON... kdens = kcps ;MAP TO MIDI NOTE VALUE TO GRAIN DENSITY else ;OTHERWISE... kdens portk gkdens, kporttime ;PORTAMENTO IS APPLIED TO SMOOTH VALUE CHANGES VIA THE SLIDERS endif ;END OF THIS CONDITIONAL BRANCH kDensRnd random 0, gkDensRnd ;DENSITY RANDOMNESS FUNCTION kdens = kdens * (1 + kDensRnd) ;APPLY RANDOMNESS kTransRnd random -gkTransRnd,gkTransRnd ktrans = ktrans * (cpsoct(8+kTransRnd)/cpsoct(8)) ifnRiseDecayShape= giattdec ;FUNCTION TABLE THAT DEFINES THE SHAPE OF THE ATTACK AND DECAY OF EACH GRAIN 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 NEW_FILE1 ;REINITIALIZE FROM LABEL 'NEW_FILE1' endif NEW_FILE1: kPtrRnd random -gkPtrRnd,gkPtrRnd ;DETERMINE POINTER RANDOM FACTOR kptr = gkptr + kPtrRnd ;ADD POINTER RANDOM FACTOR kptr wrap kptr, 0, 1 ;WRAP AROUND OUT OF RANGE POINTER VALUES kptr = kptr * (nsamp(gifnSoundFileL)/ftlen(gifnSoundFileL)) ;KPTR VALUE IS RESCALED TO ACCOUNT FOR UNUSED SAMPLES IN THE FUNCTION TABLE kBwRnd random 0,gkBwRnd ;DETERMINE BANDWIDTH RANDOM FACTOR kband = gkband * (1 + kBwRnd) ;BANDWIDTH RANDOM FACTOR IS APPLIED iNumOverLaps = 2000 ;MAXIMUM NUMBER OF OVERLAPS (OVERESTIMATATION IS A GOOD IDEA HERE TO PREVENT CRASHES) itotdur = 3600 ;JUST PUT A LARGE NUMBER HERE FOR REAL TIME - FOR NON-REAL TIME GIVE IT p3 kptr portk kptr, kporttime ;PORTAMENTO IS APPLIED TO SMOOTH VALUE CHANGES VIA THE SLIDERS aptr interp kptr ;A NEW A-RATE VARIABLE (aptr) IS CREATED BASE ON kptr kSwitch changed gktransmode ;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 CHANGE TRIGGER IS '1'... reinit START ;BEGIN A REINITIALISATION PASS FROM LABEL 'START' endif START: ; OPTIONAL-> aL fog gkamp, kdens, ktrans, aptr, gkoct, kband, gkris, gkdur, gkdec,iNumOverLaps, gifnSoundFileL, ifnRiseDecayShape, itotdur, 0, i(gktransmode), 1 aR fog gkamp, kdens, ktrans, aptr, gkoct, kband, gkris, gkdur, gkdec,iNumOverLaps, gifnSoundFileR, ifnRiseDecayShape, itotdur, 0, i(gktransmode), 1 aenv expsegr 0.0001,i(gkAtt),1,i(gkRel),0.0001 ;CLOUD AMPLITUDE ENVELOPE aL = aL * aenv ;APPLY AMPLITUDE ENVELOPE aR = aR * aenv ;APPLY AMPLITUDE ENVELOPE rireturn ;RETURN TO PERFORMANCE TIME PASSES outs aL, aR endin instr 2 ;SETS TRANSPOSITION SLIDER TO UNISON outvalue "Transposition_Factor", 1 endin instr 3 ;INIT outvalue "_SetPresetIndex", 0 endin ;INSTR | START | DURATION i 10 0 3600 ;GUI i 3 0.1 0 ;INIT 332 307 1033 437 true 241 226 185 2 2 1027 430 {aa607456-d368-4d59-8497-d16d608404c3} true 0 0 center Liberation Sans 18 3 255 255 255 0 85 0 border 5 2 X_Pointer 84 170 424 254 {2fef8117-9c62-4419-b6e0-45ef9c15647f} true 0 0 Y_Density 0.00000000 1.00000000 0.00000000 1.00000000 0.29952830 0.55511808 crosshair 1 0.00000000 jump 0 234 0 false 0 0 0 8 8 120 30 {55273d97-d39a-441c-8da6-87ea139493b6} true 0 0 event 1.00000000 On / Off (MIDI) / i1 0 -1 true false 5 185 80 30 {30475d51-935d-4644-ba93-d6629f3be4d1} true 0 0 center Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 X_Pointer 25 215 60 30 {6aac1f5d-f8dd-4e66-a987-3203fd01550f} true 0 0 center Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 Y_Density_Value 26 301 60 30 {ee3fa20e-dea1-4af9-97d7-f7ba52bdd47c} true 0 0 center Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 6 272 80 30 {653f7154-d6f2-44e6-8ea9-eacbd5ac2206} true 0 0 center Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 522 64 180 30 {8d67138b-037d-461e-8a25-108f849b03c2} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Octaviation_Index 522 41 500 27 {273087bd-e04a-4975-a3bd-5b0a8b5fd40a} true 0 0 0.00000000 4.00000000 0.00000000 lin continuous -1.00000000 false Octaviation_Index 962 64 60 30 {04617e86-7abe-4120-bb9b-1d6ccd2f0983} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 522 106 180 30 {2c02703f-de38-40de-bc69-7c787c5a13b3} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Transposition_Factor 522 83 250 27 {513ad202-18da-4809-8de3-7ddf8934ab6f} true 0 0 -2.00000000 2.00000000 1.00000000 lin continuous -1.00000000 false Transposition_Factor 712 106 60 30 {530115a7-4f85-423e-99b4-263da41530d9} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 Trans_Random 962 106 60 30 {bc7ab1c8-85da-4714-889e-9a193e4c685c} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 Trans_Random 772 83 250 27 {c4ac15bd-d639-4a18-834e-ece88a35c131} true 0 0 0.00000000 1.00000000 0.12800001 lin continuous -1.00000000 false 772 106 180 30 {4e98b9a0-55c5-4aef-b473-1d0b761def5d} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 TransMode 8 69 150 30 {6ebe0e67-d0ca-49fd-a0d9-32b697a0edf6} true 0 0 Grain by Grain 0 Continuous 1 0 false 210 65 180 30 {8076c47e-867b-4e8a-95a9-c1772cb80c76} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Portamento_Amount 210 41 300 27 {8dcbe622-f7e5-4530-906d-8d6aa856447d} true 0 0 0.00000000 1.00000000 0.10000000 lin continuous -1.00000000 false Portamento_Amount 450 65 60 30 {e5491687-e126-474a-960e-2766ba77fd36} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 MIDItoTrans 367 98 16 16 {436e46c5-37b6-49af-acef-33f5e00d9103} true 0 0 value 1.00000000 / true false MIDItoDens 494 98 16 16 {51ecf2a3-121a-4a0d-ab70-927ecf226c69} true 0 0 value 1.00000000 / true false 386 95 107 30 {1a69252b-465f-4ed7-9476-96031257eff6} true 0 0 right Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 246 95 120 30 {01925294-4552-42e4-9703-fd80721fb15d} true 0 0 right Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 _Browse1 6 133 170 30 {1757a18f-b418-4ef1-984d-bdee5e985805} true 0 0 value 1.00000000 ClassicalGuitar.wav Browse Audio File / false false _Browse1 177 134 330 28 {804f4f24-03f1-4ac2-8ba2-697f15df06cf} true 0 0 left Liberation Sans 10 3 0 0 0 229 229 229 nobackground 8 112 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 48 141 30 {942cd7e3-7e3c-4329-a2c5-0c58c18c92f1} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 522 148 180 30 {93f3e274-799d-49e8-9392-4d8c6ad43ae2} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Grain_Duration 522 125 500 27 {89741b38-8333-4828-b8b8-656cff90d564} true 0 0 0.00000000 1.00000000 0.43399999 lin continuous -1.00000000 false Grain_Duration_Value 962 148 60 30 {d1569d45-d5fa-452c-b69f-0b3e0b47cd01} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 522 190 180 30 {c477956e-c0ae-4443-b2a2-2803dc72675d} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Grain_Rise_Time 522 167 250 27 {b7a6264a-85ce-44c6-912c-79f97f9c0438} true 0 0 0.00100000 0.20000000 0.00975600 lin continuous -1.00000000 false Grain_Rise_Time 712 190 60 30 {fbb46c2d-6796-4219-b7a6-cc1ac14fe06d} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 Grain_Decay_Time 962 190 60 30 {dd67952d-a404-436b-8b6b-059d2e045177} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 Grain_Decay_Time 772 167 250 27 {25a7c11d-88d3-4c17-b1dd-d8711549b868} true 0 0 0.00100000 0.20000000 0.00975600 lin continuous -1.00000000 false 772 190 180 30 {19201594-5781-4b62-8e65-3fc781995272} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 653 104 60 18 {b19b7b33-b8aa-418a-87c7-c4ddac31fa91} true 0 0 event 1.00000000 Unison / i 2 0 0 false false 522 232 220 30 {9d200fd9-5a42-4f87-89ab-14ef5ac064ba} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Bandwidth 522 209 500 27 {ecd7a8b0-5bb3-4479-b692-e56294223499} true 0 0 0.00000000 100.00000000 10.00000000 lin continuous -1.00000000 false Bandwidth 962 232 60 30 {f275c8fd-3605-49e8-9090-67ca5f21a9f6} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 522 274 180 30 {5cde19f3-b356-4945-9c8b-43dd67c604dd} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Amplitude 522 251 500 27 {d6d73a88-8d82-47de-a067-758f1917a3f2} true 0 0 0.00000000 1.00000000 0.20000000 lin continuous -1.00000000 false Amplitude 962 274 60 30 {073ad371-9227-46fa-a005-ac10a210db79} true 0 0 right Liberation Sans 9 3 255 255 255 255 255 255 noborder 1 1 522 316 165 30 {413875db-1edf-4539-a48c-88a7383d05d2} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Pointer_Randomness 522 293 165 27 {ba02bcd6-e35d-47ad-bd59-85fad031ff58} true 0 0 0.00000000 1.00000000 0.00000000 lin continuous -1.00000000 false 689 316 165 30 {5df757ba-9e72-4f16-88d9-be4628916fa1} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Density_Randomness 689 293 165 27 {b3318f74-1f15-4f2e-a4af-3377eda94fd1} true 0 0 0.00000000 3.00000000 0.00000000 lin continuous -1.00000000 false 856 316 165 30 {a50ed81d-3863-4375-88a6-04642daf5af5} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 Bandwidth_Randomness 856 293 165 27 {9a48da69-c3f9-4d43-b89c-d0fc159e0710} true 0 0 0.00000000 3.00000000 0.00000000 lin continuous -1.00000000 false 522 344 500 80 {b83b9228-3958-46d8-adf5-262b04a121c6} true 0 0 left Liberation Sans 14 3 255 255 255 255 255 255 border 1 1 522 390 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 522 367 250 27 {dbdbe7cb-a74d-45b2-9e47-e5c0594f3ea5} true 0 0 0.05000000 10.00000000 0.05000000 lin continuous -1.00000000 false Attack_Time 712 390 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 962 390 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 772 367 250 27 {cb664a0c-f84b-41f5-92e9-7deb5a672d56} true 0 0 0.05000000 10.00000000 0.05000000 lin continuous -1.00000000 false 772 390 180 30 {7a8b8cb8-12e8-415d-b11c-fb0d460c9e6f} true 0 0 left Liberation Sans 10 3 255 255 255 255 255 255 noborder 1 1 0.29952830 0.55511808 0.00000000 0 0.30000001 0.300 20.44374847 20.444 0.00000000 0.00000000 0.000 1.00000000 1.00000000 1.000 0.12800001 0.128 0.12800001 0.00000000 0.10000000 0.10000000 0.100 0.00000000 0 0.00000000 0 ClassicalGuitar.wav ClassicalGuitar.wav 0.43399999 0.09970991 0.100 0.00975600 0.01000000 0.010 0.01000000 0.010 0.00975600 0 10.00000000 10.00000000 10.000 0.20000000 0.20000000 0.200 0.00000000 0.00000000 0.00000000 0.05000000 0.05000000 0.050 0.05000000 0.050 0.05000000