Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional drum kits #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions classes/Drums.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,49 @@ import { parseArgument } from '../helpers/parseArguments';
import BufferLoader from '../helpers/BufferLoader';
import drumMap from '../helpers/drumMap';

const typeMap = {
0: 'acoustic',
1: '808'
}

// Taking the easy route here: let's store the drums in
// global variables here so each instance of the `Drums`
// class has access to them.
let loadingDrums = false;
let drumBuffers = [];
let loadingDrums = {
acousting: false,
808: false,
}

let drumBuffers = {
acoustic: [],
808: [],
}

class Drums extends Block {
constructor(...args) {
super(...args);

// In the future we can support different drum sets
// using a simple integer argument.
// this.arguments will be used to set the type
this.type = this.arguments[0] || parseArgument(0);
this.drumType = typeMap[this.type.next()];

// Super basic lazy loading of drum sounds.
// If the buffers don't already exist and we're
// not trying to load them... do that.
if (!drumBuffers.length && !loadingDrums) {
// if we don't have buffers for this specific type and we're
// not already loading them, go ahead and load them.
if (!drumBuffers[this.drumType].length || !loadingDrums[this.drumType]) {
this.loadDrumSounds();
}
}

schedule(start, stop, note, envelopeMode) {
if (!drumBuffers.length || loadingDrums) return;
if (!drumBuffers[this.drumType].length || loadingDrums[this.drumType]) return;
// we only have 12 samples available but we shouldn't
// burden the user with that knowledge so let's use
// mod 12, which allows them to use chords, scales,
// etc. without having to think about it.
const drumSound = note % 12;

const sample = context.createBufferSource();
sample.buffer = drumBuffers[drumSound];
sample.buffer = drumBuffers[this.drumType][drumSound];

sample.start(start);
sample.stop(stop);
Expand All @@ -57,14 +68,15 @@ class Drums extends Block {
sample.connect(this.getOutput());
}
loadDrumSounds() {
loadingDrums = true;
loadingDrums[this.drumType] = true;
// Decide which map to use based on type
// Get a list of files
const files = Object.keys(drumMap).map(key => drumMap[key].file);
const files = Object.keys(drumMap[this.drumType]).map(key => drumMap[this.drumType][key].file);
// Load the files!
const loader = new BufferLoader(context, files, list => {
// set our global variable to the list of buffers. Done.
drumBuffers = list;
loadingDrums = false;
drumBuffers[this.drumType] = list;
loadingDrums[this.drumType] = false;
});
loader.load();
}
Expand Down
145 changes: 99 additions & 46 deletions helpers/drumMap.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,105 @@
const drumMap = {
0: {
file: '/audio/acoustic/kick1.mp3',
label: 'acoustic kick 1'
acoustic: {
0: {
file: '/audio/acoustic/kick1.mp3',
label: 'acoustic kick 1'
},
1: {
file: '/audio/acoustic/kick2.mp3',
label: 'acoustic kick 2'
},
2: {
file: '/audio/acoustic/kick3.mp3',
label: 'acoustic kick 3'
},
3: {
file: '/audio/acoustic/snare1.mp3',
label: 'acoustic snare 1'
},
4: {
file: '/audio/acoustic/snare2.mp3',
label: 'acoustic snare 2'
},
5: {
file: '/audio/acoustic/snare3.mp3',
label: 'acoustic snare 3'
},
6: {
file: '/audio/acoustic/hihat1.mp3',
label: 'acoustic hat 1'
},
7: {
file: '/audio/acoustic/hihat2.mp3',
label: 'acoustic hat 2'
},
8: {
file: '/audio/acoustic/hihat_open1.mp3',
label: 'acoustic hat (open) 1'
},
9: {
file: '/audio/acoustic/hihat_open2.mp3',
label: 'acoustic hat (open) 2'
},
10: {
file: '/audio/acoustic/hihat_open3.mp3',
label: 'acoustic hat (open) 3'
},
11: {
file: '/audio/acoustic/rim1.mp3',
label: 'acoustic rim'
},
},
1: {
file: '/audio/acoustic/kick2.mp3',
label: 'acoustic kick 2'
},
2: {
file: '/audio/acoustic/kick3.mp3',
label: 'acoustic kick 3'
},
3: {
file: '/audio/acoustic/snare1.mp3',
label: 'acoustic snare 1'
},
4: {
file: '/audio/acoustic/snare2.mp3',
label: 'acoustic snare 2'
},
5: {
file: '/audio/acoustic/snare3.mp3',
label: 'acoustic snare 3'
},
6: {
file: '/audio/acoustic/hihat1.mp3',
label: 'acoustic hat 1'
},
7: {
file: '/audio/acoustic/hihat2.mp3',
label: 'acoustic hat 2'
},
8: {
file: '/audio/acoustic/hihat_open1.mp3',
label: 'acoustic hat (open) 1'
},
9: {
file: '/audio/acoustic/hihat_open2.mp3',
label: 'acoustic hat (open) 2'
},
10: {
file: '/audio/acoustic/hihat_open3.mp3',
label: 'acoustic hat (open) 3'
},
11: {
file: '/audio/acoustic/rim1.mp3',
label: 'acoustic rim'
808: {
0: {
file: '/audio/808/kick1.mp3',
label: '808 kick 1'
},
1: {
file: '/audio/808/kick2.mp3',
label: '808 kick 2'
},
2: {
file: '/audio/808/kick3.mp3',
label: '808 kick 3'
},
3: {
file: '/audio/808/snare1.mp3',
label: '808 snare 1'
},
4: {
file: '/audio/808/snare2.mp3',
label: '808 snare 2'
},
5: {
file: '/audio/808/snare3.mp3',
label: '808 snare 3'
},
6: {
file: '/audio/808/hihat1.mp3',
label: '808 hat 1'
},
7: {
file: '/audio/808/hihat2.mp3',
label: '808 hat 2'
},
8: {
file: '/audio/808/hihat_open1.mp3',
label: '808 hat (open) 1'
},
9: {
file: '/audio/808/hihat_open2.mp3',
label: '808 hat (open) 2'
},
10: {
file: '/audio/808/hihat_open3.mp3',
label: '808 hat (open) 3'
},
11: {
file: '/audio/808/rim1.mp3',
label: '808 rim'
},
},
};


export default drumMap;
Binary file added public/audio/808/hihat1.mp3
Binary file not shown.
Binary file added public/audio/808/hihat2.mp3
Binary file not shown.
Binary file added public/audio/808/hihat_open1.mp3
Binary file not shown.
Binary file added public/audio/808/hihat_open2.mp3
Binary file not shown.
Binary file added public/audio/808/hihat_open3.mp3
Binary file not shown.
Binary file added public/audio/808/kick1.mp3
Binary file not shown.
Binary file added public/audio/808/kick2.mp3
Binary file not shown.
Binary file added public/audio/808/kick3.mp3
Binary file not shown.
Binary file added public/audio/808/rim1.mp3
Binary file not shown.
Binary file added public/audio/808/snare1.mp3
Binary file not shown.
Binary file added public/audio/808/snare2.mp3
Binary file not shown.
Binary file added public/audio/808/snare3.mp3
Binary file not shown.