liveBook · Manning

Semitone

As explained in Learn Haskell by Example

Semitone is a book-specific Haskell type representing an integer distance from concert A in musical semitone intervals.

A semitone is an Integer measuring musical distance from concert A. Zero represents 440 Hz; positive and negative values move above or below it. section 10.4.1

The Semitone type becomes Pitchable by converting its integer distance into frequency using 12-tone equal temperament, with concert A fixed at 440 Hz. section 10.4.1

Useful consequences of the representation include:

  • Adding 12 semitones doubles frequency, while subtracting 12 halves it. section 10.4.1
  • Semitones provide an intermediate representation for converting named chromatic notes into frequencies. section 10.4.1

GHCi calculations

toFrequency (0 :: Semitone) gives 440.0, toFrequency (12 :: Semitone) gives 880.0, and toFrequency (-12 :: Semitone) gives 220.0. section 10.4.1

Listing 10.13 A type for semitones
{-# LANGUAGE TypeSynonymInstances #-}     #1
module Composition.Pitch where

...

type Semitone = Integer      #2

instance Pitchable Semitone where
  toFrequency semitone =
    440 * (2.0 ** (fromInteger semitone / 12))    #3

Shows the Semitone alias and its Pitchable instance, including the frequency calculation. section 10.4.1

How Learn Haskell by Example covers it

How deep it goes
Defines the Integer representation, conversion formula, examples, and a QuickCheck exercise, then moves to named notes.
Examples
Uses GHCi calculations for zero, positive, negative, and octave-shifted semitone values.
What you can do after
Apply via Pitchable to calculate oscillator frequencies and test octave relationships.
How it connects
Connects semitones to Pitchable, frequency, concert A, chromatic notes, and 12-tone equal temperament.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.