better type checking for sampler url parameters

This commit is contained in:
Yotam Mann 2019-07-30 15:53:10 -04:00
parent 6e5e8b3399
commit e97ea7d5df

View file

@ -5,7 +5,7 @@ import { FrequencyClass } from "../core/type/Frequency";
import { Frequency, Interval, MidiNote, NormalRange, Note, Time } from "../core/type/Units";
import { optionsFromArguments } from "../core/util/Defaults";
import { noOp } from "../core/util/Interface";
import { isArray, isNote, isNumber } from "../core/util/TypeCheck";
import { isArray, isNote, isNumber, isString } from "../core/util/TypeCheck";
import { Instrument, InstrumentOptions } from "../instrument/Instrument";
import { ToneBufferSource, ToneBufferSourceCurve } from "../source/buffer/BufferSource";
@ -91,14 +91,16 @@ export class Sampler extends Instrument<SamplerOptions> {
const urlMap = {};
Object.keys(options.urls).forEach((note) => {
this.assert(isNote(note) || (isNumber(note) && isFinite(note)), `url key is neither a note or midi pitch: ${note}`);
const noteNumber = parseInt(note, 10);
this.assert(isNote(note)
|| (isNumber(noteNumber) && isFinite(noteNumber)), `url key is neither a note or midi pitch: ${note}`);
if (isNote(note)) {
// convert the note name to MIDI
const mid = new FrequencyClass(this.context, note).toMidi();
urlMap[mid] = options.urls[note];
} else if (isNumber(note) && isFinite(note)) {
} else if (isNumber(noteNumber) && isFinite(noteNumber)) {
// otherwise if it's numbers assume it's midi
urlMap[parseInt(note, 10)] = options.urls[note];
urlMap[noteNumber] = options.urls[noteNumber];
}
});