Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display amplitude on patches #23

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 72 additions & 4 deletions core/src/components/Patch.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<script lang="ts">
import type { ToneAudioNode } from 'Tone';
import { ToneAudioNode, Draw } from 'Tone';
import { getContext, onMount } from 'svelte';
import patches from '../state/patches';
import Label from './Label.svelte';
import type { Bang, Connection, PatchInput, PatchOutput } from '../types';
import { Connection, PatchInput, PatchOutput } from '../types';
import Bang from '../nodes/Bang';

export let x = 0;
export let y = 0;
export let label: string = undefined;
export let onConnect: (nodes: number) => void;

export let state = {
amplitude: 0,
};

export let input: PatchInput;
export let output: PatchOutput;
export let name: string;
Expand Down Expand Up @@ -41,6 +46,19 @@
output.disconnect(item.node as Bang);
}
}
if (input) {
if ('toDestination' in item.node) {
try {
item.node.disconnect(analyser);
analyser = null;
state.amplitude = 0;
} catch (e) {
console.error(e);
}
} else {
// TODO:
}
}
}
}
});
Expand All @@ -58,6 +76,13 @@
output.connect(item.node as Bang);
}
}
if (input) {
if ('toDestination' in item.node) {
analyser = item.node.context.createAnalyser();
buffer = new Float32Array(analyser.frequencyBinCount);
item.node.connect(analyser);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing the else case for handling bangs

}
}
}
});
Expand Down Expand Up @@ -127,7 +152,47 @@
}
};

let analyser: AnalyserNode | undefined;
let buffer: Float32Array | undefined;

const draw = () => {
Draw.schedule(draw, '+100hz');
if (!analyser || !buffer) {
// console.warn('no analyser or no buffer');
return;
}

analyser.getFloatTimeDomainData(buffer);
const totalSquared = buffer.reduce((total, current) => total + current * current, 0);
const rms = Math.sqrt(totalSquared / buffer.length);
state.amplitude = rms;
};

onMount(() => {
if (output) {
if ('toDestination' in output) {
const context = output.context;
analyser = context.createAnalyser();
buffer = new Float32Array(analyser.frequencyBinCount);
output.connect(analyser);
} else {
const meterBang = new Bang((_time, attack, release) => {
if (attack) {
state.amplitude = 1;
}
if (release) {
// TODO: clear timeout on attack?
setTimeout(() => {
state.amplitude = 0;
}, 100);
}
});
output.connect(meterBang);
}
}

draw();

return () => {
if (input && 'toDestination' in input) {
input.dispose();
Expand All @@ -136,6 +201,8 @@
if (output && 'toDestination' in output) {
output.dispose();
}

// TODO: stop drawing
};
});
</script>
Expand Down Expand Up @@ -165,11 +232,12 @@
on:touchstart|passive={onPatchClick}
on:mouseup={onPatchRelease}
on:touchend={onPatchRelease}
style="left: {x}px; top: {y}px;">
style="left: {x}px; top: {y}px;"
>
<svg width="18" height="18" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle cx="9" cy="9" r="9" fill="var(--color-6)" class="ring" />
<circle cx="9" cy="9" r="8" fill="var(--color-light)" />
<circle cx="9" cy="9" r="5" fill="#222" />
<circle cx="9" cy="9" r="5" style="fill: hsl(0, 0%, {state.amplitude * 100}%)" />
</svg>
{#if label}
<Label top={-1}>{label}</Label>
Expand Down