<instrument type='blue.orchestra.BlueSynthBuilder' editEnabled='true'>
  <name>Granular Saw</name>
  <comment>Granular synthesis of a morphing saw waveform.</comment>
  <globalOrc>gitbatk_&lt;INSTR_ID&gt;	ftgen	0, 0, 513, 5, .01, 512, 1
gitbwin_&lt;INSTR_ID&gt;	ftgen	0, 0, 2048, 20, 5 ;grain amplitude curve
gitbsin_&lt;INSTR_ID&gt;	ftgen	0, 0, 8192, 10, 1.0
gitbcplx1_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+1, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833, .0769, .0714, .0667, 0.0625, .0588, .0556, .0526, .05
gitbcplx2_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+2, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833, .0769, .0714, .0667, 0.0625, .0588, .0556, .0526
gitbcplx3_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+3, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833, .0769, .0714, .0667, 0.0625, .0588, .0556
gitbcplx4_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+4, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833, .0769, .0714, .0667, 0.0625, .0588
gitbcplx5_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+5, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833, .0769, .0714, .0667, 0.0625
gitbcplx6_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+6, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833, .0769, .0714, .0667
gitbcplx7_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+7, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833, .0769, .0714
gitbcplx8_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+8, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833, .0769
gitbcplx9_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+9, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909, .0833
gitbcplx10_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+10, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1, .0909
gitbcplx11_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+11, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111, .1
gitbcplx12_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+12, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125, .1111
gitbcplx13_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+13, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429, .125
gitbcplx14_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+14, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666, .1429
gitbcplx15_&lt;INSTR_ID&gt;	ftgen	&lt;INSTR_ID&gt;*100+15, 0, 1024, 10, 1, .5, .3333, .25, .2, .1666
</globalOrc>
  <globalSco/>
  <instrumentText>; p3 - duration
; p4 - frequency (pch)
; p5 - amplitude
; p6 - attack time
; p7 - decay time
; p8 - begin pitch multiplier

;;; Begin Boilerplate ;;;

iamp	=	p5
iphs	=	0
itie	tieStatus
ipitch	getFrequency	p4
idur	=	abs(p3)
ipchmlt	=	p8

;;; Envelope variables
iatk	=	p6
idecdur	=	p7 ;How much of note is decay
iatnfct	=	.95 ;Attenuation factor
idecfct	=	.001 ;Decay factor
iexpmod	=	0 ;exponential curve modifier

tigoto skipInit
 ioldpch	init	ipitch
skipInit:

;amplitude
if (itie == -1) then
 aamp1	envlpx	iamp, iatk, idur, idecdur, gitbatk_&lt;INSTR_ID&gt;, iatnfct, idecfct, iexpmod

elseif (itie == 0) then	
 aamp1	envlpx	iamp, iatk, idur, 0, gitbatk_&lt;INSTR_ID&gt;, iatnfct, idecfct, iexpmod

elseif (itie == 1) then	
 aamp1	=	iamp*iatnfct
 iphs	=	1

elseif (itie == 2) then
 aamp1	expseg	iamp*iatnfct, idur-idecdur, iamp*iatnfct, idecdur, iamp*idecfct
 iphs	=	1

endif

;;; Pitch transition envelope
kpitch	expseg	ioldpch, .1, ipitch, idur-.1, ipitch
ioldpch	=	ipitch
kpmod	line	ipchmlt, idur, 1
kpitch	=	kpitch * kpmod

;;; End Boilerplate ;;;

;setup table changes
itblrng	=	15
ktbfrq	=	&lt;tbFrq1&gt;+&lt;tbFrq2&gt;
ktmod1	oscili	(itblrng*.5)-.5, ktbfrq, gitbsin_&lt;INSTR_ID&gt;, iphs
ktmod2	oscili	(itblrng*.5)-.5, ktbfrq*1.222, gitbsin_&lt;INSTR_ID&gt;, iphs
ktmod1	=	ktmod1+(itblrng*.5)+.5
ktmod2	=	ktmod2+(itblrng*.5)+.5
kfn1	=	int(ktmod1+(&lt;INSTR_ID&gt;*100))
kfn2	=	int(ktmod2+(&lt;INSTR_ID&gt;*100))

