# some envelopes....

	# attack time, decay time, sustain level, release time
adsr[a,d,s,r] = [a:t_/a, d:1-((1-s)*t_/d), -1:s, r:s-s*t_/r];
	# attack time, decay time, sustain level, instant release (for testing)
ads[a,d,s] = [a:t_/a, d:1-((1-s)*t_/d), -1:s];
	# fixed sustain time:
adstr[a,d,st,s,r] = [a:t_/a, d:1-((1-s)*t_/d), st:s, r:s-s*t_/r];
	# no sustain/release, just attack then decay to 0
ad[a,d] = [a:t_/a, d: 1-(t_/d)];
	# attack, then sustain at 1.0, then release for r
ar[a,r] = [a: t_/a, -1: 1, r: 1-(t_/r)];
	# no attack or delay, immediately begin at sustain level
sr[s,r] = [-1: s, r: s-s*t_/r];
	# just an attack, sustains indefinitely
a[a] = [a: t_/a, -1: 1];
	# a tremolo, or a wave from (1.0-vol) to 1.0, starting at the bottom
trem[freq,vol] = ((1-vol)+((1-cos(freq*t_))*vol/2));


# some FM synthesis operators

	# no operator, just carrier
op0[x] = sin(t*x);
	# C:M
op1[C,M,A,x] = sin(C*t*(x+A*sin(M*x*t)));
	# C:(M1+M2)
op2a[C,M1,M2,A1,A2,x] = sin(C*t*(x+A1*sin(M1*x*t)+A2*sin(M2*x*t)));
	# C1:M + C2:M
op2ac[C1,C2,M,A1,A2,x] = 0.5*(op1(C1,M,A1,x)+op1(C2,M,A2,x));
	# C:M1:M2
op2r[C,M1,M2,A1,A2,x] = sin(C*t*(x+A1*sin(M1*t*(x+A2*sin(M2*x*t)))));
	# C:(M1+M2+M3)
op3a[C,M1,M2,M3,A1,A2,A3,x] = sin(C*t*(x+A1*sin(M1*x*t)+A2*sin(M2*x*t)+A3*sin(M3*x*t)));
	# C:M1:M2:M3
op3r[C,M1,M2,M3,A1,A2,A3,x] = sin(C*t*(x+A1*sin(M1*t*(x+A2*sin(M2*t*(x+A3*sin(M3*x*t)))))));


# some arpeggios
#   C  = 1.000      C# = 1.059      D  = 1.122      D# = 1.189
#   E  = 1.260      F  = 1.335      F# = 1.414      G  = 1.498
#   G# = 1.587      A  = 1.682      A# = 1.782      B  = 1.888

	# 3 octaves...
oct[l] = [l: 1, l: 2, l: 4];
oct_l1[l] = [l: 1, l: 2, l: 4]:;
oct_l2[l] = [l: 1, l: 2, l: 4, l: 2]:;
	# major chord (as the keyboard pounds)
maj[l] = [l: 1, l: 1.260, l: 1.498];
maj_l1[l] = [l: 1, l: 1.260, l: 1.498]:;
maj_l2[l] = [l: 1, l: 1.260, l: 1.498, l: 1.260]:;
maj7[l] = [l: 1, l: 1.260, l: 1.498, l: 1.888];
maj7_l1[l] = [l: 1, l: 1.260, l: 1.498, l: 1.888]:;
maj7_l2[l] = [l: 1, l: 1.260, l: 1.498, l: 1.888, l: 1.498, l: 1.260]:;
	# major chord (perfect ratios, 4-5-6 or 8-10-12-15
pmaj[l] = [l: 1, l: 1.250, l: 1.500];
pmaj_l1[l] = [l: 1, l: 1.250, l: 1.500]:;
pmaj_l2[l] = [l: 1, l: 1.250, l: 1.500, l: 1.250]:;
pmaj7[l] = [l: 1, l: 1.250, l: 1.500, l: 1.875];
pmaj7_l1[l] = [l: 1, l: 1.250, l: 1.500, l: 1.875]:;
pmaj7_l2[l] = [l: 1, l: 1.250, l: 1.500, l: 1.875, l: 1.500, l: 1.250]:;
	# minor chord (as the keyboard pounds)
min[l] = [l: 1, l: 1.189, l: 1.498];
min_l1[l] = [l: 1, l: 1.189, l: 1.498]:;
min_l2[l] = [l: 1, l: 1.189, l: 1.498, l: 1.189]:;
min7[l] = [l: 1, l: 1.189, l: 1.498, l: 1.782];
min7_l1[l] = [l: 1, l: 1.189, l: 1.498, l: 1.782]:;
min7_l2[l] = [l: 1, l: 1.189, l: 1.498, l: 1.782, l: 1.498, l: 1.189]:;
	# minor chord (perfect ratios, 16-19-24-28)
pmin[l] = [l: 1, l: 1.1875, l: 1.500];
pmin_l1[l] = [l: 1, l: 1.1875, l: 1.500]:;
pmin_l2[l] = [l: 1, l: 1.1875, l: 1.500, l: 1.1875]:;
pmin7[l] = [l: 1, l: 1.1875, l: 1.500, l: 1.750];
pmin7_l1[l] = [l: 1, l: 1.1875, l: 1.500, l: 1.750]:;
pmin7_l2[l] = [l: 1, l: 1.1875, l: 1.500, l: 1.750, l: 1.500, l: 1.1875]:;
	# white keys
