Mozart golf - mini "Rondo" Output "Mozart - Alla Turca" to stdout (see the sample for "reference implementation")

Try to find how to pack both synthesizer and the music into minimal size.

Requirements:


Format suitable for feeding into aplay -f cd (signed 16-bit little endian, 2 channels);
Entire music should be played (no skipped notes or parts, at least not less than in sample program), polyphony is not required though;
Can't just call /usr/bin/timidity, /usr/bin/sox or something like that (e.g. require to install a special music module);
Can't access network or assume that the music is available locally;


"Reference implementation" with parsable score: https://gist.github.com/vi/5478693
(Old sample Perl program: https://gist.github.com/vi/5447962)
Polyphonic, Haskell, 2826 3177 4719

Audio output:
https://www.dropbox.com/s/nba6489tfet740r/hs-golf-turca.ogg

Features:


All notes from the right hand. I could of course add the left hand, too (done that).
Proper articulation of the staccato notes etc.
Reasonably nice sound with dynamics. Not just simple volume modulation, but proper morphing of the attack character and overtone content, like you get on a real piano actually, rather more... hey this piece is supposed to be imitating Turkish Janissary bands, right?
Reverb. Doesn't sound amazingly great, but not too bad, either.
Dynamic compression. Don't ask...
Dithering of the output. This is kind of ridiculous: with proper 16-bit resolution, hardly anybody would hear the quantisation artifacts, but to avoid including the binary library, I effectively use only 7-bits resolution, which I can cover with ASCII output. The dither itself is rather loud, no noise-shaping...
Multithreaded calculation of polyphonic chords.




import Control.Parallel
main=mapM_ (\(d,s)-&gt;(\p-&gt;p&gt;&gt;p&gt;&gt;p&gt;&gt;p).putChar.toEnum.round.(+d).(*62).min 2.abs$s+1).zip(dθ 1).lim.rev.hgp 9. pl 9e6 (\_-&gt;0) . ä
 $[mT%8.1,t2%16.1,t3(∡7)%8,t4%8,t5%16,t3(∡7)%8,mT%8.1,t2%16.1,t3(tev arp8)%8,cdT%99] &gt;&gt;= \e-&gt;[e,e]
mM=ä[2-^8,1-^8,0-^8,1-^8,3-^4]
cM=ä[7-^20,8-^20,9.^4,F[(7,0),(6,1)](map((∡2).(.^4))[6,5,6])%0.75]
cMv=ä[10-^2,8.^4,9.^4,hom(.^4)[[24,5],[23,8,12],[22,4],[21,6,9],[22,3],[19,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1∡2⋎(11-^1)
em d=5-^d∡2⋎(9-^1)
cdM=ḋ[4-^8,3-^8,2.^8,3.^8,cdM%1]
cdT=ḋ[ä[3-^(8/3)∡7,10-^6,am 1,am 1,cdM&amp;7%1,dm,aam 4.05%1,em(4/3),12-^4,am 1,am 1,cdM&amp;7%1,dm,aam 1%1,eam 4%1]%12.5⋎(ä(sil%(11/24) : map((%1).(m3a&amp;))[4,4,4,0,4,1,4,4,4,0,4,1])&amp;(-18)%13.1)
 ,p(ä[ä[ä[8-^2]⊿2%h,aar%(3/8),10-^8,aar%1,aar%1,cdM&amp;7%1,11-^1,vh 11(10-^4)%1,9-^(4/3)]%7⋎(ä(map m3ra[0,0,0,0,1,0,2])&amp;(-7)%7)])%6.75
 ,ä[p(ä[12-^4])%(1/4),am 1,am 1,cdM&amp;7%1,dm,aam 1%1,eam 4%1,amf,ä[3-^4,1-^υ,5-^4,1-^υ,3-^4,1-^4,3-^4,1-^4,5-^4,1-^2]%3.75∡7,ä[amf∡(-14)]%0.56,ä[amf∡(-14)]⊿0.8%1]%12⋎(ä(sil%(1/8):map((%1).(m3a&amp;))[4,4,4,0,4,1,4,4,4]++[m3a&amp;4%h,m3a&amp;4%h,5-^(8/5)])&amp;(-18)%12)]
type D=Double
data F=N Int D|F[(Int,D)][([F],D)]
φ⇸F a fs=F a$map(\(f,d)-&gt;(map φ f,d))fs
_⇸N i d=N i d
i##c
 |i&lt;1=(i+7)##c/2
 |i&gt;7=(i-7)##c*2
 |1&gt;0=1.06**(c i+case i of{1-&gt;0;3-&gt;3;4-&gt;5;5-&gt;7;6-&gt;8;7-&gt;10;_-&gt;fri i})
pl dur acc(N n v)=(\ω η-&gt;map(sin.(\x-&gt;x+τ x^2/η). \i-&gt;v*exp(-i*η/s)*τ(i*v)*(0.8-τ((i-dur)/90))*sin(i*ω))[1..dur])(n##acc/15.5).exp$fri n/9
pl dur acc(F accm fs)=pl' dur (foldr(\(q,m)f i-&gt;if q==i then m else f i)acc accm) fs
pl' dur _ _|dur&lt;=0 = []
pl' dur _ []=map(\_-&gt;0)[1..dur]
pl' dur acc((f,dr):fs)|n&lt;-min dr dur=trans(round n)(foldr1(\a b-&gt;sum a`par`sum b`pseq`zw(+)a b)(map(pl(n+99)acc)f))$pl'(dur-dr)acc fs
trans n a b|(f,ol)&lt;-splitAt n a,(or,l)&lt;-splitAt 99 b=f++zw(+)ol or++l
fri=fromIntegral
F a fs#q=F((q,1):a)fs
N i d&amp;n=N(n+i)d
f&amp;n=(&amp;n)⇸f
N i d⇅n=N(n-i)d
f⇅n=(⇅n)⇸f
N i d⇆_=N i d
F a fs⇆n=F a.reverse$take n fs
N i d⊿v=N i$d*v
f⊿v=(⊿v)⇸f
p=(⊿0.3)
n.^q=([F[][([N n 1],s/2/q)]],s/q)
n-^q=([N n 1],s/q)
(l,d)⋎(r,_)=(l++r,d)
(l,d)∡j=(l++map(\h-&gt;ä[h⊿0%0.01,h&amp;j%100])l,d)
f%t=([f],s*t)
tr n=F[]$cycle[n-^15,(n+1)-^20]
ä=F[];ḋ=F$zip[6,3,7][1,1,1]
lp=ä.repeat
sil=N 0 0
tev f(l,d)=(map f l,d)
h=1/2
υ=4/3
s=4e+4
sq d=ä.map(-^d)
es=sq 8 
arp8 n@(N i v)=F[][([n,ä[n⊿0%(1/8),n&amp;7⊿(v/υ)%100]],s)]
arp8 f=arp8⇸f
hom q=ä.map(foldr((⋎).q)$sil%1)
dθ l=2*asin l/pi:dθ(abs.sin$l*1e+9)
rev ls=(\z-&gt;z id(foldr(\m sg-&gt;(\v-&gt;z(*v)(map(*0)[0..m*14349]++sg)sg)$abs(cos$(m*3)^2)-0.6)ls.take 9$dθ 1)ls)$(.lwp 3 0).zw.((+).)
lwp ω c(x:l)=c:lwp ω((x+c*ω)/(ω+1))l
lwp _ _ _=[]
hgp ω l=zw(-)l$lwp ω 0 l
lime e(x:l)
 |abs(e*x)&gt;1,e'&lt;-((e*8+abs(1/x))/9)=e':lime e' l
 |1&gt;0=e:lime((e*49999+1)/5e4)l
lime _[]=[]
lim ls=zw(\a u-&gt;τ$a/9+max(-2)(min 2$a*u)/6)(map(*0)[0..500]++ls).lwp 9 0.lime 1$hgp 9 ls
zw=zipWith
τ=tanh





  $ make
  ghc -o bin/def0-hs def0.hs -O2 -fllvm -threaded
  [1 of 1] Compiling Main             ( def0.hs, def0.o )
  Linking bin/def0-hs ...
  time sh -c 'bin/def0-hs +RTS -N4 > hsoutp.pcm'
  189.39user 138.41system 2:06.62elapsed 258%CPU (0avgtext+0avgdata 6440240maxresident)k
  0inputs+0outputs (0major+403037minor)pagefaults 0swaps
  ffmpeg -loglevel panic -y -f s16le -ar 44.1k -ac 2 -i hsoutp.pcm hsoutp.ogg




Here's a partially ungolfed and commented version: https://gist.github.com/leftaroundabout/5517198.