; [ʞ] digital.orc
;  ~ lexi hale <lexi@hale.su>
;  🄯 CC-NC-BY-SA 3.0
;  ? chirpy beepy blarpy noises for UI and computer
;    sound effects.

; conventions
; - all waveforms start with aw*
; useful idioms:
;  - current time factor: linseg(0,p3,1)
; std parameters
;   p2 = start time
;   p3 = duration
;   p4 = amp

#include "base.orc"

instr rumble
	aton jspline 1, 100,500
	;amp adsr 0.2,0.4,.9, 0.2
	kamp bpf linseg(0,p3,1), \
		0.0, 0.0, \
		0.1, 0.8, \
		0.5, 1.0, \
		0.7, 0.8, \
		1.0, 0.0

	aw poscil p4*kamp, 200 + 100*aton
	out aw
endin

instr spindown
	avol fade p3,p4,0.5,0.9
	kpr linseg 0, p3, 1.0
	; remap time so our spindown starts slowly but rapidly
	; speeds up as the effect progresses
	at bpf a(kpr), 0,0, 0.6,0.2, 1,1

	; noise component
	al = .3 + a(unirand:k(.7))
	af bpf at, 0, 700, .5, 400, 1,200
	aw poscil al*avol, af

	out aw
endin

instr blarp
	apr linseg 0,p3,1
	avol bpf apr, 0,0,  0.3,1,  1,0
	
	aff poscil 1.0, 10
	afr bpf aff, \
		0.0, 400, \
		0.2, 600, \
		0.5, 500, \
		0.6, 700, \
		0.9, 300, \
		1.0, 400
	aw poscil avol*p4, afr
	out aw
endin

instr chirp
	abeep poscil 1, p5
	abeep bpf abeep, \ ; apply chirp envelope
		0.0, 0.0, \
		0.4, 0.1, \
		0.6, 0.9, \
		1.0, 1.0
	aw poscil abeep*p4, p6
	out aw
endin

opcode warbulator, kkk, kkk
	kfreq, kfi, kfo xin

	kt1 init 0
	kt2 init 0
	kvol poscil 1, kfreq
	kvol bpf kvol, kfi,0, kfo,1
	if kvol == 0 then
		kt1 = unirand:k(1.0)
		kt2 = unirand:k(1.0)
	endif

	xout kvol, kt1, kt2
endop

instr warble
	afade fade p3,p4, 0.1,0.9

	kvol, kton, k_ warbulator p5, p6,p7
	kton bpf kton, 0,p8, 1,p9

	aw poscil afade * a(kvol), kton
	out aw
endin

instr warple
	afade fade p3,p4, 0.2,0.8
	ap pulse p3,.5
	kv, kwb, kwv warbulator p5, p6, p7

	atn  bpf ap, 0,p8, 1,p9
	ktno = kwb * p10
	av = a(kv) * bpf:a(a(kwv), 0,.2, 0.9,0.3, 1,1)
	aw poscil afade*av, atn+a(ktno)
	out aw

endin

instr wslope
	avol fade p3,p4,0.3,0.8

	afb bpf linseg:a(0,p3,1), 0,p7, 0.5,p8, 1,p9
	afw poscil 1.0, p5
	afw bpf afw, 0,0, 0.3,0.1, 0.7,0.8, 1,1
	afreq = afb - (afw*p6)

	aw poscil avol, afreq
	out aw
endin

instr winddown
	avol fade p3,p4,0.1,0.5

	afmf bpf linseg:a(0,p3,1), 0,100, 0.2,30, 1,1
	afm poscil 1.0, afmf

	afn bpf linseg:a(0,p3,1), 0,1200, 0.5,1000, 0,700

	aw poscil avol, afn - afm*400
	out aw
endin

instr blare
	avol fade p3,p4,0.03,0.9
	an poscil 1.0, 30
	an bpf an, 0,0, 0.9,0, 1,1
	ar bpf linseg:a(0,p3,1), 0,100, .5,300, 1,400
	aw poscil avol, 500 + (an*ar)
	out aw
endin

instr tune
	anois unirand 1
	aw poscil p4, anois * 1000
	out aw
endin
