Cabbage Logo
Back to Cabbage Site

Simple Counter

Hi there,

I’m trying to make a simple counter but I have some problem…

I’d like to use a numberbox to count every time a condition is verified
I know how to generate an event if a condition is verified, but how can I make the counter move on by adding the previous result?

Do you have some ideas on how can I get something similar?

thanks! :wink:

Something like this? Move the slider and each time it hits 4 it will increment the Count label?

<Cabbage>
form caption("Untitled") size(400, 300), colour(58, 110, 182), pluginID("def1")
rslider bounds(232, 20, 100, 100), channel("gain"), range(0, 10, 0, 1, 1)
label bounds(36, 19, 177, 19), text("Number"), identchannel("label1")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d 
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

instr 1
	kCounter init 0	
	if changed:k(chnget:k("gain")) == 1 then
		if chnget:k("gain")==4 then
			kCounter+=1
			printks "Hello", 1
			SLabelText sprintfk "text(\"Count:%d\")", kCounter
			chnset SLabelText, "label1" 
		endif
	endif
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>

Yes, that’s exactly what I need. But if I try to apply it to read a MIDI file and to get how many 69 MIDI note I have in the track, it gives strange numbers…248, 286… :joy:
Any idea about what I’m doing wrong?

 form caption("Midi Analysis") size(1000, 380), colour("black"),pluginID("add1"), guirefresh(1) 
    label bounds(36, 19, 177, 19), text("Number"), identchannel("label1")

    </Cabbage>
    <CsoundSynthesizer>
    <CsOptions>
    ; enter name of input midi file
    -F /Users/federico/Music/SampleFede/Analisi/Tarantella_01.mid
    --midi-key=4 --midi-velocity-amp=5 -odac
    </CsOptions>
    <CsInstruments>
    sr = 44100
    ksmps = 32
    nchnls = 1

    instr 1

    kMidi = p4

    kCounter init 0	
    	
    		if kMidi == 69 then
    			kCounter+=1
    			printks "Hello", 1
    			SLabelText sprintfk "text(\"Count:%d\")", kCounter
    			chnset SLabelText, "label1" 
    		endif
    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>

Thanks!!!````

First things first, you should be using i-rate variables for the MIDI note. I’ve never used MIDI files with Cabbage, but perhaps you should try using one of the MIDI note opcodes instead of trying to use --midi-key=4 --midi-velocity-amp=5. If you add the following line to your instrument code:

print notnum

Does it print the correct MIDI values? Also note that you should never use -odac with Cabbage, it looks after audio output itself not Csound. I can try this out a little later if I get a chance, but I’m a little snowed under at the minute.

Is the first time also for me that I’m using MIDI with Csound and the other MIDI opcodes seems to be mainly for MIDI controllers.
In the Csound output I can see the MIDI note number and it print “hello” every time a 69 note is played.
Sorry about -odac, I pasted the file path from a Winxsound patch.:sweat_smile:
No hurry! many thanks

Try this one.
MIDINoteCounter.csd (817 Bytes)
Feel free to bombard me with questions :wink:

It works! I understand that I should use i-rate for the MIDI note, but I have one question…why do I need a global variable for giCounter?

Because that instrument gets triggered time a new note comes in.