--midi-key=4 --midi-velocity-amp=5 -m128
--env:SSDIR+=../../SourceMaterials
/***Imitative Additive Synthesis***/
;example for CsoundQt
;joachim heintz mar 2011
sr = 44100
ksmps = 128
nchnls = 1
0dbfs = 1
;============================================================================;
;============================================================================;
; GENERAL VALUES FOR FOURIER TRANSFORMATION ;
;============================================================================;
;============================================================================;
gifftsiz = 1024 ;fft size
giovlap = gifftsiz / 4 ;overlap
giwinsiz = gifftsiz * 2 ;window size
giwintyp = 1 ;von hann window
giamps ftgen 0, 0, gifftsiz/2+1, 2, 0 ;table for bin amps
giampcpy ftgen 0, 0, gifftsiz/2+1, 2, 0 ;needed for the TabIndxNMax_k opcode
gifreqs ftgen 0, 0, gifftsiz/2+1, 2, 0 ;table for bin freqs
gimaxindc ftgen 0, 0, gifftsiz/2+1, 2, 0 ;indices for temporal maxima of giamps
gisine ftgen 0, 0, 2^10, 10, 1
massign 0, 10 ;send all midi key events to instr 10
chn_S "_MBrowse", 1 ;declare string channel
;============================================================================;
;============================================================================;
; FUNCTIONS (USER DEFINED OPCODES) ;
;============================================================================;
;============================================================================;
;OPCODES FOR USING STRINGS AS ARRAYS
;(see http://www.csounds.com/journal/issue13/StringsAsArrays.html)
opcode StrayGetEl, ii, Sijj
;returns the startindex and the endindex (= the first space after the element) for ielindex in String. if startindex returns -1, the element has not been found
Stray, ielindx, isepA, isepB xin
;DEFINE THE SEPARATORS
isep1 = (isepA == -1 ? 32 : isepA)
isep2 = (isepA == -1 && isepB == -1 ? 9 : (isepB == -1 ? isep1 : isepB))
Sep1 sprintf "%c", isep1
Sep2 sprintf "%c", isep2
;INITIALIZE SOME PARAMETERS
ilen strlen Stray
istartsel = -1; startindex for searched element
iendsel = -1; endindex for searched element
iel = 0; actual number of element while searching
iwarleer = 1
indx = 0
if ilen == 0 igoto end ;don't go into the loop if Stray is empty
loop:
Snext strsub Stray, indx, indx+1; next sign
isep1p strcmp Snext, Sep1; returns 0 if Snext is sep1
isep2p strcmp Snext, Sep2; 0 if Snext is sep2
;NEXT SIGN IS NOT SEP1 NOR SEP2
if isep1p != 0 && isep2p != 0 then
if iwarleer == 1 then; first character after a separator
if iel == ielindx then; if searched element index
istartsel = indx; set it
iwarleer = 0
else ;if not searched element index
iel = iel+1; increase it
iwarleer = 0; log that it's not a separator
endif
endif
;NEXT SIGN IS SEP1 OR SEP2
else
if istartsel > -1 then; if this is first selector after searched element
iendsel = indx; set iendsel
igoto end ;break
else
iwarleer = 1
endif
endif
loop_lt indx, 1, ilen, loop
end: xout istartsel, iendsel
endop
opcode StrayLen, i, Sjj
;returns the number of elements in Stray. elements are defined by two separators as ASCII coded characters: isep1 defaults to 32 (= space), isep2 defaults to 9 (= tab). if just one separator is used, isep2 equals isep1
Stray, isepA, isepB xin
;DEFINE THE SEPARATORS
isep1 = (isepA == -1 ? 32 : isepA)
isep2 = (isepA == -1 && isepB == -1 ? 9 : (isepB == -1 ? isep1 : isepB))
Sep1 sprintf "%c", isep1
Sep2 sprintf "%c", isep2
;INITIALIZE SOME PARAMETERS
ilen strlen Stray
icount = 0; number of elements
iwarsep = 1
indx = 0
if ilen == 0 igoto end ;don't go into the loop if String is empty
loop:
Snext strsub Stray, indx, indx+1; next sign
isep1p strcmp Snext, Sep1; returns 0 if Snext is sep1
isep2p strcmp Snext, Sep2; 0 if Snext is sep2
if isep1p == 0 || isep2p == 0 then; if sep1 or sep2
iwarsep = 1; tell the log so
else ; if not
if iwarsep == 1 then ; and has been sep1 or sep2 before
icount = icount + 1; increase counter
iwarsep = 0; and tell you are ot sep1 nor sep2
endif
endif
loop_lt indx, 1, ilen, loop
end: xout icount
endop
;OPCODES FOR TABLE OPERATIONS
opcode TabMinVal_k, k, ii
ift, iftlen xin
kminval tab 0, ift
kndx = 1
loop:
kval tab kndx, ift
kmaxval = (kval < kminval ? kval : kminval)
loop_lt kndx, 1, iftlen, loop
xout kminval
endop
opcode TabMaxValIndx_k, kk, iikk
;returns the maximum value of a function table and its index
ift, iftlen, kstrt, kshft xin; ftable, length, startindex, how many values to shift for the next value
kndx = kstrt
kresultindx = kstrt
kresultval tab kstrt, ift; first value in the table as comparision
loop:
kval tab kndx, ift
if kval > kresultval then
kresultindx = kndx
kresultval = kval
endif
loop_lt kndx, kshft, iftlen, loop
xout kresultval, kresultindx
endop
opcode TabIndxNMax_k, 0, iiiik
;returns the indices of the first khowmany largest values in iftsrc and writes them in iftdest
;bin 0 is omitted (shift value in TabMaxValIndx_k)
iftsrc, isrclen, iftcpy, iftdest, khowmany xin
tablecopy iftcpy, iftsrc ;leave iftsrc (= amp table) untouched
kmin TabMinVal_k iftcpy, isrclen; smallest value in iftsrc
kwritindx = 0
loop:
kval, kndx TabMaxValIndx_k iftcpy, isrclen, 1, 1; gets index for maximum
tabw kmin-1, kndx, iftcpy ;replace this maximum by kmin-1
tabw kndx, kwritindx, iftdest ;write the index to index table
loop_lt kwritindx, 1, khowmany, loop
endop
;MISCELLANEOUS
opcode BufPlay1, a, ii
ifn, iskip xin
icps = 1 / (ftlen(ifn) / ftsr(ifn))
iphs = iskip / (ftlen(ifn) / ftsr(ifn))
asig poscil3 1, icps, ifn, iphs
xout asig
endop
opcode Stayed, k, kk
;outputs 1 if kin has not changed since ksec seconds, otherwise outputs 0
kin, ksec xin
kout init 0
knumk = ksec * kr ;number of control cycles for ksec
kinit init 1
kcount init 0
if kinit == 1 then ;just once, at the beginning
kprevious = kin
kinit = 0
endif
if kin == kprevious then
kcount = kcount + 1
else
kcount = 0
kprevious = kin
endif
if kcount > knumk then
kout = 1
else
kout = 0
endif
xout kout
endop
opcode ShowLED_a, 0, Sakkk
;Shows an audio signal in an outvalue channel. You can choose to show the value in dB or in raw amplitudes.
;Soutchan: string with the name of the outvalue channel
;asig: audio signal which is to displayed
;kdispfreq: refresh frequency (Hz)
;kdb: 1 = show in dB, 0 = show in raw amplitudes (both in the range 0-1)
;kdbrange: if idb=1: how many db-steps are shown (e.g. if 36 you will not see anything from a signal below -36 dB)
Soutchan, asig, kdispfreq, kdb, kdbrange xin
kdispval max_k asig, kdispfreq, 1
if kdb != 0 then
kdb = dbfsamp(kdispval)
kval = (kdbrange + kdb) / kdbrange
else
kval = kdispval
endif
outvalue Soutchan, kval
endop
opcode ShowOver_a, 0, Sakk
;Shows if the incoming audio signal was more than 1 and stays there for some time
;Soutchan: string with the name of the outvalue channel
;asig: audio signal which is to displayed
;kdispfreq: refresh frequency (Hz)
;khold: time in seconds to "hold the red light"
Soutchan, asig, kdispfreq, 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/0dbfs, kdispfreq, 1
if kon == 0 && kmax > 1 then
kstart = ktim
kend = kstart + khold
outvalue Soutchan, kmax
kon = 1
endif
if kon == 1 && ktim > kend then
outvalue Soutchan, 0
kon = 0
endif
endop
;============================================================================;
;============================================================================;
; INSTRUMENT BLOCKS ;
;============================================================================;
;============================================================================;
instr 1 ;always on
turnon 999 ;monitoring the audio output
;============================================================================;
; RECEIVING USER INPUT FROM THE WIDGET PANEL ;
;============================================================================;
;INPUT TO BE ANALYZED
Sfiles chnget "_MBrowse" ;selection of sound files
kinch invalue "inch" ;number of live input channel
kgain invalue "gain" ;live input gain (dB)
kselsamp invalue "sample" ;1 = select sample input
gksampnr invalue "sampnr" ;1, 2, ... for selecting a sample from the sf bank
keysel invalue "keysel" ;1=enable selection of sample by computer number keys
ksellive invalue "live" ;1 = select live input
kdolive invalue "dolive" ;when button pressed, live input to be analyzed
;ANALYSIS PARAMETERS
kpos invalue "pos" ;position in buffer (0-1)
kfast invalue "fast" ;how fast to react to position changes (sec)
kpartsana invalue "numpartsana" ;number of strongest partials to analyze
kmanpos invalue "manpos" ;manually selected position
krandpos invalue "randpos" ;random position selected
krndpos1 invalue "rndpos1" ;lower limit for random pointer (0-1)
krndpos2 invalue "rndpos2" ;upper limit for random pointer (0-1)
khopmove invalue "hopmove" ;hop move selected
khopmvsiz invalue "hopmovsiz" ;size of hopping in fractions of the table
;PLAYBACK PARAMETERS
gkvol invalue "vol" ;overall volume (dB)
gkmaxdur invalue "maxdur" ;maximum duration of a note in seconds
gkhowmany invalue "numpartspl" ;how many partials to be played back
gkshiftpl invalue "shiftpl" ;number of strongest partials to skip for playback
gkrefpch invalue "refpch" ;midi note number for playing at original pitch
gkstcent invalue "stcent" ;how many cents to transpose for adjacent semitones on the midi keyboard
gkrndfqdv invalue "rndfqdv" ;maximal partial random frequency deviation (cent)
gkrdampdv invalue "rndampdv" ;maximal partial random amplitude deviation (dB)
gkrddurdv invalue "rnddurdv" ;maximal partial random duration deviation (%)
gkatt invalue "att" ;attack time (sec)
gkdec invalue "dec" ;decay time (sec)
gksus invalue "sus" ;sustain level (0-1)
gkrel invalue "rel" ;release time (sec)
;PRINT AND EXPORT
kprint invalue "print" ;1 = print current values
kexp1 invalue "exp1" ;export printout
gSfexp1 invalue "_Browse1" ;file for printout export
kexp2 invalue "exp2" ;export amp-freq
gSfexp2 invalue "_Browse2" ;file for amp-freq export
kexp3 invalue "exp3" ;export as tables with multipliers
gSfexp3 invalue "_Browse3" ;file for table multiplier export
;REGULATING THE SWITCH BETWEEN CERTAIN BUTTONS
;analysis source
ksllivnew changed ksellive
kslsmpnew changed kselsamp
if kslsmpnew == 1 && kselsamp == 1 then
outvalue "live", 0
elseif ksllivnew == 1 && ksellive == 1 then
outvalue "sample", 0
endif
;pointer method
kmnposnew changed kmanpos
krdposnew changed krandpos
khopmvnew changed khopmove
if kmnposnew == 1 && kmanpos == 1 then
outvalue "randpos", 0
outvalue "hopmove", 0
elseif krdposnew == 1 && krandpos == 1 then
outvalue "manpos", 0
outvalue "hopmove", 0
knewrdpos = 1
elseif khopmvnew == 1 && khopmove == 1 then
outvalue "manpos", 0
outvalue "randpos", 0
knewhpmv = 1
endif
;CALCULATING A NEW POSITION IF RANDOM OR HOPMOVE SELECTED
;if note-on message has been received
kst,k0,k0,k0 midiin
if kst == 144 then
;calculate new random position if selected
if krandpos == 1 then
kpos random krndpos1, krndpos2
outvalue "pos", kpos
knewrdpos = 1
;calculate new hop position if selected
elseif khopmove == 1 then
kpos = frac(kpos+khopmvsiz)
kpos = (kpos < 0 ? 1-kpos : kpos)
outvalue "pos", kpos
knewhpmv = 1
endif
endif
;ENABLE COMPUTER KEYS FOR SWITCHING
if keysel == 1 then
key,k0 sensekey
keychange changed key
if keychange == 1 && k0 == 1 then ;new key down
if key > 47 && key < 58 then ;number keys 0-9
event "i", 3, 0, .1, key
elseif key == 112 || key == 114 || key == 104 then ;p/r/h
event "i", 4, 0, .1, key
elseif key == 115 || key == 108 then ;s/l
event "i", 5, 0, .1, key
endif
endif
endif
;============================================================================;
; SETTING SOME BASIC VALUES ;
;============================================================================;
iftdatlen = ftlen(giamps) ;length of amp/freq tables
iftmaxlen = ftlen(gimaxindc) ;length of maxima table
kwritten init 0 ;1 = having written new values in giamps/gifreqs
kwritliv init 0 ;write the fft-analysis of the live signal ...
kwritsmp init 0 ;... or of the selected sample position to the tables
;============================================================================;
; LOADING INPUT FILES AND CALCULATING POINTER POSITION ;
;============================================================================;
;LOADING THE SELECTED FILES IN FUNCTION TABLES 1, 2, ... AND STORING SF LENGTHS
inumfils StrayLen Sfiles, 124 ;how many files (separated by '|') to load
giftsflns ftgen 0, 0, -inumfils, -2, 0 ;empty table for sound file lenghths
iload = 0
load:
ist, ien StrayGetEl Sfiles, iload, 124
Sfil strsub Sfiles, ist, ien
inoth ftgen iload+1, 0, 0, 1, Sfil, 0, 0, 1 ;storing sf data in table 1, 2, ...
ilen filelen Sfil
tabw_i ilen, iload, giftsflns ;storing sf lenghts in table giftsflns
loop_lt iload, 1, inumfils, load
;CALCULATING AND SHOWING THE POSITION OF THE POINTER
kthissamp = (gksampnr > inumfils ? inumfils : gksampnr)
kfilen tab kthissamp-1, giftsflns
kpossec = kpos * kfilen ;position in sec
outvalue "position", kpossec
;SHOWING WHENEVER A NEW SAMPLE HAS BEEN SELECTED
newfile:
itable = i(kthissamp)
ist, ien StrayGetEl Sfiles, itable-1, 124
Sfil strsub Sfiles, ist, ien
ilslash strrindex Sfil, "/"
Sname strsub Sfil, ilslash+1
Showname sprintf "File selected:\n'%s'", Sname
rireturn
knewtable changed kthissamp
if knewtable == 1 then
reinit newfile
outvalue "showname", Showname
outvalue "showtab", -kthissamp
endif
;============================================================================;
; RECEIVING LIVE INPUT ;
;============================================================================;
ain inch kinch
ain = ain*ampdb(kgain)
kdisp metro 10
ShowLED_a "livein", ain, kdisp, 1, 50
ShowOver_a "livein_over", ain, kdisp, 1
;============================================================================;
; CONTINUOUSLY STREAMING FFT SIGNALS FROM LIVE AND FROM THE SELECTED SAMPLE ;
;============================================================================;
;STREAMING THE FFT LIVE SIGNAL
flive pvsanal ain, gifftsiz, giovlap, giwinsiz, giwintyp
;STREAMING THE FFT SIGNAL FROM THE SELECTED SAMPLE WHENEVER A NEW POSITION IS GIVEN
knewpos changed kpos, kthissamp, kslsmpnew, kmnposnew, krdposnew, khopmvnew
if knewpos == 1 then
reinit readsampnew
endif
readsampnew:
fsamp pvstanal 0, 1, 1, kthissamp, 0, 0, i(kpossec), gifftsiz, giovlap, 0
rireturn
;============================================================================;
; SELECT INPUT AND SET VARIABLES FOR WRITING THE TABLES ;
;============================================================================;
;SET kwritliv=1 IN THE k-CYCLE THE "dolive" BUTTON HAS BEEN PRESSED
if ksellive == 1 then ;live source must be selected
klivenew changed kdolive
if klivenew == 1 && kdolive == 1 then
kwritliv = 1
endif
;SET writesamp=1 IF A NEW POINTER POSITION HAS BEEN SET,
;SELECTOR HAS CHANGED OR BUTTON HAS BEEN SWITCHED
elseif kselsamp == 1 then ;sample source selected
;manual position
if kmanpos == 1 then
kpstayed Stayed kpos, kfast ;returns 1 if pos has not changed for kfast seconds
knewpos changed kpstayed
if (knewpos == 1 && kpstayed == 1) || knewtable == 1 || kslsmpnew == 1 then
kwritsmp = 1
endif
;random position
elseif knewrdpos == 1 then
kwritsmp = 1
;hop position
elseif knewhpmv == 1 then
kwritsmp = 1
endif
endif
;============================================================================;
; FILL THE TABLES WITH THE CURRENT AMPLTITUDE/FREQUENCY VALUES ;
;============================================================================;
;INITIALIZE COUNTERS
ksmpcnt init 0
klivcnt init 0
;LIVE INPUT SELECTED
if kwritliv == 1 then
kwritten pvsftw flive, giamps, gifreqs
if kwritten == 1 then
klivcnt = klivcnt + 1
printks " New live values written! (%d)\n", 0, klivcnt
kwritliv = 0
endif
;SOUND FILE INPUT SELECTED
elseif kwritsmp == 1 then
kwritten pvsftw fsamp, giamps, gifreqs
if kwritten == 1 then
ksmpcnt = ksmpcnt + 1
printks " New sample values written (%d)!\n", 0, ksmpcnt
kwritsmp = 0
knewrdpos = 0
knewhpmv = 0
endif
endif
;================================================================================;
;ANALYZE BIN AMPLITUDES AND FILL INDEX-TABLE IF AMP/FREQ-TABLES HAVE BEEN UPDATED;
;================================================================================;
if kwritten == 1 then ;writing via pvsftw has been finished
TabIndxNMax_k giamps, iftdatlen, giampcpy, gimaxindc, kpartsana
gkthsnman = kpartsana ;number of partials in this analysis
;============================================================================;
; PRINT VALUES IF DESIRED AND IF NEW VALUES AVAILABLE ;
;============================================================================;
if kprint == 1 && kwritten == 1 then
kprntndx = 0
if kselsamp == 1 then
Sprint sprintfk "File '%s' at position %f seconds:\n", Sname, kpossec
elseif ksellive == 1 then
Sprint sprintfk "%s\n", "Live input:"
else
Sprint = ""
endif
printout:
ktabindx tab kprntndx, gimaxindc
kamp tab ktabindx, giamps
kfreq tab ktabindx, gifreqs
Snew sprintfk "%s%.2d) amp = %f, freq = %f, bin = %d\n", Sprint, kprntndx+1, kamp, kfreq, ktabindx
Sprint strcpyk Snew
loop_lt kprntndx, 1, kpartsana, printout
outvalue "values", Sprint
gSexpt1 strcpyk Sprint
else
outvalue "values", ""
endif
endif
;============================================================================;
; EXPORT VALUES IF DESIRED ;
;============================================================================;
kexp1new changed kexp1
kexp2new changed kexp2
kexp3new changed kexp3
;EXPORT IN THE SAME FORMAT AS IN THE WIDGET PANEL
if kexp1new == 1 && kexp1 == 1 then
event "i", 6, 0, .1
;EXPORT AS RAW AMP-FREQ PAIR PER LINE
elseif kexp2new == 1 && kexp2 == 1 then
kexpt2ndx = 0
if kselsamp == 1 then
gSexpt2 sprintfk "Amp-Freq values for file '%s' at position %f seconds:\n", Sname, kpossec
elseif ksellive == 1 then
gSexpt2 sprintfk "%s\n", "Amp-Freq values for live input:"
endif
expt2:
ktabindx tab kexpt2ndx, gimaxindc
kamp tab ktabindx, giamps
kfreq tab ktabindx, gifreqs
Snew sprintfk "%s%f %f\n", gSexpt2, kamp, kfreq
gSexpt2 strcpyk Snew
loop_lt kexpt2ndx, 1, kpartsana, expt2
event "i", 7, 0, .1
;EXPORT AS MULTIPLIERS IN FUNCTION TABLES
elseif kexp3new == 1 && kexp3 == 1 then
kexpt3cnt init 0 ;counter
kexpt3cnt = kexpt3cnt+1
;get reference frequency for frequency multiplier = 1
ktabindx tab 0, gimaxindc
kfreq0 tab ktabindx, gifreqs ;frequency corresponding to strongest amplitude
if kselsamp == 1 then
Sexpt3 sprintfk "Amp-Freq multiplier for file '%s' at position %f seconds.\nPitch at frequency multiplier 1 was %f Hz.\n", Sname, kpossec, kfreq0
elseif ksellive == 1 then
Sexpt3 sprintfk "Amp-Freq multiplier for live input.\nPitch at frequency multiplier 1 was %f Hz.\n", kfreq0
endif
Samptab sprintfk "giAmp%d ftgen 0, 0, -%d, -2", kexpt3cnt, kpartsana
Sfreqtab sprintfk "giFreq%d ftgen 0, 0, -%d, -2", kexpt3cnt, kpartsana
kexpt3ndx = 0
expt3:
ktabindx tab kexpt3ndx, gimaxindc
kamp tab ktabindx, giamps
kfreq tab ktabindx, gifreqs
kfmult = kfreq/kfreq0
Snewamp sprintfk "%s, %f", Samptab, kamp
Snewfreq sprintfk "%s, %f", Sfreqtab, kfmult
Samptab strcpyk Snewamp
Sfreqtab strcpyk Snewfreq
loop_lt kexpt3ndx, 1, kpartsana, expt3
gSexpt3 sprintfk "%s%s\n%s\n\n", Sexpt3, Samptab, Sfreqtab
event "i", 8, 0, .1
endif
kwritten = 0 ;reset kwritten at the end of each k-cycle
endin
;============================================================================;
; SUBINSTRUMENT FOR LISTENING TO THE SELECTED SAMPLE ;
;============================================================================;
instr 2
itab = i(gksampnr) ;number of ftable (1, 2, ...)
idur tab_i itab-1, giftsflns ;get length of sample
p3 = idur
aout poscil3 1, 1/p3, itab
out aout*ampdb(gkvol)
endin
;============================================================================;
; SUBINSTRUMENTS FOR PERFORMING ACTIONS BY COMPUTER KEYBOARD SHORTCUTS ;
;============================================================================;
;MAKE LIVE SNAPSHOT AND CHANGE THE SAMPLE NUMBER
instr 3
ikey = p4 ;48-57
kcyc timeinstk
if ikey == 48 then
outvalue "dolive", 1
else
outvalue "sampnr", ikey-48
endif
if kcyc == 2 then
outvalue "dolive", 0
turnoff
endif
endin
;CHANGE THE POINTER MODE
instr 4
ikey = p4
if ikey == 112 then ;p = manual pointer
outvalue "manpos", 1
elseif ikey == 114 then ;r = random pointer
outvalue "randpos", 1
elseif ikey == 104 then ;h = hop move
outvalue "hopmove", 1
endif
turnoff
endin
;SWITCH BETWEEN SAMPLE/LIVE MODE
instr 5
ikey = p4
if ikey == 115 then ;s = sample as input
outvalue "sample", 1
elseif ikey == 108 then ;l = live input
outvalue "live", 1
endif
turnoff
endin
;============================================================================;
; SUBINSTRUMENTS FOR WRITING VALUES TO THE EXPORT TEXTFILE ;
;============================================================================;
;ADD VALUES IN THE SAME FORMAT AS IN THE WIDGET PANEL TO THE APPROPRIATE FILE
instr 6
fprints gSfexp1, gSexpt1
fprints gSfexp1, "%n"
printf_i "Values written to file '%s'\n", 1, gSfexp1
turnoff
endin
;ADD VALUES AS RAW AMP-FREQ PAIRS TO THE APPROPRIATE FILE
instr 7
fprints gSfexp2, gSexpt2
fprints gSfexp2, "%n"
printf_i "Values written to file '%s'\n", 1, gSfexp2
turnoff
endin
;ADD VALUES AS AMP-FREQ MULTIPLIERS TO THE APPROPRIATE FILE
instr 8
fprints gSfexp3, gSexpt3
printf_i "Values written to file '%s'\n", 1, gSfexp3
turnoff
endin
;============================================================================;
; SUBINSTRUMENTS FOR TERMINATING THE SAMPLE LISTENING ;
;============================================================================;
instr 9
turnoff2 2, 0, 0
endin
;============================================================================;
;============================================================================;
; MIDI-TRIGGERED INSTRUMENT FOR THE ADDITIVE SYNTHESIS ;
;============================================================================;
;============================================================================;
instr 10
;============================================================================;
; RECEIVING BASIC VALUES ;
;============================================================================;
inote = p4 ;midi note number
imidamp = p5 ;midi velocity (0-1)
insnum = 100 + (inote/100) ;fractional number to differentiate keys
irefpch = i(gkrefpch)
istcent = i(gkstcent) ;cent difference for each semitone
icentdif = (inote-irefpch) * istcent
ihowmany = i(gkhowmany)
ishift = i(gkshiftpl)
imax = i(gkthsnman) ;number of partials analyzed
ihowmany = (ihowmany+ishift > imax ? imax-ishift : ihowmany)
imaxdur = i(gkmaxdur) ;maximum duration for one note (sec)
irndampdv = i(gkrdampdv) ;max amp deviation (dB)
irndfqdv = i(gkrndfqdv) ;max freq deviation (cent)
irnddurdv = i(gkrddurdv) ;max dur deviation (%)
ivol = i(gkvol) ;overall volume (dB)
idur = i(gkmaxdur) ;duration of each note (without random deviations)
idur = (idur < 0.1) ? 0.1 : idur ;make sure there will not be zero duration
;============================================================================;
; GETTING THE SUM OF ALL SELECTED AMPLITUDES FOR SCALING ;
;============================================================================;
indx = ishift
iampsum = 0
loop1:
itabindx tab_i indx, gimaxindc ;bekommt index für stärkste, zweitstärkste usw amp aus gimaxindc
iamp tab_i itabindx, giamps ;get 1., 2., etc strongest amps
iampsum = iampsum + iamp
loop_lt indx, 1, ihowmany+ishift, loop1
indx = ishift
;============================================================================;
; TRIGGERING THE INSTANCES OF THE SUBINSTRUMENT FOR PLAYING THE PARTIALS ;
;============================================================================;
loop2:
;AMPLITUDES
itabindx tab_i indx, gimaxindc ;get index for ordered amps from gimaxindc
iptamp tab_i itabindx, giamps ;get 1., 2., etc strongest amps
iptamp = iptamp * imidamp ;following midi velocity
iptdbvar rnd31 irndampdv, 0 ;dB deviation
iptamp = ampdb(ivol) * iptamp * ampdb(iptdbvar) ;resulting amp
;FREQUENCIES
iptfq tab_i itabindx, gifreqs ;get related freq to selected amp
iptfq = cent(icentdif) * iptfq ;transposed depending on key pressed
iptfqvar rnd31 irndfqdv, 0 ;cent deviation
iptfq = iptfq * cent(iptfqvar)
;DURATIONS
iptdurvar rnd31 irnddurdv, 0 ;percent of duration deviation (100=twice,-100=half as long)
iptdur = idur * 2^(iptdurvar/100)
event_i "i", insnum, 0, iptdur, iptamp, iptfq, iampsum ;call instr 100.nn
loop_lt indx, 1, ihowmany+ishift, loop2
;============================================================================;
; STOPPING THE INSTANCES OF THE SUBINSTRUMENT IF MIDI-KEY HAS BEEN RAISED ;
;============================================================================;
krel release
if krel == 1 then
turnoff2 insnum, 4, 1
endif
endin
;============================================================================;
; SUBINSTRUMENT FOR PLAYING ONE PARTIAL ;
;============================================================================;
instr 100
;GET VALUES FROM INSTR 10 AND FROM WIDGETS
iamp = p4
ifreq = p5
iampsum = p6
iatt = i(gkatt)
idec = i(gkdec)
isus = i(gksus)
irel = i(gkrel)
;PLAY A SINE WITH ADSR ENVELOPE
apart poscil iamp, ifreq, gisine
aenv madsr iatt, idec, isus, irel
out apart*aenv/iampsum
endin
;============================================================================;
; COLLECTING ALL OUTGOING AUDIO AND SENDING IT TO THE METER DISPLAY ;
;============================================================================;
instr 999
ktrigdisp metro 10
aout monitor
ShowLED_a "out", aout, ktrigdisp, 1, 48
ShowOver_a "out_over", aout, ktrigdisp, 2
endin
i 1 0 36000
15691350659true170170127button187459110030{202c13fc-5d88-4826-a232-0e263bb5b119}true00event1.00000000play!/i 10 0 5 60 .5falsefalsefalse1073471247262{fe6e4eec-d24b-4130-ac47-0509d6087482}true00centertop
Arial
203000255255255border11268335247171{ef6762d2-df69-49d4-b2de-79768f36a0e9}true00centertop
Arial
203000255255255border11522335228291{3cfa2bba-4cba-4506-8dd0-a2cf8cba83cc}true00centertop
Arial
203000255255255border11756335225147{cd6f814b-6ee2-4dae-aca2-e003f0930179}true00centertop
Arial
203000255255255border1176958410742{651e302b-ea78-4453-8d18-a8df59faf558}true00lefttop
Arial
143000255255255noborder10268508247118{ad09a76a-5d31-4cc9-a269-5668506ed9cb}true00centertop
Arial
203000255255255border1175648522599{732add43-b213-4020-acb2-56175ca913dc}true00centertop
Arial
203000255255255border11913129949{9705422d-67f5-4b92-b66a-cdadf1801e9f}true00centertop
Arial
403000255255255noborder1010221255405{d56a8527-e63b-4d47-9cfc-2f4d90bdcb24}true00lefttop
Liberation Sans
103000255255255true11values995119312502{104a6eb1-9c4e-4dc2-85c7-a27518fe09b3}true00lefttop
Arial
123000255255255noborder1099070319556{404b0422-2cbd-4c4a-9433-a4e9c805789c}true00centertop
Arial
203000255255255border11livein8611809220{aea96bd2-66c8-46ed-ba64-42a9b473cfbc}true00livein0.000000001.000000000.000000001.000000000.255499390.25549939fill10.00000000jumpnoborder#00FF0002340false000truelivein_over9471802020{714f538d-b15d-450f-8243-f731e47cec08}true00livein_over0.000000001.000000000.000000001.000000000.000000000.00000000fill10.00000000jumpnoborder#00FF0025500false000truegain8612058020{a2a3113b-ea7c-4113-8aa5-22318f22d899}true00-12.0000000012.000000001.58490566lincontinuous-1.00000000falsedolive86123010641{dacdf18f-bde4-4a64-8973-9285e8c73e68}true00value1.00000000Get Live
Snapshot!/i1 0 10falsefalsefalse109412013924{5db562b3-eb02-48d2-9792-3c725a677612}true00lefttop
Arial
143000255255255noborder10sample73811410430{c2700f76-0407-44bb-9c6a-6440205a419f}true00value1.00000000Sample/truefalsetrue10live86311510430{4f1cac68-9cfe-4663-8847-2319835a203e}true00value1.00000000Live/i1 0 10truefalsefalse108621496226{e8dc513b-db58-4aaf-97ba-50df3ef532da}true00lefttop
Arial
143000255255255noborder10inch9271484027{fd2c2663-b818-4bc2-9e3c-933480540979}true00left
Liberation Sans
140002552552551.0000000011e+12false17401485724{0ee3a50b-51d8-4bc9-813e-f0d565c01f96}true00lefttop
Arial
143000255255255noborder10sampnr8011474027{ccb65c55-41fe-46ef-a983-94752dc3b1d5}true00left
Liberation Sans
140002552552551.0000000011e+12false27401785625{216588f6-fc24-4a02-8159-df22c0689436}true00event1.00000000Play it/i 2 0 1falsefalsefalse10740225104102{96551f1f-2888-4e8b-92a5-fe5f40650b1b}true00lefttop
Arial
103000255255255noborder10keysel74020510421{c546d668-bca5-4c14-a13d-d18c5b844efe}true00value1.00000000Enable Keys/i 2 0 1truefalsefalse107971784725{1d2f4673-864c-4aa6-a813-6f4b73fe536e}true00event1.00000000Stop/i 9 0 .1falsefalsefalse1026771458262{8cbb0d66-be77-44fb-9fea-1c2d42f85655}true00centertop
Arial
203000255255255border11_MBrowse2757534329{fe1e66fe-4105-4aed-9c04-e06ef3c95aa2}true00left
Arial
123000239239239nobackground_MBrowse6207510030{7e9d3773-29f5-4906-91f7-3e35e562dfa6}true00value1.00000000fox.wav|bell.aiff|BratscheMono.wav|Wood.wavSelect Files/falsefalsefalse10pos27324244940{a8250334-53f7-4e66-aa18-2b9be5ff9466}true00pos0.000000001.000000000.000000001.000000000.053452120.05345212line10.00000000jumpnoborder#00FF00255255255false000true59128010425{47aae0f0-46ca-4f1b-a3ba-5923cc3fb761}true00centertop
Arial
143000255255255noborder10position6123025526{87aa8561-e21a-4634-9693-ad2680b9d547}true00righttop
Arial
143000255255255noborder10showtab272109450138{193d6210-1914-4ba1-b1b3-6f813a1eecbb}true0071.000000001.000000001.000000001.00000000linlintruetruetruetruetruetruetrueshowname27428217250{b7f9aafe-e4c4-4837-aacf-13c309c3899d}true00lefttop
Arial
1432552550000noborder1075636513727{e05fc469-2f58-410b-9806-dc9cb7655d4b}true0-3lefttop
Arial
143000255255255noborder1075639213228{2a973632-d5ee-4246-ae9f-4db7349afc29}true00lefttop
Arial
143000255255255noborder10_Browse18873934826{7b71257f-6fef-494d-a260-98647d7ffc68}true00value1.00000000/home/linux/Desktop/Presentation/test/export1.txtTo.../i1 0 10falsefalsefalse1075641913228{c453cf7a-dbdd-4bc9-9c96-37d7e39c3a30}true00lefttop
Arial
143000255255255noborder10_Browse28864194826{5bbf7fad-0c36-4780-a07d-a60f81d40d90}true00value1.00000000/home/linux/Desktop/Presentation/test/export2.txtTo.../i1 0 10falsefalsefalse1075644613227{d574f198-bfad-40b6-b163-c4046e3878bf}true00lefttop
Arial
143000255255255noborder10_Browse38864474825{709af31e-5c0e-483f-a8e6-d0a51351680e}true00value1.00000000/home/linux/Desktop/Presentation/test/export3.txtTo.../i1 0 10falsefalsefalse10print8923668325{33968315-e837-4af4-bdf2-98480bf44c91}true00value1.00000000Print/i1 0 10truefalsetrue10exp19303935126{109e08e0-b1c7-4c4f-a2a2-18469fd6a4bb}true00value1.00000000Now!/i1 0 10falsefalsefalse10exp29294205225{6d4111e8-cd9f-4140-9d7f-8dfb008c6564}true00value1.00000000Now!/i1 0 10falsefalsefalse10exp39294475225{7980b3f7-f6bc-4798-8a65-f58642700fb3}true00value1.00000000Now!/i1 0 10falsefalsefalse10out76751617228{dfa0444b-a225-47a5-a1db-3ac7e63f8034}true00out0.000000001.000000000.000000001.00000000-inf-inffill10.00000000jumpnoborder#00FF0002340false000trueout_over9375162828{02c1d76c-5c5d-4a11-93b4-21dc54706c90}true00out_over0.000000001.000000000.000000001.000000000.000000000.00000000fill10.00000000jumpnoborder#00FF0025500false000truevol76855114921{d0dd7c29-31c8-4847-bb6c-7707ff1f3570}true00-12.0000000012.00000000-1.53020134lincontinuous-1.00000000false9485483028{2918f563-dce4-45c5-96e8-07df0b671f27}true00lefttop
Arial
143000255255255noborder1027537018927{b609249e-e278-4c26-9182-01086a1f20e3}true00lefttop
Arial
143000255255255noborder10fast4584045025{22a49672-685f-4628-8c6f-6fe2e09a6a9c}true00right
Liberation Sans
140002552552550.100000000.11false035440310227{599be8ec-f1b9-434a-b229-1f693270447d}true00lefttop
Arial
143000255255255noborder10numpartsana4623714626{caec7ed3-498b-4458-ac4e-57fac52c4df9}true00right
Liberation Sans
140002552552551.00000000164false32hopmove2754359028{38e30cb8-9e88-4dc9-8d09-b30f75c2e0c4}true00value1.00000000Hop Move/i1 0 10truefalsefalse103804356827{5da20281-4647-40b9-8499-ee43eff43519}true00lefttop
Arial
143000255255255noborder10hopmovsiz4504375826{f7598ae6-c54e-438f-94c2-b29d932ee539}true00right
Liberation Sans
140002552552550.01000000-11false0manpos2754037128{f8579654-8952-4866-a7cf-c0d757108732}true00value1.00000000Pointer/i1 0 10truefalsetrue10numpartspl6953695025{19be0852-85d9-4ae7-8506-e9b0d40d6dda}true00right
Liberation Sans
140002552552551.00000000132false32refpch6954325025{68269bfd-79e9-4749-a934-2187c48d3734}true00right
Liberation Sans
140002552552551.000000000127false6052943213927{05fccea2-ecb7-4fdc-ae15-062016397ad8}true00lefttop
Arial
143000255255255noborder10stcent6954645025{b16b9f6e-73f5-4c22-83a7-c47d8f0a68da}true00right
Liberation Sans
140002552552551.000000000200false10052846315627{e35d6aaa-3dcb-4e83-882d-0d51db0d867e}true00lefttop
Arial
143000255255255noborder1052836916727{c4d885cd-a6ac-413a-b148-b8c7c11d3537}true00lefttop
Arial
143000255255255noborder10shiftpl6954005025{5b84e7d0-0c82-45e2-aca9-f2d1f982970a}true00right
Liberation Sans
140002552552551.00000000031false052839914927{b4cef0ea-2ef0-4c9d-ade1-702aea738fd8}true00lefttop
Arial
143000255255255noborder10rndfqdv6955285025{d09928e9-5e05-4ba0-bd7d-94f92a0630d4}true00right
Liberation Sans
140002552552551.000000000200false052751816740{8b426732-b781-4659-a813-de60b04656ac}true00lefttop
Arial
143000255255255noborder10rndampdv6955605025{34535596-8d50-43f6-908c-58a524740c90}true00right
Liberation Sans
140002552552551.000000000200false052755216239{32f46896-8772-4e3c-b3d6-730806ad9ca4}true00lefttop
Arial
143000255255255noborder10rnddurdv6955925025{f20eb136-d9d2-4559-9fbc-6c44e6864afa}true00right
Liberation Sans
140002552552551.000000000200false052758615841{40a78d4c-b76c-4fa9-a75a-8baa76abcea9}true00lefttop
Arial
143000255255255noborder10maxdur6954965025{10a57f16-265b-4774-a0d6-26c3c629bf77}true00right
Liberation Sans
140002552552550.100000000.12000false052749515627{ffe891f4-df20-436c-b300-6ab71182168d}true00lefttop
Arial
143000255255255noborder10att29054017617{c00e9cc5-61b3-4190-ab37-a0227e181716}true00att0.010000000.200000000.010000000.200000000.017556820.01755682fill10.00000000jumpnoborder#00ff002551700false424650truedec29055917617{a2de8382-412c-4b27-a075-1ae824b85ff3}true00dec0.000000001.000000000.000000001.000000000.278409090.27840909fill10.00000000jumpnoborder#00ff002551700false424650truesus29057817617{fb32d182-7973-4205-aa3e-eca37b202fc7}true00sus0.000000001.000000000.000000001.000000000.511363640.51136364fill10.00000000jumpnoborder#00ff002551700false424650truerel29159817617{d1e99338-5f53-426a-8a8f-9124b39a69b9}true00rel0.000000001.000000000.000000001.000000000.738636360.73863636fill10.00000000jumpnoborder#00ff002551700false424650trueatt4635365021{f887d4bf-9249-4736-84e6-71ae3883aae4}true00lefttop
Arial
123000255255255noborder10dec4635565021{406da2a0-8ba4-40c0-91a4-09184aba8654}true00lefttop
Arial
123000255255255noborder10sus4635765021{0b43ed62-96ca-47f4-8056-a8459f7e4ece}true00lefttop
Arial
123000255255255noborder10rel4635955021{5439df97-f182-4a18-ac2e-60995d097127}true00lefttop
Arial
123000255255255noborder102725371924{da37e1dc-1232-49cb-a919-4218bd97460a}true00lefttop
Arial
143000255255255noborder102725561924{e3ad5e24-3b8f-4013-a0d8-a1fac6cb0133}true00lefttop
Arial
143000255255255noborder102725751924{4364c297-c8a3-472b-a4f8-14dbc6c5d226}true00lefttop
Arial
143000255255255noborder102725931924{f3d92b16-98c4-4c99-98f2-483a79a2314f}true00lefttop
Arial
143000255255255noborder103504706028{6fdc67be-6b4e-4f62-99ab-fa4ed69aff12}true00lefttop
Arial
143000255255255noborder10randpos2754677728{79f6ce72-902a-4ba6-a0df-fd33b6169a3f}true00value1.00000000Random/i1 0 10truefalsefalse10rndpos14104715025{ddedf240-0604-49f7-9f8c-880d31f26d17}true00right
Liberation Sans
140002552552550.1000000000.99false0rndpos24584715025{b73dfa9b-7dfb-488c-ab7f-be3d165a31b4}true00right
Liberation Sans
140002552552550.100000000.10.99false0vol9165483329{e4f92c35-0043-4eea-9560-7c55018f479e}true00lefttop
Arial
143000255255255noborder10pos4603059225{de3cf7d9-8cc9-47f9-a205-ef243a143424}true00left
Liberation Sans
140002552552550.0000010001false0.053452145328010425{141519e4-0af6-4486-895a-b8cc5e5e0462}true00centertop
Arial
143000255255255noborder101071255150{b8f46405-6cc4-4b7d-a2da-13890ad2864c}true0-3centertop
Liberation Sans
303000255255255false1010111255110{998837f8-1449-4aa2-b0c7-66677e5ba65b}true0-3lefttop
Liberation Sans
163000255255255false10