whi[l] = [l: 1.000, l: 1.122, l: 1.260, l: 1.335, l: 1.498, l: 1.682, l: 1.888, l: 2.000];
whi_l1[l] = [l: 1.000, l: 1.122, l: 1.260, l: 1.335, l: 1.498, l: 1.682, l: 1.888, l: 2.000]:;
whi_l2[l] = [l: 1.000, l: 1.122, l: 1.260, l: 1.335, l: 1.498, l: 1.682, l: 1.888, l: 2.000, l: 1.888, l: 1.682, l: 1.498, l: 1.335, l: 1.260, l: 1.122]:;
	# black keys
bla[l] = [l: 1.059, l: 1.189, l: 1.414, l: 1.587, l: 1.782];
bla_l1[l] = [l: 1.059, l: 1.189, l: 1.414, l: 1.587, l: 1.782]:;
bla_l2[l] = [l: 1.059, l: 1.189, l: 1.414, l: 1.587, l: 1.782, l: 1.587, l: 1.414, l: 1.189]:;
	# every key (chromatic scales)
all[l] = [l: 1.000, l: 1.059, l: 1.122, l: 1.189, l: 1.260, l: 1.335, l: 1.414, l: 1.498, l: 1.587, l: 1.682, l: 1.782, l: 1.888, l: 2.000];
all_l1[l] = [l: 1.000, l: 1.059, l: 1.122, l: 1.189, l: 1.260, l: 1.335, l: 1.414, l: 1.498, l: 1.587, l: 1.682, l: 1.782, l: 1.888, l: 2.000]:;
all_l2[l] = [l: 1.000, l: 1.059, l: 1.122, l: 1.189, l: 1.260, l: 1.335, l: 1.414, l: 1.498, l: 1.587, l: 1.682, l: 1.782, l: 1.888, l: 2.000, l: 1.888, l: 1.782, l: 1.682, l: 1.587, l: 1.498, l: 1.414, l: 1.335, l: 1.260, l: 1.189, l: 1.122, l: 1.059]:;


# some instruments

  # ??
fa[x,y] := 0.2*y*adsr(0.03,0.03,0.2,0.5)*op3a(1,3,4,5,x/4,x/3,x/2,x);
  # fb_h is a synthbassy instrument
fb_h[x,y] := 0.2*y*adstr(0.03,0.03,0.1,0.2,0.5)*op3a(1,3,4,5,x/4,x/3,x/2,x/4);
  # fb1 is a minor arpeggio of fb_h with fixed spacing
fb1[x,y] := [0.2:: fb_h(x,y), 0.2:: fb_h(x*1.189,y), 0.3:: fb_h(x*1.498,y)];
  # fb2 is a minor arpeggio of fb_h with note up spacing
fb2[x,y] := [-1:: fb_h(x,y), 0.2:: fb_h(x*1.189,y), 0.3:: fb_h(x*1.498,y)];
  # fc is flat spacey
fc[x,y] := 0.1*y*adsr(0.1,0.05,0.7,1.0) * op3a(3,1,1.5,2,x/2,x/2,x/2,x);
  # fd is sharp bizarre eerie
fd[x,y] := 0.1*y*adsr(0.05,0.05,0.2,0.5) * op3a(16,1,2,4,x/2,x/2,x/2,x);
  # fe is a church organy sort of affair
fe[x,y] := 0.2*y*adsr(0.07,0.07,0.5,0.3)*op2ac(1,1.5,3,x/2,x/2,x);
  # ff is just a sample of what is to come now that i've started playing
ff[x,y] := 0.1*y*adsr(0.03,0.03,0.2,0.5)*op3a(1,3,4,5,x*4,x*30*adsr(0.1,0.1,0.1,0.5),x*2,x);
  # like ff, but 
fg[x,y] := 0.2*y*adsr(0.03,0.03,0.2,0.5)*op3a(1,3,4,5,x*4,x*30*(1.0-adsr(0.05,0.1,0.5,0.5)),x*2,x);
  # marimba
fh[x,y] := 0.2*y*adstr(0.01,0.03,0.03,0.1,1.0)*op2a(1,7,4,1000*adstr(0.03,0.03,0.03,0.1,1.0),40,x*2);
  # maximum plinky version of above, like xylophone
fi[x,y] := 0.2*y*adstr(0.005,0.03,0.03,0.1,1.0)*op2a(1,7,4,x*50*adstr(0.01,0.03,0.03,0.3,1.0),40,x*2);
  # maximum spooky
fj[x,y] := 0.2*y*adstr(1,0.03,0.03,0.1,1.0)*op2a(1,7,4,x*50*adstr(0.01,0.03,0.03,0.3,1.0),40,x*2);
  # dra is a miscellaneous drum, suitable at the bottom maybe for bass or
  # tom tom, higher up more like marimba
dra[x,y] := 0.4*y*adstr(0.005,0.02,0.1,0.05,0.2)*op1(1,7,x/2,x/8);

chirpy1[x,y] := 0.1*y*adsr(0.1,0.3,0.6,1.0)*op2a(1,sin(30*adsr(0.1,0.3,0.7,1.0)*t_)+8,4,x/3,x/2,x);
chirpy2[x,y] := 0.1*y*adsr(0.1,0.3,0.6,1.0)*op2a(1,sin((30+(x/30))*adsr(0.1,0.3,0.7,0.9)*t_)+8,4,x/3,x/2,x);

churchy1[x,y] := 0.2*y*adsr(0.1,0.3,0.5,0.3)*op2a(1,1.5,3,x/2,adsr(0.5,0.1,0.9,0.5)*4000+.4,x);

pulsating1[x,y] := 0.2*y*adsr(0.1,0.1,0.5,0.6)*trem(7,0.85)*op2a(1,1.5,3,x/2,adsr(0.1,0.1,0.9,0.6)*trem(4.241,0.5)*1500+.4,x);
