-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLily.hs
More file actions
150 lines (118 loc) · 4.35 KB
/
Copy pathLily.hs
File metadata and controls
150 lines (118 loc) · 4.35 KB
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
144
145
146
147
148
149
150
-- This file is part of KSQuant2.
-- Copyright (c) 2010 - 2011, Kilian Sprotte. All rights reserved.
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
module Lily (showLily
, Score
, Part
, Voice
, Dur(..)
, Elt(..)
, Measure(..)
, Measures
, Name(..)
, Accidental(..)
, Pitch(..)
, powerToSimpleDur)
where
import qualified Types as T (WInt, WRat)
import Data.Ratio ((%), denominator)
import Data.List (intercalate)
import qualified AbstractScore as A (Score
, Part
, Voice
, scoreParts
, partVoices
, voiceItems)
data Measure = Measure Int Int [Elt]
deriving Show
type Score = A.Score Measures
type Part = A.Part Measures
type Voice = A.Voice Measures
type Measures = [Measure]
-- | This represents durations without dot.
data SimpleDur = D1 | D2 | D4 | D8 | D16 | D32 | D64 | D128
deriving (Show,Enum)
-- | 1 / 2^n
powerToSimpleDur :: Int -> SimpleDur
powerToSimpleDur = toEnum
data Dur = Dur SimpleDur Int
deriving Show
type Tied = Bool
type Octave = T.WInt
data Name = A | B | C | D | E | F | G
deriving Show
data Accidental = Natural | Sharp | Flat
deriving Show
data Pitch = Pitch Name Accidental Octave
deriving Show
data Elt = Chord Dur [Pitch] Tied
| Rest Dur
| Times Int Int [Elt]
deriving Show
simpleDurToRatio :: SimpleDur -> T.WRat
simpleDurToRatio x =
case x of
D1 -> 1 % 1
D2 -> 1 % 2
D4 -> 1 % 4
D8 -> 1 % 8
D16 -> 1 % 16
D32 -> 1 % 32
D64 -> 1 % 64
D128 -> 1 % 128
pitchToLily :: Pitch -> String
pitchToLily (Pitch B Natural 3) = "b"
pitchToLily (Pitch C Natural 4) = "c'"
pitchToLily (Pitch C Sharp 4) = "cis'"
pitchToLily (Pitch D Natural 4) = "d'"
pitchToLily (Pitch E Natural 4) = "e'"
pitchToLily (Pitch F Natural 4) = "f'"
pitchToLily p = error $ "pitchToLily not implemented: " ++ show p
durToLily :: Dur -> String
durToLily (Dur x d) = (show . denominator . simpleDurToRatio) x ++ replicate d '.'
eltToLily :: Elt -> String
eltToLily (Chord d ps tie) = "<" ++ ps' ++ ">" ++ durToLily d ++ if tie then " ~" else ""
where ps' = unwords (map pitchToLily ps)
eltToLily (Rest d) = 'r' : durToLily d
eltToLily (Times n d xs) = "\\times " ++ show n ++ "/" ++ show d ++ " { " ++
unwords (map eltToLily xs) ++ " }"
measureToLily :: (Measure, Bool) -> String
measureToLily (Measure n d xs, change) =
(if change then
"\\time " ++ show n ++ "/" ++ show d ++ " "
else "")
++
unwords (map eltToLily xs) ++ " |"
-- | Return a list of equal length as xs indicating if the corresponing
-- elt of xs is different from its predecessor.
indicateChanges :: Eq b => [b] -> [Bool]
indicateChanges xs = True : map (not . uncurry (==)) (zip (drop 1 xs) xs)
measureTimeSignature :: Measure -> (Int, Int)
measureTimeSignature (Measure n d _) = (n,d)
measureChanges :: Measures -> [Bool]
measureChanges xs = indicateChanges (map measureTimeSignature xs)
measuresToLily :: Measures -> String
measuresToLily xs = intercalate "\n " $ zipWith (curry measureToLily) xs (measureChanges xs)
voiceToLily :: Voice -> String
voiceToLily v = "{ " ++ measuresToLily (A.voiceItems v) ++ " }"
wrap :: String -> String
wrap content =
"\\version \"2.12.3\"\n" ++
"\\header { }\n" ++
"\\score {\n" ++
" <<\n " ++
content ++ "\n" ++
" >>\n" ++
" \\layout { }\n" ++
"}\n"
showLily :: Score -> String
showLily s = wrap (intercalate "\n" (map voiceToLily (concatMap A.partVoices (A.scoreParts s))))