/
Accidentals.purs
77 lines (68 loc) · 2.16 KB
/
Accidentals.purs
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
-- | Accidentals are a set of mappings from a PitchClass to an Accidental
-- | They are useful, for example, when interpreting a bar of music where
-- | the accidental nature of a note may depend on the existence of a previous
-- | note of the same pitch within the bar.
module Data.Abc.Accidentals
( Accidentals
, empty
, add
, fromKeySet
, lookup
, member
, fromKeySig
, implicitInKeySet
) where
import Prelude ((==), map)
import Data.Abc (PitchClass, Accidental, Pitch(..), KeySet, KeySignature)
import Data.Maybe (Maybe(..))
import Data.Map as Map
import Data.Tuple (Tuple(..))
-- | A set of accidentals
type Accidentals =
Map.Map PitchClass Accidental
-- | create an empty set of Key Accidentals
empty :: Accidentals
empty =
Map.empty
-- | add an accidental to the set
add :: PitchClass -> Accidental -> Accidentals -> Accidentals
add pc acc accs =
Map.insert pc acc accs
-- | build Accidentals from a KeySet
fromKeySet :: KeySet -> Accidentals
fromKeySet ks =
let
f kar =
case kar of
Pitch p ->
Tuple p.pitchClass p.accidental
tuples = map f ks
in
Map.fromFoldable tuples
-- | lookup a pitch class and see of it exists in the Accidentals set
lookup :: PitchClass -> Accidentals -> Maybe Accidental
lookup =
Map.lookup
-- | lookup a KeyAccidental (represented as a Pitch) and see if it's a member of
-- | the Accidentals set (i.e. the value of the Accidental matches for the supplied pitch)
member :: Pitch -> Accidentals -> Boolean
member (Pitch p) accs =
let
macc =
lookup p.pitchClass accs
in
(Just p.accidental) == macc
-- | extract the KeyAccidental Pitch from a KeySignature
fromKeySig :: KeySignature -> Pitch
fromKeySig ks =
Pitch
{ pitchClass: ks.pitchClass
, accidental: ks.accidental
}
-- | Return an accidental if it is implicitly there in the supplied KeySet
-- | (which is obtained from a key signature)
-- | attached to the pitch class of the note. In ABC, notes generally inherit
-- | their (sharp, flat or natural) accidental nature from the key signature.
implicitInKeySet :: PitchClass -> KeySet -> Maybe Accidental
implicitInKeySet pc keyset =
lookup pc (fromKeySet keyset)