The purpose of this assignment is to introduce you to loops and playing sound files.


  1. Test your environment.  To check that your programming environment is configured to use our standard libraries (e.g., to play sound), compile and execute SayHelloWorld.java. The result of executing the program should be sound (instead of text or graphics).

    The people, google, siri subdirectories of the project file contains a large number of WAV files of Hello World spoken in different voices and languages, both real (from various individuals) and synthetic (from Google Translate and Apple Siri). Below are some sample executions:

    ~/Desktop/sound-loops> javac-introcs SayHelloWorld.java
    
    ~/Desktop/sound-loops> java-introcs SayHelloWorld people/KevinWayne.wav
    
    
    
    
    ~/Desktop/sound-loops> java-introcs SayHelloWorld google/English.wav
    
    
    
    
    ~/Desktop/sound-loops> java-introcs SayHelloWorld google/Spanish.wav
    
    
    
    
    ~/Desktop/sound-loops> java-introcs SayHelloWorld siri/Kyoko.wav
    
    
    

    The key method call is the following, which reads and plays the specified WAV file:

    StdAudio.play("filename.wav");
    


  2. Drum loop.  Write a program DrumLoop.java that takes the name of a WAV file as a command-line argument and repeatedly plays the specified WAV file in an infinite loop.

    As an example, the Amen break is one of the most widely sampled tracks in history and has been used in a variety of music genres, including hip hop, jungle, and industrial. This 4-bar drum loop was created by the multiracial soul group The Winstons in their 1969 track "Amen, Brother."

    Here are some sample executions:

    ~/Desktop/sound-loops> javac-introcs DrumLoop.java
    
    ~/Desktop/sound-loops> java-introcs DrumLoop AmenBreak.wav
    
    
    
    
    ~/Desktop/sound-loops> java-introcs DrumLoop Fresh.wav
    
    
    
    
    ~/Desktop/sound-loops> java-introcs DrumLoop heartbeat.wav
    
    
    
    
    ~/Desktop/sound-loops> java-introcs DrumLoop SOS.wav
    
    
    
    Extra credit: find and submit other WAV files, that when looped, produces an interesting effect (e.g., famous drum loop, ultrasound, bird chirp).


  3. We Will Rock You.  The British rock band Queen's iconic song We Will Rock You uses stomping and clapping as a rhythmic percussion beat.

    In this exercise, you will create a version of this stomp–stomp–clap rhythm. To do so, write an infinite loop to repeatedly play the four WAV files in the given order: stomp.wav, stomp.wav, clap.wav, and silence.wav.

    ~/Desktop/sound-loops> javac-introcs StompStompClap.java
    
    ~/Desktop/sound-loops> java-introcs StompStompClap
    
    
    

    Extra credit: find and submit another sequence of WAV file, that when played in succession and repeated, produces an interesting effect (e.g., a percussive beat).


  4. Major scale.  A major scale is a sequence of 8 notes in a specific interval pattern, starting with a root note and ending with the same note one octave higher. For convenience, we'll label each note with its MIDI note number (an integer between 0 and 127, with middle C numbered 60 and concert A numbered 69). Using this numbering, the interval pattern for a major scale is 2, 2, 1, 2, 2, 2, and 1. For example, a C major scale (starting at middle C) consists of the MIDI notes 60, 62, 64, 65, 67, 69, 71, and 72.

    Write a program MajorScale.java that takes three command-line arguments (a MIDI note number, a number of octaves, and an instrument name) and plays a major scale starting at the specified root note, for the specified number of octaves, both ascending and descending. To play a note, use the corresponding WAV file in the directory specified by the instrument name. For example, piano/60.wav is middle C on a piano and opera/69.wav is concert A from an opera singer.

    For example, here are the notes in a 1-octave A major scale, starting at concert A (69), ascending and descending:

    And here are the notes in a 2-octave C major scale, starting at middle C (60), ascending and descending:

    Note that a k-octave major scale has 7k + 1 notes ascending and 7k + 1 notes descending.

    ~/Desktop/sound-loops> javac-introcs MajorScale.java
    
    ~/Desktop/sound-loops> java-introcs MajorScale 60 1 piano
    
    
    
    
    ~/Desktop/sound-loops> java-introcs MajorScale 69 1 piano
    
    
    
    
    ~/Desktop/sound-loops> java-introcs MajorScale 60 3 opera
    
    
    

    Write programs to produce other musical scales, such as minor, chromatic, natural minor, harmonic major, harmonic minor, melodic minor, and major pentatonic.


  5. Musical dice game.  A Musikalisches Würfelspiel is a process for generating music randomly by concatenating a sequence of precomposed musical elements. In this exercise, you will compose a Viennese waltz in the style of Mozart by playing a sequence of 32 precomputed measures that are chosen according to the random process described below. The waltz has two parts—the minuet, followed by the trio.

    The mozart subdirectory of the project file contains the 272 WAV files (\(11 \times 16 = 176\) for the minuet measures and \(6 \times 16 = 96\) for the trio measures), using the naming conventions described above.

    ~/Desktop/sound-loops> javac-introcs MusicalDiceGame.java
    
    ~/Desktop/sound-loops> java-introcs MusicalDiceGame
    
    
    
    
    ~/Desktop/sound-loops> java-introcs MusicalDiceGame
    
    
    
    Context: each time you run the program you will compose a piece of music that has never been heard before! The precomposed measures obey a rigid harmonic structure so that each waltz reflects Mozart's distinct style. Unfortunately, due to this rigid structure, the process never results in anything truly extraordinary.


    Submission. Submit DrumLoop.java, StompStompClap.java, MajorScale.java, and MusicalDiceGame.java. You may not call library functions except those in the java.lang (such as Integer.parseInt() and Math.sqrt()). Do not use Java features that have not yet been introduced in the course.

    This assignment was developed by Kevin Wayne.
    Copyright © 2022.