Assignment 11

Home

Computer Music

Related Reading:  Chapters 20 and 21 in SOE.

Due:  Anytime during Reading Week, but no later that midnight May 4.

Preliminaries: 

  1. Read Chapters 20 and 21 in SOE.
  2. Download and unzip the SOE source code.
  3. Copy (or move) all the files in SOE/haskore/src to SOE/src.
  4. To test the system, fire up GHCi in the directory SOE/src, and load the module MDL.  Then try running:
    bullet"testNT cMajArp" on Windows XP, or
    bullet"testWin95 cMajArp" on older versions of Windows, or
    bullet"testLinux cMajArp" on Linux.
  5. If none of these work, then type "test cMajArp", which will write a MIDI file called "test.mid" in SOE/src,
    which you can then double-click to play using whatever your computer's default MIDI player is.

Assignment:

  1. Write a simple monophonic (i.e. one-voice) melody in MDL.  This could be your own invention, or a simple melody that you know.  If you don't know music, remember that a melody is just a sequence of notes -- so write anything that you want!
     
  2. If the above melody is called m, then compose a song in which you are not allowed to introduce new notes, but you can use m as many times as you like, and you must use each of the following operators at least once:
    bulletsequential composition (i.e. :+:)
    bulletparallel composition (i.e. :=:)
    bulletdelay
    bulletrevM
    bulletTempo
    bulletTrans
    bulletInstr
     
  3. Write a function called invert that takes a Music value and "inverts" each note in it with respect to a given pitch.  For example, if the given pitch is (C,5), then the "inversion" of (A,4) is (Ef,5), and so on.  The type of invert should be Pitch -> Music -> Music.  (Hint: use the functions absPitch and pitch to make your life easier.)  Test your solution.
     
  4. Extra credit:  Prove that invert p . invert p = id, where p is any pitch and id is the identity function.

 

Solutions

Here are solutions to problems 3 and 4. However, I have not tested this code, so take it with a grain of salt!

Problem 3

> invert :: Pitch -> Music -> Music
> invert p (Note p0 d) =
>   let p' = pitch (2 * absPitch p - absPitch p0)
>   in Note p' d
> invert p (Rest d) = Rest d
> invert p (m1 :+: m2) = invert p m1 :+: invert p m2
> invert p (m1 :=: m2) = invert p m1 :=: invert p m2
> invert p (Tempo r m) = Tempo r (invert p m)
> invert p (Trans n m) = Trans (-n) (invert p m)
> invert p (Instr i m) = Instr i (invert p m)


Problem 4

Prove that invert p . invert p = id.

Unfortunately, this theorem is in fact false! The reason is that its proof relies on two lemmas:

Lemma:
    pitch . absPitch = id
    absPitch . pitch = id

But the first one is actually false because, for example:
    pitch (absPitch (Df,0)) = pitch 1 = (Cs,0)
In other words, a D-flat gets converted into a C-sharp.

The right theorem involves the notion of "interpretation" discussed in Chapter 21, since a D-flat indeed "sounds" the same as a C-sharp. But instead of doing that, I will just assume that the two lemmas above are correct, and prove a false theorem :-).

Proof by induction.

Base cases:

invert p (invert p (Note p0 d))
= invert p (Note (pitch (2 * absPitch p - abspitch p0)) d)
= Note (pitch (2 * absPitch p - abspitch (pitch (2 * absPitch p - absPitch p0)))) d
= Note (pitch (2 * absPitch p - (2 * absPitch p - absPitch p0))) d
= Note (pitch (absPitch p0)) d
= Note p0 d

invert p (invert (Rest d))
= invert p (Rest d)
= Rest d


Induction steps:

invert p (invert p (m1 :+: m2))
= invert p (invert p m1 :+: invert p m2)
= invert p (invert p m1) :+: invert p (invert p m2)
= m1 :+: m2


Similarly for :=:, the other binary operator.

invert p (invert p (Tempo r m))
= invert p (Tempo r (invert p m))
= Tempo r (invert p (invert p m))
= Tempo r m


Similarly for Instr.

Last but not least:

invert p (invert p (Trans n m))
= invert p (Trans (-n) (invert p m))
= Trans n (invert p (invert p m))
= Trans n m