|
| 1 | +import { createSignal, onCleanup, createEffect } from "solid-js"; |
| 2 | +import { render } from "solid-js/web"; |
| 3 | +import * as Tone from "tone"; |
| 4 | +import type { RendererContext } from "@/types/contexts"; |
| 5 | +import type { PitchShifterPluginConfig } from "./index"; |
| 6 | + |
| 7 | +/** |
| 8 | + * 🎵 Pitch Shifter Plugin (Tone.js + Solid.js Edition) |
| 9 | + * ✅ Real-time pitch updates |
| 10 | + * ✅ Single slider instance |
| 11 | + * ✅ Clean removal on disable |
| 12 | + * ✅ Dynamic slider color (cool → neutral → warm) |
| 13 | + * ✅ Glassmorphism-ready UI |
| 14 | + * Author: TheSakyo |
| 15 | + */ |
| 16 | +export const onPlayerApiReady = async ( |
| 17 | + _, |
| 18 | + { getConfig, setConfig }: RendererContext<PitchShifterPluginConfig> |
| 19 | +) => { |
| 20 | + console.log("[pitch-shifter] Renderer (Solid) initialized ✅"); |
| 21 | + |
| 22 | + const userConfig = await getConfig(); |
| 23 | + const [enabled, setEnabled] = createSignal(userConfig.enabled); |
| 24 | + const [semitones, setSemitones] = createSignal(userConfig.semitones ?? 0); |
| 25 | + |
| 26 | + let media: HTMLMediaElement | null = null; |
| 27 | + let pitchShift: Tone.PitchShift | null = null; |
| 28 | + let nativeSource: MediaStreamAudioSourceNode | null = null; |
| 29 | + let mount: HTMLDivElement | null = null; |
| 30 | + |
| 31 | + /** 🎧 Wait for <video> element */ |
| 32 | + const waitForMedia = (): Promise<HTMLMediaElement> => |
| 33 | + new Promise((resolve) => { |
| 34 | + const check = () => { |
| 35 | + const el = |
| 36 | + document.querySelector("video") || |
| 37 | + document.querySelector("audio") || |
| 38 | + document.querySelector("ytmusic-player video"); |
| 39 | + if (el) resolve(el as HTMLMediaElement); |
| 40 | + else setTimeout(check, 400); |
| 41 | + }; |
| 42 | + check(); |
| 43 | + }); |
| 44 | + |
| 45 | + media = await waitForMedia(); |
| 46 | + console.log("[pitch-shifter] Media found 🎧", media); |
| 47 | + |
| 48 | + await Tone.start(); |
| 49 | + const toneCtx = Tone.getContext(); |
| 50 | + const stream = |
| 51 | + (media as any).captureStream?.() || (media as any).mozCaptureStream?.(); |
| 52 | + if (!stream) { |
| 53 | + console.error("[pitch-shifter] ❌ captureStream() unavailable"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + /** 🎚️ Setup pitch shifting (only once) */ |
| 58 | + const setupPitchShift = () => { |
| 59 | + if (pitchShift) return; |
| 60 | + pitchShift = new Tone.PitchShift({ |
| 61 | + pitch: semitones(), |
| 62 | + windowSize: 0.1, |
| 63 | + }).toDestination(); |
| 64 | + nativeSource = toneCtx.createMediaStreamSource(stream); |
| 65 | + Tone.connect(nativeSource, pitchShift); |
| 66 | + media!.muted = true; |
| 67 | + console.log("[pitch-shifter] Pitch processor active 🎶"); |
| 68 | + }; |
| 69 | + |
| 70 | + /** 📴 Teardown cleanly */ |
| 71 | + const teardownPitchShift = () => { |
| 72 | + pitchShift?.dispose(); |
| 73 | + pitchShift = null; |
| 74 | + nativeSource?.disconnect(); |
| 75 | + nativeSource = null; |
| 76 | + media!.muted = false; |
| 77 | + console.log("[pitch-shifter] Pitch processor stopped 📴"); |
| 78 | + }; |
| 79 | + |
| 80 | + /** 🎨 Solid component for slider UI */ |
| 81 | + const PitchUI = () => { |
| 82 | + /** 💡 Utility: compute slider gradient based on pitch */ |
| 83 | + const getSliderGradient = (value: number) => { |
| 84 | + // Map -12 → 0, 0 → 0.5, 12 → 1 |
| 85 | + const normalized = (value + 12) / 24; |
| 86 | + const cold = [77, 166, 255]; // blue |
| 87 | + const neutral = [255, 77, 77]; // red |
| 88 | + const warm = [255, 170, 51]; // orange |
| 89 | + |
| 90 | + let color: number[]; |
| 91 | + if (value < 0) { |
| 92 | + // blend blue → red |
| 93 | + const t = normalized * 2; |
| 94 | + color = cold.map((c, i) => Math.round(c + (neutral[i] - c) * t)); |
| 95 | + } else { |
| 96 | + // blend red → orange |
| 97 | + const t = (normalized - 0.5) * 2; |
| 98 | + color = neutral.map((c, i) => Math.round(c + (warm[i] - c) * t)); |
| 99 | + } |
| 100 | + return `linear-gradient(90deg, rgb(${color.join(",")}) 0%, #fff 100%)`; |
| 101 | + }; |
| 102 | + |
| 103 | + /** 🎚️ Update slider color when pitch changes */ |
| 104 | + const updateSliderColor = (slider: HTMLInputElement, value: number) => { |
| 105 | + slider.style.background = getSliderGradient(value); |
| 106 | + }; |
| 107 | + |
| 108 | + return ( |
| 109 | + <div class="pitch-wrapper"> |
| 110 | + <input |
| 111 | + type="range" |
| 112 | + min="-12" |
| 113 | + max="12" |
| 114 | + step="1" |
| 115 | + value={semitones()} |
| 116 | + class="pitch-slider" |
| 117 | + onInput={(e) => { |
| 118 | + const slider = e.target as HTMLInputElement; |
| 119 | + const v = parseInt(slider.value); |
| 120 | + setSemitones(v); |
| 121 | + setConfig({ semitones: v }); |
| 122 | + if (pitchShift) pitchShift.pitch = v; |
| 123 | + updateSliderColor(slider, v); |
| 124 | + |
| 125 | + const labelEl = document.querySelector(".pitch-label"); |
| 126 | + if (labelEl) { |
| 127 | + labelEl.classList.add("active"); |
| 128 | + setTimeout(() => labelEl.classList.remove("active"), 200); |
| 129 | + } |
| 130 | + }} |
| 131 | + ref={(el) => updateSliderColor(el, semitones())} |
| 132 | + /> |
| 133 | + <span class="pitch-label"> |
| 134 | + {semitones() >= 0 ? "+" : ""} |
| 135 | + {semitones()} semitones |
| 136 | + </span> |
| 137 | + <button |
| 138 | + class="pitch-reset" |
| 139 | + title="Reset pitch" |
| 140 | + onClick={() => { |
| 141 | + setSemitones(0); |
| 142 | + setConfig({ semitones: 0 }); |
| 143 | + if (pitchShift) pitchShift.pitch = 0; |
| 144 | + const slider = document.querySelector( |
| 145 | + ".pitch-slider" |
| 146 | + ) as HTMLInputElement; |
| 147 | + if (slider) updateSliderColor(slider, 0); |
| 148 | + |
| 149 | + const labelEl = document.querySelector(".pitch-label"); |
| 150 | + if (labelEl) { |
| 151 | + labelEl.classList.add("active"); |
| 152 | + setTimeout(() => labelEl.classList.remove("active"), 200); |
| 153 | + } |
| 154 | + }} |
| 155 | + > |
| 156 | + 🔄 |
| 157 | + </button> |
| 158 | + </div> |
| 159 | + ); |
| 160 | + }; |
| 161 | + |
| 162 | + /** 🧱 Mount UI (only once) */ |
| 163 | + const injectUI = () => { |
| 164 | + const tabs = document.querySelector("tp-yt-paper-tabs.tab-header-container"); |
| 165 | + if (tabs && tabs.parentElement && !document.querySelector(".pitch-wrapper")) { |
| 166 | + mount = document.createElement("div"); |
| 167 | + tabs.parentElement.insertBefore(mount, tabs); |
| 168 | + render(() => <PitchUI />, mount); |
| 169 | + console.log("[pitch-shifter] UI injected via Solid ✅"); |
| 170 | + } |
| 171 | + }; |
| 172 | + |
| 173 | + /** 🧹 Remove UI on disable */ |
| 174 | + const removeUI = () => { |
| 175 | + const existing = document.querySelector(".pitch-wrapper"); |
| 176 | + if (existing) { |
| 177 | + existing.remove(); |
| 178 | + mount = null; |
| 179 | + console.log("[pitch-shifter] UI removed ❌"); |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + /** 🔁 React to plugin state */ |
| 184 | + createEffect(() => { |
| 185 | + if (enabled()) { |
| 186 | + setupPitchShift(); |
| 187 | + injectUI(); |
| 188 | + } else { |
| 189 | + teardownPitchShift(); |
| 190 | + removeUI(); |
| 191 | + } |
| 192 | + }); |
| 193 | + |
| 194 | + /** ⏱️ Periodically sync config */ |
| 195 | + const interval = setInterval(async () => { |
| 196 | + const conf = await getConfig(); |
| 197 | + if (conf.enabled !== enabled()) setEnabled(conf.enabled); |
| 198 | + if (conf.semitones !== semitones()) setSemitones(conf.semitones); |
| 199 | + }, 1000); |
| 200 | + |
| 201 | + onCleanup(() => { |
| 202 | + clearInterval(interval); |
| 203 | + teardownPitchShift(); |
| 204 | + removeUI(); |
| 205 | + }); |
| 206 | +}; |
0 commit comments