-- Copyright 2013 Evan Laforge
-- This program is distributed under the terms of the GNU General Public
-- License 3.0, see COPYING or http://www.gnu.org/licenses/gpl-3.0.txt

{-# LANGUAGE CPP #-}
{- | Functions to manipulate pitches in scales according to the rules of
    standard western music theory.

    Pitches are represented as 'Pitch.Pitch'es and 'Pitch.Degree's.  They're
    generalized to work with any number of 'Pitch.PitchClass'es, but since each
    scale carries an implied key 'Layout' it can only handle pitch classes in
    a certain range.  Internally there are no checks that the pitch class is in
    range, so the range has to be checked on parse or show.  Parsing and
    showing is handled in "Derive.Scale.TheoryFormat".

    @
        db      eb  fb      gb      ab      bb  cb
        c#      d#      e#  f#      g#      a#      b#
    c       d       e   f       g       a       b   c
    |   |   |   |   |   |   |   |   |   |   |   |   |
    @
-}
module Derive.Scale.Theory (
    -- * constants
    piano_intervals, piano_layout, diatonic_layout
    -- * NoteNumber diatonic transposition
    , diatonic_to_chromatic
    -- * symbolic transposition
    , transpose_diatonic, transpose_chromatic
    -- * input
    , enharmonics_of
    , pitch_to_semis, degree_to_semis
    , semis_to_pitch, pick_enharmonic, semis_to_pitch_sharps
    , semis_to_nn, fsemis_to_nn, nn_to_semis
    -- ** key
    , Key(key_tonic, key_name, key_intervals, key_signature, key_layout), key
    , accidentals_at_pc
    , Signature, Intervals
    , layout
    , layout_pc_per_octave, layout_semis_per_octave
    , contains_degree
#ifndef TESTING
    , Layout(layout_intervals)
#else
    , Layout(..)
    , calculate_signature, step_of
#endif
) where
import qualified Data.List as List
import qualified Data.Maybe as Maybe
import qualified Data.Vector as Boxed
import qualified Data.Vector as Vector

import qualified Util.Num as Num
import qualified Util.Pretty as Pretty
import qualified Util.Lists as Lists
import qualified Util.Vector as Vector

import qualified Perform.Pitch as Pitch

import           Global


-- * constants

piano_intervals :: [Pitch.Semi]
piano_intervals :: [Int]
piano_intervals = [Int
2, Int
2, Int
1, Int
2, Int
2, Int
2, Int
1]

-- | The layout of keys on everyone's favorite boxed harp.
piano_layout :: Layout
piano_layout :: Layout
piano_layout = [Int] -> Layout
layout [Int]
piano_intervals

diatonic_layout :: Pitch.PitchClass -> Layout
diatonic_layout :: Int -> Layout
diatonic_layout Int
per_oct = [Int] -> Layout
layout forall a b. (a -> b) -> a -> b
$ forall a. Int -> a -> [a]
replicate Int
per_oct Int
1


-- * NoteNumber diatonic transposition

-- | Convert a fractional number of diatonic steps to chromatic steps.
diatonic_to_chromatic :: Key -> Pitch.Degree -> Double -> Pitch.FSemi
diatonic_to_chromatic :: Key -> Degree -> FSemi -> FSemi
diatonic_to_chromatic Key
key Degree
degree FSemi
steps
    | FSemi
steps forall a. Eq a => a -> a -> Bool
== FSemi
0 = FSemi
0
    | FSemi
steps forall a. Ord a => a -> a -> Bool
> FSemi
0 = forall a. (Eq a, Num a) => a -> a -> a -> a
Num.scale (Int -> FSemi
transpose Int
isteps) (Int -> FSemi
transpose (Int
istepsforall a. Num a => a -> a -> a
+Int
1)) FSemi
frac
    | Bool
otherwise =
        forall a. (Eq a, Num a) => a -> a -> a -> a
Num.scale (Int -> FSemi
transpose (Int
istepsforall a. Num a => a -> a -> a
-Int
1)) (Int -> FSemi
transpose Int
isteps) (FSemi
1 forall a. Num a => a -> a -> a
- forall a. Num a => a -> a
abs FSemi
frac)
    where
    (Int
isteps, FSemi
frac) = forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction FSemi
steps
    transpose :: Int -> FSemi
transpose = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Degree -> Int -> Int
chromatic_steps Key
key Degree
degree

-- | Convert diatonic steps to chromatic steps.
chromatic_steps :: Key -> Pitch.Degree -> Int -> Pitch.Semi
chromatic_steps :: Key -> Degree -> Int -> Int
chromatic_steps Key
key Degree
degree Int
steps =
    case Intervals
table forall a. Vector a -> Int -> Maybe a
Vector.!? (Int
middle forall a. Num a => a -> a -> a
+ Int
step forall a. Num a => a -> a -> a
+ Int
steps2) of
        Just Int
val -> Int
oct_semis forall a. Num a => a -> a -> a
+ Int
val forall a. Num a => a -> a -> a
- Intervals
table forall a. Vector a -> Int -> a
Vector.! (Int
middle forall a. Num a => a -> a -> a
+ Int
step)
        -- 'make_table' should build a table big enough that this can't happen.
        Maybe Int
Nothing -> forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"ran out of transpose table for "
            forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show (Int
middle, Int
step, Int
steps2) forall a. [a] -> [a] -> [a]
++ [Char]
": " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Intervals
table
    where
    step :: Int
step = Key -> Degree -> Int
step_of Key
key Degree
degree
    (Int
octaves, Int
steps2) = Int
steps forall a. Integral a => a -> a -> (a, a)
`divMod` Key -> Int
key_steps_per_octave Key
key
    oct_semis :: Int
oct_semis = if Int
octaves forall a. Eq a => a -> a -> Bool
== Int
0 then Int
0
        else Int
octaves forall a. Num a => a -> a -> a
* Layout -> Int
layout_semis_per_octave (Key -> Layout
key_layout Key
key)
    middle :: Int
middle = forall a. Vector a -> Int
Vector.length Intervals
table forall a. Integral a => a -> a -> a
`div` Int
2
    table :: Intervals
table = Key -> Intervals
key_transpose_table Key
key

-- * symbolic transposition

-- | Transpose a pitch diatonically.  If the key is diatonic (i.e. there is
-- a 1:1 relationship between note letters and scale degrees), then this will
-- increment or decrement the note letters by the number of steps and adjust
-- the accidentals based on the key signature.  Otherwise (i.e. for scales like
-- whole-tone or octatonic), it will figure out the number of chromatic steps
-- to transpose and act like 'transpose_chromatic'.
transpose_diatonic :: Key -> Step -> Pitch.Pitch -> Pitch.Pitch
transpose_diatonic :: Key -> Int -> Pitch -> Pitch
transpose_diatonic Key
key Int
steps
        pitch :: Pitch
pitch@(Pitch.Pitch Int
oct degree :: Degree
degree@(Pitch.Degree Int
pc Int
accs))
    | Int
steps forall a. Eq a => a -> a -> Bool
== Int
0 = Pitch
pitch
    | Bool
otherwise = case Key -> Maybe Intervals
key_signature Key
key of
        Just Intervals
_ -> Int -> Degree -> Pitch
Pitch.Pitch (Int
oct forall a. Num a => a -> a -> a
+ Int
oct2) forall a b. (a -> b) -> a -> b
$ Int -> Int -> Degree
Pitch.Degree Int
pc2 forall a b. (a -> b) -> a -> b
$
            Int
accs forall a. Num a => a -> a -> a
- Key -> Int -> Int
accidentals_at_pc Key
key Int
pc forall a. Num a => a -> a -> a
+ Key -> Int -> Int
accidentals_at_pc Key
key Int
pc2
        Maybe Intervals
Nothing -> Key -> Int -> Pitch -> Pitch
transpose_chromatic
            Key
key (Key -> Degree -> Int -> Int
chromatic_steps Key
key Degree
degree Int
steps) Pitch
pitch
    where
    (Int
oct2, Int
pc2) = (Int
pc forall a. Num a => a -> a -> a
+ Int
steps) forall a. Integral a => a -> a -> (a, a)
`divMod` Key -> Int
key_steps_per_octave Key
key

-- | Chromatic transposition.  Try to pick a spelling that makes sense for the
-- given key.
transpose_chromatic :: Key -> Pitch.Semi -> Pitch.Pitch -> Pitch.Pitch
transpose_chromatic :: Key -> Int -> Pitch -> Pitch
transpose_chromatic Key
key Int
steps Pitch
pitch
    | Int
steps forall a. Eq a => a -> a -> Bool
== Int
0 = Pitch
pitch
    | Bool
otherwise = Key -> Int -> Pitch
semis_to_pitch Key
key forall a b. (a -> b) -> a -> b
$ Layout -> Pitch -> Int
pitch_to_semis Layout
layout Pitch
pitch forall a. Num a => a -> a -> a
+ Int
steps
    where layout :: Layout
layout = Key -> Layout
key_layout Key
key

pitch_to_semis :: Layout -> Pitch.Pitch -> Pitch.Semi
pitch_to_semis :: Layout -> Pitch -> Int
pitch_to_semis Layout
layout (Pitch.Pitch Int
oct Degree
degree) =
    Int
oct forall a. Num a => a -> a -> a
* Layout -> Int
layout_semis_per_octave Layout
layout forall a. Num a => a -> a -> a
+ Layout -> Degree -> Int
degree_to_semis Layout
layout Degree
degree

degree_to_semis :: Layout -> Pitch.Degree -> Pitch.Semi
degree_to_semis :: Layout -> Degree -> Int
degree_to_semis Layout
layout (Pitch.Degree Int
pc_ Int
accs) =
    forall a. Num a => Vector a -> a
Vector.sum (forall a. Int -> Vector a -> Vector a
Vector.take Int
pc (Layout -> Intervals
layout_intervals Layout
layout)) forall a. Num a => a -> a -> a
+ Int
accs
        forall a. Num a => a -> a -> a
+ Int
oct forall a. Num a => a -> a -> a
* Layout -> Int
layout_semis_per_octave Layout
layout
    where (Int
oct, Int
pc) = Int
pc_ forall a. Integral a => a -> a -> (a, a)
`divMod` Layout -> Int
layout_pc_per_octave Layout
layout

-- | Pick the most sensible enharmonic for the given pitch.
--
-- TODO I reduce to semis and then pick an enharmonic, so 5b# becomes 6c.  But
-- if they asked for 5b# they should get it.
pick_enharmonic :: Key -> Pitch.Pitch -> Pitch.Pitch
pick_enharmonic :: Key -> Pitch -> Pitch
pick_enharmonic Key
key = Key -> Int -> Pitch
semis_to_pitch Key
key forall b c a. (b -> c) -> (a -> b) -> a -> c
. Layout -> Pitch -> Int
pitch_to_semis (Key -> Layout
key_layout Key
key)

-- | Convert an absolute semitones value to a pitch.  This is a bit
-- complicated because it wants to find the best spelling for the given key.
semis_to_pitch :: Key -> Pitch.Semi -> Pitch.Pitch
semis_to_pitch :: Key -> Int -> Pitch
semis_to_pitch Key
key Int
semis = (Int, Degree) -> Pitch
mkpitch forall a b. (a -> b) -> a -> b
$ case Key -> Maybe Intervals
key_signature Key
key of
    Just Intervals
sig -> case forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
List.find (forall {a}. Intervals -> (a, Degree) -> Bool
in_scale Intervals
sig) [(Int, Degree)]
enharmonics of
        Maybe (Int, Degree)
Nothing -> forall {a}. Num a => Bool -> [(a, Degree)] -> (a, Degree)
pick_enharmonic (forall {v :: * -> *} {a}. (Vector v a, Ord a, Num a) => v a -> Bool
sharp_signature Intervals
sig) [(Int, Degree)]
enharmonics
        Just (Int, Degree)
pitch -> (Int, Degree)
pitch
    Maybe Intervals
Nothing -> forall {a}. Num a => Bool -> [(a, Degree)] -> (a, Degree)
pick_enharmonic (Key -> Bool
sharp_tonic Key
key) [(Int, Degree)]
enharmonics
    where
    mkpitch :: (Int, Degree) -> Pitch
mkpitch (Int
oct, Degree
degree) = Int -> Degree -> Pitch
Pitch.Pitch (Int
octave forall a. Num a => a -> a -> a
+ Int
oct) Degree
degree
    -- The (Pitch.Degree (-1) 0) error value is icky, but here's why it should
    -- never happen: It happens when enharmonics is empty.  Since the values of
    -- layout_enharmonics are never [] as per the definition of 'layout', it
    -- means the mod of semis is out of range for the array, which means the
    -- sum of the intervals is larger than the length of layout_enharmonics.
    -- That shouldn't happen because layout_enharmonics is initialized to
    -- [..  | i <- intervals, a <- [0..i-1]].
    pick_enharmonic :: Bool -> [(a, Degree)] -> (a, Degree)
pick_enharmonic Bool
use_sharps [(a, Degree)]
notes = forall a. a -> Maybe a -> a
fromMaybe (a
0, Int -> Int -> Degree
Pitch.Degree (-Int
1) Int
0) forall a b. (a -> b) -> a -> b
$
        forall k a. Ord k => (a -> k) -> [a] -> Maybe a
Lists.minimumOn (forall {b}. (Ord b, Num b) => b -> (Bool, b)
key forall b c a. (b -> c) -> (a -> b) -> a -> c
. Degree -> Int
Pitch.degree_accidentals forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(a, Degree)]
notes
        where key :: b -> (Bool, b)
key b
accs = (if Bool
use_sharps then b
accs forall a. Ord a => a -> a -> Bool
< b
0 else b
accs forall a. Ord a => a -> a -> Bool
> b
0, forall a. Num a => a -> a
abs b
accs)
    in_scale :: Intervals -> (a, Degree) -> Bool
in_scale Intervals
sig (a
_, Degree
degree) =
        Intervals
sig forall a. Vector a -> Int -> Maybe a
Vector.!? Key -> Degree -> Int
step_of Key
key Degree
degree
            forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just (Degree -> Int
Pitch.degree_accidentals Degree
degree)
    enharmonics :: [(Int, Degree)]
enharmonics = forall a. a -> Maybe a -> a
fromMaybe [] forall a b. (a -> b) -> a -> b
$ Layout -> Vector [(Int, Degree)]
layout_enharmonics Layout
layout forall a. Vector a -> Int -> Maybe a
Boxed.!? Int
steps
    (Int
octave, Int
steps) = Int
semis forall a. Integral a => a -> a -> (a, a)
`divMod` Layout -> Int
layout_semis_per_octave Layout
layout
    layout :: Layout
layout = Key -> Layout
key_layout Key
key
    -- Sharpish looking key signatures favor sharps.
    sharp_signature :: v a -> Bool
sharp_signature v a
sig = forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Int
Vector.count (forall a. Ord a => a -> a -> Bool
>a
0) v a
sig forall a. Ord a => a -> a -> Bool
>= forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> Int
Vector.count (forall a. Ord a => a -> a -> Bool
<a
0) v a
sig
    sharp_tonic :: Key -> Bool
sharp_tonic = (forall a. Ord a => a -> a -> Bool
>=Int
0) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Degree -> Int
Pitch.degree_accidentals forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Degree
key_tonic

-- | Like 'semis_to_pitch', but only emits sharps, so it doesn't require a key.
semis_to_pitch_sharps :: Layout -> Pitch.Semi -> Pitch.Pitch
semis_to_pitch_sharps :: Layout -> Int -> Pitch
semis_to_pitch_sharps Layout
layout Int
semis = Int -> Degree -> Pitch
Pitch.Pitch (Int
octave forall a. Num a => a -> a -> a
+ Int
oct) Degree
degree
    where
    (Int
octave, Int
steps) = Int
semis forall a. Integral a => a -> a -> (a, a)
`divMod` Layout -> Int
layout_semis_per_octave Layout
layout
    (Int
oct, Degree
degree) = forall a. [a] -> a
head forall a b. (a -> b) -> a -> b
$ Vector [(Int, Degree)]
enharmonics forall a. Vector a -> Int -> a
Boxed.! Int
steps
    enharmonics :: Vector [(Int, Degree)]
enharmonics = Layout -> Vector [(Int, Degree)]
layout_enharmonics Layout
layout

-- | Convert Semis to integral NNs.  This is only valid for 12TET, which is the
-- only scale where Semis correspond directly to NNs.
--
-- It doesn't return 'Pitch.NoteNumber' because these values are specifically
-- integral.
--
-- NOTE [middle-c] Middle C is 5 octaves above NN 0, but is conventially called
-- 4c.  Therefore, a 'Pitch' with octave 0 actually starts at NN 12 (in 12TET),
-- and I have to add an octave when converting from NNs and subtract an octave
-- when converting from NNs.
--
-- Previously I considered the octave offset a part of formatting, and added
-- an octave in 'TheoryFormat.p_octave' and subtracted an octave in
-- 'TheoryFormat.show_octave'.  But I was unsatisfied because it applied to
-- all scales, and it seemed confusing to ask for a Pitch with octave 4 and get
-- a note with octave 3.  TODO maybe the add/subtract octave should just go in
-- TheoryFormat.absolute_c?
semis_to_nn :: Pitch.Semi -> Int
semis_to_nn :: Int -> Int
semis_to_nn = (forall a. Num a => a -> a -> a
+Int
12)

fsemis_to_nn :: Pitch.FSemi -> Pitch.NoteNumber
fsemis_to_nn :: FSemi -> NoteNumber
fsemis_to_nn = FSemi -> NoteNumber
Pitch.NoteNumber forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+FSemi
12)

