superdough: add logger

This commit is contained in:
Felix Roos 2023-08-17 10:08:55 +02:00
parent 0c6a0720af
commit 8bdbc9ec4c
5 changed files with 19 additions and 7 deletions

View File

@ -8,3 +8,4 @@ export * from './superdough.mjs';
export * from './sampler.mjs'; export * from './sampler.mjs';
export * from './helpers.mjs'; export * from './helpers.mjs';
export * from './synth.mjs'; export * from './synth.mjs';
export * from './logger.mjs';

View File

@ -0,0 +1,7 @@
let log = (msg) => console.log(msg);
export const logger = (...args) => log(...args);
export const setLogger = (fn) => {
log = fn;
};

View File

@ -1,6 +1,7 @@
import { noteToMidi, valueToMidi } from './util.mjs'; import { noteToMidi, valueToMidi } 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';
const bufferCache = {}; // string: Promise<ArrayBuffer> const bufferCache = {}; // string: Promise<ArrayBuffer>
const loadCache = {}; // string: Promise<ArrayBuffer> const loadCache = {}; // string: Promise<ArrayBuffer>
@ -24,7 +25,7 @@ function humanFileSize(bytes, si) {
export const getSampleBufferSource = async (s, n, note, speed, freq, bank, resolveUrl) => { export const getSampleBufferSource = async (s, n, note, speed, freq, bank, resolveUrl) => {
let transpose = 0; let transpose = 0;
if (freq !== undefined && note !== undefined) { if (freq !== undefined && note !== undefined) {
// logger('[sampler] hap has note and freq. ignoring note', 'warning'); logger('[sampler] hap has note and freq. ignoring note', 'warning');
} }
let midi = valueToMidi({ freq, note }, 36); let midi = valueToMidi({ freq, note }, 36);
transpose = midi - 36; // C3 is middle C transpose = midi - 36; // C3 is middle C
@ -64,7 +65,7 @@ export const getSampleBufferSource = async (s, n, note, speed, freq, bank, resol
export const loadBuffer = (url, ac, s, n = 0) => { export const loadBuffer = (url, ac, s, n = 0) => {
const label = s ? `sound "${s}:${n}"` : 'sample'; const label = s ? `sound "${s}:${n}"` : 'sample';
if (!loadCache[url]) { if (!loadCache[url]) {
//logger(`[sampler] load ${label}..`, 'load-sample', { url }); logger(`[sampler] load ${label}..`, 'load-sample', { url });
const timestamp = Date.now(); const timestamp = Date.now();
loadCache[url] = fetch(url) loadCache[url] = fetch(url)
.then((res) => res.arrayBuffer()) .then((res) => res.arrayBuffer())
@ -72,7 +73,7 @@ export const loadBuffer = (url, ac, s, n = 0) => {
const took = Date.now() - timestamp; const took = Date.now() - timestamp;
const size = humanFileSize(res.byteLength); const size = humanFileSize(res.byteLength);
// const downSpeed = humanFileSize(res.byteLength / took); // const downSpeed = humanFileSize(res.byteLength / took);
//logger(`[sampler] load ${label}... done! loaded ${size} in ${took}ms`, 'loaded-sample', { url }); logger(`[sampler] load ${label}... done! loaded ${size} in ${took}ms`, 'loaded-sample', { url });
const decoded = await ac.decodeAudioData(res); const decoded = await ac.decodeAudioData(res);
bufferCache[url] = decoded; bufferCache[url] = decoded;
return decoded; return decoded;
@ -224,12 +225,12 @@ export async function onTriggerSample(t, value, onended, bank, resolveUrl) {
// asny stuff above took too long? // asny stuff above took too long?
if (ac.currentTime > t) { if (ac.currentTime > t) {
//logger(`[sampler] still loading sound "${s}:${n}"`, 'highlight'); logger(`[sampler] still loading sound "${s}:${n}"`, 'highlight');
// console.warn('sample still loading:', s, n); // console.warn('sample still loading:', s, n);
return; return;
} }
if (!bufferSource) { if (!bufferSource) {
//logger(`[sampler] could not load "${s}:${n}"`, 'error'); logger(`[sampler] could not load "${s}:${n}"`, 'error');
return; return;
} }
bufferSource.playbackRate.value = Math.abs(speed) * bufferSource.playbackRate.value; bufferSource.playbackRate.value = Math.abs(speed) * bufferSource.playbackRate.value;

View File

@ -11,6 +11,7 @@ import { clamp } 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';
import { logger } from './logger.mjs';
export const soundMap = map(); export const soundMap = map();
export function registerSound(key, onTrigger, data = {}) { export function registerSound(key, onTrigger, data = {}) {
@ -195,7 +196,7 @@ export const superdough = async (value, deadline, hapDuration) => {
return; return;
} }
if (ac.currentTime > t) { if (ac.currentTime > t) {
// logger('[webaudio] skip hap: still loading', ac.currentTime - t); logger('[webaudio] skip hap: still loading', ac.currentTime - t);
return; return;
} }
const chain = []; // audio nodes that will be connected to each other sequentially const chain = []; // audio nodes that will be connected to each other sequentially

View File

@ -5,9 +5,11 @@ This program is free software: you can redistribute it and/or modify it under th
*/ */
import * as strudel from '@strudel.cycles/core'; import * as strudel from '@strudel.cycles/core';
import { superdough, getAudioContext } from 'superdough'; import { superdough, getAudioContext, setLogger } from 'superdough';
const { Pattern, logger } = strudel; const { Pattern, logger } = strudel;
setLogger(logger);
const hap2value = (hap) => { const hap2value = (hap) => {
hap.ensureObjectValue(); hap.ensureObjectValue();
return { ...hap.value, velocity: hap.context.velocity }; return { ...hap.value, velocity: hap.context.velocity };