Untitled conversation
- Usercode
- Assistant
"""
# What We Are Doing Here in This Chat
1. **Goal:**
Develop a reliable *GPTative Synthesizer* —
a userscript that detects **strictly formatted musical sheets** in chat messages
and **synthesizes audio** (musical tones) based on those sheets.
2. **Challenges:**
- The musical sheets must be formatted **strictly and clearly** so the script can parse them reliably.
- The chat environment dynamically loads messages and bot generates content continuously.
- Browser restrictions (autoplay policy, CSP) limit how and when audio can be played.
- Userscript scope and page security may restrict loading external scripts or accessing page internals.
3. **How We Do It Right:**
- **Strict Musical Sheet Format:**
- Embed musical sheets inside **Markdown triple-backtick code blocks**
with exact tags `[BEGIN MUSICAL SHEET]` and `[END MUSICAL SHEET]`.
- Sheet content uses **notes with pitch + octave + duration + optional dynamics** per token.
Example token: `C4♩[mf]` means note C octave 4, quarter note, mezzo-forte.
- Measures separated by `|`, rests as `∅`.
- Metadata lines allowed at the top (`Title:`, `Composer:`, etc.).
- **Userscript Design:**
- Use a **stable DOM selector** to watch for new chat messages (e.g., `.bubble-wrap.svelte-gjtrl6`).
- Detect the **bot is generating** using clear CSS class cues (`.generating`).
- Wait for **user interaction** (click) to resume AudioContext (due to autoplay policies).
- Dynamically **parse** latest musical sheet messages when they appear.
- Deduplicate to avoid re-playing the same sheet repeatedly.
- Use **Tone.js** to synthesize notes with vibrato, ADSR envelopes, and smooth scheduling.
- Debounce generation end detection to avoid rapid playback toggling.
- Log extensively for debugging and fine-tuning.
4. **Result:**
- Chat streams musical sheet data →
- Userscript detects, parses, and plays it live →
- You get a seamless auditory experience synchronized with GPT chat output.
---
# Summary
```plaintext
[BEGIN MUSICAL SHEET]
Title: Galactic Melody
Composer: AI Composer
Key Signature: C major
Time Signature: 4/4
C4♩[mf] D4♪[p] E4♬[mp] | F4♩[f] ∅ G4♩[mp] F#4♪[mf] E4♬[mf] |
[END MUSICAL SHEET]
Send exactly formatted sheets like above.
Ensure you click the page once to enable audio.
The GPTative userscript takes it from there.
This is the right approach: combine strict input format, stable detection, robust parsing, and modern web audio synthesis with proper browser compliance.
Happy composing & synthesizing! 🎶🚀
now add how link tonejs lib properly. outshow one huge text in one code block how all should be.
# Complete Guide: How to Properly Link Tone.js and Build the GPTative Userscript
This guide explains step-by-step how to **properly include Tone.js** in your userscript and implement a robust GPTative Synthesizer that:
- Detects strictly formatted musical sheets in chat streams
- Parses and interprets pitch, duration, and dynamics correctly
- Synthesizes and plays music live synchronized with chat generation
---
## 1. How to Link Tone.js Library Properly in a Userscript
For maximum compatibility and to avoid Content Security Policy (CSP) or sandbox issues, use the **`@require` metadata directive** to load Tone.js in your userscript.
### Example Meta Block:
```js
// ==UserScript==
// @name GPTative Synthesizer - Full Tone.js Integration
// @version 1.0
// @description Synthesizes musical sheets in chat using Tone.js
// @match *://yuntian-deng-chatgpt.hf.space/*
// @grant none
// @run-at document-idle
// @require https://cdn.jsdelivr.net/npm/tone@14.8.39/build/Tone.min.js
// ==/UserScript==
This will load Tone.js before your script runs, ensuring you have immediate, reliable access to the global Tone object without needing to inject script tags dynamically.
2. Correct Musical Sheet Format to Generate or Use in Chat
Your musical sheets must be strictly wrapped and formatted for the userscript parser.
[BEGIN MUSICAL SHEET]
Title: Galactic Overture
Composer: AI Composer
Key Signature: C major
Time Signature: 4/4
C4♩[mf] D4♪[p] E4♬[mp] | F4♩[f] ∅ G4♩[mp] F#4♪[mf] E4♬[mf] |
A4♩[mf] B4♪[p] C5♬[mf] | D5♩[f] ∅ ∅ E4♩[mp] D#4♪[mf] C#4♩[p] |
[END MUSICAL SHEET]
Must be inside a triple backtick Markdown code block (```plaintext ... ```) in the chat.
Notes have pitch (e.g., C4), duration (♩, ♪, ♬), optional dynamics ([mf]).
Measures separated by pipe |.
Rests as ∅ optionally followed by duration.
3. Full Working Userscript Code
(() => {
"use strict";
const Tone = window.Tone;
// Constants
const BEGIN_TAG = "[BEGIN MUSICAL SHEET]";
const END_TAG = "[END MUSICAL SHEET]";
const TEMPO_BPM = 90;
const BEAT_DURATION = 60 / TEMPO_BPM;
const DURATION_MAP = { "♩": BEAT_DURATION, "♪": BEAT_DURATION / 2, "♬": BEAT_DURATION / 4 };
const DYNAMICS = { pp: 0.2, p: 0.4, mp: 0.6, mf: 0.75, f: 1.0, ff: 1.3 };
const REST_SYMBOL = "∅";
const NOTE_TO_SEMITONE = {
C: -9, "C#": -8, Db: -8, D: -7, "D#": -6, Eb: -6,
E: -5, F: -4, "F#": -3, Gb: -3, G: -2, "G#": -1,
Ab: -1, A: 0, "A#": 1, Bb: 1, B: 2,
};
// Variables
let isGenerating = false;
let isPlaying = false;
let currentSheet = null;
let botStoppedTimestamp = 0;
const STOP_DELAY_MS = 900;
let synth = null;
let vibratoLFO = null;
let part = null;
// Updates generation state
function updateStates() {
const genOverlay = document.querySelector(".wrap.default.full.svelte-ls20lj.generating");
const translucentOverlay = document.querySelector(".wrap.default.minimal.svelte-ls20lj.translucent:not(.hide)");
const currentlyGenerating = !!genOverlay && !!translucentOverlay;
if (currentlyGenerating !== isGenerating) {
isGenerating = currentlyGenerating;
if (!isGenerating) {
botStoppedTimestamp = performance.now();
console.log("[GPTative] Generation ended");
} else {
botStoppedTimestamp = 0;
console.log("[GPTative] Generation started");
}
}
}
// Extract latest fully wrapped sheet from chat messages
function extractSheet() {
const msgWrap = document.querySelector(".bubble-wrap.svelte-gjtrl6");
if (!msgWrap) return null;
const messages = Array.from(msgWrap.querySelectorAll(".bubble-content")).reverse();
for (const msg of messages) {
const text = msg.textContent || "";
const start = text.indexOf(BEGIN_TAG);
const end = text.indexOf(END_TAG);
if (start !== -1 && end !== -1 && end > start) {
const sheet = text.substring(start + BEGIN_TAG.length, end).trim();
return sheet;
}
}
return null;
}
// Sanitize and split tokens properly from sheet lines
function sanitizeAndSplitTokens(line) {
line = line.replace(/[𝄞]/g," "); // remove symbols like clef
line = line.replace(/Rest/gi, " ");
line = line.replace(/∅[♩♪♬]?/g, "∅");
line = line.replace(/\s+/g, " ").trim();
const rawTokens = line.split(" ");
let tokens = [];
rawTokens.forEach(token => {
const split = token.match(/[A-Ga-g][#♯b♭]?\d?[♩♪♬](\[(pp|p|mp|mf|f|ff)\])?|∅/g);
if (split) tokens.push(...split);
});
return tokens;
}
// Parse sheet to note events
function parseSheet(sheetText) {
const lines = sheetText.split('\n').map(l => l.trim()).filter(l => l.length > 0 && !l.includes(":"));
let tokens = [];
lines.forEach(line => {
tokens.push(...sanitizeAndSplitTokens(line.replace(/\|/g," | ")));
});
let currentDynamic = "mf";
let currentTime = 0;
let events = [];
for(const token of tokens) {
if(token === "|") continue;
if(/^\[(pp|p|mp|mf|f|ff)\]$/i.test(token)) {
currentDynamic = token.slice(1, -1).toLowerCase();
continue;
}
if(token === REST_SYMBOL) {
currentTime += BEAT_DURATION;
continue;
}
const note = parseNoteToken(token);
if(!note) {
console.warn("[GPTative] Unknown token skipped:", token);
continue;
}
const freq = noteToFrequency(note.noteName, note.octave);
if(!freq || !Number.isFinite(freq) || freq <= 0) {
console.warn("[GPTative] Invalid frequency, skipping token:", token);
continue;
}
const duration = DURATION_MAP[note.duration] || BEAT_DURATION;
if(!duration || !Number.isFinite(duration) || duration <= 0) {
console.warn("[GPTative] Invalid duration, skipping token:", token);
continue;
}
const gain = DYNAMICS[note.dynamic || currentDynamic] || 0.75;
events.push({time: currentTime, freq, duration, gain});
currentTime += duration;
}
return events;
}
// Parse a single note token: e.g., C#4♩[mf]
function parseNoteToken(token) {
const re = /^([A-Ga-g])([#♯b♭]?)(\d?)([♩♪♬])(\[(pp|p|mp|mf|f|ff)\])?$/;
const m = token.match(re);
if(!m) return null;
const [, note, accidental, octaveStr, duration, , dynamic] = m;
const noteName = note.toUpperCase() + (accidental ? accidental.replace("♯", "#").replace("♭", "b") : "");
const octave = octaveStr ? parseInt(octaveStr, 10) : 4;
const dyn = dynamic ? dynamic.toLowerCase() : "mf";
if(!(noteName in NOTE_TO_SEMITONE) || !(dyn in DYNAMICS)) {
return null;
}
return {noteName, octave, duration, dynamic: dyn};
}
// Frequency calculation from note + octave
function noteToFrequency(noteName, octave) {
const semitonesFromA4 = NOTE_TO_SEMITONE[noteName] + (octave - 4)*12;
return 440 * Math.pow(2, semitonesFromA4/12);
}
// Initialize synthesizer and vibrato LFO
function initSynth() {
if(!synth) {
synth = new Tone.Synth({
oscillator: {type: "triangle"},
envelope: {attack: 0.01, decay: 0.1, sustain: 0.7, release: 0.3}
}).toDestination();
vibratoLFO = new Tone.LFO(5, -10, 10).start();
vibratoLFO.connect(synth.frequency);
}
}
// Play note events via Tone.Part & Transport
async function playEvents(events) {
if(!synth) initSynth();
if(part) {
part.stop();
part.dispose();
part = null;
}
part = new Tone.Part((time, note) => {
synth.triggerAttackRelease(note.freq, note.duration, time, note.gain);
}, events.map(e => [e, e.time]));
part.start(0);
Tone.Transport.bpm.value = TEMPO_BPM;
if(Tone.Transport.state !== "started") {
Tone.Transport.start();
}
isPlaying = true;
const totalDuration = events.reduce((acc, e) => acc + e.duration, 0);
setTimeout(() => {
if(part) {
part.stop();
part.dispose();
part = null;
}
Tone.Transport.stop();
isPlaying = false;
console.log("[GPTative] Playback ended");
}, (totalDuration + 1)*1000);
}
// Control loop checking generation and playback
async function controlLoop() {
updateStates();
if(!isGenerating) {
if(isPlaying) {
const now = performance.now();
if(botStoppedTimestamp && now - botStoppedTimestamp < STOP_DELAY_MS) {
return; // debounce stopping playback
}
if(part) part.stop();
if(synth) synth.dispose();
Tone.Transport.stop();
isPlaying = false;
currentSheet = null;
console.log("[GPTative] Stopped playback (generation ended)");
}
return;
}
const sheetText = extractSheet();
if(!sheetText) {
if(isPlaying) {
const now = performance.now();
if(botStoppedTimestamp && now - botStoppedTimestamp < STOP_DELAY_MS) {
return; // debounce stopping playback
}
if(part) part.stop();
if(synth) synth.dispose();
Tone.Transport.stop();
isPlaying = false;
currentSheet = null;
console.log("[GPTative] Stopped playback (sheet missing)");
}
return;
}
if(sheetText !== currentSheet) {
if(Tone.context.state === "suspended") {
await Tone.start();
console.log("[GPTative] AudioContext resumed on user gesture");
}
currentSheet = sheetText;
const events = parseSheet(sheetText);
if(isPlaying) {
if(part) part.stop();
if(synth) synth.dispose();
Tone.Transport.stop();
isPlaying = false;
}
console.log("[GPTative] Playing new sheet:", events.length, "notes");
await playEvents(events);
}
}
// Debounce timer and state tracker
let controlTimeout = null;
let botStoppedTimestamp = 0;
// Mutation observer scoped to chat container
const chatContainerSelector = ".bubble-wrap.svelte-gjtrl6";
const chatWrap = document.querySelector(chatContainerSelector);
if (chatWrap) {
const observer = new MutationObserver(() => {
if(controlTimeout) clearTimeout(controlTimeout);
controlTimeout = setTimeout(controlLoop, 150);
});
observer.observe(chatWrap, { childList: true, subtree: true, attributes: true, attributeFilter: ["class", "style"] });
} else {
// fallback observe whole body if chatWrap missing
const observer = new MutationObserver(() => {
if(controlTimeout) clearTimeout(controlTimeout);
controlTimeout = setTimeout(controlLoop, 150);
});
observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ["class", "style"] });
}
// Periodic fallback polling
setInterval(controlLoop, 1500);
// Resume AudioContext on user interaction (click/touch)
window.addEventListener("click", async () => {
if(Tone.context.state === "suspended") {
await Tone.start();
console.log("[GPTative] AudioContext resumed via user gesture");
}
});
console.log("[GPTative] Synthesizer ready and running.");
})();
""".
fix this full userscript properly:
// ==UserScript==
// @name GPTative Synthesizer - Strict Code Block Sheet Parser
// @version 3.0
// @description Parse and synthesize strictly wrapped musical sheets inside code blocks during chat generation.
// @match *://yuntian-deng-chatgpt.hf.space/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(() => {
"use strict";
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContextClass();
const BEGIN_TAG = "[BEGIN MUSICAL SHEET]";
const END_TAG = "[END MUSICAL SHEET]";
const BASE_FREQ = 432;
const FADE_TIME = 0.05;
const TEMPO_BPM = 90;
const BEAT_DURATION = 60 / TEMPO_BPM;
const DURATION_MAP = { '♩': 1 * BEAT_DURATION, '♪': 0.5 * BEAT_DURATION, '♬': 0.25 * BEAT_DURATION };
const DYNAMICS = { pp: 0.2, p: 0.4, mp: 0.6, mf: 0.75, f: 1.0, ff: 1.3 };
const REST_SYMBOL = '∅';
let isGenerating = false;
let isError = false;
let isPlaying = false;
let currentSheet = null;
let osc = null;
let gainNode = null;
let playTimeout = null;
// ======= State detection =======
function updateStates() {
const genOverlay = document.querySelector('.wrap.default.full.svelte-ls20lj.generating');
const translucentOverlay = document.querySelector('.wrap.default.minimal.svelte-ls20lj.translucent:not(.hide)');
const errorStatus = document.querySelector('.clear-status.svelte-ls20lj .error');
const errorToast = document.querySelector('.toast-body.error.svelte-15qa81b');
const genActive = !!genOverlay && !!translucentOverlay;
const errorActive = !!errorStatus || !!errorToast;
if (genActive !== isGenerating) {
isGenerating = genActive;
console.log("[GPTative] Generating state:", isGenerating);
}
if (errorActive !== isError) {
isError = errorActive;
console.log("[GPTative] Error state:", isError);
}
}
// ======= Extract latest fully wrapped sheet inside code blocks =======
function extractStrictSheetFromCodeBlocks() {
const codeBlocks = Array.from(document.querySelectorAll('pre, code'));
for (let i = codeBlocks.length - 1; i >= 0; i--) {
const block = codeBlocks[i];
const text = block.textContent || "";
const beginIdx = text.indexOf(BEGIN_TAG);
const endIdx = text.indexOf(END_TAG);
if (beginIdx !== -1 && endIdx !== -1 && endIdx > beginIdx) {
// Extract content strictly inside the tags
const sheet = text.substring(beginIdx + BEGIN_TAG.length, endIdx).trim();
console.log("[GPTative] Extracted musical sheet:");
console.log(sheet);
return sheet;
}
}
return null;
}
// ======= Parse sheet content into events =======
function parseSheet(sheet) {
// Split lines and filter out metadata lines (with colon)
const lines = sheet.split('\n').map(l => l.trim()).filter(l => l.length > 0 && !l.includes(':'));
// Join into single musical line string
const musicStr = lines.join(' ');
// Tokenize by spaces and filter out empty
const tokens = musicStr.split(/\s+/).filter(t => t.length > 0);
const events = [];
let currentGain = 0.25;
for (const token of tokens) {
// Dynamics token, e.g. [mf]
let dynMatch = token.match(/^\[(pp|p|mp|mf|f|ff)\]$/i);
if (dynMatch) {
const dyn = dynMatch[1].toLowerCase();
currentGain = DYNAMICS[dyn] || 0.25;
continue;
}
// Rest token
if (token === REST_SYMBOL) {
events.push({ freq: null, duration: BEAT_DURATION, gain: 0 });
continue;
}
// Notes tokens with possible accidentals ignored, duration symbol only ♩ ♪ ♬
// We parse by note duration character anywhere inside the token:
let noteMatch = token.match(/[♩♪♬]/);
if (noteMatch) {
const durSym = noteMatch[0];
events.push({ freq: BASE_FREQ, duration: DURATION_MAP[durSym] || BEAT_DURATION, gain: currentGain });
continue;
}
// Unknown tokens ignored silently
}
return events;
}
// ======= Play parsed events sequentially ====
function playEvents(events) {
if (events.length === 0) return;
let idx = 0;
function playNext() {
if (idx >= events.length) {
stopSound();
return;
}
const ev = events[idx++];
if (ev.freq === null) {
// Rest
if (gainNode) gainNode.gain.setTargetAtTime(0, audioCtx.currentTime + 0.01, 0.01);
playTimeout = setTimeout(playNext, ev.duration * 1000);
return;
}
if (!osc) {
osc = audioCtx.createOscillator();
gainNode = audioCtx.createGain();
osc.type = "triangle";
osc.connect(gainNode);
gainNode.connect(audioCtx.destination);
osc.start();
console.log("[GPTative] Oscillator started");
}
osc.frequency.setTargetAtTime(ev.freq, audioCtx.currentTime, 0.01);
gainNode.gain.cancelScheduledValues(audioCtx.currentTime);
gainNode.gain.setValueAtTime(0, audioCtx.currentTime);
gainNode.gain.linearRampToValueAtTime(ev.gain, audioCtx.currentTime + FADE_TIME);
gainNode.gain.setTargetAtTime(0, audioCtx.currentTime + ev.duration - FADE_TIME, FADE_TIME);
playTimeout = setTimeout(playNext, ev.duration * 1000);
}
playNext();
}
// ======= Stop audio gracefully ====
function stopSound() {
if (playTimeout) clearTimeout(playTimeout);
playTimeout = null;
if (gainNode) {
gainNode.gain.linearRampToValueAtTime(0, audioCtx.currentTime + FADE_TIME);
}
if (osc) {
setTimeout(() => {
osc.stop();
osc.disconnect();
if (gainNode) gainNode.disconnect();
osc = null;
gainNode = null;
console.log("[GPTative] Oscillator stopped");
}, FADE_TIME * 1000);
}
}
// ======= Main control loop ====
async function controlLoop() {
updateStates();
if (!isGenerating) {
if (isPlaying) {
stopSound();
isPlaying = false;
currentSheet = null;
}
return;
}
const sheetText = extractStrictSheetFromCodeBlocks();
if (!sheetText) {
if (isPlaying) {
stopSound();
isPlaying = false;
currentSheet = null;
}
return;
}
if (sheetText !== currentSheet) {
currentSheet = sheetText;
const events = parseSheet(sheetText);
if (isPlaying) {
stopSound();
}
isPlaying = true;
try {
if (audioCtx.state === 'suspended') await audioCtx.resume();
} catch {}
playEvents(events);
}
}
// ======= Observe DOM mutations for chat changes ====
let controlTimeout = null;
const observer = new MutationObserver(() => {
if (controlTimeout) clearTimeout(controlTimeout);
controlTimeout = setTimeout(controlLoop, 150);
});
observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['class', 'style'] });
// Periodic fallback
setInterval(controlLoop, 1000);
// User interaction to resume audio context per browser autoplay policies
window.addEventListener('click', () => {
if (audioCtx.state === 'suspended') {
audioCtx.resume().then(() => console.log("[GPTative] AudioContext resumed on user interaction"));
}
});
console.log("[GPTative] Synthesizer userscript loaded, waiting for musical sheets...");
})();
output fully properly fixed "GPTative".
// ==UserScript==
// @name GPTative Synthesizer - Strict Code Block Sheet Parser - Fixed & Enhanced
// @version 3.1
// @description Parses strictly formatted musical sheets inside markdown code blocks during chat generation and synthesizes audio with Web Audio API. Respects autoplay policies and robustly handles generation state.
// @match *://yuntian-deng-chatgpt.hf.space/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(() => {
"use strict";
// ============ Constants & Config ============
// Important tags delimiting the sheet inside code blocks
const BEGIN_TAG = "[BEGIN MUSICAL SHEET]";
const END_TAG = "[END MUSICAL SHEET]";
// Audio / Playback parameters
const TEMPO_BPM = 90;
const BEAT_DURATION = 60 / TEMPO_BPM; // in seconds
const FADE_TIME = 0.035; // seconds for smooth fade-in/out of notes
const REST_SYMBOL = "∅";
// Base frequency for A4 tuning (standard 440Hz or alternative 432Hz)
const BASE_A4_FREQ = 432;
// Mapping rhythmic durations symbols to seconds
const DURATION_MAP = {
"♩": 1 * BEAT_DURATION, // quarter note
"♪": 0.5 * BEAT_DURATION, // eighth note
"♬": 0.25 * BEAT_DURATION // sixteenth note
};
// Dynamics (volume multipliers)
const DYNAMICS = {
pp: 0.2,
p: 0.4,
mp: 0.6,
mf: 0.75,
f: 1.0,
ff: 1.3
};
// Note name to semitone offset from A (A = 0)
const NOTE_SEMITONE_OFFSETS = {
"C": -9,
"C#": -8, "Db": -8,
"D": -7,
"D#": -6, "Eb": -6,
"E": -5,
"F": -4,
"F#": -3, "Gb": -3,
"G": -2,
"G#": -1, "Ab": -1,
"A": 0,
"A#": 1, "Bb": 1,
"B": 2
};
// ============ State Variables ============
let isGenerating = false; // true when bot is generating
let isError = false; // true if error detected in chat
let isPlaying = false; // true when playback active
let currentSheet = null; // text content of the currently playing sheet
// Audio API nodes & management
let audioCtx = null;
let osc = null;
let gainNode = null;
let vibratoLFO = null;
let playTimeout = null;
let currentTimeouts = [];
/**
* Clear all scheduled timeouts for playing notes.
*/
function clearAllTimeouts() {
currentTimeouts.forEach(id => clearTimeout(id));
currentTimeouts = [];
}
// ============ Audio Context Init ============
function initAudioCtx() {
if (!audioCtx) {
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
audioCtx = new AudioContextClass();
}
}
/**
* Ensures AudioContext is resumed (required for autoplay policy).
* Returns a Promise.
*/
async function ensureAudioCtxResumed() {
initAudioCtx();
if (audioCtx.state === "suspended") {
try {
await audioCtx.resume();
console.log("[GPTative] AudioContext resumed");
} catch (e) {
console.warn("[GPTative] AudioContext resume failed:", e);
}
}
}
// ============ Utility Functions ============
/**
* Converts note string to frequency in Hz.
* @param {string} noteName Example: C, D#, Gb...
* @param {number} octave Octave number (default 4 if missing)
* @returns {number} frequency in Hz
*/
function noteToFrequency(noteName, octave = 4) {
let semitoneOffset = NOTE_SEMITONE_OFFSETS[noteName];
if (semitoneOffset === undefined) return null;
let semitonesFromA4 = semitoneOffset + (octave - 4) * 12;
return BASE_A4_FREQ * Math.pow(2, semitonesFromA4 / 12);
}
/**
* Parses a single token representing a note with optional octave, duration, dynamic.
* Format example: C4♩[mf]
* @param {string} token
* @returns {object|null} Parsed note event or null if invalid
*/
function parseNoteToken(token) {
// Regex breakdown:
// 1: note letter A-G or a-g
// 2: optional accidental #, ♯, b, ♭
// 3: optional octave digit (0-9)
// 4: duration symbol one of ♩♪♬
// 5: optional dynamics inside square brackets [mf], [pp], etc.
const re = /^([A-Ga-g])([#♯b♭]?)(\d?)([♩♪♬])(\[(pp|p|mp|mf|f|ff)\])?$/;
const match = token.match(re);
if (!match) return null;
let [, note, accidental, octaveStr, durationSym, , dynamics] = match;
note = note.toUpperCase();
// Normalize accidentals to sharps or flats as standard
if (accidental === "♯") accidental = "#";
if (accidental === "♭") accidental = "b";
let noteName = note + (accidental || "");
let octave = octaveStr !== "" ? parseInt(octaveStr, 10) : 4;
let duration = durationSym;
let dynamic = dynamics ? dynamics.toLowerCase() : "mf";
if (!(noteName in NOTE_SEMITONE_OFFSETS)) {
console.warn(`[GPTative] Invalid note name: ${noteName}`);
return null;
}
if (!(dynamic in DYNAMICS)) {
console.warn(`[GPTative] Unknown dynamic: ${dynamic}, defaulting to mf`);
dynamic = "mf";
}
return {
noteName,
octave,
duration,
dynamic
};
}
/**
* Sanitizes and splits a line of music tokens into discrete tokens,
* properly handling rests, measures, and multiple tokens separated by spaces.
* @param {string} line
* @returns {string[]} tokens
*/
function sanitizeAndSplitTokens(line) {
// Remove unwanted symbols, replace "Rest" by rest symbol ∅
line = line.replace(/[𝄞𝄢𝄫𝄫]/g, " "); // Common musical symbols to spaces
line = line.replace(/Rest/gi, REST_SYMBOL);
// Normalize multiple whitespaces
line = line.replace(/\s+/g, " ").trim();
// Insert spaces around measure bar (|) to separate tokens correctly
line = line.replace(/\|/g, " | ");
// Split on spaces
const rawTokens = line.split(" ").filter(t => t.length > 0);
const tokens = [];
rawTokens.forEach(token => {
if (token === "|") {
tokens.push(token);
return;
}
if (token === REST_SYMBOL || token === `${REST_SYMBOL}♩` || token === `${REST_SYMBOL}♪` || token === `${REST_SYMBOL}♬`) {
// Normalize rest duration symbol if any
if(token.length === 1) tokens.push(REST_SYMBOL);
else tokens.push(token);
return;
}
// Notes tokens - there may be compound tokens stuck together, try to extract all matches
// Examples: C4♩[mf], D#5♪, E♬, F4♩, etc.
const noteRegex = /([A-Ga-g][#♯b♭]?\d?[♩♪♬](\[(pp|p|mp|mf|f|ff)\])?)/g;
let m;
while ((m = noteRegex.exec(token)) !== null) {
tokens.push(m[1]);
}
});
return tokens;
}
// ============ Generations & Parsing ============
/**
* Extracts the latest fully wrapped musical sheet strictly contained inside
* a markdown code block with the required BEGIN and END tags.
* It scans from the latest code blocks backward.
* @returns {string|null} extracted sheet text or null if none found
*/
function extractStrictSheetFromCodeBlocks() {
// Select all <pre> and <code> blocks (where code blocks content usually live)
const codeBlocks = Array.from(document.querySelectorAll("pre, code"));
for (let i = codeBlocks.length - 1; i >= 0; i--) {
const block = codeBlocks[i];
// Only consider blocks that appear as code blocks (heuristic)
if (!block.textContent) continue;
const text = block.textContent;
const beginIdx = text.indexOf(BEGIN_TAG);
const endIdx = text.indexOf(END_TAG);
if (beginIdx !== -1 && endIdx !== -1 && endIdx > beginIdx) {
const sheet = text.substring(beginIdx + BEGIN_TAG.length, endIdx).trim();
if (sheet.length > 0) {
return sheet;
}
}
}
return null;
}
/**
* Parses a full musical sheet string into an array of events for playback.
* @param {string} sheetText Raw musical sheet content between BEGIN and END tags
* @returns {Array<{time:number, freq:number|null, duration:number, gain:number}>} Array of note/rest events
*/
function parseSheet(sheetText) {
// Split into lines, exclude metadata lines (lines with colon ':')
const allLines = sheetText.split("\n").map(l => l.trim());
// Extract musical content lines only (exclude metadata lines)
let musicLines = allLines.filter(line => line.length > 0 && !line.includes(":"));
// Accumulate tokens from all music lines
const tokens = [];
musicLines.forEach(line => {
tokens.push(...sanitizeAndSplitTokens(line));
});
// Parse tokens into note/rest events with timing and gain
let currentTime = 0;
let currentDynamic = "mf"; // Default dynamic
const events = [];
for (const token of tokens) {
if (token === "|") {
// Measure bar - no time increment, optional place for alignment if needed
continue;
}
// Change dynamic if token is only dynamic marker, e.g. [mf]
const dynMatch = token.match(/^\[(pp|p|mp|mf|f|ff)\]$/i);
if (dynMatch) {
currentDynamic = dynMatch[1].toLowerCase();
continue;
}
// Rest token
if (token === REST_SYMBOL || token.startsWith(REST_SYMBOL)) {
// Determine duration from rest token suffix if any eg. '∅♩' means quarter rest
let restDur = BEAT_DURATION;
const durSymMatch = token.match(/[♩♪♬]/);
if (durSymMatch) {
restDur = DURATION_MAP[durSymMatch[0]] || BEAT_DURATION;
}
events.push({
time: currentTime,
freq: null,
duration: restDur,
gain: 0
});
currentTime += restDur;
continue;
}
// Otherwise, parse note token
const noteInfo = parseNoteToken(token);
if (!noteInfo) {
console.warn("[GPTative] Skipping invalid token:", token);
continue;
}
// Calculate frequency from note info
const freq = noteToFrequency(noteInfo.noteName, noteInfo.octave);
if (!freq || freq <= 0 || !isFinite(freq)) {
console.warn("[GPTative] Invalid frequency for token:", token);
continue;
}
// Map duration symbol to seconds
const dur = DURATION_MAP[noteInfo.duration] || BEAT_DURATION;
// Map dynamic to gain multiplier
const gainVal = DYNAMICS[noteInfo.dynamic] || DYNAMICS[currentDynamic] || 0.75;
// Create event
events.push({
time: currentTime,
freq,
duration: dur,
gain: gainVal
});
currentTime += dur;
}
return events;
}
// ============ Playback Engine ============
/**
* Initializes oscillator, gain and vibrato
* and connects to audio destination.
*/
function initOscillator() {
if (!audioCtx) initAudioCtx();
if (osc) return; // already created
osc = audioCtx.createOscillator();
gainNode = audioCtx.createGain();
gainNode.gain.value = 0;
// Vibrato LFO (5Hz vibrato with ±10Hz freq modulation)
vibratoLFO = audioCtx.createOscillator();
vibratoLFO.frequency.value = 5; // 5Hz vibrato rate
const vibratoGain = audioCtx.createGain();
vibratoGain.gain.value = 10; // ±10 Hz vibrato depth
// Connect vibrato: vibratoLFO -> vibratoGain -> osc.frequency
vibratoLFO.connect(vibratoGain);
vibratoGain.connect(osc.frequency);
osc.type = "triangle";
osc.connect(gainNode);
gainNode.connect(audioCtx.destination);
osc.start();
vibratoLFO.start();
console.log("[GPTative] Oscillator & vibrato initialized and started");
}
/**
* Stops and disposes oscillator and gain nodes gracefully
*/
function stopOscillator() {
if (!osc) return;
// Fade out gain smoothly before stopping
gainNode.gain.cancelScheduledValues(audioCtx.currentTime);
gainNode.gain.setTargetAtTime(0, audioCtx.currentTime, FADE_TIME);
// Stop oscillators after fade time
setTimeout(() => {
try {
osc.stop();
vibratoLFO.stop();
} catch (_) {}
osc.disconnect();
gainNode.disconnect();
vibratoLFO.disconnect();
osc = null;
gainNode = null;
vibratoLFO = null;
console.log("[GPTative] Oscillator & vibrato stopped and cleaned up");
}, FADE_TIME * 1000);
}
/**
* Plays given note/rest events sequentially.
* Uses scheduled timeouts for note timing.
* @param {Array} events - Array of {time, freq, duration, gain}
*/
function playEvents(events) {
if (!events.length) return;
initOscillator();
let currIndex = 0;
/**
* Plays a single event at index currIndex
*/
function playNext() {
if (currIndex >= events.length) {
isPlaying = false;
stopOscillator();
return;
}
const ev = events[currIndex++];
if (ev.freq === null) {
// Rest: fade gain to zero (if not already)
if (gainNode) {
gainNode.gain.cancelScheduledValues(audioCtx.currentTime);
gainNode.gain.setTargetAtTime(0, audioCtx.currentTime, FADE_TIME);
}
// Schedule next note after rest duration
const id = setTimeout(playNext, ev.duration * 1000);
currentTimeouts.push(id);
return;
}
// Play note frequency with gain and fades
try {
osc.frequency.setTargetAtTime(ev.freq, audioCtx.currentTime, 0.01);
gainNode.gain.cancelScheduledValues(audioCtx.currentTime);
gainNode.gain.setValueAtTime(0, audioCtx.currentTime);
gainNode.gain.linearRampToValueAtTime(ev.gain, audioCtx.currentTime + FADE_TIME);
gainNode.gain.setTargetAtTime(0, audioCtx.currentTime + ev.duration - FADE_TIME, FADE_TIME);
} catch (e) {
console.warn("[GPTative] Audio param scheduling failed:", e);
}
// Schedule next note after current note duration
const id = setTimeout(playNext, ev.duration * 1000);
currentTimeouts.push(id);
}
isPlaying = true;
playNext();
}
/**
* Stops the current playback immediately and cleans all timeouts/audio nodes.
*/
function stopCurrentPlayback() {
if (!isPlaying) return;
clearAllTimeouts();
isPlaying = false;
stopOscillator();
}
// ============ Generation and Playback Control ============
/**
* Checks chat DOM to determine if bot is generating.
*/
function updateGeneratingState() {
// Query generation overlay elements from app UI indicators
const genOverlay = document.querySelector(".wrap.default.full.svelte-ls20lj.generating");
const translucentOverlay = document.querySelector(".wrap.default.minimal.svelte-ls20lj.translucent:not(.hide)");
const currentlyGenerating = !!genOverlay && !!translucentOverlay;
if (currentlyGenerating !== isGenerating) {
isGenerating = currentlyGenerating;
console.log("[GPTative] Generating state updated:", isGenerating);
}
}
/**
* Checks for error state presence (optional).
*/
function updateErrorState() {
const errorStatus = document.querySelector(".clear-status.svelte-ls20lj .error");
const errorToast = document.querySelector(".toast-body.error.svelte-15qa81b");
const errorActive = !!errorStatus || !!errorToast;
if (errorActive !== isError) {
isError = errorActive;
console.log("[GPTative] Error state updated:", isError);
}
}
/**
* Main control loop that detects new sheets, manages playback lifecycle.
*/
async function controlLoop() {
updateGeneratingState();
updateErrorState();
if (isError) {
if (isPlaying) {
stopCurrentPlayback();
currentSheet = null;
}
return;
}
if (!isGenerating) {
if (isPlaying) {
stopCurrentPlayback();
currentSheet = null;
}
return;
}
// Extract current musical sheet from latest code blocks
const sheetText = extractStrictSheetFromCodeBlocks();
if (!sheetText) {
// No sheet found - stop playing if currently playing
if (isPlaying) {
stopCurrentPlayback();
currentSheet = null;
}
return;
}
// New sheet detected, play it
if (sheetText !== currentSheet) {
currentSheet = sheetText;
// Parse sheet into playable events
const events = parseSheet(sheetText);
if (isPlaying) {
stopCurrentPlayback();
}
// Make sure AudioContext resumed on user interaction
await ensureAudioCtxResumed();
if (events.length > 0) {
console.log(`[GPTative] Playing new sheet with ${events.length} events`);
playEvents(events);
} else {
console.warn("[GPTative] No playable note events found");
}
}
// else: same sheet playing, no action needed
}
// ============ Mutation Observer for Chat Updates ============
/**
* Set up mutation observer on chat container or fallback document.body
*/
function setupMutationObserver() {
// The main chat container selector (adjust if UI changes)
const chatContainerSelector = ".bubble-wrap.svelte-gjtrl6";
const chatWrap = document.querySelector(chatContainerSelector);
const observerTarget = chatWrap || document.body;
const mutationObserver = new MutationObserver(() => {
// Debounce control loop calls
if (controlLoopTimeout) clearTimeout(controlLoopTimeout);
controlLoopTimeout = setTimeout(controlLoop, 150);
});
mutationObserver.observe(observerTarget, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ["class", "style"]
});
}
let controlLoopTimeout = null;
// ============ Initialization ============
/**
* Registers user interaction to resume AudioContext (due to browser autoplay policy)
*/
function setupUserInteractionForAudio() {
window.addEventListener("click", async () => {
if (audioCtx && audioCtx.state === "suspended") {
try {
await audioCtx.resume();
console.log("[GPTative] AudioContext resumed via user gesture");
} catch (e) {
console.warn("[GPTative] Failed to resume AudioContext on user interaction", e);
}
}
}, { once: true }); // only once needed
}
/**
* Start all observers and polling
*/
function startController() {
setupMutationObserver();
setupUserInteractionForAudio();
// Fallback polling in case mutation observer misses something
setInterval(controlLoop, 1500);
console.log("[GPTative] Synthesizer userscript initialized and running.");
}
// Run initialization now
startController();
})();
Strict tag-based sheet extraction:
Now scanning all <pre> and <code> blocks for exact [BEGIN MUSICAL SHEET]...[END MUSICAL SHEET] with trimming.
Robust token parsing:
Using regex to parse notes with optional accidentals, octave, duration, dynamics.
Duration, dynamics fully interpreted per token:
Supports quarter, eighth, sixteenth notes; dynamics map to gain multipliers.
Accurate note frequency mapping:
From note name and octave via semitone offset from A4 at chosen tuning (432Hz).
Smooth audio fades on note start/end (FADE_TIME ~35ms).
Added vibrato LFO for richer tone.
Playback manages timeouts and clears them on stop to avoid overlapping playbacks.
Autoplay policies handled:
Waits for user click to resume AudioContext.
Robust generation detection and error detection included.
MutationObserver on chat container or fallback document.body to respond to DOM changes promptly.
Extensive console logging for debugging visible events.
Use this complete script as your GPTative Synthesizer userscript for the target yuntian-deng-chatgpt.hf.space environment. It listens for strictly formatted markdown code blocks with musical sheets inside, parses their notes accurately, and synthesizes audio live with a pleasant triangle wave + vibrato tone.
Make sure you click the page once after loading to enable audio playback due to browser auto-play policy.
Happy composing and listening! 🎶🚀
Not yet included in a published release.