Skip to content

Commit

Permalink
small fixes to oscilloscope
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubobubobubobubo committed Oct 23, 2023
1 parent bbe4b52 commit ae29bab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/AudioVisualisation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const drawEmptyBlinkers = (app: Editor) => {

export interface OscilloscopeConfig {
enabled: boolean;
refresh: number;
color: string;
thickness: number;
fftSize: number; // multiples of 256
Expand All @@ -139,9 +140,18 @@ export const runOscilloscope = (
const canvasCtx = canvas.getContext("2d")!;
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
let lastDrawTime = 0;
let frameInterval = 1000 / 30;


function draw() {
const currentTime = Date.now();
requestAnimationFrame(draw);
if (currentTime - lastDrawTime < frameInterval) {
return;
}
lastDrawTime = currentTime;

if (!app.osc.enabled) {
canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
return;
Expand All @@ -156,7 +166,9 @@ export const runOscilloscope = (

canvasCtx.fillStyle = "rgba(0, 0, 0, 0)";
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
if (app.clock.time_position.pulse % app.osc.refresh == 0) {
canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
}

canvasCtx.lineWidth = app.osc.thickness;

Expand Down Expand Up @@ -202,6 +214,5 @@ export const runOscilloscope = (

canvasCtx.stroke();
}

draw();
};
9 changes: 5 additions & 4 deletions src/documentation/oscilloscope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const oscilloscope = (application: Editor): string => {
You can turn on the oscilloscope to generate interesting visuals or to inspect audio. Use the <ic>scope()</ic> function to turn it on and off. The oscilloscope is off by default.
${makeExample(
"Oscilloscope configuration",
`
"Oscilloscope configuration",
`
scope({
enabled: true, // off by default
color: "#fdba74", // any valid CSS color or "random"
Expand All @@ -18,10 +18,11 @@ scope({
orientation: "horizontal", // "vertical" or "horizontal"
is3D: false, // 3D oscilloscope
size: 1, // size of the oscilloscope
refresh: 1 // refresh rate (in pulses)
})
`,
true
)}
true
)}
Note that these values can be patterned as well! You can transform the oscilloscope into its own light show if you want. The picture is not stable anyway so you won't have much use of it for precision work :)
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class Editor {
enabled: false,
color: "#fdba74",
thickness: 4,
refresh: 1,
fftSize: 256,
orientation: "horizontal",
is3D: false,
Expand Down

0 comments on commit ae29bab

Please sign in to comment.