FIRST: Create a folder or subdirectory in your 15-104 working area on your computer or in your Andrew private folder for handin-10. You will put all of your work inside this folder and then compress or zip it to create handin-10.zip to submit to Autolab.
In this part of the deliverable, you will download this Word file that contains 4 questions about concepts taught in class. You may download it anywhere on your computer. You should fill in your answers in the spaces provided, and include your name, andrewID and section letter at the top of page 1.
Once you are finished with this part of the deliverable, print/save it as a PDF and store this PDF in your handin-10 folder with the name andrewID-10-concepts.pdf. For example, if your andrewID is acarnegie, then you would save the PDF under the name acarnegie-10-concepts.pdf.
In this assignment, you will create a Player Piano on the canvas that can play a tune stored in a pair of arrays to create an automated music player.
Start with the special template template2023-p5all.zip that includes additional code for using sound files. Rename the folder andrewID-10-assignment. Move it to inside of the handin-10 folder. DO NOT EDIT THE ZIP FILE DIRECTLY--- UNCOMPRESS IT FIRST!
Use the following code from class to start your sketch.js file:
function setup() { createCanvas(800, 400); useSound(); } function soundSetup() { osc = new p5.Oscillator(); osc.amp(0.25); osc.setType('square'); // pick which waveform you'd like to use osc.start(); } // Frequencies in Hz for each of the white and black keys, left to right var white_freqs = [261.625, 293.665, 329.63, 349.23, 392, 440, 493.88, 523.25]; var black_freqs = [277, 311, 0, 370, 415, 466]; // index 2 not used function draw() { background(200); drawPiano(); } function drawPiano() { //black keys fill(0); stroke(255); for (var i = 0; i < 6; i++) { if (i != 2) { rect(i*100 + 50, 0, 100, 200); } } //white keys fill(255); stroke(0); for (var i = 0; i < 8; i++) { rect(i*100, 200, 100, 200); } //stop key (red) fill(255, 0, 0); stroke(255, 0, 0); rect(650, 0, 100, 200); } function mousePressed() { // 1. when a user clicks on a white key, the oscillator // is set to the corresponding frequency in white_freqs // 2. when a user clicks on a black key, the oscillator // is set to the corresponding frequency in black_freqs // 3. when a user clicks on the red key, the oscillator // is turned off if (mouseY >= 200 && mouseY <= 400 && mouseX >= 0 && mouseX < 800) { // pressed a white key osc.start(); keynum = floor(mouseX / 100); osc.freq(white_freqs[keynum]); } else if (mouseY >= 0 && mouseY < 200 && mouseX >= 50 && mouseX < 750) { // pressed a black or red key keynum = floor((mouseX-50) / 100); if (keynum == 6) { osc.stop(); } else if (keynum != 2) { osc.start(); osc.freq(black_freqs[keynum]); } } }
The code above draws the piano keys on the canvas as rectangles and plays notes as you click on the "keys". (Try it out and make sure you understand how the entire program works.)
Modify this sketch so that it plays a tune stored in two arrays, one for the pitches of the notes and one for the duration of the notes. The tunes are given below, with pitches shown as MIDI values and durations shown in beats. A value of 0 for a pitch means a rest (i.e. silence). As each note is played, you should highlight that key in yellow while it is playing.
Here is a sample pair of arrays to use to test the program:
Pitch array:
[ 62, 62, 69, 69, 71, 71, 69, 0, 67, 67, 66, 66, 64, 64, 62, 0, 69, 69, 67, 67, 66, 66, 64, 0, 69, 69, 67, 67, 66, 66, 64, 0, 62, 62, 69, 69, 71, 71, 69, 0, 67, 67, 66, 66, 64, 64, 62, 0 ]
Duration array:
[ 4, 4, 4, 4, 4, 4, 6, 2, 4, 4, 4, 4, 4, 4, 6, 2, 4, 4, 4, 4, 4, 4, 6, 2, 4, 4, 4, 4, 4, 4, 6, 2, 4, 4, 4, 4, 4, 4, 6, 2, 4, 4, 4, 4, 4, 4, 7, 1 ]
One beat is equal to one frame for this program, so a note that lasts 4 beats will play for 4 frames before you move on to the next note. Use a frame rate of 8 for your program. You should call the freq function only on the frame when a note starts. Do not call it on every single frame!
Here is the general idea:
In this Project, you are asked to create a very short story with sound. We are expecting 15-30 seconds. You should have at least four simple “characters” in your story (e.g. a cloud, a bird, a train, etc.). The characters can be abstract and basic, no need for fine detail. The characters should move around the canvas and make their distinctive sound(s) when appropriate. You may include captions on the canvas to help tell the story but be sure they’re visible long enough to be readable (e.g. “The bird flew away as the train left the station.”). You can set the frame rate very low (e.g. 1) so that each frame is essentially one picture of the story.
Start with the special template template2023-p5all.zip that includes additional code for using sound files. Rename the folder andrewID-10-project. Move it to inside of the handin-10 folder. DO NOT EDIT THE ZIP FILE DIRECTLY--- UNCOMPRESS IT FIRST!
Use the system variable frameCount to trigger parts of your story. For example:
if (frameCount >= 20 && frameCount < 25) { trainLeavesStation(); }
Suggestions
You can find lots of free sounds at freesound.org. You can also search for “sound effects” and find many sources. “Sound effects” can be interpreted broadly. The sounds can be spoken words, musical tones, animal sounds, natural sound, etc. No profanity, please.
Once you get a collection of sounds, you may wish to edit them or select the parts you want. We suggest using Audacity unless you already have a favorite audio editor. In Audacity, you can easily select the part of a sound that you want, and then use “File:Export Selected Audio…” to write the selection as a .wav file. You can use monophonic or stereo sounds. Audacity gives you many other options, e.g. you can adjust the volume (amplitude) of a sound.
As with images, load all sounds in preload() so that the sounds are actually available to play when you try to play them.
Remember: If you use sound files, you will need to run a local server to make the sounds accessible to the browser running your sketch. Put the sound files in the same directory/folder as index.html and sketch.js. See APPENDIX below for more details.
Requirements
Write a comment in your program that explains the general story line. Hand in the program on Autolab with the sound files you are using. Keep the sound files very short. They should be generally 1-3 seconds at most per sound.
Your handin folder handin-10 should have the folders described above.
You will zip up the handin-10 folder and submit this to Autolab. Your overall folder organization should look something like this (indentation indicates subfolders):
handin-10 andrewID-10-assignment index.html sketch.js andrewID-10-concepts.pdf andrewID-10-project index.html sketch.js additional sound files here
Once you are ready to submit, zip (compress) the handin-10 folder (which will likely be named handin-10.zip) and hand in the ZIP FILE into the Deliverable 10 submission area on Autolab. Once you handin, check your handin history and click on the magnifying glass to look at what you submitted to make sure it looks right. IF YOU SUBMIT THE WRONG ZIP FILE, YOU RISK GETTTING A 0 ON THIS DELIVERABLE!
You may submit as many times as you’d like (in case you find inspiration and want to improve your work) up until the deadline. If you submit up to one day late, even if you submitted on time, you will be marked late. We only grade the final submission you upload to us via Autolab.
As discussed in class, you have the ability to load a sound file into your program just as you load an image. However, unlike images, there is no functional equivalent to imgur.com that allow you to reference your images online without your browser blocking their content in your sketches. So instead, you will need to run a local web server that doesn’t block audio content for your sketches.
The following webpage has information about a number of ways you can set up a local webserver:
https://github.com/processing/p5.js/wiki/Local-server