Skip to content

Commit 968e3e7

Browse files
authored
Merge pull request #3 from hedgecrw/gabriel/instrument-gen-update
loadInstument can now take in raw instrument data
2 parents bbad303 + 168ffee commit 968e3e7

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

library/webaudioapi/modules/Instrument.mjs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import * as WebAudioApiErrors from './Errors.mjs';
1515
/**
1616
* Loads an existing {@link Instrument} object capable of mapping audio data to musical output.
1717
*
18-
* If the `url` parameter is set to `null`, a sine-wave oscillator will be used to generate
18+
* If the `url_or_data` parameter is set to `null`, a sine-wave oscillator will be used to generate
1919
* all audio output.
2020
*
2121
* @param {AudioContext} audioContext - Reference to the global browser {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext}
2222
* @param {string} name - Name of the instrument to load
23-
* @param {string|null} url - URL pointing to the instrument data to load or `null`
23+
* @param {string|[Uint8Array]|null} url_or_data - URL pointing to the instrument data to load,
24+
* the instrument data itself, or `null`
2425
* @returns {Promise<Instrument>} Newly loaded {@link Instrument}
2526
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext}
2627
* @see {@link Instrument}
2728
* @async
2829
*/
29-
export async function loadInstrument(audioContext, name, url) {
30+
export async function loadInstrument(audioContext, name, url_or_data) {
3031

3132
// Private internal Instrument functions
3233
function loadNumberFromArray(array, numBytes, offset) {
@@ -176,16 +177,30 @@ export async function loadInstrument(audioContext, name, url) {
176177

177178
// Actually load and return the instrument
178179
console.log('Loading instrument:', name + '...');
179-
if (url == null) {
180+
console.log('using new version!!!');
181+
if (url_or_data == null) {
180182
instrumentInstance.getNote = function (note) {
181183
return new OscillatorNode(audioContext, { frequency: Frequency[note] });
182184
};
183185
instrumentInstance.getNoteOffline = function (offlineContext, note) {
184186
return new OscillatorNode(offlineContext, { frequency: Frequency[note] });
185187
};
186188
}
189+
else if (url_or_data instanceof Array) {
190+
instrumentInstance.getNote = function (note) {
191+
if (note < 0 || note > url_or_data.length)
192+
throw new WebAudioApiErrors.WebAudioInstrumentError(`The specified note (${note}) is not defined`);
193+
return new AudioBufferSourceNode(audioContext, { buffer: url_or_data[note] });
194+
}
195+
instrumentInstance.getNoteOffline = function (note) {
196+
if (note < 0 || note > url_or_data.length)
197+
throw new WebAudioApiErrors.WebAudioInstrumentError(`The specified note (${note}) is not defined`);
198+
return new AudioBufferSourceNode(offlineContext, { buffer: url_or_data[note] });
199+
}
200+
201+
}
187202
else {
188-
const [noteData, metadata] = await loadInstrument(url);
203+
const [noteData, metadata] = await loadInstrument(url_or_data);
189204
instrumentInstance.getNote = function (note) {
190205
if (note && (note < metadata.minValidNote) || (note > metadata.maxValidNote))
191206
throw new WebAudioApiErrors.WebAudioInstrumentError(`The specified note (${note}) is unplayable on this instrument. Valid notes are [${metadata.minValidNote}, ${metadata.maxValidNote}]`);

0 commit comments

Comments
 (0)