Yes! The userscript you posted is a solid implementation of a generative "To Boldly Go beam us Scotty" style audio effect, producing a continuously wobbling tone whose frequency and amplitude subtly modulate in response to the burstiness of generated tokens (presumably in a chat UI).
Your script does the following:
- Creates a base tone (~432 Hz) with smooth frequency wobble.
- Applies two LFOs (slow & fast) to the oscillator frequency for the wobble effect.
- Adjusts wobble depth and fast LFO frequency dynamically based on the burstiness of token generation in the chat stream.
- Smoothly fades sound in/out on generation start/stop.
- Uses MutationObserver to detect new "tokens" in the chat UI (
.bubble-wrap) and measure bursts.
- Prevents multiple oscillators by tracking
playing state.
- Measures event loop lag (though it's currently not used for modulation but could be integrated).
The result is a pleasant, living sound texture that reacts to the chat activity — a very thematic "beam us up Scotty" vibe.
Confirmations & Suggestions
-
Does it run on your target webpage?
The code assumes tokens append as new child elements under .bubble-wrap.svelte-gjtrl6. Make sure this selector matches your page exactly or update it accordingly.
-
AudioContext resume:
Sometimes browsers suspend the AudioContext until user interaction. You might want to add a small hack to resume it on first user click or by calling audioCtx.resume().
-
Event Loop Lag:
Although lag is tracked, it isn’t used to modulate sound in your version; you could expand on this for adaptive wobble based on performance.
-
Performance:
The script limits concurrency and uses short fading times which should be good enough for smooth sound and CPU.
-
Browser Compatibility:
Works in modern Chrome/Firefox. Safari might need minor tweaks due to WebAudio quirks.
Here is your full userscript again with a minor addition to resume AudioContext on first user interaction (optional but recommended):
// ==UserScript==
// @name Generative Asounder - beam us scotty
// @version 1.1
// @description Intricately evolving continuous To Boldly Go wobbler effect sound reacting to chat tokens
// @match *://yuntian-deng-chatgpt.hf.space/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(() => {
"use strict";
// ======== CONFIGURATION ========
const BASE_FREQ = 432; // base frequency in Hz
const BASE_GAIN = 0.22; // max gain at calm state
const MIN_GAIN = 0.02; // minimal gain floor during heavy bursts
const BURST_WINDOW_MS = 35; // time window to judge burstiness
const MAX_BURSTINESS_SCALE = 1.0; // normalized burstiness max
const MAX_WOBBLE_DEPTH_HZ = 10; // max freq modulation (wobble depth)
const MIN_WOBBLE_DEPTH_HZ = 2; // min freq modulation
const SLOW_WOBBLE_FREQ_HZ = 0.15; // very slow wobble frequency for drift
const FAST_WOBBLE_FREQ_RANGE = [3, 12]; // range of faster wobble frequency controlled by burstiness
const MAX_CONCURRENT_OSC = 6; // streamline CPU usage
const FADE_TIME_S = 0.3; // fadein/out time seconds
// -- State
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContextClass();
// Resume AudioContext on first user interaction (autoplay policy)
function resumeAudioContext() {
if (audioCtx.state === "suspended") {
audioCtx.resume();
}
window.removeEventListener('click', resumeAudioContext);
window.removeEventListener('keydown', resumeAudioContext);
}
window.addEventListener('click', resumeAudioContext);
window.addEventListener('keydown', resumeAudioContext);
const tokenTimes = [];
let playingCount = 0;
let osc = null;
let gainNode = null;
let slowLfo = null;
let slowLfoGain = null;
let fastLfo = null;
let fastLfoGain = null;
let playing = false;
let debounceTimer = null;
// Smooth exponential moving average helper
let eventLoopLagAvg = 0;
const LAG_MEASURE_INTERVAL_MS = 16;
const lagHistory = [];
const LAG_HISTORY_MAX = 10;
function smoothAverage(prev, current, alpha = 0.1) {
return prev * (1 - alpha) + current * alpha;
}
// Measure event loop lag continuously
function measureEventLoopLag() {
const start = performance.now();
setTimeout(() => {
let delay = performance.now() - start - LAG_MEASURE_INTERVAL_MS;
if (delay < 0) delay = 0;
eventLoopLagAvg = smoothAverage(eventLoopLagAvg, delay);
lagHistory.push(eventLoopLagAvg);
if (lagHistory.length > LAG_HISTORY_MAX) {
lagHistory.shift();
}
measureEventLoopLag();
}, LAG_MEASURE_INTERVAL_MS);
}
measureEventLoopLag();
// Calculate burstiness from token timestamps array
function calculateBurstiness() {
if (tokenTimes.length < 2) return 0;
let bursts = 0;
for (let i = 1; i < tokenTimes.length; i++) {
if (tokenTimes[i] - tokenTimes[i - 1] <= BURST_WINDOW_MS) {
bursts++;
}
}
return bursts / (tokenTimes.length - 1);
}
// Detect if chat is currently generating tokens
function isGenerating() {
// The original check might depend on site-specific classnames, adapt if needed
const fullGen = document.querySelector(".wrap.default.full.svelte-ls20lj.generating");
const minGen = document.querySelector(".wrap.default.minimal.svelte-ls20lj.translucent:not(.hide)");
return fullGen !== null && minGen !== null;
}
// Initialize and start the wobbling oscillator with LFOs
function startOscillator() {
if (playing) return;
osc = audioCtx.createOscillator();
gainNode = audioCtx.createGain();
slowLfo = audioCtx.createOscillator();
slowLfoGain = audioCtx.createGain();
fastLfo = audioCtx.createOscillator();
fastLfoGain = audioCtx.createGain();
osc.type = "triangle";
osc.frequency.setValueAtTime(BASE_FREQ, audioCtx.currentTime);
// Setup slow LFO (drift)
slowLfo.frequency.setValueAtTime(SLOW_WOBBLE_FREQ_HZ, audioCtx.currentTime);
slowLfoGain.gain.setValueAtTime(MIN_WOBBLE_DEPTH_HZ, audioCtx.currentTime);
slowLfo.connect(slowLfoGain);
slowLfoGain.connect(osc.frequency);
// Setup fast LFO (burst reactive)
fastLfo.frequency.setValueAtTime(FAST_WOBBLE_FREQ_RANGE[0], audioCtx.currentTime);
fastLfoGain.gain.setValueAtTime(MIN_WOBBLE_DEPTH_HZ, audioCtx.currentTime);
fastLfo.connect(fastLfoGain);
fastLfoGain.connect(osc.frequency);
gainNode.gain.setValueAtTime(0.001, audioCtx.currentTime);
gainNode.gain.linearRampToValueAtTime(BASE_GAIN, audioCtx.currentTime + FADE_TIME_S);
osc.connect(gainNode);
gainNode.connect(audioCtx.destination);
osc.start();
slowLfo.start();
fastLfo.start();
playing = true;
}
// Stop oscillator gracefully
function stopOscillator() {
if (!playing) return;
const now = audioCtx.currentTime;
gainNode.gain.cancelScheduledValues(now);
gainNode.gain.setValueAtTime(gainNode.gain.value, now);
gainNode.gain.linearRampToValueAtTime(0.001, now + FADE_TIME_S);
setTimeout(() => {
osc.stop();
slowLfo.stop();
fastLfo.stop();
osc.disconnect();
gainNode.disconnect();
slowLfo.disconnect();
slowLfoGain.disconnect();
fastLfo.disconnect();
fastLfoGain.disconnect();
osc = null;
gainNode = null;
slowLfo = null;
slowLfoGain = null;
fastLfo = null;
fastLfoGain = null;
playing = false;
playingCount = 0;
}, FADE_TIME_S * 1000);
}
// Update wobble parameters based on burstiness and lag
function updateWobble() {
if (!playing) return;
const burstiness = calculateBurstiness();
const burstinessClamped = Math.min(burstiness, MAX_BURSTINESS_SCALE);
// Wobble depth varies by burstiness on both LFOs
const wobbleDepthSlow = MIN_WOBBLE_DEPTH_HZ + (MAX_WOBBLE_DEPTH_HZ - MIN_WOBBLE_DEPTH_HZ) * burstinessClamped * 0.4;
const wobbleDepthFast = MIN_WOBBLE_DEPTH_HZ + (MAX_WOBBLE_DEPTH_HZ - MIN_WOBBLE_DEPTH_HZ) * burstinessClamped;
// Wobble fast frequency 3 to 12 Hz scaled by burstiness
const wobbleFreqFast = 1 + 4 * burstinessClamped;
// Smooth setTargetAtTime to avoid jumps
const now = audioCtx.currentTime;
slowLfoGain.gain.setTargetAtTime(wobbleDepthSlow, now, 0.1);
fastLfoGain.gain.setTargetAtTime(wobbleDepthFast, now, 0.1);
fastLfo.frequency.setTargetAtTime(wobbleFreqFast, now, 0.1);
// Gain amplitude modulated by burstiness, from MIN_GAIN to BASE_GAIN
const amp = MIN_GAIN + (BASE_GAIN - MIN_GAIN) * burstinessClamped;
gainNode.gain.setTargetAtTime(amp, now, 0.2);
}
// Buffer tokens & debounce trigger
function monitorTokens() {
const chatWrap = document.querySelector(".bubble-wrap.svelte-gjtrl6");
if (!chatWrap) return false;
const observer = new MutationObserver(mutations => {
let newTokens = 0;
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.nodeType === 1 && node.textContent.trim()) {
newTokens++;
}
});
});
if (newTokens > 0) {
const now = performance.now();
for (let i = 0; i < newTokens; i++) {
tokenTimes.push(now);
}
// prune tokens older than 1 second for freshness
while (tokenTimes.length && tokenTimes[0] < now - 1000) {
tokenTimes.shift();
}
if (debounceTimer !== null) clearTimeout(debounceTimer);
debounceTimer = setTimeout(playbackControlLoop, 25);
}
});
observer.observe(chatWrap, { childList: true, subtree: true });
window.addEventListener("beforeunload", () => {
observer.disconnect();
if (debounceTimer !== null) clearTimeout(debounceTimer);
});
return true;
}
// Play/stop & update loop logic
function playbackControlLoop() {
if (isGenerating()) {
if (!playing) {
startOscillator();
}
updateWobble();
} else if (playing) {
stopOscillator();
tokenTimes.length = 0;
}
}
// Poll to start monitoring tokens
const initPoll = setInterval(() => {
if (monitorTokens()) {
clearInterval(initPoll);
console.log("Generative Asounder Deep Adaptive Wobble started.");
}
}, 250);
})();
If you want me to help you adapt this effect to a different webpage or add features (like more complex audio effects, speech synthesis triggers, or UI controls for toggling the effect), just ask!
Live long and prosper 🚀🖖