Analyze normalized signal

Analyze normalized signal

<CsoundSynthesizer>
<CsOptions>
-odac   -iadc   ;;;realtime audio I/O
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 1
0dbfs  = 1

pyinit

instr 1
	pyruni "import time"
	kLastRms init 0
	kLastAttack init 0
	iRmsDiffThreshold init .1

	aIn in

	kRmsOrig rms aIn
	kSmoothRms tonek kRmsOrig, 0.01
	kSmoothRms max kSmoothRms, 0.0001

	aNorm = 0.1 * aIn / a(kSmoothRms)

	kRms rms aNorm
	kRmsDiff = kRms - kLastRms

	kTime times

	if (kRmsDiff > iRmsDiffThreshold && kTime - kLastAttack > 0.05) then
		kLastAttack times
		pyrun "print 'clap detected', time.time()"
	endif

	out aNorm
	kLastRms = kRms
endin
</CsInstruments>
<CsScore>

i 1 0 50
e
</CsScore>
</CsoundSynthesizer>

<CsoundSynthesizer>
<CsOptions>
-odac   -iadc   ;;;realtime audio I/O
</CsOptions>
<CsInstruments>

sr = 44100
ksmps = 32
nchnls = 1
0dbfs  = 1

pyinit

instr 1
	pyruni "import time"
	kLastRms init 0
	kLastAttack init 0
	iRmsDiffThreshold init .0002

	aIn in
	kRms rms aIn
	kRmsDiff = kRms - kLastRms

	kSmoothRms tonek kRms, 0.01

	kTime times

	if (kRmsDiff > iRmsDiffThreshold && kTime - kLastAttack > 0.05) then
		kLastAttack times
		pyrun "print 'clap detected', time.time()"
	endif

	printk2 kLastAttack

	aIn = 0.1 * aIn / a(kSmoothRms)

	;if (kRatio < 0.5) then
		;TODO: check time since last transient (attack)
	;	kLastDescent times
	;endif

	;printk2 kRms
	;printks "lastattack", 0.01, kLastAttack

	out aIn
	kLastRms = kRms
endin
</CsInstruments>
<CsScore>

i 1 0 50
e
</CsScore>
</CsoundSynthesizer>


Got to work on Windows, removed irrelevancies, commented.

<CsoundSynthesizer>
<CsOptions>

; Audio     Audio
; output    output
; software  hardware  Audio        MIDI
; buffer    buffer    output       input
; frames    frames    port number  port number

; Command-line options for Windows:

  -b100     -B100     -o dac8      -M 0

</CsOptions>
<CsInstruments>

; Sample frames per second.

sr = 44100

; Sample frames per control period.

ksmps = 100

; Audio output channel.

nchnls = 1

; Output amplitudes will be normalized so 32767 is maximum.

0dbfs = 32767

; Allocate global sine wave table for oscillators.

giafno ftgen 1,          0,      65537,  10,     1

instr 1
			; Note duration is -1 for MIDI (note stays on till turned off), or else comes from the score.
                        mididefault             -1, p3
			; p4 is MIDI key translated to linear octaves, p5 is MIDI velocity, or else comes from the score.
                        midinoteonoct           p4, p5
			; Print pfield values.
                        print                   p2, p3, p4, p5
			; Simple sine wave instrument.
a1                      oscili                  ampdb(p5), cpsoct(p4), giafno
                        out                     a1
endin

</CsInstruments>
</CsoundSynthesizer>

<CsoundSynthesizer>
<CsOptions>
csound -b100 -B100 -odac:plughw:0 -M3
</CsOptions>
<CsInstruments>
sr = 44100
kr = 441
ksmps = 100
nchnls = 1
0dbfs = 32767
iafno ftgen 1,          0,      65537,  10,     1 ; Sine wave.
print ampdb(80)

instr 1,2,3 ; Filtered noise, Michael Bergeman
; INITIALIZATION
                        mididefault             20, p3
                        midinoteonoct           p4, p5
                        print                   p2, p3, p4, p5
a1                      oscili                  ampdb(p4), cpsoct(p4), iafno
                        out                     a1
endin

</CsInstruments>
<CsScore>
f 0 1000

</CsScore>
</CsoundSynthesizer>