# [Haskell-cafe] help with musical data structures

Hans Aberg haberg at math.su.se
Sun Nov 15 13:52:09 EST 2009

```On 15 Nov 2009, at 12:55, Stephen Tetley wrote:

>>  but maybe it is not what you need, since it distinguishes between
>> C sharp
>> and D flat and so on.

> The enharmonic doublings and existing Ord instance make Haskore's
> PitchClass a tricky proposition for representing the Serialist's view
> of pitch classes. An integer (or Z12) represent would be simpler.

A Z12 representation is really only suitable for serial music, which
in effect uses 12 scale degrees per octave.

> To get pitch names I would recover them with a post-processing step,
> spelling pitches with respect to a "scale" (here a SpellingMap):
>
>> spell :: SpellingMap -> Pitch -> Pitch
>
> The spell function returns the note in the scale (SpellingMap) if
> present, otherwise it returns the original to be printed with an
> accidental.
>
> I have my own pitch representation, but a SpellingMap for Haskore
> would be
>
>> type SpellingMap = Data.Map PitchClass PitchClass
>
> Scales here are functions that generate SpellingMaps rather than
> objects themselves.
> The modes and major and minor scales have easy generation as they are
> someways rotational over the circle of fifths (I've have implemented a
> useful algorithm for this but can't readily describe it[1]). Hijaz and
> klezmer fans need to construct their spelling maps by hand.

The pitch and notation systems that Western music uses can be
described as generated by a minor second m and major second M. Sharps
and flats alter with the interval M - m. If departing from two
independent intervals, like a perfect fifth and the octave, then m and
M can be computed. - I have written some code for ChucK which does
that and makes them playable on the (typing) keyboard in a two-
dimensional layout.

The pitch system, which I call a "diatonic pitch system", is then the
set of combinations p m + q M, where p, q are integers (relative a
tuning frequency). The sum d = p + q acts a scale degree of the pitch
system. Sharps and flats do not alter this scale degree. Typical
common 7 note scales have adjacent scale degrees. This is also true
for scales like hijaz.

The note name can then be computed as follows:

First one needs (p, q) values representing the note names a b c d e f
g having scale degrees 0, ..., 6, plus a value for the octave. If
given an arbitrary combination (p, q), first reduce its octave, and
then compute its scale degree; subtract the (p, q) value of the note
name with the same scale degree. There results a note with p + q = 0,
i.e., p = - q. If q > 0, it is is the number of sharps, if p > 0 it is
the number of flats.

This method can be generalized. It is not necessary to have 7 notes
per diapason, and the diapason need not
be the octave. By adding neutral seconds, one can describe more
general pitch systems (one is enough for Arab, Persian and Turkish
scales).

Hans

```