Humans are always looking for new sounds, and recently a growing number of artists have been using programs to create experiences that instruments are unable to produce. Here are two examples of songs that were created with computer generated sounds:

Write a program that reads in notes from a file and play the composition back to the user.

Audio

There are three Audio methods that will allow you to generate and play notes.

ArrayList<Double> samples = Audio.getNoteSamples(frequency, length)
Audio.play(arrayListOfDoubles)

getNoteSamples is a method that takes in two numbers, a frequency and a length in samples (there are 44,100 samples per second in CD quality sound). The frequency defines the note being played. For example a C note has frequency 130.81 Hz and a G note has frequency 196.00 Hz. You should use the contants defined. This method returns an ArrayList of Doubles, which represents the samples of the wave that makes the sound.

Let's take the wave below as an example. It has a frequency of 130.81 Hz which is a C note. The method will sample the wave, and return the doubles collected from the sample, in the range (-1, 1):


Image courtesy of Keith

play is a method that takes in a list of samples (which must be of type ArrayList<Double>) and plays it to the user.

getNotes is a method that takes in the name of a song, and returns an ArrayList<String> with all the notes in the song. The letters in the ArrayList can be lower or upper case notes (a-g and p for pause). Here is an example ArrayList: {a, a, G, f, p, B}.

1) Play a Note

To get started try to generate a single C note of length NOTE_LENGTH and play it back to the user. Then try to play some different notes. Try to play the notes E, C, C. Finally, play a note an octave up and make sure you can tell the difference.

2) Make a Musical Program

This project is open ended. At this point you can program what ever you would like. Would you like to create a graphical piano? How about generating more complex sounds? See the extensions and try and implement as many as you would like.

Tips and Tricks

Playing up an Octave
To play an octave up all you have to do is double the frequency. For example, if the original C is 130.81, then the higher C one octave up is 2 * 130.81 = 261.62

Use Methods
You might find it easier to write a method to play each note:

private void playG() {
   ArrayList<Double> example = Audio.getNoteSamples(G, NOTE_LENGTH);
   Audio.play(example);
}

Compare Strings

To compare strings, don't use the == operator. Instead you should use:

s1.equals(s2)

That's all! Welcome to the world of ArrayLists and computer music. If you finish early try an extension.


Extension 1: Write your Own Music

Try and compose a melody from different notes. Add higher level phrases to the songs. Use loops and methods to make complex sounds.

Extension 2: Chords

Two play two (or more) notes simultaneously you simply add up each value in the sample. Then, to make the volume normal, divide the resulting sample by its maximum value.

You can make richer sounds, that are closer to the ones used by Tycho and Pretty Lights (in the examples at the beggining).

Extension 3: Graphical Interface

Make a graphical interface using a grid of GRects where each row represents a note (C, D, G, E, A) and each column represents a point in time. If a GRect is turned on (is colored blue) you should play that note at that time.

Ask us about buttons