;Written by Iain McCurdy, 2006 ; Modified for QuteCsound by René, January 2011 ; Tested on Ubuntu 10.04 with csound-float 5.13.0 and QuteCsound svn rev 817 ;Notes on modifications from original csd: ; Add limits for Lower/Upper frequencies ; Removed "Instructions and Info Panel" for the gui to fit in a 1200x800 screen ; Pitch ; ------------------------------------------------------------------------------------------------------------------------------------------- ; This example presents a simple demonstration of the 'pitch' opcode, used for pitch and amplitude tracking. ; It requires a live audio input so you should have a microphone connected to your computer and get ready to sing into it! ; 'Pitch' outputs two k-rate variables and for frequency and the other for the amplitude of an input audio signal. ; For clarity, the usage of this data in this example is quite trivial in that it is used merely as input data for the ; frequency and amplitude of a simple synthesizing oscillator. With a little imagination, many more interesting applications are possible. ; 'Update Period' defines the time period over which output data is updated. Higher values here will stabilise the output stream of ; frequency data at the expense of response time. Lower values will allow a faster response to changing pitch. ; Upper and lower limits of pitch detection can be defined (in the octave point decimal format). Careful setting of these limits can help ; prevent incorrect pitch detections. ; 'Threshold of Detection' allows the user to define an amplitude threshold (in dB) above which detection will begin. ; Once the threshold is crossed the signal amplitude must drop to beyond 6 dB lower than the threshold before detection ceases. ; This parameter can be used to prevent extraneous and environmental noise from triggering the tracking process. ; 'Octave Divisions' allows us to quantize the number of allowed pitches in an octave. A setting of 12 will limit the output frequency ; values to twelve per octave (as in the equally tempered tuning system). Maximum allowed setting here is 120. ; 'Conformations' defines the number of confirmations that will be required before the tracker shifts to a higher or lower octave. ; This parameter can be used to control unstable jumping between octaves. Experimentation is required to find the appropriate setting. ; 'Starting Pitch of the Tracker' defines the starting pitch (in oct) of the tracker. 'Octave Decimations' defines the number of octave ; decimations in the spectrum. ; This may need adjusting depending on the nature of the sound being tracked. Experimentation is required. ; 'Q of Filters' defines the sharpness or resonance of the analysis filters. Again this should be fine tuned according to the nature of ; the sound being tracked. ; Noisy or spectrally unstable sounds may require lower settings for 'Q' to allow sucessful pitch tracking. ; 'Number of Partials' defines the number of partials that will be used in the pitch tracking process. ; Higher values here may facilitate more successful pitch tracking in sounds with a weak fundemental, but at the expense CPU. ; 'Rolloff' defines the amplitude rolloff (expressed as a fraction of an octave) of the filters used in tracking. 'Skip', if activated, skips ; initialisation. An alternative opcode for pitch and amplitude tracking worth investigating is 'pitchamdf'. ; Pitch tracking is a much more involved and CPU intensive process than amplitude tracking. ; If you only require amplitude tracking it is recommended that you use either the 'follow' or 'follow2' opcodes. ;my flags on Ubuntu: -dm0 -iadc -odac -+rtaudio=jack -b16 -B4096 -+rtmidi=null sr = 44100 ;SAMPLE RATE ksmps = 16 ;NUMBER OF AUDIO SAMPLES IN EACH CONTROL CYCLE nchnls = 2 ;NUMBER OF CHANNELS (2=STEREO) 0dbfs = 1 ;MAXIMUM AMPLITUDE REGARDLESS OF BIT DEPTH instr 1 ;GUI ;(ALWAYS ON - SEE SCORE) PLAYS FILE AND SENSES FADER MOVEMENT AND RESTARTS INSTR 2 FOR I-RATE CONTROLLERS klimit init 0 idelta = 0.1 ktrig metro 10 if (ktrig == 1) then gkskip invalue "Skip" gkupdte invalue "Update" gklo invalue "Lower" gkhi invalue "Upper" if gklo > gkhi - idelta then gklo = gkhi - idelta klimit = 1 else klimit = 0 endif outvalue "Lower_Value", gklo outvalue "Limit", klimit gkdbthresh invalue "Threshold" gkstrt invalue "Starting" gkq invalue "Q_Filters" gkrolloff invalue "Rolloff" gkfrqs invalue "Divisions" gkconf invalue "Conformations" gkocts invalue "Decimations" gknptls invalue "Harmonics" endif kSwitch changed gkupdte, gklo, gkhi, gkdbthresh, gkfrqs, gkconf, gkstrt, gkocts, gkq, gknptls, gkrolloff ;GENERATE A MOMENTARY '1' PULSE IN OUTPUT 'kSwitch' IF ANY OF THE SCANNED INPUT VARIABLES CHANGE. (OUTPUT 'kSwitch' IS NORMALLY ZERO) ; ITRIGGER | IMINTIM | IMAXNUM | IINSNUM | IWHEN | IDUR (-1 MEANS A NOTE OF INFINITE DURATION) schedkwhen kSwitch, 0, 0, 2, 0, -1 ;RESTART INSTRUMENT 2 WITH A SUSTAINED (HELD) NOTE WHENEVER kSwitch=1 endin instr 2 aL, aR ins ;READ STEREO INPUT TO THE COMPUTER ;CREATE A PITCH TRACKING VARIABLE AND AN AMPLITUDE TRACKING VARIABLE USING THE 'pitch' OPCODE - IMPROVED RESULTS CAN BE ACHIEVED BY FIRST ATTENUATING THE AMPLITUDE OF THE INPUT SIGNAL, YOU WILL NOTICE THAT 'aL' IS FIRST DIVIDED BY 50 koct, kamp pitch aL/50, i(gkupdte), i(gklo), i(gkhi), i(gkdbthresh), i(gkfrqs), i(gkconf), i(gkstrt), i(gkocts), i(gkq), i(gknptls), i(gkrolloff), i(gkskip) kamp port kamp, 0.01 ;SMOOTH OUT DISCONTINUITIES IN THE AMPLITUDE PARAMETER STREAM aamp interp kamp/500 ;RECALIBRATE THE AMPLITUDE FOLLOWING SIGNAL - ALSO INTERPOLATE TO CREATE AN A-RATE SIGNAL TO PREVENT CLICKS AS THE AMPLITUDE FLUCTUATES asig oscili aamp, cpsoct(koct), 1 ;CREATE A SYNTHESIZED TONE THAT MIMICS THE FREQUENCY AND AMPLITUDE ATTRIBUTES OF THE INPUT SIGNAL outs asig, asig ;SEND THE STNTHESIZED TONE TO THE OUTPUTS endin f 1 0 1024 10 1 .2 .15 .1 .2 .1 .05 .02 .01 ;A RICH TONE - USED BY THE PITCH TRACKING SYNTHESIZER f 2 0 131072 10 1 ;A SINE WAVE ;INSTR | START | DURATION i 1 0 3600 ;GUI (THIS ALSO KEEPS PERFORMANCE GOING) 72 179 400 200 true 241 226 185 2 2 514 456 {aa607456-d368-4d59-8497-d16d608404c3} true 0 0 center Liberation Sans 18 3 0 0 0 158 220 158 border 5 2 8 156 180 30 {63ab529c-3f14-4886-8215-fd6e8f1a37ce} true 0 0 left Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Upper 8 139 500 27 {a7c37920-a18a-446b-8b66-6a1119503da0} true 0 0 4.00000000 14.00000000 11.02000000 lin continuous -1.00000000 false Upper 448 156 60 30 {b453c0e4-809f-4074-94fd-506ebb68f3b8} true 0 0 right Liberation Sans 9 3 0 0 0 255 255 255 noborder 1 1 8 49 180 30 {93cc32a5-c161-4212-baeb-c28b120f89a8} true 0 0 left Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Update 8 32 500 27 {107836e5-a726-4a83-83b5-c368481d9a48} true 0 0 0.00100000 0.10000000 0.00991000 lin continuous -1.00000000 false Update 448 49 60 30 {71f0e6eb-f79d-4cd7-80f7-a22cc355e64f} true 0 0 right Liberation Sans 9 3 0 0 0 255 255 255 noborder 1 1 8 85 180 30 {e193ffd3-920b-40d4-9eaa-bd11dad75bff} true 0 0 left Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Threshold 8 68 500 27 {e4aa4e7b-5b74-4a61-8bd7-4beea2c0bb93} true 0 0 -90.00000000 20.00000000 -0.46000000 lin continuous -1.00000000 false Threshold 448 85 60 30 {dc879380-3a9e-4939-b482-a0b4ee46dac5} true 0 0 right Liberation Sans 9 3 0 0 0 255 255 255 noborder 1 1 8 121 180 30 {866a933e-0c0d-493a-84af-29f3557f8e4a} true 0 0 left Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Lower 8 104 500 27 {fbbd0dd3-c206-4214-aa5e-10d42b909252} true 0 0 4.00000000 14.00000000 4.00000000 lin continuous -1.00000000 false Lower_Value 448 121 60 30 {31c3cb1b-308f-47cd-9396-c81d85a1c30a} true 0 0 right Liberation Sans 9 3 0 0 0 255 255 255 noborder 1 1 Limit 438 177 70 20 {f98fd774-20f8-4893-b3e6-bf114c64ffef} true 0 0 0.00000000 1.00000000 0.00000000 1.00000000 0.00000000 0.00000000 fill 1 0.00000000 jump 255 0 0 false 0 0 0 438 176 70 20 {729f7333-9653-4a15-906e-244e730dee00} true 0 0 center Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Divisions 82 210 80 30 {c465316b-7488-436a-92e7-5a8df08a26d5} true 0 0 center Liberation Sans 12 0 0 0 255 255 255 1.00000000 1 120 false 120 53 239 130 29 {8ade1b4d-d8c8-4654-b667-180b451c2362} true 0 0 center Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Conformations 209 210 80 30 {aba72173-7514-4b8c-b838-edb9c8958119} true 0 0 center Liberation Sans 12 0 0 0 255 255 255 1.00000000 1 100 false 10 183 239 130 29 {150649c9-6403-47c0-95a5-22291e533ef9} true 0 0 center Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Decimations 341 210 80 30 {d15c95fc-cdea-4be2-8aec-1cc4adb9d83a} true 0 0 center Liberation Sans 12 0 0 0 255 255 255 1.00000000 1 100 false 6 313 239 130 29 {86f60b7a-d8dc-44b8-9554-2b52982c6354} true 0 0 center Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 8 291 180 30 {7819af6f-a4b0-4b0a-b121-88f6c51493f7} true 0 0 left Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Starting 8 274 500 27 {831982c9-56fa-43a3-9d99-98b0d9cc4012} true 0 0 4.00000000 14.00000000 7.00000000 lin continuous -1.00000000 false Starting 448 291 60 30 {22528a34-170f-4f51-95fa-9cdb38c5374e} true 0 0 right Liberation Sans 9 3 0 0 0 255 255 255 noborder 1 1 8 327 180 30 {6c0cca1d-0205-4af5-9689-f0ae09584691} true 0 0 left Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Q_Filters 8 310 500 27 {928b4ab1-3eb0-4f63-9d91-e93d30ed3d7d} true 0 0 0.00000000 50.00000000 10.00000000 lin continuous -1.00000000 false Q_Filters 448 327 60 30 {642c0bde-f5a4-4152-a74d-b9f752e6e26f} true 0 0 right Liberation Sans 9 3 0 0 0 255 255 255 noborder 1 1 Harmonics 209 364 80 30 {4b481150-4539-41ae-b10b-dc27d4aa2c83} true 0 0 center Liberation Sans 12 0 0 0 255 255 255 1.00000000 1 50 false 10 79 389 130 29 {4e135e75-5181-4d12-b801-79f5db9fb50c} true 0 0 right Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Skip 311 364 130 30 {b984279b-6ce2-47f5-805d-d341e65b26f2} true 0 0 value 1.00000000 Skip Initialisation / true false 8 426 180 30 {351e790b-4ed4-4578-92e9-b6bc8e9965ef} true 0 0 left Liberation Sans 10 3 0 0 0 255 255 255 noborder 1 1 Rolloff 8 409 500 27 {16ac42a7-e8d7-4cb7-81ff-fab93f2203f0} true 0 0 0.00100000 0.99000000 0.75066200 lin continuous -1.00000000 false Rolloff 448 426 60 30 {e687fcfa-dd20-4c7b-aa5c-d5823fdbdb7b} true 0 0 right Liberation Sans 9 3 0 0 0 255 255 255 noborder 1 1