-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
Ok here's what I'm thinking
We need some sort of mapping from keyboard keys to notes. This will look something like:
var noteMap = {
65: 'C5', // a
83: 'D5', // s
68: 'E5', // d
70: 'F5' // f
}
We can add more later but this should be good for now.
We also need some sort of data structure for storing the current notes played by users:
var userNotes = {
'0hFf738zgw5yzQsEAAAD': {
65: someAudiobuffer // this means "a" is currently pressed for this user, and contains the note being played
}
}
Now comes the more complicated task of
- Receiving the updated keys from the server
- Checking to see if any new keys are pressed, and starting the corresponding notes
- Checking to see if any keys are no longer pressed, and stopping the corresponding notes
We can get a note right now by typing var note = getAudioBuffer(someNote, someInstrument);
To start playing that note, we call note.start();
and to stop playing we call note.stop();
.
In our keysUpdated
listener we might have something like this:
socket.on('keysUpdated', function(keyData) {
var id = keyData.id;
if (!userNotes[id]) {
userNotes[id] = {};
}
var currentKeysPressed = userNotes[id];
var currentKeysPressedArr = Object.keys(currentKeysPressed);
var newKeysPressed = keyData.keysPressed;
var newKeysPressedArr = Object.keys(newKeysPressed);
for (var key in currentKeysPressedArr) {
if (!newKeysPressed[key]) {
currentKeysPressed[key].stop(); // stop note, key was released
delete currentKeysPressed[key];
}
}
for (var key in newKeysPressedArr) {
if (!currentKeysPressed[key] && noteMap[key]) {
var newNote = getAudioBuffer(noteMap[key], grandPianoBuffers);
currentKeysPressed[key] = newNote;
newNote.start(); // start note, key was pressed
}
}
});
Metadata
Metadata
Assignees
Labels
No labels