Lab 8: Player Piano

This assignment assumes that you have read up through Chapter 7. Please bring your book to lab, as you will need it as a reference. Each lab assignment is more work than the previous one, so get started early. The objectives for the assignment are to:
  • Learn about the basics of digital sound generation.
  • Practice working with arrays.
  • Practice decomposing complex programs into small functions.
  • Learn how to design a program that translates a high-level specification into a detailed output.
  • Getting Started

    As in the previous lab, start by creating a directory just for this assignment, and changing to that directory:
    mkdir cse20211/lab8
    cd cse20211/lab8
    
    Read about the Simple Sound Library and try out the example program, which generates a single note.

    Part One: Player Piano

    Write a program piano.c that reads in music instructions from the console and produces a file music.wav which contains the indicated music. The musical notation is a sequence of simple characters, where each character either plays a note, or changes the octave and/or length of the following notes.

    CodeAction
    a Play A-flat: 415.30 Hz
    A A: 440.00 Hz
    b B-flat: 466.16 Hz
    B B: 493.88 Hz
    C C: 523.25 Hz
    d D-flat: 554.37 Hz
    D D: 587.33 Hz
    e E-flat: 622.25 Hz
    E E: 659.26 Hz
    F F: 698.46 Hz
    g G-flat: 739.99 Hz
    G G: 783.99 Hz
    . Play a rest (silence) for the length of the current note type.
    + Play one octave higher. (double frequency of all notes)
    - Play one octave lower. (halve frequency of all notes)
    1 Change the current note length to a whole note. (two seconds)
    2 ... a half note. (one second)
    4 ... a quarter note. (half a second)
    8 ... an eighth note. (1/4 of a second)
    6 ... a sixteenth note. (1/8 of a second)
    X End of song - stop the program.

    When the program starts, the default note length is one quarter note. The default tempo is two quarter notes per second. Each note is a simple sine wave at the desired frequency.

    Any white space in the input should be ignored. Any other character not listed above should cause the program to emit a useful error message and continue processing data.

    When you run the program, it should simply read in one character at a time from the console and write out the desired notes to music.wav. For testing, you can type in the music at the keyboard. Even better, you can put the music text into a file example.music and then run this command:

    piano < example.music
    
    which will cause the text input to be read from example.music automatically and generate the file music.wav.

    Sample Music Files

    For initial testing, start with some simple music files of your own creation. When you are ready, try the following:
  • scale.music
  • invention.music
  • laudate.music
  • fiddler.music
  • Part Two: Synthesizer

    Copy your completed piano.c to synth.c and then modify synth.c so that it uses a much more interesting waveform than a simple sine wave. Try out different shapes with multiple harmonics until it sounds like an electric guitar, a trumpet, or some kind of space-age instrument. Add some attack and decay to the beginning and end of each note, so that it doesn't stop and start so abruptly. Combine multiple sounds together for strange effects. In short, be creative!

    Turning In

    Please review the general instructions for lab reports.

    Turn in piano.c and synth.c. (Do not turn in any WAV files, as we will test your program's ability to create them.) Your lab report should explain how the synthesizer waveform was constructed, and what you intend for it to sound like.

    This assignment is due on Monday, Nov 11th at noon. Late assignments are not accepted.