The purpose of this assignment is to introduce you to programming with loops, in the context of music.


  1. Music loop.  A music loop is a section of music that is designed to be repeated seamlessly. Music loops are used in wide variety of music genres, from rock, hip hop, and rap to ringtones, video game soundtracks, and Buddhist music boxes. Here are two famous examples:

    Write a program MusicLoop.java that takes two command-line arguments (a string filename of a .wav file and a positive integer) and plays the .wav file in a music loop the specified number of times. Here are some sample executions:

    ~/Desktop/loops> javac-introcs MusicLoop.java
    ~/Desktop/loops> java-introcs MusicLoop StompStompClap.wav 25
    
    
    
    
    ~/Desktop/loops> java-introcs MusicLoop AmenBreak.wav 10
    
    
    
    
    ~/Desktop/loops> java-introcs MusicLoop Fresh.wav 50
    
    
    
    
    ~/Desktop/loops> java-introcs MusicLoop Heartbeat.wav 60
    
    
    
    
    ~/Desktop/loops> java-introcs MusicLoop Ringtones/Marimba6.wav 6
    
    
    

    Optional: Create and submit a WAV file MyMusicLoop.wav that, when looped, produces an interesting effect (such as a drum loop, ultrasound, bird chirp). You can do this either via a personal recording or by dowloading a file and cropping it.


  2. Major scale (one octave).  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. 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 two command-line arguments (an integer MIDI note number and a string instrument name) and plays a major scale starting at the specified root note. To play a note, use the corresponding WAV file in the directory specified by the instrument name. For example, piano/piano69.wav is concert A on a piano and opera/opera60.wav is middle C from an opera singer.

    For example, here are the notes in a C major scale, starting at middle C (60):

    Here are the notes in an A major scale, starting at concert A (69):

    You must use a single for loop in this program. Note that the MIDI note number increases by 2 in each step, except after the 3rd and 7th notes, in which case it goes up by 1.

    Here are some sample executions:

    ~/Desktop/loops> javac-introcs MajorScale.java
    ~/Desktop/loops> java-introcs MajorScale 60 piano
    
    
    
    
    ~/Desktop/loops> java-introcs MajorScale 69 piano
    
    
    
    
    ~/Desktop/loops> java-introcs MajorScale 45 opera
    
    
    
    
    ~/Desktop/loops> java-introcs MajorScale 60 solfege
    
    
    


  3. Major scale (multiple octaves).  Now, as a continuation to the previous exercise, you will write a program that plays a multiple-octave scale, both ascending and descending. For example, here are the notes in a 1-octave A major scale, starting at concert A (69), ascending and descending:

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

    Write a program DeluxeMajorScale.java that takes three command-line arguments (an integer MIDI note number, an integer number of octaves, and a string instrument name) and plays a major scale starting at the specified root note, for the specified number of octaves, both ascending and descending. Note that a \(k\)-octave major scale has \(7k + 1\) notes ascending and \(7k + 1\) notes descending; moreover, each note is played exactly twice (the \(i\)th and (\(14k + 3 - i\))th notes are the same).

    ~/Desktop/loops> javac-introcs DeluxeMajorScale.java
    ~/Desktop/loops> java-introcs DeluxeMajorScale 69 1 piano
    
    
    
    ~/Desktop/loops> java-introcs DeluxeMajorScale 60 2 piano
    
    
    
    ~/Desktop/loops> java-introcs DeluxeMajorScale 50 2 mbira
    
    
    
    ~/Desktop/loops> java-introcs DeluxeMajorScale 36 3 guitar
    
    
    


  4. Sepia filter (using standard picture). Write a program SepiaFilter.java to apply a sepia filter to an image (using StdPicture). A sepia filter adds a warm reddish-brown tone to the image, giving it a vintage look. Sepia filters are popular on Instagram, VSCO, Facetime, and Photoshop.

    “Nassau Hall facing Cannon Green” by Nick Barberio
    sepia filter


    To apply a sepia filter to an image, consider each pixel one at a time.

    Write a program SepiaFilter.java that takes the name of an image file as a command-line argument, applies a sepia filter to that image, and displays the results in a window. Use StdPicture to read, modify, and display the picture.

    Here are some sample executions for the images NassauHall.jpg and JohnsonArch.jpg:

    ~/Desktop/loops> javac-introcs SepiaFilter.java
    ~/Desktop/loops> java-introcs SepiaFilter NassauHall.jpg
    
    Nassau Hall Sepia
    
    ~/Desktop/loops> java-introcs SepiaFilter JohnsonArch.jpg
    
    Johnson Arch Sepia
    
    

    Take a photo of a building, archway, gate, or other structure on campus and apply the sepia filter to that image. Submit a JPG file MyPhoto.jpg of the source image.


    Submission. Submit MusicLoop.java, MyLoop.wav, MajorScale.java, DeluxeMajorScale.java, SepiaFilter.java and MyPhoto.jpg. Also submit a readme.txt file and answer the questions. You may not call library functions except those in the java.lang (such as Integer.parseInt() and Math.random()). Do not use Java features that have not yet been introduced in the course.


    Grading.
    File Points
    MusicLoop.java 7
    MajorScale.java 7
    DeluxeMajorScale.java 8
    SepiaFilter.java 8
    MyPhoto.jpg 5
    readme.txt 5
    Total 40

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