; this is purely experimental thing which is tracking voice via microphone and outputing MIDI notes
;
; this need a LOOOOOOOOOOT of EXPERIMENTS to be properly implemented
<Cabbage>
form caption("Whistleblower") size(400, 300), colour(58, 110, 182), pluginid("def1")
rslider bounds(8, 12, 100, 100), channel("gain"), range(0, 4, 1, 0.5, 0.01), text("Gain"), trackercolour(0, 255, 0, 255), outlinecolour(0, 0, 0, 50), textcolour(0, 0, 0, 255)
vslider bounds(118, 12, 50, 99) range(0, 1, 0, 1, 0.001) channel("amp") active(0)
vslider bounds(176, 12, 50, 101) range(0, 1, 0, 1, 0.001) active(0) channel("rms")

hslider bounds(8, 120, 387, 50) range(0, 800, 0, 0.5, 0.01) channel("freq") active(0)
label bounds(78, 180, 142, 68) text("A") channel("note") identchannel("notewidget") value(1)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
ksmps = 16
nchnls = 2
0dbfs = 1


opcode F2M, k, kk
    kFq, kRnd xin
    kNotNum = 12 * (log(kFq/220)/log(2)) + 57
    kNotNum = (kRnd == 1 ? round(kNotNum) : kNotNum)
    xout kNotNum
endop

instr 1


ifftsize = 1024
iwtype = 1    /* cleaner with hanning window */

a1 inch 1 ;Realtime audio input
kgain chnget "gain"

aSig = a1 * kgain


kRms  rms  aSig
fsig pvsanal   aSig, ifftsize, ifftsize/4, ifftsize, iwtype
kfr, kamp pvspitch   fsig, 0.03

kfreqp portk kfr, 0.05
kampp portk kamp, 0.2


; kamp = 0 z nějakého důvodu
;printks "kfr = %d, kamp=%f, krms=%f\n", .025, kfr, kamp, kRms

chnset kamp, "amp"
chnset kRms, "rms"
chnset kfreqp, "freq"

kmetro metro 8

if kmetro==1 && kfreqp>1 then
    kmidinote F2M kfreqp, 1
    
    printks "%d, %f\n", 0, kmidinote, kampp
    
    
    SIdent sprintfk "text(%d)", kmidinote % 12
    chnset SIdent, "notewidget"
endif

aOut vco2 kampp, kfreqp, 12
outs aOut, aOut 


endin

</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
f0 z
;starts instrument 1 and runs it for a week
i1 0 [60*60*24*7] 
</CsScore>
</CsoundSynthesizer>
