Merge pull request #983 from tidalcycles/clear-hydra

fix: clear hydra on reset
This commit is contained in:
Felix Roos 2024-03-10 00:42:50 +01:00 committed by GitHub
commit 5ebaa3e1ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -1,6 +1,8 @@
import { getDrawContext } from '@strudel/draw'; import { getDrawContext } from '@strudel/draw';
import { controls } from '@strudel/core';
let latestOptions; let latestOptions;
let hydra;
export async function initHydra(options = {}) { export async function initHydra(options = {}) {
// reset if options have changed since last init // reset if options have changed since last init
@ -25,7 +27,7 @@ export async function initHydra(options = {}) {
hydraConfig.canvas = canvas; hydraConfig.canvas = canvas;
await import(/* @vite-ignore */ src); await import(/* @vite-ignore */ src);
const hydra = new Hydra(hydraConfig); hydra = new Hydra(hydraConfig);
if (feedStrudel) { if (feedStrudel) {
const { canvas } = getDrawContext(); const { canvas } = getDrawContext();
canvas.style.display = 'none'; canvas.style.display = 'none';
@ -34,4 +36,14 @@ export async function initHydra(options = {}) {
} }
} }
export function clearHydra() {
if (hydra) {
hydra.hush();
}
globalThis.s0?.clear();
document.getElementById('hydra-canvas')?.remove();
globalThis.speed = controls.speed;
globalThis.shape = controls.shape;
}
export const H = (p) => () => p.queryArc(getTime(), getTime())[0].value; export const H = (p) => () => p.queryArc(getTime(), getTime())[0].value;

View File

@ -18,6 +18,7 @@ import {
import { defaultAudioDeviceName } from '../settings.mjs'; import { defaultAudioDeviceName } from '../settings.mjs';
import { getAudioDevices, setAudioDevice } from './util.mjs'; import { getAudioDevices, setAudioDevice } from './util.mjs';
import { StrudelMirror, defaultSettings } from '@strudel/codemirror'; import { StrudelMirror, defaultSettings } from '@strudel/codemirror';
import { clearHydra } from '@strudel/hydra';
import { useCallback, useEffect, useRef, useState } from 'react'; import { useCallback, useEffect, useRef, useState } from 'react';
import { settingsMap, useSettings } from '../settings.mjs'; import { settingsMap, useSettings } from '../settings.mjs';
import { import {
@ -76,6 +77,11 @@ export function Repl({ embedded = false }) {
onUpdateState: (state) => { onUpdateState: (state) => {
setReplState({ ...state }); setReplState({ ...state });
}, },
onToggle: (playing) => {
if (!playing) {
clearHydra();
}
},
afterEval: (all) => { afterEval: (all) => {
const { code } = all; const { code } = all;
setLatestCode(code); setLatestCode(code);
@ -166,6 +172,7 @@ export function Repl({ embedded = false }) {
const resetEditor = async () => { const resetEditor = async () => {
resetGlobalEffects(); resetGlobalEffects();
clearCanvas(); clearCanvas();
clearHydra();
resetLoadedSounds(); resetLoadedSounds();
editorRef.current.repl.setCps(0.5); editorRef.current.repl.setCps(0.5);
await prebake(); // declare default samples await prebake(); // declare default samples
@ -190,6 +197,7 @@ export function Repl({ embedded = false }) {
setActivePattern(patternData.id); setActivePattern(patternData.id);
setViewingPatternData(patternData); setViewingPatternData(patternData);
clearCanvas(); clearCanvas();
clearHydra();
resetLoadedSounds(); resetLoadedSounds();
resetGlobalEffects(); resetGlobalEffects();
await prebake(); // declare default samples await prebake(); // declare default samples