nn_to_semis :: Int -> Pitch.Semi
nn_to_semis :: Int -> Int
nn_to_semis = forall a. Num a => a -> a -> a
subtract Int
12

-- * input

-- | Enharmonics of a pitch.
--
-- This choses the next highest enharmonic until it wraps around, so if you
-- repeatedly pick the first one you'll cycle through them all.
enharmonics_of :: Layout -> Pitch.Pitch -> [Pitch.Pitch]
enharmonics_of :: Layout -> Pitch -> [Pitch]
enharmonics_of Layout
layout Pitch
pitch =
    [ Int -> Degree -> Pitch
Pitch.Pitch (Pitch -> Int
Pitch.pitch_octave Pitch
pitch forall a. Num a => a -> a -> a
+ Int
oct) Degree
n
    | (Int
oct, Degree
n) <-
        Intervals -> Degree -> [(Int, Degree)]
get_enharmonics (Layout -> Intervals
layout_intervals Layout
layout) (Pitch -> Degree
Pitch.pitch_degree Pitch
pitch)
    ]

-- * step

{- | A degree is one step of a scale.  Unlike 'Pitch.PitchClass' it's relative
    to the tonic of the key, but also may have a different range.  This is
    because some scales, such as whole-tone or octatonic, have fewer or more
    degrees than 7, even though the underlying notation system uses only
    7 letters.  This means that not every Degree will map to a PitchClass.

    Another approach is to change the number of PitchClasses, which would
    result in a--h for octatonic, but it would not admit easy modulation from
    an octatonic scale to a septatonic one.

    'Key' has more documentation about the PitchClass and Step distinction.
-}
type Step = Int

