Skip to content
Merged
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
25 changes: 20 additions & 5 deletions library/webaudioapi/modules/Instrument.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Instrument>} 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) {
Expand Down Expand Up @@ -176,16 +177,30 @@ 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] });
};
instrumentInstance.getNoteOffline = function (offlineContext, note) {
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}]`);
Expand Down