module Music.Pitch.Common.Quality
(
Quality(..),
qualityTypes,
HasQuality(..),
invertQuality,
isPerfect,
isMajor,
isMinor,
isAugmented,
isDiminished,
QualityType(..),
expectedQualityType,
Direction(..),
qualityToAlteration,
qualityToDiff
) where
import Music.Pitch.Augmentable
import Music.Pitch.Common.Types
import Music.Pitch.Common.Number
import Data.Maybe (fromMaybe)
class HasQuality a where
quality :: a -> Quality
isPerfect :: HasQuality a => a -> Bool
isPerfect a = case quality a of { Perfect -> True ; _ -> False }
isMajor :: HasQuality a => a -> Bool
isMajor a = case quality a of { Major -> True ; _ -> False }
isMinor :: HasQuality a => a -> Bool
isMinor a = case quality a of { Minor -> True ; _ -> False }
isAugmented :: HasQuality a => a -> Bool
isAugmented a = case quality a of { Augmented _ -> True ; _ -> False }
isDiminished :: HasQuality a => a -> Bool
isDiminished a = case quality a of { Diminished _ -> True ; _ -> False }
instance HasQuality Quality where
quality = id
instance Augmentable Quality where
augment Major = Augmented 1
augment Minor = Major
augment Perfect = Augmented 1
augment (Augmented n) = Augmented (n + 1)
augment (Diminished n) = Diminished (n 1)
diminish Major = Minor
diminish Minor = Diminished 1
diminish Perfect = Diminished 1
diminish (Augmented n) = Augmented (n 1)
diminish (Diminished n) = Diminished (n + 1)
invertQuality :: Quality -> Quality
invertQuality = go
where
go Major = Minor
go Minor = Major
go Perfect = Perfect
go (Augmented n) = Diminished n
go (Diminished n) = Augmented n
expectedQualityType :: Number -> QualityType
expectedQualityType x = if ((abs x 1) `mod` 7) + 1 `elem` [1,4,5]
then PerfectType else MajorMinorType
qualityTypes :: Quality -> [QualityType]
qualityTypes Perfect = [PerfectType]
qualityTypes Major = [MajorMinorType]
qualityTypes Minor = [MajorMinorType]
qualityTypes _ = [PerfectType, MajorMinorType]
isValidQualityNumber :: Quality -> Number -> Bool
isValidQualityNumber q n = expectedQualityType n `elem` qualityTypes q
data Direction = Upward |Downward
deriving (Eq, Ord, Show)
qualityToAlteration :: Direction -> QualityType -> Quality -> Maybe ChromaticSteps
qualityToAlteration d qt q = fmap fromIntegral $ go d qt q
where
go Upward MajorMinorType (Augmented n) = Just $ 0 + n
go Upward MajorMinorType Major = Just $ 0
go Upward MajorMinorType Minor = Just $ (1)
go Upward MajorMinorType (Diminished n) = Just $ (1 + n)
go Downward MajorMinorType (Augmented n) = Just $ (1 + n)
go Downward MajorMinorType Major = Just $ 1
go Downward MajorMinorType Minor = Just $ 0
go Downward MajorMinorType (Diminished n) = Just $ 0 + n
go Upward PerfectType (Augmented n) = Just $ 0 + n
go Upward PerfectType Perfect = Just $ 0
go Upward PerfectType (Diminished n) = Just $ 0 n
go Downward PerfectType (Augmented n) = Just $ 0 n
go Downward PerfectType Perfect = Just $ 0
go Downward PerfectType (Diminished n) = Just $ 0 + n
go _ qt q = Nothing
qualityToDiff x qt q = fromMaybe e $ qualityToAlteration (f x) qt q
where
f True = Upward
f False = Downward
e = error $
"qualityToDiff: Unknown interval expression ("
++ show qt ++ ", " ++ show q ++ ")"