-- * Key

{- | A Key is a scale along with a tonic Pitch.

    There's a distinction between \"diatonic\" and \"chromatic\" keys.  It's
    not really standard terminology, but within this module I call scales with
    a 1:1 'Pitch.PitchClass' to 'Degree' mapping \"diatonic\", and the ones
    without \"chromatic\".  That's because diatonic transposition for the
    former kind of scale is defined in terms of pitch classes, regardless of
    what accidentals the 'Pitch.Degree' may have, but the latter kind of scale
    must resort to chromatic transposition, losing the spelling of the original
    note.  Ultimately there is a tension between diatonic and chromatic
    systems.
-}
data Key = Key {
    Key -> Degree
key_tonic :: !Pitch.Degree
    -- | This is the name of the key without reference to its tonic, e.g.
    -- \"dorian\" or \"major\".
    , Key -> Text
key_name :: !Text
    -- | Semitones between each scale degree.  This should have at least two
    -- octaves of intervals, as needed by 'chromatic_steps'.  If this is a
    -- diatonic key, each interval is one pitch class.
    , Key -> Intervals
key_intervals :: Intervals
    -- | Nothing for a chromatic key.
    , Key -> Maybe Intervals
key_signature :: Maybe Signature
    -- | Table to speed up diatonic transposition, see 'make_table'.
    , Key -> Intervals
key_transpose_table :: Intervals
    -- | White and black keys, this is constant for keys
    , Key -> Layout
key_layout :: Layout
    } deriving (Key -> Key -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Key -> Key -> Bool
$c/= :: Key -> Key -> Bool
== :: Key -> Key -> Bool
$c== :: Key -> Key -> Bool
Eq, Int -> Key -> ShowS
[Key] -> ShowS
Key -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [Key] -> ShowS
$cshowList :: [Key] -> ShowS
show :: Key -> [Char]
$cshow :: Key -> [Char]
showsPrec :: Int -> Key -> ShowS
$cshowsPrec :: Int -> Key -> ShowS
Show)

-- | Map from a Step to the number of sharps or flats at that Step.
type Signature = Vector.Vector Pitch.Accidentals
-- | Semitones between each scale degree.
type Intervals = Vector.Vector Pitch.Semi

-- | Make a Key given intervals and a layout.  If the number of intervals are
-- equal to the number of intervals in the layout, the scale is considered
-- diatonic and will get a 'Signature'.
key :: Pitch.Degree -> Text -> [Pitch.Semi] -> Layout -> Key
key :: Degree -> Text -> [Int] -> Layout -> Key
key Degree
tonic Text
name [Int]
intervals Layout
layout = Key
    { key_tonic :: Degree
key_tonic = Degree
tonic
    , key_name :: Text
key_name = Text
name
    , key_intervals :: Intervals
key_intervals = Intervals
ints
    , key_signature :: Maybe Intervals
key_signature = Degree -> Layout -> Intervals -> Maybe Intervals
generate_signature Degree
tonic Layout
layout Intervals
ints
    , key_transpose_table :: Intervals
key_transpose_table = [Int] -> Intervals
make_table [Int]
intervals
    , key_layout :: Layout
key_layout = Layout
layout
    }
    where ints :: Intervals
ints = forall a. [a] -> Vector a
Vector.fromList [Int]
intervals

-- | Precalculated transpositions so I can figure out a transposition with
-- a single table lookup.  This goes out to two octaves on either direction
-- so I can start at any degree and go up to an octave of transposition.
-- Everything past an octave is chopped off by divMod and transposed with
-- multiplication.
make_table :: [Pitch.Semi] -> Intervals
make_table :: [Int] -> Intervals
make_table [Int]
intervals = forall a. [a] -> Vector a
Vector.fromList forall a b. (a -> b) -> a -> b
$
    forall a. [a] -> [a]
