Created
February 20, 2017 15:43
-
-
Save angelaperrone/1bcb772bd010df0d7aa4a6a3d9c1c22c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Music player | |
Plays a directory of music | |
Plays songs in a subdirectory of the sketch called music | |
Put any songs you want in the music subdirectory, then | |
copy the list of song names into the songs[] array. | |
created by Tom Igoe | |
5 Feb 2017 | |
http://www.asciitable.com/ | |
*/ | |
var song; // the sound file to be played | |
var serial; | |
var portName = '/dev/cu.usbmodem1421'; | |
var inData; | |
// the list of songs: | |
var songs = ['songA.mp3','songB.mp3', 'songC.mp3', 'songD.mp3', 'songE.mp3']; | |
var songCount = songs.length; // number of songs in the music dir | |
var currentSong = 0; // current song number | |
function preload() { // load the first song on preload | |
song = loadSound('music/' + songs[currentSong]); | |
} | |
function setup() { | |
createCanvas(1000, 800); | |
serial = new p5.SerialPort(); | |
//serial.on('list', printList); | |
serial.on('connected', serverConnected); | |
serial.on('open', portOpen); | |
serial.on('data', serialEvent); | |
serial.on('error', serialError); | |
serial.on('close', portClose); | |
serial.list(); | |
serial.open(portName); | |
} | |
function serverConnected() { | |
println('connected to server'); | |
} | |
function portOpen() { | |
println('the serial port opened'); | |
} | |
function serialEvent() { | |
inData = Number(serial.read()); //converts to regular number not ASCIII??? | |
console.log(inData); | |
controlSound(inData); | |
} | |
function serialError(err) { | |
println('something went wrong with the serial port. ' + err); | |
} | |
function portClose(){ | |
println('the serial port closed') | |
} | |
function draw() { | |
background(255, 10, 80); | |
fill(255, 255, 20); | |
// draw the song's name and current time in seconds: | |
textSize(60); | |
text(songs[currentSong], 20, 100); | |
text(song.currentTime().toFixed(3), 20, 200); | |
textSize(20); | |
text('sensor value: ' + inData, 750, 750) | |
//fill (inData+100, inData-40, inData*2); | |
noStroke(); | |
fill(0); | |
ellipse(10 +(2*inData), 350, 20, 20); | |
rect(14+(2*inData), 315, 5, 40 ); | |
rect(14+(2*inData), 315, 20, 5); | |
rect(14+(2*inData), 325, 20, 5); | |
} | |
function controlSound(input) { | |
switch(input) { | |
case 49: // start/stop, press 1 | |
song.rate(1.0); | |
if (song.isPlaying()){ | |
song.stop(); | |
} else { | |
song.play(); | |
} | |
break; | |
case 50: // play/pause, press 2 | |
song.rate(1.0); | |
if (song.isPlaying()){ | |
song.pause(); | |
} else { | |
song.play(); | |
} | |
break; | |
case 51: // skip ahead, press 3 | |
// make sure the song number is valid, and increment: | |
if (currentSong < songs.length-1) { | |
currentSong++; | |
} else { | |
currentSong = 0; | |
} | |
// get new song: | |
getSong(currentSong); | |
break; | |
case 52: // skip back, press 4 | |
// in the first second, just rewind the current track: | |
if (song.currentTime() > 1.0) { | |
song.jump(0); | |
// if more than a second has elapsed, then | |
// make sure the song number is valid, and decrement: | |
} else { | |
if (currentSong > 0) { | |
currentSong--; | |
} else { | |
currentSong = songs.length-1; | |
} | |
// get new song: | |
getSong(currentSong); | |
} | |
break; | |
case 53: // fast forward, press 5 | |
song.rate(2.0); // double the play speed | |
if (!song.isPlaying()){ | |
song.play(); | |
} | |
break; | |
case 54: // random song, press 6 | |
currentSong = Math.round(random(songCount)); // get a new song number | |
getSong(currentSong); // play it | |
break; | |
} | |
} | |
function getSong(songNumber) { | |
if (songNumber < songs.length) { // if the song number is in range | |
if (song.isPlaying()) { | |
song.stop(); | |
} | |
// load a new song: | |
song = loadSound('music/'+ songs[currentSong], resumePlay); | |
return true; | |
} else { // if the song number was out of range, return false | |
return false; | |
} | |
} | |
function resumePlay() { | |
// if the song isn't playing, play it | |
if (song.isPlaying()){ | |
song.stop(); | |
} else { | |
song.play(); | |
} | |
} | |
// function keyReleased() { | |
// controlSound(keyCode); // send the ASCII number of the key | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment