add nanFallback for better errors

This commit is contained in:
Felix Roos 2023-08-24 08:50:42 +02:00
parent eb0301d74e
commit 281ad5fac9
3 changed files with 12 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { noteToMidi, valueToMidi } from './util.mjs'; import { noteToMidi, valueToMidi, nanFallback } from './util.mjs';
import { getAudioContext, registerSound } from './index.mjs'; import { getAudioContext, registerSound } from './index.mjs';
import { getEnvelope } from './helpers.mjs'; import { getEnvelope } from './helpers.mjs';
import { logger } from './logger.mjs'; import { logger } from './logger.mjs';
@ -33,6 +33,7 @@ export const getSampleBufferSource = async (s, n, note, speed, freq, bank, resol
const ac = getAudioContext(); const ac = getAudioContext();
let sampleUrl; let sampleUrl;
if (Array.isArray(bank)) { if (Array.isArray(bank)) {
n = nanFallback(n, 0);
sampleUrl = bank[n % bank.length]; sampleUrl = bank[n % bank.length];
} else { } else {
const midiDiff = (noteA) => noteToMidi(noteA) - midi; const midiDiff = (noteA) => noteToMidi(noteA) - midi;

View File

@ -7,7 +7,7 @@ This program is free software: you can redistribute it and/or modify it under th
import './feedbackdelay.mjs'; import './feedbackdelay.mjs';
import './reverb.mjs'; import './reverb.mjs';
import './vowel.mjs'; import './vowel.mjs';
import { clamp } from './util.mjs'; import { clamp, nanFallback } from './util.mjs';
import workletsUrl from './worklets.mjs?url'; import workletsUrl from './worklets.mjs?url';
import { getFilter, gainNode } from './helpers.mjs'; import { getFilter, gainNode } from './helpers.mjs';
import { map } from 'nanostores'; import { map } from 'nanostores';
@ -168,6 +168,7 @@ export const superdough = async (value, deadline, hapDuration) => {
size = 2, size = 2,
velocity = 1, velocity = 1,
} = value; } = value;
gain = nanFallback(gain, 1);
gain *= velocity; // legacy fix for velocity gain *= velocity; // legacy fix for velocity
let toDisconnect = []; // audio nodes that will be disconnected when the source has ended let toDisconnect = []; // audio nodes that will be disconnected when the source has ended
const onended = () => { const onended = () => {

View File

@ -51,3 +51,11 @@ export const valueToMidi = (value, fallbackValue) => {
} }
return fallbackValue; return fallbackValue;
}; };
export function nanFallback(value, fallback) {
if (isNaN(Number(value))) {
logger(`"${value}" is not a number, falling back to ${fallback}`, 'warning');
return fallback;
}
return value;
}