;setup granular synthesis
iwfn	=	gitbwin_&lt;INSTR_ID&gt;
kphs	=	0
kfmd	=	(kpitch*&lt;fmd&gt;)+&lt;fmd2&gt;
kpmd	=	&lt;pmd&gt;
kgdur   =	&lt;gdur&gt;
kdens   =	&lt;densA&gt;+&lt;densB&gt;
kmaxovr	=	kdens*kgdur
imaxovr	=	(&lt;densA&gt;+&lt;densB&gt;)*&lt;gdur&gt;+1
kfrpow	=	&lt;frpow&gt;
kprpow	=	&lt;prpow&gt;
iseed	=	100
imode	=	0 + iphs

; granularize both channels
agrain1	grain3	kpitch, kphs, kfmd, kpmd, kgdur, kdens, imaxovr, kfn1, iwfn, kfrpow, kprpow, iseed, imode
agrain2	grain3	kpitch, kphs, kfmd, kpmd, kgdur, kdens, imaxovr, kfn2, iwfn, kfrpow, kprpow, iseed, imode
;ablk1	dcblock	agrain1*aamp1
;ablk2	dcblock	agrain2*aamp1
ablk1	=	agrain1*aamp1
ablk2	=	agrain2*aamp1

; balance signals
cigoto	(imaxovr &gt; 1), bal
iscal	=	1
goto end

bal:
iscal	=	sqrt(imaxovr) * .666666

end:
aout1	=	ablk1/iscal
aout2	=	ablk2/iscal

