I currently have a little JavaScript (paperscript) that creates a variable with keys (sorry, I don't know the technical term) that also uses howlerjs to load sounds and colors to various keys.
<script type = "text/paperscript" canvas="myCanvas">
var sound_dir = "lib/sounds/";
var circles = [];
var keyData = {
q: {
sound: new Howl({
src: [sound_dir+'drum machine/'+'808 kick.wav']
}),
color: '#1abc9c'
},
w: {
sound: new Howl({
src: [sound_dir+'clay.mp3']
}),
color: '#2ecc71'
},
e: {
sound: new Howl({
src: [sound_dir+'confetti.mp3']
}),
color: '#3498db'
},
r: {
sound: new Howl({
src: [sound_dir+'corona.mp3']
}),
color: '#9b59b6'
},
t: {
sound: new Howl({
src: [sound_dir+'dotted-spiral.mp3']
}),
color: '#34495e'
},
y: {
sound: new Howl({
src: [sound_dir+'flash-1.mp3']
}),
color: '#16a085'
},
u: {
sound: new Howl({
src: [sound_dir+'flash-2.mp3']
}),
color: '#27ae60'
},
i: {
sound: new Howl({
src: [sound_dir+'flash-3.mp3']
}),
color: '#2980b9'
},
o: {
sound: new Howl({
src: [sound_dir+'glimmer.mp3']
}),
color: '#8e44ad'
},
p: {
sound: new Howl({
src: [sound_dir+'moon.mp3']
}),
color: '#2c3e50'
},
a: {
sound: new Howl({
src: [sound_dir+'pinwheel.mp3']
}),
color: '#f1c40f'
},
s: {
sound: new Howl({
src: [sound_dir+'piston-1.mp3']
}),
color: '#e67e22'
},
d: {
sound: new Howl({
src: [sound_dir+'piston-2.mp3']
}),
color: '#e74c3c'
},
f: {
sound: new Howl({
src: [sound_dir+'prism-1.mp3']
}),
color: '#95a5a6'
},
g: {
sound: new Howl({
src: [sound_dir+'prism-2.mp3']
}),
color: '#f39c12'
},
h: {
sound: new Howl({
src: [sound_dir+'prism-3.mp3']
}),
color: '#d35400'
},
j: {
sound: new Howl({
src: [sound_dir+'splits.mp3']
}),
color: '#1abc9c'
},
k: {
sound: new Howl({
src: [sound_dir+'squiggle.mp3']
}),
color: '#2ecc71'
},
l: {
sound: new Howl({
src: [sound_dir+'strike.mp3']
}),
color: '#3498db'
},
z: {
sound: new Howl({
src: [sound_dir+'suspension.mp3']
}),
color: '#9b59b6'
},
x: {
sound: new Howl({
src: [sound_dir+'timer.mp3']
}),
color: '#34495e'
},
c: {
sound: new Howl({
src: [sound_dir+'ufo.mp3']
}),
color: '#16a085'
},
v: {
sound: new Howl({
src: [sound_dir+'veil.mp3']
}),
color: '#27ae60'
},
b: {
sound: new Howl({
src: [sound_dir+'wipe.mp3']
}),
color: '#2980b9'
},
n: {
sound: new Howl({
src: [sound_dir+'zig-zag.mp3']
}),
color: '#8e44ad'
},
m: {
sound: new Howl({
src: [sound_dir+'moon.mp3']
}),
color: '#2c3e50'
}
}
function onKeyDown(event) {
console.log(event.key, Key.isDown('a'), event.key === "a");
if(keyData[event.key]){
console.log(event.key, Key.isDown('a'), event.key === "a");
var maxPoint = new Point(view.size.width, view.size.height);
var randomPoint = Point.random(); // gives non inclusive obect b/w 0 and 1
var point = maxPoint * randomPoint;
// var newCir = new Path.Circle(new Point(point), 500);
var newCir = shape(point);
newCir.fillColor = keyData[event.key].color;
keyData[event.key].sound.play();
circles.push(newCir);
}
}
So, when I press a key, a color is set and keyData[event.key].sound
is played.
This works great. However, I'm thinking there's got to be a better way to set up keyData
. The folder currently has 26 files...but say I had a folder with X many sounds, and I want to set each keyboard key, say a-z, 0-9, and "F" keys, to one of those sounds. How would I create that function?
There's got to be a better way than manually adding the color and sound file each time, no?