Copyright | (c) Hans Hoglund, Edward Lilley 2012–2014 |
---|---|
License | BSD-style |
Maintainer | hans@hanshoglund.se |
Stability | experimental |
Portability | non-portable (TF,GNTD) |
Safe Haskell | None |
Language | Haskell2010 |
Music.Pitch.Common.Interval
Contents
Description
Provides standard intervals.
- data Quality
- class HasQuality a where
- invertQuality :: Quality -> Quality
- isPerfect :: HasQuality a => a -> Bool
- isMajor :: HasQuality a => a -> Bool
- isMinor :: HasQuality a => a -> Bool
- isAugmented :: HasQuality a => a -> Bool
- isDiminished :: HasQuality a => a -> Bool
- data Number
- class HasNumber a where
- unison :: Number
- prime :: Number
- second :: Number
- third :: Number
- fourth :: Number
- fifth :: Number
- sixth :: Number
- seventh :: Number
- octave :: Number
- ninth :: Number
- tenth :: Number
- twelfth :: Number
- duodecim :: Number
- thirteenth :: Number
- fourteenth :: Number
- fifteenth :: Number
- newtype Interval = Interval {
- getInterval :: (Int, Int)
- mkInterval :: Quality -> Number -> Interval
- perfect :: Number -> Interval
- major :: Number -> Interval
- minor :: Number -> Interval
- augmented :: Number -> Interval
- diminished :: Number -> Interval
- doublyAugmented :: Number -> Interval
- doublyDiminished :: Number -> Interval
- isNegative :: Interval -> Bool
- isPositive :: Interval -> Bool
- isNonNegative :: Interval -> Bool
- isPerfectUnison :: Interval -> Bool
- isStep :: Interval -> Bool
- isLeap :: Interval -> Bool
- isSimple :: Interval -> Bool
- isCompound :: Interval -> Bool
- separate :: Interval -> (Octaves, Interval)
- simple :: Interval -> Interval
- octaves :: Interval -> Octaves
- invert :: Interval -> Interval
- asInterval :: Interval -> Interval
- data IntervalBasis
- convertBasis :: Interval -> Interval -> Interval -> Maybe (Int, Int)
- convertBasisFloat :: (Fractional t, Eq t) => Interval -> Interval -> Interval -> Maybe (t, t)
- intervalDiv :: Interval -> Interval -> Int
- basis_P1 :: Interval
- basis_A1 :: Interval
- basis_d2 :: Interval
- basis_P8 :: Interval
- basis_P5 :: Interval
- intervalDiff :: Interval -> Int
- mkInterval' :: Int -> Int -> Interval
Quality
Interval quality is either perfect, major, minor, augmented, and
diminished. This representation allows for an arbitrary number of
augmentation or diminishions, so augmented is represented by Augmented
1
, doubly augmented by Augmented 2
and so on.
The quality of a compound interval is the quality of the simple interval on which it is based.
Note that (Augmented 0) and (Diminished 0) are superfluous identity values, use Perfect instead. Augmented and Diminished must also take only positive arguments.
Instances
Eq Quality | |
Ord Quality | |
Show Quality | |
Augmentable Quality | Augmentable Quality instance This Augmentable instance exists solely for use of the getQuality function, which ensures that there is never any ambiguity around diminishedaugmented intervals turning into majorminor/perfect intervals. |
HasQuality Quality |
class HasQuality a where Source
Instances
invertQuality :: Quality -> Quality Source
Invert a quality.
Perfect is unaffected, major becomes minor and vice versa, augmented becomes diminished and vice versa.
isPerfect :: HasQuality a => a -> Bool Source
Returns whether the given quality is perfect.
isMajor :: HasQuality a => a -> Bool Source
Returns whether the given quality is major.
isMinor :: HasQuality a => a -> Bool Source
Returns whether the given quality is minor.
isAugmented :: HasQuality a => a -> Bool Source
Returns whether the given quality is augmented (including double augmented etc).
isDiminished :: HasQuality a => a -> Bool Source
Returns whether the given quality is diminished (including double diminished etc).
Number
The number portion of an interval (i.e. second, third, etc).
Note that the interval number is always one step larger than number of steps spanned by
the interval (i.e. a third spans two diatonic steps). Thus number
does not distribute
over addition:
number (a + b) = number a + number b - 1
A synonym for 13
.
A synonym for 14
.
Intervals
An interval is the difference between two pitches, incuding negative intervals.
Intervals and pitches can be added using .+^
. To get the interval between
two pitches, use .-.
.
c .+^ minor third == eb f .-. c == perfect fourth
Adding intervals preserves spelling. For example:
m3 ^+^ _M3 = _P5 d5 ^+^ _M6 = m10
The scalar type of Interval
is Int
, using ^*
to stack intervals of a certain
type on top of each other. For example _P5 ^* 2
is a stack of 2 perfect fifths, or a
major ninth. The Num
instance works as expected for +
, negate
and abs
, and
(arbitrarily) uses minor seconds for multiplication. If you find yourself *
, or
signum
on intervals, consider switching to *^
or normalized
.
Intervals are generally described in terms of Quality
and Number
. To
construct an interval, use the interval
constructor, the utility
constructors major
, minor
, augmented
and diminished
, or the
interval literals:
m5 == minor fifth == interval Minor 5 _P4 == perfect fourth == interval Perfect 5 d5 == diminished fifth == diminish (perfect fifth)
Constructors
Interval | |
Fields
|
Instances
Eq Interval | |
Num Interval | Avoid using '(*)', or |
Ord Interval | |
Show Interval | |
Monoid Interval | |
IsInterval Interval | |
Augmentable Interval | |
Semigroup Interval | |
HasBasis Interval | |
VectorSpace Interval | |
AdditiveGroup Interval | |
HasSemitones Interval | |
HasNumber Interval | |
HasQuality Interval | |
Typeable * Interval | |
type Basis Interval = IntervalBasis | |
type Scalar Interval = Integer |
Creating intervals
mkInterval :: Quality -> Number -> Interval Source
perfect :: Number -> Interval Source
Creates a perfect interval. If given an inperfect number, constructs a major interval.
major :: Number -> Interval Source
Creates a major interval. If given a perfect number, constructs a perfect interval.
minor :: Number -> Interval Source
Creates a minor interval. If given a perfect number, constructs a diminished interval.
diminished :: Number -> Interval Source
Creates a diminished interval.
doublyAugmented :: Number -> Interval Source
Creates a doubly augmented interval.
doublyDiminished :: Number -> Interval Source
Creates a doubly diminished interval.
Inspecting intervals
isNegative :: Interval -> Bool Source
Returns whether the given interval is negative.
isPositive :: Interval -> Bool Source
Returns whether the given interval is positive.
isNonNegative :: Interval -> Bool Source
Returns whether the given interval is non-negative. This implies that it is either positive or a perfect unison.
isPerfectUnison :: Interval -> Bool Source
Returns whether the given interval a perfect unison.
isStep :: Interval -> Bool Source
Returns whether the given interval is a step (a second or smaller).
Only diatonic number
is taken into account, so _A2
is considered
a step and m3
a leap, even though they have the same number of
semitones.
isLeap :: Interval -> Bool Source
Returns whether the given interval is a leap (larger than a second).
Only the diatonic number
is taken into account, so _A2
is considered
a step and m3
a leap, even though they have the same number of
semitones.
Simple and compound intervals
isSimple :: Interval -> Bool Source
Returns whether the given interval is simple.
A simple interval is a non-negative interval spanning less than one octave.
isCompound :: Interval -> Bool Source
Returns whether the given interval is compound.
A compound interval is either a negative interval, or a positive interval spanning one octave or more.
separate :: Interval -> (Octaves, Interval) Source
Separate a compound interval into octaves and a simple interval.
(perfect octave)^*x + y = z iff (x, y) = separate z
simple :: Interval -> Interval Source
Returns the simple part of an interval.
(perfect octave)^*x + y = z iff y = simple z
octaves :: Interval -> Octaves Source
Returns the non-simple part of an interval.
_P8^*octaves x ^+^ simple x = x
Inversion
invert :: Interval -> Interval Source
Intervallic inversion.
The inversion an interval is determined as follows:
- The number of a simple interval the difference of nine and the number of its inversion.
- The quality of a simple interval is the inversion of the quality of its inversion.
- The inversion of a compound interval is the inversion of its simple component.
Utility
asInterval :: Interval -> Interval Source
This is just the identity function, but is useful to fix the type of Interval
.
Basis values
data IntervalBasis Source
Converting basis
convertBasis :: Interval -> Interval -> Interval -> Maybe (Int, Int) Source
Represent an interval i in a new basis (j, k).
We want x,y where i = x*j + y*k
e.g., convertBasis basis_d2 _P5 basis_P8 == Just (-12,7), as expected.
convertBasisFloat :: (Fractional t, Eq t) => Interval -> Interval -> Interval -> Maybe (t, t) Source
Same as above, but don't worry if new interval has non-integer coefficients -- useful when getting a value to use as a frequency ratio in a tuning system.
intervalDiv :: Interval -> Interval -> Int Source
Integer div of intervals: i / di = x, where x is an integer
Basis values (TODO cleanup)
Utility
intervalDiff :: Interval -> Int Source