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)
ArrayList<String> notes = Audio.getNotes(songName)

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) Print out Songs

The next step after figuring out how to play a single note is to play a song. To do that, you need to get a song from the user. Write some code that will ask the user for a song, and print out the associated notes. Audio.getNotes will come in handy for this part.

3) Play Songs

Finally, convert each note into an ArrayList to build up the entire song. For each note:

  • If the note is lower case (a, b, c, d, e, f or g), add the samples for that note to the song
  • If the note is upper case (A, B, C, D, E, F, or G), add a note up one octave from that note to the song
  • If the note is a 'p', add NOTE_LENGTH number of samples with value 0 for a pause

That's all! Welcome to the world of ArrayLists and computer music. You should read the tips and tricks section below for some hints for the tricky parts. If you finish early try an extension.

Tips and Tricks

Playing up an Octave
To play an octave up (when the note is an upper case letter), 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: Note Duration

In music, not all notes are the same length. In order to play a larger range of songs, it is helpful to be able to play notes for a shorter and longer duration.

Modify your music player to play sound for a variable length. To assist you, there is another method in Audio called getDurations that takes in a song and gives you an ArrayList of all the note durations in units of NOTE_FREQUENCY.

ArrayList<Double> durations = Audio.getDurations(songName);

You can use these durations when you call Audio.play to vary how long a note is played for. Once you have it done, see how happybirthday sounds

Extension 2: Write your Own Music

Take a look at the song files and you will see that they are incrediblly simple. This means that you can easily write your own! Try adding your own songs and playing them with the audio player you created

Extension 2: Algorithmic Song

Add higher level phrases to the songs. Use loops and methods to make complex sounds. For example, maybe you want to read an 'h' for "chorus" (c is taken for playing the note 'c') and then you can algorithmically create a series of notes that make up the chorus of a song.

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

Please share any songs you create with us.

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