reverse (forall a. Int -> [a] -> [a]
drop Int
1 (forall {a} {a}. Num a => (a -> a -> a) -> [a] -> [a]
make (-) (forall a. [a] -> [a]
reverse [Int]
intervals))) forall a. [a] -> [a] -> [a]
++ forall {a} {a}. Num a => (a -> a -> a) -> [a] -> [a]
make forall a. Num a => a -> a -> a
(+) [Int]
intervals
    where make :: (a -> a -> a) -> [a] -> [a]
make a -> a -> a
f = forall a. Int -> [a] -> [a]
take (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
intervals forall a. Num a => a -> a -> a
* Int
2) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. (b -> a -> b) -> b -> [a] -> [b]
scanl a -> a -> a
f a
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
cycle

generate_signature :: Pitch.Degree -> Layout -> Intervals -> Maybe Signature
generate_signature :: Degree -> Layout -> Intervals -> Maybe Intervals
generate_signature Degree
tonic Layout
layout Intervals
intervals
    | forall a. Vector a -> Int
Vector.length (Layout -> Intervals
layout_intervals Layout
layout) forall a. Eq a => a -> a -> Bool
/= forall a. Vector a -> Int
Vector.length Intervals
intervals =
        forall a. Maybe a
Nothing
    | Bool