blueMixerOut	aout1, aout2
</instrumentText>
  <graphicInterface editEnabled='false'>
    <uniqueNameManager nameIndex='-1' defaultPrefix='bsbObj'/>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBLabel'>
      <objectName></objectName>
      <x>321</x>
      <y>408</y>
      <label>Variation (multiple of pitch) of grain frq.</label>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBKnob' version='2'>
      <objectName>pmd</objectName>
      <x>26</x>
      <y>29</y>
      <minimum>0.0</minimum>
      <maximum>1.0</maximum>
      <value>1.0</value>
      <knobWidth>60</knobWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBLabel'>
      <objectName></objectName>
      <x>12</x>
      <y>112</y>
      <label>Variation of phase</label>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBKnob' version='2'>
      <objectName>gdur</objectName>
      <x>134</x>
      <y>21</y>
      <minimum>0.0</minimum>
      <maximum>2.0</maximum>
      <value>0.18470252</value>
      <knobWidth>120</knobWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBLabel'>
      <objectName></objectName>
      <x>157</x>
      <y>165</y>
      <label>Grain duration</label>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBKnob' version='2'>
      <objectName>frpow</objectName>
      <x>316</x>
      <y>3</y>
      <minimum>-2.0</minimum>
      <maximum>2.0</maximum>
      <value>-2.0</value>
      <knobWidth>80</knobWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBLabel'>
      <objectName></objectName>
      <x>296</x>
      <y>105</y>
      <label>Rnd. frq. distribution</label>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBKnob' version='2'>
      <objectName>prpow</objectName>
      <x>449</x>
      <y>4</y>
      <minimum>-2.0</minimum>
      <maximum>2.0</maximum>
      <value>1.148881</value>
      <knobWidth>80</knobWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBLabel'>
      <objectName></objectName>
      <x>425</x>
      <y>103</y>
      <label>Rnd. phase distribution</label>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBHSlider'>
      <objectName>densA</objectName>
      <x>10</x>
      <y>187</y>
      <minimum>0.0</minimum>
      <maximum>100.0</maximum>
      <resolution>1.0</resolution>
      <value>40.0</value>
      <sliderWidth>400</sliderWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBHSlider'>
      <objectName>densB</objectName>
      <x>9</x>
      <y>221</y>
      <minimum>0.0</minimum>
      <maximum>2000.0</maximum>
      <resolution>100.0</resolution>
      <value>0.0</value>
      <sliderWidth>400</sliderWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBLabel'>
      <objectName></objectName>
      <x>146</x>
      <y>257</y>
      <label>Num. of grains per second</label>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBKnob' version='2'>
      <objectName>tbFrq1</objectName>
      <x>9</x>
      <y>280</y>
      <minimum>0.0</minimum>
      <maximum>1.0</maximum>
      <value>0.21014585</value>
      <knobWidth>80</knobWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBHSlider'>
      <objectName>tbFrq2</objectName>
      <x>99</x>
      <y>304</y>
      <minimum>0.0</minimum>
      <maximum>200.0</maximum>
      <resolution>1.0</resolution>
      <value>0.0</value>
      <sliderWidth>200</sliderWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBLabel'>
      <objectName></objectName>
      <x>135</x>
      <y>346</y>
      <label>Table change frequency</label>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBKnob' version='2'>
      <objectName>fmd</objectName>
      <x>365</x>
      <y>265</y>
      <minimum>0.0</minimum>
      <maximum>0.5</maximum>
      <value>0.017024606</value>
      <knobWidth>120</knobWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBHSlider'>
      <objectName>fmd2</objectName>
      <x>334</x>
      <y>434</y>
      <minimum>0.0</minimum>
      <maximum>20.0</maximum>
      <resolution>1.0</resolution>
      <value>0.0</value>
      <sliderWidth>150</sliderWidth>
    </bsbObject>
    <bsbObject type='blue.orchestra.blueSynthBuilder.BSBLabel'>
      <objectName></objectName>
      <x>352</x>
      <y>466</y>
      <label>Variation (in hz) of grain frq.</label>
    </bsbObject>
  </graphicInterface>
  <bsbParameterList>
    <parameter uniqueId='-512379925' name='densA' label='' min='0.0' max='100.0' resolution='1.0' automationEnabled='false'>
      <line name='' version='2' max='100.0' min='0.0' resolution='1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='40.0'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379923' name='densB' label='' min='0.0' max='2000.0' resolution='100.0' automationEnabled='false'>
      <line name='' version='2' max='2000.0' min='0.0' resolution='100.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='0.0'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379917' name='fmd' label='' min='0.0' max='0.5' resolution='-1.0' automationEnabled='false'>
      <line name='' version='2' max='0.5' min='0.0' resolution='-1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='0.017024606'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379915' name='fmd2' label='' min='0.0' max='20.0' resolution='1.0' automationEnabled='false'>
      <line name='' version='2' max='20.0' min='0.0' resolution='1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='0.0'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379929' name='frpow' label='' min='-2.0' max='2.0' resolution='-1.0' automationEnabled='false'>
      <line name='' version='2' max='2.0' min='-2.0' resolution='-1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='-2.0'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379931' name='gdur' label='' min='0.0' max='2.0' resolution='-1.0' automationEnabled='false'>
      <line name='' version='2' max='2.0' min='0.0' resolution='-1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='0.18470252'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379933' name='pmd' label='' min='0.0' max='1.0' resolution='-1.0' automationEnabled='false'>
      <line name='' version='2' max='1.0' min='0.0' resolution='-1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='1.0'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379927' name='prpow' label='' min='-2.0' max='2.0' resolution='-1.0' automationEnabled='false'>
      <line name='' version='2' max='2.0' min='-2.0' resolution='-1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='1.148881'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379921' name='tbFrq1' label='' min='0.0' max='1.0' resolution='-1.0' automationEnabled='false'>
      <line name='' version='2' max='1.0' min='0.0' resolution='-1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='0.21014585'/>
      </line>
    </parameter>
    <parameter uniqueId='-512379919' name='tbFrq2' label='' min='0.0' max='200.0' resolution='1.0' automationEnabled='false'>
      <line name='' version='2' max='200.0' min='0.0' resolution='1.0' color='-8355712' rightBound='false'>
        <linePoint x='0.0' y='0.0'/>
      </line>
    </parameter>
  </bsbParameterList>
  <presetGroup name='Presets'/>
  <opcodeList>
    <udo>
      <opcodeName>getFrequency</opcodeName>
      <outTypes>i</outTypes>
      <inTypes>i</inTypes>
      <codeBody>ipch 	xin

iout	= (ipch &lt; 15 ? cpspch(ipch) : ipch)
	
	xout	iout
</codeBody>
    </udo>
    <udo>
      <opcodeName>tieStatus</opcodeName>
      <outTypes>i</outTypes>
      <inTypes>0</inTypes>
      <codeBody>itie tival

if (itie == 0 &amp;&amp; p3 &lt; 0) ithen
    ; this is an initial note within a group of tied notes
    itiestatus = 0
       
elseif (p3 &lt; 0 &amp;&amp; itie == 1) ithen
    ; this is a middle note within a group of tied notes 
    itiestatus = 1

elseif (p3 &gt; 0 &amp;&amp; itie == 1) ithen
    ; this is an end note out of a group of tied notes
    itiestatus = 2

elseif (p3 &gt; 0 &amp;&amp; itie == 0) ithen
    ; this note is a standalone note
    itiestatus = -1

endif  

    xout    itiestatus
</codeBody>
    </udo>
  </opcodeList>
</instrument>