You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
9.7KB

  1. ; FrequencyShifter.csd
  2. ; Iain McCurdy, 2013.
  3. ;
  4. ; Frequency shifting using the hilbert filter
  5. ;
  6. ; CONTROLS
  7. ; --------
  8. ; Input -- Choose audio input: Live, sine tone or pink noise
  9. ; Polarity -- 3 options: 'Positive' = multiply 'Freq.' by 1, 'Negative' = multiply 'Freq.' by -1, 'Dual' = sum of 'Positive' and 'Negative' outputs
  10. ; Mix -- Dry/Wet mix control
  11. ; Freq. -- Principle frequency of the shifting frequency (before modulation by other controls)
  12. ; Mult. -- multipler of shifting frequency. Can be useful for finer control of shifting frequency around zero.
  13. ; Feedback -- Amount of frequency shifter that is fed back into its input
  14. ; Level -- Output level
  15. ; Dual Mono / Stereo -- 'Dual Mono' = both channels treated in the same way. 'Stereo' = right channel 180 degrees out of phase with respect to the left
  16. ; Stereo mode most apparent if shifting frequency is close to zero
  17. ; zero freq -- set 'Freq.' to zero
  18. ; [LFO~]
  19. ; Modulate Frequency -- Switch to activate LFO modulation of shifting frequency
  20. ; Shape -- Shape of the LFO: sine / triangle / square / random sample and hold / random splines
  21. ; Rate -- Rate of LFO (in hertz)
  22. ; Min -- Minimum frequency of the LFO modulation
  23. ; Max -- Maximum frequency of the LFO modulation
  24. ; Pan Mod -- Amount of panning modulation (strictly speaking, stereo balance modulation). Rate of modulation governed also by 'Rate'
  25. ; Sync LFO -- Restart LFO. Can be useful if 'random spline' modulation becomes 'stuck' at a low frequency
  26. <Cabbage>
  27. form caption("Time Domain Frequency Shifter") size(500,180), pluginID("fshi")
  28. image bounds( 0, 0, 500,180), colour("darkslategrey"), outline("silver"), line(6), shape("rounded")
  29. label bounds(22, 7, 60, 11), text("INPUT"), fontcolour("white")
  30. combobox bounds(10, 18, 65, 20), channel("input"), value(1), text("Live","Tone","Noise")
  31. label bounds(14, 42, 60, 11), text("POLARITY"), fontcolour("white")
  32. combobox bounds(10, 53, 65, 20), channel("polarity"), value(1), text("Positive","Negative","Dual")
  33. rslider bounds( 75, 10, 70, 70), text("Mix"), channel("mix"), range(0, 1.00, 0.5), colour("darkslategrey"), fontcolour("white), tracker("yellow")
  34. rslider bounds(145, 10, 70, 70), text("Freq."), channel("freq"), range(-4000, 4000, -50), colour("darkslategrey"), fontcolour("white), tracker("yellow")
  35. rslider bounds(215, 10, 70, 70), text("Mult."), channel("mult"), range(-1, 1.00, 0.1), colour("darkslategrey"), fontcolour("white), tracker("yellow")
  36. rslider bounds(285, 10, 70, 70), text("Feedback"), channel("fback"), range(0, 0.75, 0.6), colour("darkslategrey"), fontcolour("white), tracker("yellow")
  37. rslider bounds(355, 10, 70, 70), text("Level"), channel("level"), range(0, 1.00, 1), colour("darkslategrey"), fontcolour("white), tracker("yellow")
  38. checkbox bounds(425, 20, 12, 12), channel("r1") fontcolour("white") colour(yellow) value(1)
  39. checkbox bounds(425, 32, 12, 12), channel("r2") fontcolour("white") colour(yellow)
  40. label bounds(438, 21, 60, 9), text("DUAL MONO"), fontcolour("white")
  41. label bounds(438, 33, 60, 9), text("STEREO"), fontcolour("white")
  42. button bounds(425, 50, 65, 20), colour("Green"), text("Zero Freq", "Zero Freq"), channel("Zerofreq"), value(0)
  43. line bounds( 10, 90, 480, 2), colour("Grey")
  44. checkbox bounds( 10,100,150, 20), channel("ModOnOff") text("LFO Modulate Freq."), fontcolour("white") colour(lime) value(0)
  45. label bounds( 30,127, 75, 11), text("SHAPE"), fontcolour("white")
  46. combobox bounds( 10,138, 85, 20), channel("LFOShape"), value(7), text("Sine","Triangle","Square","Saw Up","Saw Down","Rand.S&H","Rand.Spline")
  47. rslider bounds( 145,100, 70, 70), text("Rate"), channel("LFORate"), range(0, 30, 1.5, 0.5, 0.001), colour("lightslategrey"), fontcolour("white), tracker("yellow")
  48. rslider bounds( 215,100, 70, 70), text("Min"), channel("LFOMin"), range(-2000, 2000, -600), colour("lightslategrey"), fontcolour("white), tracker("yellow")
  49. rslider bounds( 285,100, 70, 70), text("Max"), channel("LFOMax"), range(-2000, 2000, 600), colour("lightslategrey"), fontcolour("white), tracker("yellow")
  50. rslider bounds( 355,100, 70, 70), text("Pan Mod."), channel("PanSpread"),range(0, 1.00, 1), colour("lightslategrey"), fontcolour("white), tracker("yellow")
  51. button bounds(425,100, 65, 20), colour("Green"), text("Sync LFO", "Sync LFO"), channel("SyncLFO"), value(0)
  52. </Cabbage>
  53. <CsoundSynthesizer>
  54. <CsOptions>
  55. -d -n
  56. </CsOptions>
  57. <CsInstruments>
  58. sr = 44100
  59. ksmps = 32
  60. nchnls = 2
  61. 0dbfs = 1
  62. ;Iain McCurdy, 2012
  63. ;http://iainmccurdy.org/csound.html
  64. ;Frequency shifter effect based around the hilbert filter.
  65. gisine ftgen 0,0,4096,10,1 ;A SINE WAVE SHAPE
  66. gicos ftgen 0,0,4096,11,1 ;A COSINE WAVE SHAPE
  67. gishapes ftgen 0,0,8,-2,0,1,2,4,5
  68. opcode FreqShifter,a,akkkki
  69. adry,kmix,kfreq,kmult,kfback,ifn xin ;READ IN INPUT ARGUMENTS
  70. iWet ftgenonce 0,0,1024,-7,0,512,1,512,1 ;RESCALING FUNCTION FOR WET LEVEL CONTROL
  71. iDry ftgenonce 0,0,1024,-7,1,512,1,512,0 ;RESCALING FUNCTION FOR DRY LEVEL CONTROL
  72. kWet table kmix, iWet, 1 ;RESCALE WET LEVEL CONTROL ACCORDING TO FUNCTION TABLE giWet
  73. kDry table kmix, iDry, 1 ;RESCALE DRY LEVEL CONTROL ACCORDING TO FUNCTION TABLE giWet
  74. aFS init 0 ;INITILISE FEEDBACK SIGNAL (FOR FIRST K-PASS)
  75. ain = adry + (aFS * kfback) ;ADD FEEDBACK SIGNAL TO INPUT (AMOUNT OF FEEDBACK CONTROLLED BY 'Feedback Gain' SLIDER)
  76. areal, aimag hilbert ain ;HILBERT OPCODE OUTPUTS TWO PHASE SHIFTED SIGNALS, EACH 90 OUT OF PHASE WITH EACH OTHER
  77. kfshift = kfreq*kmult
  78. ;QUADRATURE OSCILLATORS. I.E. 90 OUT OF PHASE WITH RESPECT TO EACH OTHER
  79. ;OUTUTS OPCODE AMPLITUDE | FREQ. | FUNCTION_TABLE | INITIAL_PHASE (OPTIONAL;DEFAULTS TO ZERO)
  80. asin oscili 1, kfshift, ifn, 0
  81. acos oscili 1, kfshift, ifn, 0.25
  82. ;RING MODULATE EACH SIGNAL USING THE QUADRATURE OSCILLATORS AS MODULATORS
  83. amod1 = areal * acos
  84. amod2 = aimag * asin
  85. ;UPSHIFTING OUTPUT
  86. aFS = (amod1 - amod2)
  87. aout sum aFS*kWet, adry*kDry ;CREATE WET/DRY MIX
  88. xout aout ;SEND AUDIO BACK TO CALLER INSTRUMENT
  89. endop
  90. opcode CabbageRadio2,k,SS ; change opcode name and number is string variable inputs
  91. S1,S2 xin ; add string inputs for the required number of inputs
  92. kon = 0
  93. koff = 0
  94. ksum = 0
  95. ktrigsum = 0
  96. #define READ_CHANGES(NAME) #
  97. k$NAME chnget $NAME
  98. ksum = ksum + k$NAME
  99. kon$NAME trigger k$NAME,0.5,0
  100. ktrigsum = ktrigsum + kon$NAME#
  101. #define WRITE_CHANGES(NAME'COUNT) #
  102. if kon$NAME!=1 then
  103. chnset koff,$NAME
  104. else
  105. kval = $COUNT
  106. endif#
  107. $READ_CHANGES(S1)
  108. $READ_CHANGES(S2) ; add macro expansions for the required number of radio buttons
  109. if ktrigsum>0 then
  110. $WRITE_CHANGES(S1'1)
  111. $WRITE_CHANGES(S2'2) ; add macro expansions for the required number of radio buttons
  112. endif
  113. kval = (ksum=0?0:kval)
  114. xout kval
  115. endop
  116. instr 1
  117. kporttime linseg 0,0.001,0.05
  118. koff = 0
  119. kmix chnget "mix" ; read input widgets
  120. kfreq chnget "freq"
  121. kfreq portk kfreq,kporttime
  122. kmult chnget "mult"
  123. kmult portk kmult,kporttime
  124. kfback chnget "fback"
  125. klevel chnget "level"
  126. kpolarity chnget "polarity"
  127. kStereoMode CabbageRadio2 "r1","r2" ; call UDO
  128. kZeroFreq chnget "Zerofreq"
  129. ktrig changed kZeroFreq
  130. if ktrig=1 then
  131. chnset koff,"freq"
  132. endif
  133. kModOnOff chnget "ModOnOff"
  134. kLFOShape chnget "LFOShape"
  135. kLFORate chnget "LFORate"
  136. kLFOMin chnget "LFOMin"
  137. kLFOMax chnget "LFOMax"
  138. kPanSpread chnget "PanSpread"
  139. kSyncLFO chnget "SyncLFO"
  140. /* INPUT */
  141. kinput chnget "input"
  142. if kinput=1 then
  143. a1,a2 ins
  144. elseif kinput=2 then
  145. a1 oscils 0.2, 300, 0
  146. a2 = a1
  147. else
  148. a1 pinkish 0.2
  149. a2 pinkish 0.2
  150. endif
  151. /* LFO */
  152. if kModOnOff=1 then
  153. ktrig changed kLFOShape,kSyncLFO
  154. if ktrig=1 then
  155. reinit RESTART_LFO
  156. endif
  157. RESTART_LFO:
  158. if i(kLFOShape)=6 then
  159. kLFOFreq randomh kLFOMin,kLFOMax,kLFORate
  160. elseif i(kLFOShape)=7 then ; random spline
  161. ;kLFOFreq randomi kLFOMin,kLFOMax,kLFORate,1
  162. ;kLFOFreq portk kLFOFreq,1/kLFORate
  163. kLFOFreq rspline kLFOMin,kLFOMax,kLFORate,kLFORate*2
  164. else
  165. ishape table i(kLFOShape)-1,gishapes
  166. kLFOFreq lfo 1,kLFORate,ishape
  167. kLFOFreq scale (kLFOFreq*0.5)+0.5,kLFOMin,kLFOMax
  168. endif
  169. rireturn
  170. endif
  171. kfreq = kfreq+kLFOFreq
  172. /* FREQUENCY SHIFTERS */
  173. ktrig changed kStereoMode
  174. if ktrig=1 then
  175. reinit RESTART_FREQUENCY_SHIFTERS
  176. endif
  177. RESTART_FREQUENCY_SHIFTERS:
  178. if kpolarity=1 then ; polarity is positive...
  179. a1 FreqShifter a1,kmix,kfreq,kmult,kfback,gisine
  180. if i(kStereoMode)=2 then
  181. a2 FreqShifter a2,kmix,kfreq,kmult,kfback,gicos ; cosine version
  182. else
  183. a2 FreqShifter a2,kmix,kfreq,kmult,kfback,gisine
  184. endif
  185. elseif kpolarity=2 then ; polarity is negative...
  186. a1 FreqShifter a1,kmix,-kfreq,kmult,kfback,gisine
  187. if i(kStereoMode)=2 then
  188. a2 FreqShifter a2,kmix,-kfreq,kmult,kfback,gicos ; cosine version
  189. else
  190. a2 FreqShifter a2,kmix,-kfreq,kmult,kfback,gisine
  191. endif
  192. else ; polarity is dual...
  193. aa FreqShifter a1,kmix,kfreq,kmult,kfback,gisine ; positive
  194. if i(kStereoMode)=2 then
  195. ab FreqShifter a2,kmix,kfreq,kmult,kfback,gicos ; cosine version
  196. else
  197. ab FreqShifter a2,kmix,kfreq,kmult,kfback,gisine
  198. endif
  199. ac FreqShifter a1,kmix,-kfreq,kmult,kfback,gisine ; negative
  200. if i(kStereoMode)=2 then
  201. ad FreqShifter a2,kmix,-kfreq,kmult,kfback,gicos ; cosine version
  202. else
  203. ad FreqShifter a2,kmix,-kfreq,kmult,kfback,gisine
  204. endif
  205. rireturn
  206. a1 = (aa+ac)*0.5 ; sum positive and negative and attenuate
  207. a2 = (ab+ad)*0.5
  208. endif
  209. /* PANNING */
  210. if kModOnOff=1 then
  211. kpan randomi 0.5-(kPanSpread*0.5),0.5+(kPanSpread*0.5),kLFORate,1
  212. kpan portk kpan,1/kLFORate
  213. a1 = a1 * sin(kpan*$M_PI_2)
  214. a2 = a2 * cos(kpan*$M_PI_2)
  215. ;a1 = a1*kpan
  216. ;a2 = a2*(1-kpan)
  217. endif
  218. a1 = a1 * klevel ; scale using level control
  219. a2 = a2 * klevel
  220. outs a1,a2
  221. endin
  222. </CsInstruments>
  223. <CsScore>
  224. i 1 0 [60*60*24*7]
  225. </CsScore>
  226. </CsoundSynthesizer>