otherwise = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
        Degree -> Intervals -> Intervals -> Intervals
calculate_signature Degree
tonic (Layout -> Intervals
layout_intervals Layout
layout) Intervals
intervals

calculate_signature :: Pitch.Degree -> Intervals -> Intervals -> Intervals
calculate_signature :: Degree -> Intervals -> Intervals -> Intervals
calculate_signature (Pitch.Degree Int
pc Int
accs) Intervals
layout Intervals
intervals =
    forall a. Int -> Vector a -> Vector a
Vector.take (forall a. Vector a -> Int
Vector.length Intervals
intervals) forall a b. (a -> b) -> a -> b
$
        forall a b c. (a -> b -> c) -> Vector a -> Vector b -> Vector c
Vector.zipWith forall a. Num a => a -> a -> a
subtract (forall a b. (a -> b -> a) -> a -> Vector b -> Vector a
Vector.scanl forall a. Num a => a -> a -> a
(+) Int
0 (forall a. Int -> Vector a -> Vector a
rotate Int
pc Intervals
layout))
            (forall a b. (a -> b -> a) -> a -> Vector b -> Vector a
Vector.scanl forall a. Num a => a -> a -> a
(+) Int
accs Intervals
intervals)
    where
    rotate :: Int -> Vector a -> Vector a
rotate Int
n Vector a
xs = Vector a
post forall a. Semigroup a => a -> a -> a
<> Vector a
pre
        where (Vector a
pre, Vector a
post) = forall a. Int -> Vector a -> (Vector a, Vector a)
Vector.splitAt Int
n Vector a
xs

key_is_diatonic :: Key -> Bool
key_is_diatonic :: Key -> Bool
key_is_diatonic = forall a. Maybe a -> Bool
Maybe.isJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Maybe Intervals
key_signature

instance Pretty Key where
    format :: Key -> Doc
format key :: Key
key@(Key Degree
tonic Text
name Intervals
ints Maybe Intervals
sig Intervals
_table Layout
_layout) = Doc -> [(Text, Doc)] -> Doc
Pretty.record Doc
title
        [ (Text
"intervals",
            forall a. Pretty a => a -> Doc
Pretty.format (forall a. Int -> Vector a -> Vector a
Vector.take (Key -> Int
key_steps_per_octave Key
key) Intervals
ints))
        , (Text
"signature", forall a. Pretty a => a -> Doc
Pretty.format Maybe Intervals
sig)
        ]
        where
        title :: Doc
