mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-24 12:08:28 +00:00
Revert "refactor: remove old draw logic"
This reverts commit 95719654f3bfc5d3cc98c33816b402e2c0e38bd7.
This commit is contained in:
parent
12228c56d9
commit
6e02bf59e9
@ -4,7 +4,7 @@ Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/st
|
|||||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Pattern } from './index.mjs';
|
import { Pattern, getTime, State, TimeSpan } from './index.mjs';
|
||||||
|
|
||||||
export const getDrawContext = (id = 'test-canvas') => {
|
export const getDrawContext = (id = 'test-canvas') => {
|
||||||
let canvas = document.querySelector('#' + id);
|
let canvas = document.querySelector('#' + id);
|
||||||
@ -19,9 +19,45 @@ export const getDrawContext = (id = 'test-canvas') => {
|
|||||||
return canvas.getContext('2d');
|
return canvas.getContext('2d');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Pattern.prototype.draw = function (callback, { from, to, onQuery }) {
|
||||||
|
if (window.strudelAnimation) {
|
||||||
|
cancelAnimationFrame(window.strudelAnimation);
|
||||||
|
}
|
||||||
|
const ctx = getDrawContext();
|
||||||
|
let cycle,
|
||||||
|
events = [];
|
||||||
|
const animate = (time) => {
|
||||||
|
const t = getTime();
|
||||||
|
if (from !== undefined && to !== undefined) {
|
||||||
|
const currentCycle = Math.floor(t);
|
||||||
|
if (cycle !== currentCycle) {
|
||||||
|
cycle = currentCycle;
|
||||||
|
const begin = currentCycle + from;
|
||||||
|
const end = currentCycle + to;
|
||||||
|
setTimeout(() => {
|
||||||
|
events = this.query(new State(new TimeSpan(begin, end)))
|
||||||
|
.filter(Boolean)
|
||||||
|
.filter((event) => event.part.begin.equals(event.whole.begin));
|
||||||
|
onQuery?.(events);
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
callback(ctx, events, t, time);
|
||||||
|
window.strudelAnimation = requestAnimationFrame(animate);
|
||||||
|
};
|
||||||
|
requestAnimationFrame(animate);
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
export const cleanupDraw = (clearScreen = true) => {
|
export const cleanupDraw = (clearScreen = true) => {
|
||||||
const ctx = getDrawContext();
|
const ctx = getDrawContext();
|
||||||
clearScreen && ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
|
clearScreen && ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
|
||||||
|
if (window.strudelAnimation) {
|
||||||
|
cancelAnimationFrame(window.strudelAnimation);
|
||||||
|
}
|
||||||
|
if (window.strudelScheduler) {
|
||||||
|
clearInterval(window.strudelScheduler);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Pattern.prototype.onPaint = function (onPaint) {
|
Pattern.prototype.onPaint = function (onPaint) {
|
||||||
|
|||||||
@ -20,6 +20,7 @@ export * from './evaluate.mjs';
|
|||||||
export * from './repl.mjs';
|
export * from './repl.mjs';
|
||||||
export * from './cyclist.mjs';
|
export * from './cyclist.mjs';
|
||||||
export * from './logger.mjs';
|
export * from './logger.mjs';
|
||||||
|
export * from './time.mjs';
|
||||||
export * from './draw.mjs';
|
export * from './draw.mjs';
|
||||||
export * from './animate.mjs';
|
export * from './animate.mjs';
|
||||||
export * from './pianoroll.mjs';
|
export * from './pianoroll.mjs';
|
||||||
|
|||||||
@ -4,7 +4,7 @@ Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/st
|
|||||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Pattern, noteToMidi, freqToMidi } from './index.mjs';
|
import { Pattern, noteToMidi, getDrawContext, freqToMidi, isNote } from './index.mjs';
|
||||||
|
|
||||||
const scale = (normalized, min, max) => normalized * (max - min) + min;
|
const scale = (normalized, min, max) => normalized * (max - min) + min;
|
||||||
const getValue = (e) => {
|
const getValue = (e) => {
|
||||||
@ -29,6 +29,134 @@ const getValue = (e) => {
|
|||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Pattern.prototype.pianoroll = function ({
|
||||||
|
cycles = 4,
|
||||||
|
playhead = 0.5,
|
||||||
|
overscan = 1,
|
||||||
|
flipTime = 0,
|
||||||
|
flipValues = 0,
|
||||||
|
hideNegative = false,
|
||||||
|
// inactive = '#C9E597',
|
||||||
|
// inactive = '#FFCA28',
|
||||||
|
inactive = '#7491D2',
|
||||||
|
active = '#FFCA28',
|
||||||
|
// background = '#2A3236',
|
||||||
|
background = 'transparent',
|
||||||
|
smear = 0,
|
||||||
|
playheadColor = 'white',
|
||||||
|
minMidi = 10,
|
||||||
|
maxMidi = 90,
|
||||||
|
autorange = 0,
|
||||||
|
timeframe: timeframeProp,
|
||||||
|
fold = 0,
|
||||||
|
vertical = 0,
|
||||||
|
} = {}) {
|
||||||
|
const ctx = getDrawContext();
|
||||||
|
const w = ctx.canvas.width;
|
||||||
|
const h = ctx.canvas.height;
|
||||||
|
let from = -cycles * playhead;
|
||||||
|
let to = cycles * (1 - playhead);
|
||||||
|
|
||||||
|
if (timeframeProp) {
|
||||||
|
console.warn('timeframe is deprecated! use from/to instead');
|
||||||
|
from = 0;
|
||||||
|
to = timeframeProp;
|
||||||
|
}
|
||||||
|
const timeAxis = vertical ? h : w;
|
||||||
|
const valueAxis = vertical ? w : h;
|
||||||
|
let timeRange = vertical ? [timeAxis, 0] : [0, timeAxis]; // pixel range for time
|
||||||
|
const timeExtent = to - from; // number of seconds that fit inside the canvas frame
|
||||||
|
const valueRange = vertical ? [0, valueAxis] : [valueAxis, 0]; // pixel range for values
|
||||||
|
let valueExtent = maxMidi - minMidi + 1; // number of "slots" for values, overwritten if autorange true
|
||||||
|
let barThickness = valueAxis / valueExtent; // pixels per value, overwritten if autorange true
|
||||||
|
let foldValues = [];
|
||||||
|
flipTime && timeRange.reverse();
|
||||||
|
flipValues && valueRange.reverse();
|
||||||
|
|
||||||
|
this.draw(
|
||||||
|
(ctx, events, t) => {
|
||||||
|
ctx.fillStyle = background;
|
||||||
|
ctx.globalAlpha = 1; // reset!
|
||||||
|
if (!smear) {
|
||||||
|
ctx.clearRect(0, 0, w, h);
|
||||||
|
ctx.fillRect(0, 0, w, h);
|
||||||
|
}
|
||||||
|
const inFrame = (event) =>
|
||||||
|
(!hideNegative || event.whole.begin >= 0) && event.whole.begin <= t + to && event.whole.end >= t + from;
|
||||||
|
events.filter(inFrame).forEach((event) => {
|
||||||
|
const isActive = event.whole.begin <= t && event.whole.end > t;
|
||||||
|
ctx.fillStyle = event.context?.color || inactive;
|
||||||
|
ctx.strokeStyle = event.context?.color || active;
|
||||||
|
ctx.globalAlpha = event.context.velocity ?? 1;
|
||||||
|
const timePx = scale((event.whole.begin - (flipTime ? to : from)) / timeExtent, ...timeRange);
|
||||||
|
let durationPx = scale(event.duration / timeExtent, 0, timeAxis);
|
||||||
|
const value = getValue(event);
|
||||||
|
const valuePx = scale(
|
||||||
|
fold ? foldValues.indexOf(value) / foldValues.length : (Number(value) - minMidi) / valueExtent,
|
||||||
|
...valueRange,
|
||||||
|
);
|
||||||
|
let margin = 0;
|
||||||
|
const offset = scale(t / timeExtent, ...timeRange);
|
||||||
|
let coords;
|
||||||
|
if (vertical) {
|
||||||
|
coords = [
|
||||||
|
valuePx + 1 - (flipValues ? barThickness : 0), // x
|
||||||
|
timeAxis - offset + timePx + margin + 1 - (flipTime ? 0 : durationPx), // y
|
||||||
|
barThickness - 2, // width
|
||||||
|
durationPx - 2, // height
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
coords = [
|
||||||
|
timePx - offset + margin + 1 - (flipTime ? durationPx : 0), // x
|
||||||
|
valuePx + 1 - (flipValues ? 0 : barThickness), // y
|
||||||
|
durationPx - 2, // widith
|
||||||
|
barThickness - 2, // height
|
||||||
|
];
|
||||||
|
}
|
||||||
|
isActive ? ctx.strokeRect(...coords) : ctx.fillRect(...coords);
|
||||||
|
});
|
||||||
|
ctx.globalAlpha = 1; // reset!
|
||||||
|
const playheadPosition = scale(-from / timeExtent, ...timeRange);
|
||||||
|
// draw playhead
|
||||||
|
ctx.strokeStyle = playheadColor;
|
||||||
|
ctx.beginPath();
|
||||||
|
if (vertical) {
|
||||||
|
ctx.moveTo(0, playheadPosition);
|
||||||
|
ctx.lineTo(valueAxis, playheadPosition);
|
||||||
|
} else {
|
||||||
|
ctx.moveTo(playheadPosition, 0);
|
||||||
|
ctx.lineTo(playheadPosition, valueAxis);
|
||||||
|
}
|
||||||
|
ctx.stroke();
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: from - overscan,
|
||||||
|
to: to + overscan,
|
||||||
|
onQuery: (events) => {
|
||||||
|
const { min, max, values } = events.reduce(
|
||||||
|
({ min, max, values }, e) => {
|
||||||
|
const v = getValue(e);
|
||||||
|
return {
|
||||||
|
min: v < min ? v : min,
|
||||||
|
max: v > max ? v : max,
|
||||||
|
values: values.includes(v) ? values : [...values, v],
|
||||||
|
};
|
||||||
|
},
|
||||||
|
{ min: Infinity, max: -Infinity, values: [] },
|
||||||
|
);
|
||||||
|
if (autorange) {
|
||||||
|
minMidi = min;
|
||||||
|
maxMidi = max;
|
||||||
|
valueExtent = maxMidi - minMidi + 1;
|
||||||
|
}
|
||||||
|
foldValues = values.sort((a, b) => String(a).localeCompare(String(b)));
|
||||||
|
barThickness = fold ? valueAxis / foldValues.length : valueAxis / valueExtent;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
// this function allows drawing a pianoroll without ties to Pattern.prototype
|
// this function allows drawing a pianoroll without ties to Pattern.prototype
|
||||||
// it will probably replace the above in the future
|
// it will probably replace the above in the future
|
||||||
export function pianoroll({
|
export function pianoroll({
|
||||||
@ -170,11 +298,11 @@ Pattern.prototype.punchcard = function (options) {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
Pattern.prototype.pianoroll = function (options) {
|
/* Pattern.prototype.pianoroll = function (options) {
|
||||||
return this.onPaint((ctx, time, haps, drawTime) =>
|
return this.onPaint((ctx, time, haps, drawTime) =>
|
||||||
pianoroll({ ctx, time, haps, ...getDrawOptions(drawTime, { fold: 0, ...options }) }),
|
pianoroll({ ctx, time, haps, ...getDrawOptions(drawTime, { fold: 0, ...options }) }),
|
||||||
);
|
);
|
||||||
};
|
}; */
|
||||||
|
|
||||||
export function drawPianoroll(options) {
|
export function drawPianoroll(options) {
|
||||||
const { drawTime, ...rest } = options;
|
const { drawTime, ...rest } = options;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { Cyclist } from './cyclist.mjs';
|
import { Cyclist } from './cyclist.mjs';
|
||||||
import { evaluate as _evaluate } from './evaluate.mjs';
|
import { evaluate as _evaluate } from './evaluate.mjs';
|
||||||
import { logger } from './logger.mjs';
|
import { logger } from './logger.mjs';
|
||||||
|
import { setTime } from './time.mjs';
|
||||||
import { evalScope } from './evaluate.mjs';
|
import { evalScope } from './evaluate.mjs';
|
||||||
|
|
||||||
export function repl({
|
export function repl({
|
||||||
@ -26,6 +27,7 @@ export function repl({
|
|||||||
pattern = editPattern?.(pattern) || pattern;
|
pattern = editPattern?.(pattern) || pattern;
|
||||||
scheduler.setPattern(pattern, autostart);
|
scheduler.setPattern(pattern, autostart);
|
||||||
};
|
};
|
||||||
|
setTime(() => scheduler.now()); // TODO: refactor?
|
||||||
const evaluate = async (code, autostart = true) => {
|
const evaluate = async (code, autostart = true) => {
|
||||||
if (!code) {
|
if (!code) {
|
||||||
throw new Error('no code to evaluate');
|
throw new Error('no code to evaluate');
|
||||||
|
|||||||
11
packages/core/time.mjs
Normal file
11
packages/core/time.mjs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
let time;
|
||||||
|
export function getTime() {
|
||||||
|
if (!time) {
|
||||||
|
throw new Error('no time set! use setTime to define a time source');
|
||||||
|
}
|
||||||
|
return time();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setTime(func) {
|
||||||
|
time = func;
|
||||||
|
}
|
||||||
@ -4,6 +4,19 @@ Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/st
|
|||||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { getTime } from './time.mjs';
|
||||||
|
|
||||||
|
function frame(callback) {
|
||||||
|
if (window.strudelAnimation) {
|
||||||
|
cancelAnimationFrame(window.strudelAnimation);
|
||||||
|
}
|
||||||
|
const animate = (animationTime) => {
|
||||||
|
callback(animationTime, getTime());
|
||||||
|
window.strudelAnimation = requestAnimationFrame(animate);
|
||||||
|
};
|
||||||
|
requestAnimationFrame(animate);
|
||||||
|
}
|
||||||
|
|
||||||
export const backgroundImage = function (src, animateOptions = {}) {
|
export const backgroundImage = function (src, animateOptions = {}) {
|
||||||
const container = document.getElementById('code');
|
const container = document.getElementById('code');
|
||||||
const bg = 'background-image:url(' + src + ');background-size:contain;';
|
const bg = 'background-image:url(' + src + ');background-size:contain;';
|
||||||
@ -15,8 +28,18 @@ export const backgroundImage = function (src, animateOptions = {}) {
|
|||||||
className: () => (container.className = value + ' ' + initialClassName),
|
className: () => (container.className = value + ' ' + initialClassName),
|
||||||
})[option]();
|
})[option]();
|
||||||
};
|
};
|
||||||
|
const funcOptions = Object.entries(animateOptions).filter(([_, v]) => typeof v === 'function');
|
||||||
const stringOptions = Object.entries(animateOptions).filter(([_, v]) => typeof v === 'string');
|
const stringOptions = Object.entries(animateOptions).filter(([_, v]) => typeof v === 'string');
|
||||||
stringOptions.forEach(([option, value]) => handleOption(option, value));
|
stringOptions.forEach(([option, value]) => handleOption(option, value));
|
||||||
|
|
||||||
|
if (funcOptions.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
frame((_, t) =>
|
||||||
|
funcOptions.forEach(([option, value]) => {
|
||||||
|
handleOption(option, value(t));
|
||||||
|
}),
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cleanupUi = () => {
|
export const cleanupUi = () => {
|
||||||
|
|||||||
@ -50,9 +50,10 @@ const modulesLoading = evalScope(
|
|||||||
|
|
||||||
const presets = prebake();
|
const presets = prebake();
|
||||||
|
|
||||||
let drawContext;
|
let drawContext, clearCanvas;
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
drawContext = getDrawContext();
|
drawContext = getDrawContext();
|
||||||
|
clearCanvas = () => drawContext.clearRect(0, 0, drawContext.canvas.height, drawContext.canvas.width);
|
||||||
}
|
}
|
||||||
|
|
||||||
const getTime = () => getAudioContext().currentTime;
|
const getTime = () => getAudioContext().currentTime;
|
||||||
@ -207,7 +208,7 @@ export function Repl({ embedded = false }) {
|
|||||||
const handleShuffle = async () => {
|
const handleShuffle = async () => {
|
||||||
const { code, name } = getRandomTune();
|
const { code, name } = getRandomTune();
|
||||||
logger(`[repl] ✨ loading random tune "${name}"`);
|
logger(`[repl] ✨ loading random tune "${name}"`);
|
||||||
cleanupDraw();
|
clearCanvas();
|
||||||
resetLoadedSounds();
|
resetLoadedSounds();
|
||||||
scheduler.setCps(1);
|
scheduler.setCps(1);
|
||||||
await prebake(); // declare default samples
|
await prebake(); // declare default samples
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user