-
Notifications
You must be signed in to change notification settings - Fork 0
/
chorale.hs
208 lines (154 loc) · 6.34 KB
/
chorale.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import Control.Applicative
import Data.List (permutations, nub, intersperse, transpose, inits, group, minimumBy)
import Data.Char (toLower, isUpper, isLetter)
import System.Environment (getArgs)
import Debug.Trace (trace)
type Pitch = Integer
type Interval = Integer
type Note = Integer
type Chord = (Pitch, ChordType)
type ChordType = [Interval]
type PitchedChord = [Pitch]
majorChord :: ChordType
majorChord = [0,4,7]
minorChord :: ChordType
minorChord = [0,3,7]
dimChord :: ChordType
dimChord = [0,3,6]
augChord :: ChordType
augChord = [0,4,8]
maj7Chord :: ChordType
maj7Chord = [0,4,7,11]
dom7Chord :: ChordType
dom7Chord = [0,4,7,10]
min7Chord :: ChordType
min7Chord = [0,3,7,10]
toPitchedChord :: Chord -> PitchedChord
toPitchedChord (pitch, chordType) = map (+pitch) chordType
-- c2 = 0
type Range = (Pitch, Pitch)
bassRange = (9, 28) -- (4, 28)
tenorRange = (12, 26) -- (12, 31)
altoRange = (22, 36) -- (19, 36)
sopranoRange = (24, 40) --(24, 43)
ranges :: [Range]
ranges = [bassRange, tenorRange, altoRange, sopranoRange]
ascending :: PitchedChord -> Bool
ascending (x:xs)
| null xs = True
| x < head xs = ascending xs
| otherwise = False
withinOctaveGaps :: PitchedChord -> Bool
withinOctaveGaps (x:xs)
| null xs = True
| x < (head xs) && (head xs) - x <= 12 = withinOctaveGaps xs
| otherwise = False
isValid :: PitchedChord -> Bool
isValid xx@(x:xs) = (x < (head xs)) && withinOctaveGaps xs
inRangeChords :: PitchedChord -> [PitchedChord]
inRangeChords pChord = filter isValid $ concatMap (combine.process) producedChords
where process = zipWith clamp ranges
producedChords = shuffle pChord
clamp :: Range -> Note -> [Pitch]
clamp range@(lo, hi) x
| x < lo = clamp range (x+12)
| x > hi = clamp range (x-12)
| otherwise = takeWhile (>=lo) [x,x-12..] ++ takeWhile (<=hi) [x+12, x+24..]
shuffle :: [a] -> [[a]]
shuffle xx@(x:xs) = permutations $ x:xx
shuffle1 :: Eq a => [a] -> [[a]]
shuffle1 xs = nub $ concatMap (\x->permutations $ x:xs) xs
combine :: [[a]] -> [[a]]
combine [] = [[]]
combine (x:xs) = (:) <$> x <*> (combine xs)
-- Functions for transitions between chords
badTransition :: PitchedChord -> PitchedChord -> Bool
badTransition [] b = False
badTransition a b = parallel (a,b) 7 || parallel (a,b) 12 -- || jumpsMoreThan 7 (a,b))
bestTransition :: PitchedChord -> [PitchedChord] -> PitchedChord
bestTransition previousChord chords = snd $ minimumBy compareScores $ map ((,) previousChord) chords
where compareScores a b = compare (transitionScore a) (transitionScore b)
parallel :: (PitchedChord, PitchedChord) -> Interval -> Bool
parallel (a,b) interval = any parallelPitches pitchPairs
where pitchPairs = pairs $ zip a b
parallelPitches ((a,a'),(b,b')) = b-a == interval && a'-a == b'-b
pairs :: Eq a => [a] -> [(a,a)]
pairs [] = []
pairs (x:xs) = pairsOf x xs ++ pairs xs
where pairsOf x xs = map ( (,) x ) xs
-- transitionScore (a,b) | trace ("scores " ++ show jScore ++ " " ++ show oScore) False = jScore + oScore
-- where jScore = jumpScore (a,b)
-- oScore = opennessScore (a,b)
transitionScore :: (PitchedChord, PitchedChord) -> Float
transitionScore (a,b) = jScore -- - oScore
where jScore = jumpScore (a,b)
oScore = opennessScore (a,b)
jumpScore :: (PitchedChord, PitchedChord) -> Float
jumpScore (a,b) = fromIntegral $ sum $ map (\(x,y)->square (x-y)) pitchPairs
where pitchPairs = zip a b
opennessScore :: (PitchedChord, PitchedChord) -> Float
opennessScore (_,b) = transformOpenness $ map distance pitchPairs
where pitchPairs = zip b (tail b)
distance (a,b) = (a-b)
transformOpenness :: [Integer] -> Float
transformOpenness xInts = (*0.2) $ sum $ map (\x-> square (x-5) ) xs
where xs = map fromIntegral xInts
square :: Num a => a -> a
square x = x * x
-- Main functions
main = do
[file] <- getArgs
text <- readFile file
putStrLn $ printProgression $ makeProgression [] $ map parseChord (words text)
selectChord :: Chord -> PitchedChord
selectChord = head . chordOptions
chordOptions :: Chord -> [PitchedChord]
chordOptions = inRangeChords . toPitchedChord
makeProgression :: PitchedChord -> [Chord] -> [PitchedChord]
makeProgression lastChord (chord:chords) = selectedChord:(makeProgression selectedChord chords)
where selectedChord = bestTransition lastChord $ filter (not.badTransition lastChord) (chordOptions chord)
makeProgression lastChord [] = []
-- Test functions
test :: [PitchedChord]
test = nub $ inRangeChords $ toPitchedChord (0, majorChord)
test1 :: String
test1 = concatMap (\x-> "<<" ++ (concat $ intersperse " " $ map toLilypond x) ++ ">> ") test
test2 :: String
test2 = printProgression test
-- Functions for converting data to Lilypond output
toLilypond :: Integer -> String
toLilypond pitch = note ++ (mRanges !! octave) ++ "2"
where mRanges = ",":(inits $ repeat '\'')
(octave, rem) = divMod (fromIntegral pitch) 12
note = ["c", "cis", "d", "dis", "e", "f", "fis", "g",
"gis", "a", "ais", "b"] !! rem
chordsToVoices :: [PitchedChord] -> [String]
chordsToVoices xs = map (\x -> "{" ++ (concat $ intersperse " " $ map toLilypond x) ++ "}\n") (transpose xs)
voicedChordsToLilypondSplits :: [String] -> String
voicedChordsToLilypondSplits xs = concat $ zipWith (++) ["bassMusic = ","tenorMusic = ","altoMusic = ","sopMusic = "] xs
printProgression :: [PitchedChord] -> String
printProgression = voicedChordsToLilypondSplits . chordsToVoices
-- Functions for converting string input to data
parseChord :: String -> Chord
parseChord str = (pitch, chordType)
where pitch = parseChordPitch (map toLower str)
chordType = parseChordType str
parseChordPitch :: String -> Integer
parseChordPitch str
| str == "i" = 0
| str == "iib" = 1
| str == "ii" = 2
| str == "iiib" = 3
| str == "iii" = 4
| str == "iv" = 5
| str == "vb" = 6
| str == "v" = 7
| str == "vib" = 8
| str == "vi" = 9
| otherwise = error ("Bad Chord: " ++ str)
where lowerStr = map toLower str
parseChordType :: String -> ChordType
parseChordType str
| length (group cases) == 1 = if (and cases) then majorChord else minorChord
| otherwise = error ("Chord " ++ str ++ " should be written in all uppercase or all lowercase.")
where cases = map (\c -> isLetter c && isUpper c) $ filter (/= 'b') str