-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lilypond.hs
144 lines (115 loc) · 4.11 KB
/
Lilypond.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
{-# LANGUAGE OverlappingInstances,
EmptyDataDecls,
OverloadedStrings,
MultiParamTypeClasses,
UndecidableInstances,
IncoherentInstances,
DataKinds,
FunctionalDependencies,
FlexibleContexts,
RankNTypes,
TypeSynonymInstances,
ScopedTypeVariables,
UnicodeSyntax,
GADTSyntax,
GADTs,
TypeFamilies,
ConstraintKinds,
InstanceSigs,
GeneralizedNewtypeDeriving,
StandaloneDeriving,
ViewPatterns,
FlexibleInstances #-}
module LilyPrint (lilypond, writeLilypond, lilypondFile, htmlFile) where
import Music (AbstractNote(..),Note2(..), Name(..), Accidental(..),
AbstractPitch2(..), AbstractDur2(..), AbstractInt2(..), AbstractPhrase(..),
Note(..), Music(..), Pitch(..), Interval(..), explodeVoices, apRest)
import Shortcuts
import Data.Ratio
import Data.Char
import qualified Data.Music.Lilypond as L
import Text.Pretty hiding (int)
import System.IO
import System.Environment
import System.Posix.Temp
import System.Process
import GHC.IO.Exception
----
class Lily t where
toLily :: t -> L.Music
lilypond :: t -> Printer
lilypond n = (pretty . toLily) n
instance Lily Note2 where
toLily (AbstractPitch p d) = L.Note (toLPitch p) (Just (toLDur d)) []
toLily r@(Rest _) = let rs = splitRest r
lrs = map toLRest rs
in L.Sequential lrs
toLily (Directive d) = d
toLRest (Rest d) = L.Rest (Just (toLDur d)) []
toLPitch (AbstractPitch2 n a) = L.NotePitch (L.Pitch (toLName n, toLAcc a, toLOctave n)) Nothing
toLName A = L.A
toLName B = L.B
toLName C = L.C
toLName D = L.D
toLName E = L.E
toLName F = L.F
toLName G = L.G
toLName (Up n) = toLName n
toLName (Down n) = toLName n
toLAcc Na = 0
toLAcc (Sh a) = (toLAcc a) + 1
toLAcc (Fl a) = (toLAcc a) - 1
toLOctave (Up n) = (toLOctave n) + 1
toLOctave (Down n) = (toLOctave n) - 1
toLOctave A = 5
toLOctave B = 5
toLOctave _ = 6
toLDur (AbstractDur2 r) = L.Duration r
instance Lily (AbstractPhrase Note2) where
toLily (AbstractPhrase ns) = L.Sequential (map toLily ns)
instance Lily (Music Note2) where
toLily (Voices vs) = L.Simultaneous False (map toLily vs)
toLily (Start v) = toLily (explodeVoices (Start v))
splitRest :: Note2 -> [Note2]
splitRest (Rest d) =
map rest $ splitRest' [breve, semibreve, minim, crotchet, quaver, semiquaver] d
where splitRest' [] r = [r]
splitRest' ((AbstractDur2 c):cs) (AbstractDur2 r)
| (r == c) = [AbstractDur2 c]
| (r >= c) = (AbstractDur2 c) : (splitRest' ((AbstractDur2 c):cs) (AbstractDur2 (r - c)))
| otherwise = splitRest' cs (AbstractDur2 r)
header = vcat ["\\version \"2.15.36\""]
layout = vcat $ ["\\layout {",
-- " \\override Staff.BarLine #'transparent = ##t",
" \\context {",
" \\Staff",
" \\RemoveEmptyStaves",
" }",
" \\context {",
" \\Voice",
" \\remove \"Note_heads_engraver\"",
" \\remove \"Rest_engraver\"",
" \\consists \"Completion_heads_engraver\"",
" \\consists \"Completion_rest_engraver\"",
" }",
"}"]
snippet s = vcat $ [-- "\\clef alto",
-- "\\time 4/2",
s]
lilypondFile m = vcat [header, layout, (snippet (lilypond m))]
writeLilypond dir l = do (t,h) <- mkstemp (dir ++ "/lily.")
hPutStrLn h (runPrinter l)
putStrLn t
htmlFile s = vcat $
[ "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">",
"<!-- header_tag -->",
"<HTML>",
"<body>",
"<p>",
"<lilypond>",
layout,
lilypond s,
"</lilypond>",
"</p>",
"</body>",
"</html>"]