A chromatic note name identifies one of twelve semitone-spaced names within an octave, such as A, As, B, C, or Cs. It separates a note’s name from its octave number, making pitch values easier to represent and pattern-match. section 10.4.1
Listing 10.12 A class for types that can be converted to a frequency
{-# LANGUAGE TypeSynonymInstances #-}
module Composition.Pitch where
class Pitchable a where #1
toFrequency :: a -> Hz
instance Pitchable Hz where
toFrequency = id #2
Use the accompanying pitch-conversion design to connect chromatic notes with frequency calculations. section 10.4.1
A practical Haskell representation separates the two parts:
- ChromaticName enumerates the twelve names and derives Show and Eq. section 10.4.1
- Chromatic pairs a ChromaticName with a nonnegative Natural octave number. section 10.4.1
- Helper functions such as a, as, and gs construct notes by supplying only the octave. section 10.4.1
- Conversion assigns each name an offset from A4 and combines it with the octave displacement. section 10.4.1
Converting a note to frequency
Evaluating toFrequency (a 4) produces 440.0, while a 5 produces 880.0. Notes can then form a melody whose frequencies are written to a WAV file. section 10.4.1
