Inspired by, and built off an assignment by Keith Schwarz. Created by Chris Piech and Bryce Cronkite-Ratcliff
Example:
Snow
Handouts:
ArrayLists
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 the user (or a pause) until the user enters the String "play." Then play the composition back to the user.
Just for fun: Can you figure out what the above song plays?
There are two 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. The method returns an ArrayList of Doubles, which represents the samples of the wave that makes the sound.
Say the wave below has a frequency of 130.81 Hz (and is thus a C note). The method will sample the wave, and return the doubles collected from the sample, in the range (-1, 1):
play is a method that takes in a list of samples (which must be of type ArrayList<Double>) and plays it to the users.
To get started try and generate a single C note of length NOTE_LENGTH and play it back to the user.
Write code that continually collects notes from the user (until they type "play") and puts them together into a single song. If the user enters one of the notes in the pentatonic scale (C, D, G, E or A) add the samples for that note to the song. If the user enters "pause", add NOTE_LENGTH number of samples with value 0.
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.
If you want to play two notes at the same time the solution is simple, you just add the waves together (in other words you add each value in the list of doubles together).
When you add two waves together, some points may become larger than 1 or smaller than -1 (the bounds for a sound wave). After you add your samples together you should normalize the wave. In plainer words, find the maximum value in your ArrayList and divide all values by the max.
Add "c-chord" to the list of commands the user can enter. C chords are made up of the notes C, E and G played at the same time.
Add higher level phrases that the user can enter. Use loops and methods to make complex sounds. For example, maybe you want to let the user enter the text "chorus" 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.
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