title = Text -> Doc
Pretty.text Text
"Key" Doc -> Doc -> Doc
Pretty.<+> forall a. Pretty a => a -> Doc
Pretty.format Degree
tonic
            Doc -> Doc -> Doc
Pretty.<+> Text -> Doc
Pretty.text Text
name

-- | The number of accidentals in the key signature at the given pitch class.
accidentals_at_pc :: Key -> Pitch.PitchClass -> Pitch.Accidentals
accidentals_at_pc :: Key -> Int -> Int
accidentals_at_pc Key
key Int
pc = forall a. a -> Maybe a -> a
fromMaybe Int
0 forall a b. (a -> b) -> a -> b
$ do
    Intervals
sig <- Key -> Maybe Intervals
key_signature Key
key
    Intervals
sig forall a. Vector a -> Int -> Maybe a
Vector.!? Key -> Int -> Int
diatonic_step_of Key
key Int
pc

-- | Number of degrees in an octave for this scale.
--
-- This is different from the number of PCs per octave, because scales like
-- octatonic or whole tone don't have a mapping from PCs to scale degrees.
key_steps_per_octave :: Key -> Step
key_steps_per_octave :: Key -> Int
key_steps_per_octave = forall a. Vector a -> Int
Vector.length forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Intervals
key_intervals

-- | Figure out the relative scale step of a degree in the given key.
step_of :: Key -> Pitch.Degree -> Step
step_of :: Key -> Degree -> Int
step_of Key
key Degree
degree
    | Key -> Bool
key_is_diatonic Key
key = Key -> Int -> Int
diatonic_step_of Key
key (Degree -> Int
Pitch.degree_pc Degree
degree)
    | Bool
otherwise = forall (v :: * -> *). Vector v Int => Int -> v Int -> Int
Vector.find_before Int
semis (Key -> Intervals
key_intervals Key
key)
    where semis :: Int
semis = Layout -> Degree -> Int
degree_to_semis (Key -> Layout
key_layout Key
key) Degree
degree

-- | Figure out the (relative) scale step of an absolute PitchClass in
-- a diatonic key.  In a diatonic key, the step and pitch class are relative
-- and absolute versions of the same thing.
diatonic_step_of :: Key -> Pitch.PitchClass -> Step
diatonic_step_of :: Key -> Int -> Int
diatonic_step_of Key
key Int
pc =
    (Int
pc forall a. Num a => a -> a -> a
- Degree -> Int
Pitch.degree_pc (Key -> Degree
key_tonic Key
key)) forall a. Integral a => a -> a -> a
`mod` Key -> Int
key_steps_per_octave Key
key

-- ** Layout

-- | A Layout represents the configuration of white and black keys.
data Layout = Layout {
    -- | Map PitchClass to the number of sharps above it.
    Layout -> Intervals
layout_intervals :: !Intervals
    -- | Map Pitch.Semis to the enharmonic Notes at that PitchClass.
    , Layout -> Vector [(Int, Degree)]
layout_enharmonics :: !(Boxed.Vector [(Pitch.Octave, Pitch.Degree)])
    } deriving (Layout -> Layout -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Layout -> Layout -> Bool
$c/= :: Layout -> Layout -> Bool
== :: Layout -> Layout -> Bool
$c== :: Layout -> Layout -> Bool
Eq, Int -> Layout -> ShowS
[Layout] -> ShowS
Layout -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [Layout] -> ShowS
$cshowList :: [Layout] -> ShowS
show :: Layout -> [Char]
$cshow :: Layout -> [Char]
showsPrec :: Int -> Layout -> ShowS
$cshowsPrec :: Int -> Layout -> ShowS
Show)

layout_semis_per_octave :: Layout -> Pitch.Semi
layout_semis_per_octave :: Layout -> Int
layout_semis_per_octave = forall a. Num a => Vector a -> a
Vector.sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. Layout -> Intervals
layout_intervals

layout_pc_per_octave :: Layout -> Pitch.PitchClass
layout_pc_per_octave :: Layout -> Int
layout_pc_per_octave = forall a. Vector a -> Int
Vector.length forall b c a. (b -> c) -> (a -> b) -> a -> c
. Layout -> Intervals
layout_intervals

