-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadMidi.lhs
549 lines (465 loc) · 22.9 KB
/
ReadMidi.lhs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
> {-# OPTIONS -w #-}
\section{Reading Midi files}
\label{readMidi}
Now that we have translated a raw Midi file into a {\tt MidiFile} data
type, we can translate that {\tt MidiFile} into a {\tt Music} object.
\begin{verbatim}
> module ReadMidi (readMidi, module Basics, module Performance,
> module MidiFile)
> where
>
> import Ratio
> import Basics
> import Performance
> import MidiFile
> import HaskToMidi
> import GeneralMidi
\end{verbatim}
The main function. Note that we output a Context and a UserPatchMap
as well as a Music object.
\begin{verbatim}
> readMidi :: MidiFile -> (Music, Context, UserPatchMap)
> readMidi mf@(MidiFile _ d trks) =
> let trk1 = removeEOTs $ format mf
> upm = makeUPM trks
> (trk2,dur) = getFirst trk1
> trksrs = getRest dur (newTracks dur (splitBy (isTempoChg) trk2))
> read (t,r) = Tempo r (readTrack (tDiv d) upm t)
> m = optimize $ line $ map read trksrs
> in (m,
> Context {cTime = 0,
> cPlayer = fancyPlayer,
> cInst = getInst upm,
> cDur = float dur / 250000,
> cKey = 0,
> cVol = 100},
> upm)
\end{verbatim}
Remove the {\tt EndOfTrack}'s that end up in the middle of merged or
sequenced tracks.
\begin{verbatim}
> removeEOTs :: Track -> Track
> removeEOTs ((MetaEvent _ EndOfTrack):es) = removeEOTs es
> removeEOTs (e:es) = e:(removeEOTs es)
> removeEOTs [] = []
\end{verbatim}
Make one big track out of the individual tracks of a MidiFile, using
different methods depending on the format of the MidiFile.
\begin{verbatim}
> format (MidiFile 0 _ [trk]) = trk
> format (MidiFile 1 _ trks) = mergeTracks trks
> format (MidiFile 2 _ trks) = seqTracks trks
> format _ = error ("readMidi: unfamiliar format or file corrupt")
\end{verbatim}
Used for a list of simultaneous tracks (format 1). It merges together
the events of all the tracks, putting the events in order by their
elapsed time. A generalization of this algorithm could be useful as a
merge of an arbitrary number of lists.
\begin{verbatim}
> mergeTracks :: [Track] -> Track
> mergeTracks [[]] = []
> mergeTracks ([]:trks) = mergeTracks trks
> mergeTracks trks = let minTs = foldl1 minT trks
> minT [] trk = trk
> minT trk [] = trk
> minT (trk1@(e1:e1s)) (trk2@(e2:e2s)) =
> if getTime e1 <= getTime e2 then trk1 else trk2
> newList (trk@(e:es):trks) trk'
> | e == (head trk') = es:trks
> | otherwise = trk:(newList trks trk')
> newList ([]:trks) trk' = []:newList trks trk'
> newList [[]] _ = [[]]
> in
> (head (minTs)) : mergeTracks (newList trks (minTs))
\end{verbatim}
Used for a list of sequential tracks (format 2). It puts the events
of the tracks in order one after the other. About its auxiliary
functions: {\tt seqTracks'} is the same as seqTracks, only it keeps a
running total of the time increment; {\tt incTrack} increments the
elapsed-time of each event in the given track; {\tt incEvent}
increments the elapsed-time of a single event.
\begin{verbatim}
> seqTracks :: [Track] -> Track
> seqTracks trks = seqTracks' 0 trks
> where
> seqTracks' _ [] = []
> seqTracks' _ [[]] = []
> seqTracks' n ([]:trks) = seqTracks' n trks
> seqTracks' n (trk:trks) = a ++ seqTracks' (b+n) trks
> where (a,b) = incTrack n trk
> incTrack n [] = error ("incTrack: Empty list")
> incTrack n [e] = ([incEvent n e],getTime e)
> incTrack n (e:es) = ((incEvent n e):a, b)
> where (a,b) = incTrack n es
> incEvent n (MidiEvent t x) = MidiEvent (t+n) x
> incEvent n (MetaEvent t x) = MetaEvent (t+n) x
\end{verbatim}
Get the elapsed time of an event.
\begin{verbatim}
> getTime :: MEvent -> ElapsedTime
> getTime (MidiEvent t _) = t
> getTime (MetaEvent t _) = t
\end{verbatim}
Look through the given tracks, using Program Changes to make a
UserPatchMap.
\begin{verbatim}
> makeUPM :: [Track] -> UserPatchMap
> makeUPM trks = removeDups $ map makeTriple $ concatMap searchPC trks
> where makeTriple (ch,num) = let gmName = lookupInst genMidiMap num
> iName = case ch of
> 9 -> "drums"
> _ -> gmName
> in (iName, gmName, ch)
> searchPC ((MidiEvent _ (ProgChange ch num)):es) = (ch, num) : searchPC es
> searchPC ((MidiEvent t (NoteOn _ _ _)):es) | t > 0 = []
> searchPC (e:es) = searchPC es
> searchPC [] = []
> searchInst ((MetaEvent _ (InstrName iName)):es) = Just iName
> searchInst ((MidiEvent t (NoteOn _ _ _)):es) | t > 0 = Nothing
> searchInst (e:es) = searchInst es
> searchInst [] = Nothing
\end{verbatim}
Remove consecutive duplicates from a list, used in this case to take
out redundant elements in the UserPatchMap.
\begin{verbatim}
> removeDups :: Eq a => [a] -> [a]
> removeDups [] = []
> removeDups [x] = [x]
> removeDups (x:y:xys) | x == y = removeDups (y:xys)
> | otherwise = x : removeDups (y:xys)
\end{verbatim}
Translate Divisions into the number of ticks per quarter note.
\begin{verbatim}
> tDiv :: Division -> Int
> tDiv (Ticks x) = x
> tDiv (SMPTE _ _) = error "Sorry, SMPTE not yet implemented."
\end{verbatim}
{\tt getFirst} gets the information that occurs at the beginning of
the piece: the default tempo and the default key signature. A
SetTempo in the middle of the piece should translate to a tempo change
({\tt Tempo x y m}), but a SetTempo at time 0 should set the default
tempo for the entire piece, by translating to Context tempo. {\tt
getFirst} takes care of all events that occur at time 0 so that if any
SetTempo appears at time 0, it can translate to Context even if it is
not the very first event.
\begin{verbatim}
> getFirst :: Track -> (Track, Int)
> getFirst trk1 = getFirst' trk1 []
> where
> getFirst' :: Track -> Track -> (Track, Int)
> getFirst' ((MetaEvent 0 (SetTempo tempo)):es) tAcc = (((reverse tAcc)++es), tempo)
> getFirst' (e@(MetaEvent 0 _):es) tAcc = getFirst' es (e:tAcc)
> getFirst' (e@(MidiEvent 0 _):es) tAcc = getFirst' es (e:tAcc)
> getFirst' es tAcc = (((reverse tAcc)++es), defST)
> getFirst' [] _ = ([] , defST)
\end{verbatim}
Manages the tempo changes in the piece; it translates each MidiFile
{\tt SetTempo} into a ratio between the new tempo and the tempo at the
beginning.
\begin{verbatim}
> getRest :: Int -> [Track] -> [(Track,Ratio Int)]
> getRest d (((MetaEvent _ (SetTempo tempo)):es):trks) = (es,r) : getRest d trks
> where r = d % tempo
> getRest d (trk:trks) = (trk,1) : getRest d trks
> getRest _ [] = []
\end{verbatim}
Get the first instrument from the UserPatchMap, to use as the default
in the Context.
\begin{verbatim}
> getInst :: UserPatchMap -> String
> getInst ((iName, gmName, channel):xs) = iName
> getInst [] = "piano"
\end{verbatim}
{\tt splitBy} takes a boolean test and a list; it divides up the list
and turns it into a {\em list of sub-lists}; each sub-list consists of
(1) one element for which the test is true (or the first element in
the list), and (2) all elements after that element for which the test
is false. Used to split a track into sub-tracks by tempo. For
example, {\tt splitBy (>10) [27, 0, 2, 1, 15, 3, 42, 4]} yields
{\tt [ [27,0,2,1], [15,3], [42,4] ]}.
\begin{verbatim}
> splitBy :: (a -> Bool) -> [a] -> [[a]]
> splitBy test xs = splitBy' test xs []
> where splitBy' :: (a -> Bool) -> [a] -> [a] -> [[a]]
> splitBy' test (x1:x2:xs) acc =
> if (test x2)
> then (reverse (x1:acc)) : (splitBy' test (x2:xs) [])
> else splitBy' test (x2:xs) (x1:acc)
> splitBy' test [x] acc = [(reverse (x:acc))]
> splitBy' _ [] _ = [[]]
> isTempoChg :: MEvent -> Bool
> isTempoChg (MetaEvent _ (SetTempo _)) = True
> isTempoChg _ = False
\end{verbatim}
{\tt readTrack} is the heart of the {\tt readMidi} operation. It
reads a track that has been "processed" by {\tt newTracks}, and
returns the track as Music.
\begin{verbatim}
> readTrack :: Int -> UserPatchMap -> Track -> Music
> readTrack _ _ []
> = Rest 0
> readTrack ticks upm (nOn @(MidiEvent t1 (NoteOn ch p v))
> : nOff@(MidiEvent t2 (NoteOff _ _ _))
> : nOn2@(MidiEvent t3 (NoteOn _ _ _))
> : es)
> | (t2 < t1) || (t3 < t1) = error "readTrack: elapsed time values out of order"
> | t2 == t3 = n :+: readTrack ticks upm (nOn2:es)
> | t1 == t3 = n :=: readTrack ticks upm (nOn2:es)
> | t3 < t2 = n :=: (Rest (diff ticks t1 t3) :+: readTrack ticks upm (nOn2:es))
> | t3 > t2 = n :+: Rest (diff ticks t2 t3) :+: readTrack ticks upm (nOn2:es)
> where plainNote = Note (pitch p) (diff ticks t1 t2) (makeVol v)
> n = if ch == 9 then Instr "drums" plainNote
> else Instr (lookupUPM ch upm) plainNote
> readTrack ticks upm (nOn@ (MidiEvent _ (NoteOn _ _ _))
> : nOff@(MidiEvent _ (NoteOff _ _ _))
> : x
> : es)
> = readTrack ticks upm (x : nOn : nOff : es)
> readTrack ticks upm (MidiEvent t1 (NoteOn ch p v)
> : MidiEvent t2 (NoteOff _ _ _)
> : es)
> | t2 < t1 = error "readTrack: elapsed time values of last note out of order"
> | otherwise = n :+: readTrack ticks upm es
> where plainNote = Note (pitch p) (diff ticks t1 t2) (makeVol v)
> n = if ch == 9 then Instr "drums" plainNote
> else Instr (lookupUPM ch upm) plainNote
> readTrack ticks upm ((MidiEvent t (ProgChange ch num)):es)
> = readTrack ticks (progChange ch num upm) es
> readTrack ticks upm (e:es)
> = readTrack ticks upm es
\end{verbatim}
Take the division in ticks and two time values and calculates the note
duration (quarter note, eighth note, etc.) that expresses their difference.
\begin{verbatim}
> diff :: Int -> Int -> Int -> Dur
> diff ticks t1 t2 = ((t2 - t1) % ticks) / 8
\end{verbatim}
Look up an instrument name from a UserPatchMap given its channel number.
\begin{verbatim}
> lookupUPM :: Int -> UserPatchMap -> String
> lookupUPM ch ((name,_,midiChan):xs)
> | ch == midiChan = name
> | otherwise = lookupUPM ch xs
> lookupUPM ch [] = error ("Invalid channel in user patch map")
\end{verbatim}
Implement a {\em Program Change}: a change in the UserPatchMap in
which a channel changes from one instrument to another.
\begin{verbatim}
> progChange :: Int -> Int -> UserPatchMap -> UserPatchMap
> progChange ch num ((_, _, midiChan):xs) | ch == midiChan =
> let n = lookupInst genMidiMap num
> in ((n,n,ch):xs)
> progChange ch num (x:xs) = x:(progChange ch num xs)
> progChange _ _ [] = []
> progChange _ _ _ = error "progChange: Uh oh."
\end{verbatim}
Look up an instrument in the General Midi table given its program number.
\begin{verbatim}
> lookupInst :: GenMidiTable -> Int -> IName
> lookupInst ((inst,num):xs) n | (num == n) = inst
> | otherwise = lookupInst xs n
\end{verbatim}
Interpret a MidiFile velocity, creating a Music NoteAttribute. The
MIDI specification calls for some sort of exponential scale, but for
now it's just linear.
\begin{verbatim}
> makeVol :: Int -> [NoteAttribute]
> makeVol x = [Volume (float x)]
\end{verbatim}
The {\tt newTracks} function changes the order of the events in a list
of sub-tracks so that they can be handled by readTrack: each NoteOff
is put directly after its corresponding NoteOn. Its first and second
arguments are the elapsed time and value (in microseconds per quarter
note) of the SetTempo currently in effect.
\begin{verbatim}
> newTracks :: Int -> [Track] -> [Track]
> newTracks dur = newTracks' 0 dur
> where
> newTracks' :: Int -> Int -> [Track] -> [Track]
> newTracks' _ _ [] = []
> newTracks' _ _ [[]] = [[]]
> newTracks' stt stv ([]:trks) = [] : newTracks' stt stv trks
> newTracks' _ _ ((e@(MetaEvent newStt (SetTempo newStv)):es):trks) =
> let (trk':trks') = newTracks' newStt newStv (es:trks)
> in ((e:trk'):trks')
> newTracks' stt stv ((e1@(MidiEvent tno (NoteOn c p _)):es):trks) =
> let (e2, leftover) = search (stt-tno) stv stv tno stt c p (es:trks) [[]]
> (trk':trks') = newTracks' stt stv leftover
> in ((e1:e2:trk'):trks')
> newTracks' stt stv ((e:es):trks) = let (trk':trks') = newTracks' stt stv (es:trks)
> in ((e:trk'):trks')
> newTracks' stt stv (trk:trks) = trk:(newTracks' stt stv trks)
\end{verbatim}
{\tt search} takes a track that has been divided by tempo into
sub-lists; it looks through the list of sub-tracks to find the NoteOff
corresponding to the given NoteOn. A NoteOff corresponds to an
earlier NoteOn if it is the first in the track to have the same
channel and pitch. If it's in a different sub-track than its NoteOn,
it puts it in the NoteOn's subtrack and calculates what the new
elapsed-time should be after one or more tempo changes. This function
takes a ridiculous number of arguments, I know, but I don't think it
can do without any of the information. Maybe there is a simpler way.
\begin{verbatim}
> search :: Int -> -- time interval between NoteOn and most recent SetTempo,
> -- in terms of the tempo at the NoteOn
> Int -> Int -> -- SetTempo values: the one at the NoteOn and the most
> -- recent one
> Int -> Int -> -- elapsed times of the NoteOn and the most recent SetTempo
> Int -> Int -> -- channel and pitch of NoteOn (NoteOff must match)
> [Track] -> -- the tracks left to be searched
> [Track] -> -- accumulator: what's left after NoteOff is found
> (MEvent,[Track]) -- the needed event and the remainder of the tracks
> search int ost nst tno stt c1 p1 ((e@(MidiEvent t (NoteOff c2 p2 v)):es):trks)
> (aTrk:aTrks)
> | c1 == c2 && p1 == p2
> = ((MidiEvent eTime (NoteOff c2 p2 v)),
> (reverse aTrks)++(((reverse aTrk)++es):trks))
> where eTime = tno + int + round ((float (t-stt))*(float ost)/(float nst))
> search int ost nst tno stt c p ((e@(MetaEvent t (SetTempo st)):es):trks)
> (aTrk:aTrks) =
> search newInt ost st tno t c p (es:trks) ((e:aTrk):aTrks)
> where newInt = int + round ((float (t-stt))*(float ost)/(float nst))
> search _ _ _ tno _ c p [[]] (aTrk:aTrks) =
> ((MidiEvent tno (NoteOff c p 0)), aTrks++[reverse aTrk])
> search _ _ _ tno _ c p [] (aTrk:aTrks) =
> ((MidiEvent tno (NoteOff c p 0)), aTrks++[reverse aTrk])
> search int ost nst tno stt c p ((e:es):trks) (aTrk:aTrks) =
> search int ost nst tno stt c p (es:trks) ((e:aTrk):aTrks)
> search int ost nst tno stt c p ([] :trks) (aTrk:aTrks) =
> search int ost nst tno stt c p trks ([]:(reverse aTrk):aTrks)
> search _ _ _ _ _ _ _ [] _ =
> error "newTracks: search fell thru"
\end{verbatim}
Music objects that come out of {\tt readMidi} almost always contain
redundancies, like rests of zero duration and redundant instrument
specifications. {\tt optimize} reduces the redundancy to make a Music
file less cluttered and more efficient to use.
\begin{verbatim}
> optimize, optRests, optTempo, optInstr, optVol :: Music -> Music
> optimize m = optTempo $ optVol $ optInstr $ optRests m
\end{verbatim}
Remove rests of zero duration.
\begin{verbatim}
> optRests (m :+: Rest 0) = optRests m
> optRests (m :=: Rest 0) = optRests m
> optRests (Rest 0 :+: m) = optRests m
> optRests (Rest 0 :=: m) = optRests m
> optRests (m1 :+: m2) = optRests m1 :+: optRests m2
> optRests (m1 :=: m2) = optRests m1 :=: optRests m2
> optRests (Tempo a m) = Tempo a $ optRests m
> optRests (Trans a m) = Trans a $ optRests m
> optRests (Instr a m) = Instr a $ optRests m
> optRests (Player a m) = Player a $ optRests m
> optRests (Phrase a m) = Phrase a $ optRests m
> optRests x = x
\end{verbatim}
Remove redundant {\tt Tempo}'s.
\begin{verbatim}
> optTempo (Tempo 1 m) = optTempo m
> optTempo (Tempo a (Tempo b m)) = Tempo (a*b) $ optTempo m
> optTempo (m1 :+: m2) = optTempo m1 :+: optTempo m2
> optTempo (m1 :=: m2) = optTempo m1 :=: optTempo m2
> optTempo (Tempo a m) = Tempo a $ optTempo m
> optTempo (Trans a m) = Trans a $ optTempo m
> optTempo (Instr a m) = Instr a $ optTempo m
> optTempo (Player a m) = Player a $ optTempo m
> optTempo (Phrase a m) = Phrase a $ optTempo m
> optTempo x = x
\end{verbatim}
Remove unnecessary {\tt Instr}'s.
\begin{verbatim}
> optInstr m1 = let (m2,changed) = optInstr' m1
> in if changed then optInstr m2
> else m2
> where
> optInstr' :: Music -> (Music, Bool)
> optInstr' ((m1 :+: m2) :+: m3) = (m1 :+: m2 :+: m3, True)
> optInstr' ((m1 :=: m2) :=: m3) = (m1 :=: m2 :=: m3, True)
> optInstr' (Instr a (Instr b m)) = (Instr a m, True)
> optInstr' (Instr a m1 :+: Instr b m2) | a == b = (Instr a (m1 :+: m2), True)
> optInstr' (Instr a m1 :=: Instr b m2) | a == b = (Instr a (m1 :=: m2), True)
> optInstr' (Instr a m1 :+: Instr b m2 :+: x)
> | a == b = (Instr a (m1 :+: m2) :+: x, True)
> optInstr' (Instr a m1 :=: Instr b m2 :=: x)
> | a == b = (Instr a (m1 :=: m2) :=: x, True)
> optInstr' (Instr a m :+: Rest r) = (Instr a (m :+: Rest r), True)
> optInstr' (Instr a m :=: Rest r) = (Instr a (m :=: Rest r), True)
> optInstr' (Rest r :+: Instr a m) = (Instr a (Rest r :+: m), True)
> optInstr' (Rest r :=: Instr a m) = (Instr a (Rest r :=: m), True)
> optInstr' (m1 :+: m2) = let (m3,c3) = optInstr' m1
> (m4,c4) = optInstr' m2
> in (m3 :+: m4, c3 || c4)
> optInstr' (m1 :=: m2) = let (m3,c3) = optInstr' m1
> (m4,c4) = optInstr' m2
> in (m3 :=: m4, c3 || c4)
> optInstr' (Tempo a m) = (Tempo a $ optInstr m, False)
> optInstr' (Trans a m) = (Trans a $ optInstr m, False)
> optInstr' (Instr a m) = (Instr a $ optInstr m, False)
> optInstr' (Player a m) = (Player a $ optInstr m, False)
> optInstr' (Phrase a m) = (Phrase a $ optInstr m, False)
> optInstr' x = (x, False)
\end{verbatim}
Change repeated Volume Note Attributes to Phrase Attributes.
\begin{verbatim}
> optVol m1 = let (m2,changed) = optVol' m1
> in if changed then optVol m2
> else m2
> optVol' ((m1 :+: m2) :+: m3) = (m1 :+: m2 :+: m3, True)
> optVol' ((m1 :=: m2) :=: m3) = (m1 :=: m2 :=: m3, True)
> optVol' (Phrase p1 (Phrase p2 m)) | p1 == p2 = (Phrase p1 m, True)
> | otherwise = (Phrase (p1 ++ p2) m, True)
> optVol' (Note p1 d1 [Volume v1] :+: Note p2 d2 [Volume v2]) | v1 == v2 =
> (Phrase [Dyn (Loudness v1)] (Note p1 d1 [] :+: Note p2 d2 []), True)
> optVol' (Note p1 d1 [Volume v1] :=: Note p2 d2 [Volume v2]) | v1 == v2 =
> (Phrase [Dyn (Loudness v1)] (Note p1 d1 [] :=: Note p2 d2 []), True)
> optVol' (Note p1 d1 [Volume v1] :+: Note p2 d2 [Volume v2] :+: x) | v1 == v2 =
> ((Phrase [Dyn (Loudness v1)] (Note p1 d1 [] :+: Note p2 d2 [])) :+: x, True)
> optVol' (Note p1 d1 [Volume v1] :=: Note p2 d2 [Volume v2] :=: x) | v1 == v2 =
> ((Phrase [Dyn (Loudness v1)] (Note p1 d1 [] :=: Note p2 d2 [])) :=: x, True)
> optVol' (Phrase p1 m1 :+: Phrase p2 m2) | p1 == p2 = (Phrase p1 (m1 :+: m2), True)
> optVol' (Phrase p1 m1 :=: Phrase p2 m2) | p1 == p2 = (Phrase p1 (m1 :=: m2), True)
> optVol' (Phrase p1 m1 :+: Phrase p2 m2 :+: x) | p1 == p2 =
> (Phrase p1 (m1 :+: m2 :+: x),True)
> optVol' (Phrase p1 m1 :=: Phrase p2 m2 :=: x) | p1 == p2 =
> (Phrase p1 (m1 :=: m2 :=: x),True)
> optVol' (Phrase phr@[Dyn (Loudness l)] m :+: Note p d [Volume v]) | l == v =
> (Phrase phr (m :+: Note p d []), True)
> optVol' (Phrase phr@[Dyn (Loudness l)] m :=: Note p d [Volume v]) | l == v =
> (Phrase phr (m :=: Note p d []), True)
> optVol' (Phrase phr@[Dyn (Loudness l)] m :+: Note p d [Volume v] :+: x) | l == v =
> (Phrase phr (m :+: Note p d []) :+: x,True)
> optVol' (Phrase phr@[Dyn (Loudness l)] m :=: Note p d [Volume v] :=: x) | l == v =
> (Phrase phr (m :=: Note p d []) :=: x,True)
> optVol' (Note p d [Volume v] :+: Phrase phr@[Dyn (Loudness l)] m) | l == v =
> (Phrase phr (Note p d [] :+: m),True)
> optVol' (Note p d [Volume v] :=: Phrase phr@[Dyn (Loudness l)] m) | l == v =
> (Phrase phr (Note p d [] :=: m),True)
> optVol' (Note p d [Volume v] :+: Phrase phr@[Dyn (Loudness l)] m :+: x) | l == v =
> ((Phrase phr (Note p d [] :+: m)) :+: x, True)
> optVol' (Note p d [Volume v] :=: Phrase phr@[Dyn (Loudness l)] m :=: x) | l == v =
> ((Phrase phr (Note p d [] :=: m)) :=: x, True)
> optVol' (Rest r :+: Note p d [Volume v]) =
> (Phrase [Dyn (Loudness v)] (Rest r :+: Note p d []),True)
> optVol' (Rest r :=: Note p d [Volume v]) =
> (Phrase [Dyn (Loudness v)] (Rest r :=: Note p d []),True)
> optVol' (Note p d [Volume v] :+: Rest r) =
> (Phrase [Dyn (Loudness v)] (Note p d [] :+: Rest r),True)
> optVol' (Note p d [Volume v] :=: Rest r) =
> (Phrase [Dyn (Loudness v)] (Note p d [] :=: Rest r),True)
> optVol' (Rest r :+: Phrase p m) = (Phrase p (Rest r :+: m), True)
> optVol' (Rest r :=: Phrase p m) = (Phrase p (Rest r :=: m), True)
> optVol' (Phrase p m :+: Rest r) = (Phrase p (m :+: Rest r), True)
> optVol' (Phrase p m :=: Rest r) = (Phrase p (m :=: Rest r), True)
> optVol' (m1 :+: m2) = let (m3,c3) = optVol' m1
> (m4,c4) = optVol' m2
> in (m3 :+: m4, c3 || c4)
> optVol' (m1 :=: m2) = let (m3,c3) = optVol' m1
> (m4,c4) = optVol' m2
> in (m3 :=: m4, c3 || c4)
> optVol' (Tempo a m) = (Tempo a $ optVol m, False)
> optVol' (Trans a m) = (Trans a $ optVol m, False)
> optVol' (Instr a m) = (Instr a $ optVol m, False)
> optVol' (Player a m) = (Player a $ optVol m, False)
> optVol' (Phrase a m) = (Phrase a $ optVol m, False)
> optVol' x = (x, False)
\end{verbatim}