draw straight line when no analyser is defined yet

+ add todo for memory leak
This commit is contained in:
Felix Roos 2024-03-16 03:12:00 +01:00
parent dd78dc9606
commit cdc200e1da
2 changed files with 28 additions and 4 deletions

View File

@ -15,13 +15,23 @@ export function drawTimeScope(
id = 1, id = 1,
} = {}, } = {},
) { ) {
const dataArray = getAnalyzerData('time', id);
ctx.lineWidth = thickness; ctx.lineWidth = thickness;
ctx.strokeStyle = color; ctx.strokeStyle = color;
let canvas = ctx.canvas;
if (!analyser) {
// if analyser is undefined, draw straight line
// it may be undefined when no sound has been played yet
ctx.beginPath();
let y = pos * canvas.height;
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
return;
}
const dataArray = getAnalyzerData('time', id);
ctx.beginPath(); ctx.beginPath();
let canvas = ctx.canvas;
const bufferSize = analyser.frequencyBinCount; const bufferSize = analyser.frequencyBinCount;
let triggerIndex = align let triggerIndex = align
@ -49,6 +59,14 @@ export function drawFrequencyScope(
analyser, analyser,
{ color = 'white', scale = 0.25, pos = 0.75, lean = 0.5, min = -150, max = 0, ctx = getDrawContext(), id = 1 } = {}, { color = 'white', scale = 0.25, pos = 0.75, lean = 0.5, min = -150, max = 0, ctx = getDrawContext(), id = 1 } = {},
) { ) {
if (!analyser) {
ctx.beginPath();
let y = pos * canvas.height;
ctx.moveTo(0, y);
ctx.lineTo(canvas.width, y);
ctx.stroke();
return;
}
const dataArray = getAnalyzerData('frequency', id); const dataArray = getAnalyzerData('frequency', id);
const canvas = ctx.canvas; const canvas = ctx.canvas;
@ -119,7 +137,7 @@ Pattern.prototype.tscope = function (config = {}) {
return this.analyze(id).draw( return this.analyze(id).draw(
() => { () => {
clearScreen(config.smear, '0,0,0', config.ctx); clearScreen(config.smear, '0,0,0', config.ctx);
analysers[id] && drawTimeScope(analysers[id], config); drawTimeScope(analysers[id], config);
}, },
{ id }, { id },
); );

View File

@ -25,5 +25,11 @@ registerWidget('twist', (id, options = {}, pat) => {
registerWidget('osci', (id, options = {}, pat) => { registerWidget('osci', (id, options = {}, pat) => {
options = { width: 500, height: 60, pos: 0.5, scale: 1, ...options }; options = { width: 500, height: 60, pos: 0.5, scale: 1, ...options };
const ctx = getCanvasWidget(id, options).getContext('2d'); const ctx = getCanvasWidget(id, options).getContext('2d');
// TODO: find way to clear previous analysers to avoid memory leak
// .scope passes id to Pattern.analyze, which is picked up by superdough
// .. which calls getAnalyserById(analyze), creating a new analyzer (+buffer) for that key
// the id here is the col number where the osci function ends (as passed by the transpiler)
// effectively, this means for each evaluation of .osci on a unique col, a new analyser will be created
// the problem is that the old ones will never get deleted.. this might pile up some memory
return pat.scope({ ...options, ctx, id }); return pat.scope({ ...options, ctx, id });
}); });