layout :: [Pitch.Semi] -> Layout
layout :: [Int] -> Layout
layout [Int]
intervals = Layout
    { layout_intervals :: Intervals
layout_intervals = Intervals
vec
    , layout_enharmonics :: Vector [(Int, Degree)]
layout_enharmonics = forall a. [a] -> Vector a
Boxed.fromList forall a b. (a -> b) -> a -> b
$
        forall a b. (a -> b) -> [a] -> [b]
map (\Degree
n -> (Int
0, Degree
n) forall a. a -> [a] -> [a]
: Intervals -> Degree -> [(Int, Degree)]
get_enharmonics Intervals
vec Degree
n) [Degree]
notes
    }
    where
    vec :: Intervals
vec = forall a. [a] -> Vector a
Vector.fromList [Int]
intervals
    notes :: [Degree]
notes =
        [ Int -> Int -> Degree
Pitch.Degree Int
pc Int
accs
        | (Int
pc, Int
int) <- forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Int]
intervals, Int
accs <- [Int
0..Int
intforall a. Num a => a -> a -> a
-Int
1]
        ]

-- | Enharmonics of a note, along with an octave offset if the enharmonic
-- wrapped an octave boundary.
--
-- This choses the next highest enharmonic until it wraps around, so if you
-- repeatedly pick the first one you'll cycle through them all.
get_enharmonics :: Intervals -> Pitch.Degree -> [(Pitch.Octave, Pitch.Degree)]
get_enharmonics :: Intervals -> Degree -> [(Int, Degree)]
get_enharmonics Intervals
intervals (Pitch.Degree Int
note_pc Int
note_accs) =
    [ forall {a}. Vector a -> Int -> Int -> (Int, Degree)
mknote Intervals
intervals (Int
note_pc forall a. Num a => a -> a -> a
+ Int
pc) (Int
note_accs forall a. Num a => a -> a -> a
+ Int
accs)
    | (Int
pc, Int
accs) <- [(Int, Int)]
pcs, forall a. Num a => a -> a
abs (Int
note_accs forall a. Num a => a -> a -> a
+ Int
accs) forall a. Ord a => a -> a -> Bool
< Int
3
    ]
    where
    -- Find the distance in semitones from neighbor pcs.
    pcs :: [(Int, Int)]
pcs =
        [ (Int
1, -[Int] -> Int
diffs [Int
0])
        , (Int
2, -[Int] -> Int
diffs [Int
0, Int
1])
        , (-Int
2, [Int] -> Int
diffs [-Int
2, -Int
1])
        , (-Int
1, [Int] -> Int
diffs [-Int
1])
        ]
    diffs :: [Int] -> Int
diffs = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
Num.sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (Intervals -> Int -> Int
layout_at Intervals
intervals forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int
note_pc+))
    mknote :: Vector a -> Int -> Int -> (Int, Degree)
mknote Vector a
intervals Int
pc Int
accs = (Int
oct, Int -> Int -> Degree
Pitch.Degree Int
pc2 Int
accs)
        where (Int
oct, Int
pc2) = Int
pc forall a. Integral a => a -> a -> (a, a)
`divMod` forall a. Vector a -> Int
Vector.length Vector a
intervals

layout_at :: Intervals -> Pitch.PitchClass -> Pitch.Accidentals
layout_at :: Intervals -> Int -> Int
layout_at Intervals
intervals Int
pc =
    forall a. a -> Maybe a -> a
fromMaybe Int
0 forall a b. (a -> b) -> a -> b
$ Intervals
intervals forall a. Vector a -> Int -> Maybe a
Vector.!? (Int
pc forall a. Integral a => a -> a -> a
`mod` forall a. Vector a -> Int
Vector.length Intervals
intervals)

-- | True if the degree exists as its own key in the layout.
--
-- For a relative scale, the Intervals should be from 'key_intervals', which
-- considers that the tonic is shifted to PC 0.  For an absolute scale, the
-- keyboard never shifts, so use 'layout_intervals'.
contains_degree :: Intervals -> Pitch.Degree -> Bool
contains_degree :: Intervals -> Degree -> Bool
contains_degree Intervals
intervals (Pitch.Degree Int
pc Int
acc)
    | Int
acc forall a. Ord a => a -> a -> Bool
>= Int
0 = Int
acc forall a. Ord a => a -> a -> Bool
< Intervals -> Int -> Int
layout_at Intervals
intervals Int
pc
    | Bool
otherwise = Intervals -> Int -> Int
layout_at Intervals
intervals (Int
pc forall a. Num a => a -> a -> a
- Int
1) forall a. Num a => a -> a -> a
+ Int
acc forall a. Ord a => a -> a -> Bool
> Int
0