diff --git a/library/webaudioapi/modules/Instrument.mjs b/library/webaudioapi/modules/Instrument.mjs index e8b3851..c09a429 100644 --- a/library/webaudioapi/modules/Instrument.mjs +++ b/library/webaudioapi/modules/Instrument.mjs @@ -15,18 +15,19 @@ import * as WebAudioApiErrors from './Errors.mjs'; /** * Loads an existing {@link Instrument} object capable of mapping audio data to musical output. * - * If the `url` parameter is set to `null`, a sine-wave oscillator will be used to generate + * If the `url_or_data` parameter is set to `null`, a sine-wave oscillator will be used to generate * all audio output. * * @param {AudioContext} audioContext - Reference to the global browser {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext} * @param {string} name - Name of the instrument to load - * @param {string|null} url - URL pointing to the instrument data to load or `null` + * @param {string|[Uint8Array]|null} url_or_data - URL pointing to the instrument data to load, + * the instrument data itself, or `null` * @returns {Promise} Newly loaded {@link Instrument} * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext} * @see {@link Instrument} * @async */ -export async function loadInstrument(audioContext, name, url) { +export async function loadInstrument(audioContext, name, url_or_data) { // Private internal Instrument functions function loadNumberFromArray(array, numBytes, offset) { @@ -176,7 +177,8 @@ export async function loadInstrument(audioContext, name, url) { // Actually load and return the instrument console.log('Loading instrument:', name + '...'); - if (url == null) { + console.log('using new version!!!'); + if (url_or_data == null) { instrumentInstance.getNote = function (note) { return new OscillatorNode(audioContext, { frequency: Frequency[note] }); }; @@ -184,8 +186,21 @@ export async function loadInstrument(audioContext, name, url) { return new OscillatorNode(offlineContext, { frequency: Frequency[note] }); }; } + else if (url_or_data instanceof Array) { + instrumentInstance.getNote = function (note) { + if (note < 0 || note > url_or_data.length) + throw new WebAudioApiErrors.WebAudioInstrumentError(`The specified note (${note}) is not defined`); + return new AudioBufferSourceNode(audioContext, { buffer: url_or_data[note] }); + } + instrumentInstance.getNoteOffline = function (note) { + if (note < 0 || note > url_or_data.length) + throw new WebAudioApiErrors.WebAudioInstrumentError(`The specified note (${note}) is not defined`); + return new AudioBufferSourceNode(offlineContext, { buffer: url_or_data[note] }); + } + + } else { - const [noteData, metadata] = await loadInstrument(url); + const [noteData, metadata] = await loadInstrument(url_or_data); instrumentInstance.getNote = function (note) { if (note && (note < metadata.minValidNote) || (note > metadata.maxValidNote)) throw new WebAudioApiErrors.WebAudioInstrumentError(`The specified note (${note}) is unplayable on this instrument. Valid notes are [${metadata.minValidNote}, ${metadata.maxValidNote}]`);