loukai-app 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +558 -0
- package/bin/loukai.js +32 -0
- package/package.json +243 -0
- package/src/main/appState.js +250 -0
- package/src/main/audioEngine.js +478 -0
- package/src/main/creator/conversionService.js +503 -0
- package/src/main/creator/downloadManager.js +1128 -0
- package/src/main/creator/ffmpegService.js +487 -0
- package/src/main/creator/installLogger.js +51 -0
- package/src/main/creator/keyDetection.js +212 -0
- package/src/main/creator/llmService.js +370 -0
- package/src/main/creator/lrclibService.js +340 -0
- package/src/main/creator/python/crepe_runner.py +189 -0
- package/src/main/creator/python/demucs_runner.py +158 -0
- package/src/main/creator/python/whisper_runner.py +172 -0
- package/src/main/creator/pythonRunner.js +268 -0
- package/src/main/creator/stemBuilder.js +491 -0
- package/src/main/creator/systemChecker.js +474 -0
- package/src/main/handlers/appHandlers.js +45 -0
- package/src/main/handlers/audioHandlers.js +33 -0
- package/src/main/handlers/autotuneHandlers.js +28 -0
- package/src/main/handlers/canvasHandlers.js +84 -0
- package/src/main/handlers/creatorHandlers.js +159 -0
- package/src/main/handlers/editorHandlers.js +98 -0
- package/src/main/handlers/effectsHandlers.js +100 -0
- package/src/main/handlers/fileHandlers.js +45 -0
- package/src/main/handlers/index.js +78 -0
- package/src/main/handlers/libraryHandlers.js +96 -0
- package/src/main/handlers/mixerHandlers.js +64 -0
- package/src/main/handlers/playerHandlers.js +39 -0
- package/src/main/handlers/preferencesHandlers.js +46 -0
- package/src/main/handlers/queueHandlers.js +81 -0
- package/src/main/handlers/rendererHandlers.js +63 -0
- package/src/main/handlers/settingsHandlers.js +42 -0
- package/src/main/handlers/webServerHandlers.js +105 -0
- package/src/main/main.js +2351 -0
- package/src/main/preload.js +252 -0
- package/src/main/settingsManager.js +139 -0
- package/src/main/statePersistence.js +193 -0
- package/src/main/utils/pathValidator.js +112 -0
- package/src/main/webServer.js +2535 -0
- package/src/native/autotune.js +417 -0
- package/src/renderer/adapters/ElectronBridge.js +677 -0
- package/src/renderer/canvas.html +80 -0
- package/src/renderer/components/App.jsx +303 -0
- package/src/renderer/components/AppRoot.jsx +37 -0
- package/src/renderer/components/AudioDeviceSettings.jsx +145 -0
- package/src/renderer/components/EffectsPanelWrapper.jsx +267 -0
- package/src/renderer/components/MixerTab.jsx +233 -0
- package/src/renderer/components/MixerTabWrapper.jsx +31 -0
- package/src/renderer/components/PortalSelect.jsx +239 -0
- package/src/renderer/components/QueueTab.jsx +116 -0
- package/src/renderer/components/RequestsListWrapper.jsx +78 -0
- package/src/renderer/components/ServerTab.jsx +472 -0
- package/src/renderer/components/SongInfoBarWrapper.jsx +77 -0
- package/src/renderer/components/StatusBar.jsx +92 -0
- package/src/renderer/components/TabNavigation.jsx +77 -0
- package/src/renderer/components/TransportControlsWrapper.jsx +69 -0
- package/src/renderer/components/creator/CreateTab.jsx +1236 -0
- package/src/renderer/dist/assets/kaiPlayer-CoMx__a_.js +2 -0
- package/src/renderer/dist/assets/kaiPlayer-CoMx__a_.js.map +1 -0
- package/src/renderer/dist/assets/microphoneEngine-BaCUhhQc.js +2 -0
- package/src/renderer/dist/assets/microphoneEngine-BaCUhhQc.js.map +1 -0
- package/src/renderer/dist/assets/player-DVrqp7N5.js +3 -0
- package/src/renderer/dist/assets/player-DVrqp7N5.js.map +1 -0
- package/src/renderer/dist/assets/songLoaders-BaTgGib4.js +2 -0
- package/src/renderer/dist/assets/songLoaders-BaTgGib4.js.map +1 -0
- package/src/renderer/dist/assets/webrtcManager-BhCHWceK.js +2 -0
- package/src/renderer/dist/assets/webrtcManager-BhCHWceK.js.map +1 -0
- package/src/renderer/dist/js/autoTuneWorklet.js +224 -0
- package/src/renderer/dist/js/micPitchDetectorWorklet.js +137 -0
- package/src/renderer/dist/js/musicAnalysisWorklet.js +216 -0
- package/src/renderer/dist/js/phaseVocoderWorklet.js +341 -0
- package/src/renderer/dist/js/soundtouch-worklet.js +1395 -0
- package/src/renderer/dist/renderer.css +1 -0
- package/src/renderer/dist/renderer.js +62 -0
- package/src/renderer/dist/renderer.js.map +1 -0
- package/src/renderer/dist/renderer.woff2 +0 -0
- package/src/renderer/hooks/useKeyboardShortcuts.js +154 -0
- package/src/renderer/index.html +24 -0
- package/src/renderer/index.html.backup +372 -0
- package/src/renderer/js/PlayerInterface.js +267 -0
- package/src/renderer/js/autoTuneWorklet.js +224 -0
- package/src/renderer/js/butterchurnVerify.js +46 -0
- package/src/renderer/js/canvas-app.js +114 -0
- package/src/renderer/js/cdgPlayer.js +685 -0
- package/src/renderer/js/kaiPlayer.js +1200 -0
- package/src/renderer/js/karaokeRenderer.js +3392 -0
- package/src/renderer/js/micPitchDetectorWorklet.js +137 -0
- package/src/renderer/js/microphoneEngine.js +656 -0
- package/src/renderer/js/musicAnalysisWorklet.js +216 -0
- package/src/renderer/js/phaseVocoderWorklet.js +341 -0
- package/src/renderer/js/player.js +232 -0
- package/src/renderer/js/referencePitchTracker.js +130 -0
- package/src/renderer/js/songLoaders.js +334 -0
- package/src/renderer/js/soundtouch-worklet.js +1395 -0
- package/src/renderer/js/webrtcManager.js +511 -0
- package/src/renderer/lib/butterchurn.min.js +6739 -0
- package/src/renderer/lib/butterchurnPresets.min.js +1 -0
- package/src/renderer/lib/cdgraphics-wrapper.js +16 -0
- package/src/renderer/lib/cdgraphics.js +299 -0
- package/src/renderer/public/js/autoTuneWorklet.js +224 -0
- package/src/renderer/public/js/micPitchDetectorWorklet.js +137 -0
- package/src/renderer/public/js/musicAnalysisWorklet.js +216 -0
- package/src/renderer/public/js/phaseVocoderWorklet.js +341 -0
- package/src/renderer/public/js/soundtouch-worklet.js +1395 -0
- package/src/renderer/react-entry.jsx +44 -0
- package/src/renderer/styles/tailwind.css +106 -0
- package/src/renderer/utils/qrCodeGenerator.js +98 -0
- package/src/renderer/vite.config.js +31 -0
- package/src/shared/adapters/BridgeInterface.js +195 -0
- package/src/shared/components/EffectsPanel.jsx +177 -0
- package/src/shared/components/LibraryPanel.jsx +701 -0
- package/src/shared/components/LineDetailCanvas.jsx +167 -0
- package/src/shared/components/LyricLine.jsx +505 -0
- package/src/shared/components/LyricRejection.jsx +84 -0
- package/src/shared/components/LyricSuggestion.jsx +80 -0
- package/src/shared/components/LyricsEditorCanvas.jsx +271 -0
- package/src/shared/components/MixerPanel.jsx +94 -0
- package/src/shared/components/PlayerControls.jsx +206 -0
- package/src/shared/components/PortalSelect.jsx +239 -0
- package/src/shared/components/QueueList.jsx +365 -0
- package/src/shared/components/QuickSearch.jsx +126 -0
- package/src/shared/components/RequestsList.jsx +121 -0
- package/src/shared/components/SongEditor.jsx +1362 -0
- package/src/shared/components/SongInfoBar.jsx +81 -0
- package/src/shared/components/ThemeToggle.jsx +106 -0
- package/src/shared/components/Toast.jsx +30 -0
- package/src/shared/components/VisualizationSettings.jsx +243 -0
- package/src/shared/constants.js +95 -0
- package/src/shared/context/BridgeContext.jsx +32 -0
- package/src/shared/contexts/AudioContext.jsx +37 -0
- package/src/shared/contexts/PlayerContext.jsx +66 -0
- package/src/shared/contexts/SettingsContext.jsx +50 -0
- package/src/shared/defaults.js +158 -0
- package/src/shared/formatUtils.js +59 -0
- package/src/shared/formatUtils.test.js +207 -0
- package/src/shared/hooks/useAppState.js +97 -0
- package/src/shared/hooks/useAudioEngine.js +264 -0
- package/src/shared/hooks/usePlayer.js +89 -0
- package/src/shared/hooks/useSettingsPersistence.js +74 -0
- package/src/shared/hooks/useWebRTC.js +118 -0
- package/src/shared/ipcContracts.js +299 -0
- package/src/shared/package.json +3 -0
- package/src/shared/services/creatorService.js +373 -0
- package/src/shared/services/creatorService.test.js +413 -0
- package/src/shared/services/editorService.js +213 -0
- package/src/shared/services/editorService.test.js +219 -0
- package/src/shared/services/effectsService.js +271 -0
- package/src/shared/services/effectsService.test.js +418 -0
- package/src/shared/services/libraryService.js +438 -0
- package/src/shared/services/libraryService.test.js +474 -0
- package/src/shared/services/mixerService.js +172 -0
- package/src/shared/services/mixerService.test.js +399 -0
- package/src/shared/services/playerService.js +221 -0
- package/src/shared/services/playerService.test.js +357 -0
- package/src/shared/services/preferencesService.js +219 -0
- package/src/shared/services/queueService.js +226 -0
- package/src/shared/services/queueService.test.js +430 -0
- package/src/shared/services/requestsService.js +155 -0
- package/src/shared/services/requestsService.test.js +362 -0
- package/src/shared/services/serverSettingsService.js +151 -0
- package/src/shared/services/settingsService.js +257 -0
- package/src/shared/services/settingsService.test.js +295 -0
- package/src/shared/state/StateManager.js +263 -0
- package/src/shared/utils/audio.js +42 -0
- package/src/shared/utils/format.js +32 -0
- package/src/shared/utils/lyricsUtils.js +162 -0
- package/src/test/setup.js +40 -0
- package/src/utils/cdgLoader.js +180 -0
- package/src/utils/m4aLoader.js +333 -0
- package/src/web/App.jsx +578 -0
- package/src/web/adapters/WebBridge.js +428 -0
- package/src/web/components/PlayerSettingsPanel.jsx +231 -0
- package/src/web/components/SongSearch.jsx +180 -0
- package/src/web/dist/assets/index-0H-RnRrV.js +51 -0
- package/src/web/dist/assets/index-0H-RnRrV.js.map +1 -0
- package/src/web/dist/assets/index-DYW2zB0u.css +1 -0
- package/src/web/dist/index.html +15 -0
- package/src/web/index.html +14 -0
- package/src/web/main.jsx +10 -0
- package/src/web/package-lock.json +1765 -0
- package/src/web/pages/SongRequestPage.jsx +619 -0
- package/src/web/styles/tailwind.css +68 -0
- package/src/web/vite.config.js +27 -0
- package/static/fonts/material-icons.woff2 +0 -0
- package/static/images/butterchurn-screenshots/Aderrasi - Potion of Spirits.png +0 -0
- package/static/images/butterchurn-screenshots/Aderrasi - Songflower _Moss Posy_.png +0 -0
- package/static/images/butterchurn-screenshots/Aderrasi - Storm of the Eye _Thunder_ - mash0000 - quasi pseudo meta concentrics.png +0 -0
- package/static/images/butterchurn-screenshots/Aderrasi _ Geiss - Airhandler _Kali Mix_ - Canvas Mix.png +0 -0
- package/static/images/butterchurn-screenshots/An AdamFX n Martin Infusion 2 flexi - Why The Sky Looks Diffrent Today - AdamFx n Martin Infusion - Tack Tile Disfunction B.png +0 -0
- package/static/images/butterchurn-screenshots/Cope - The Neverending Explosion of Red Liquid Fire.png +0 -0
- proton lights __Krash_s beat code_ _Phat_remix02b.png +0 -0
- package/static/images/butterchurn-screenshots/Eo_S_ _ Phat - cubetrace - v2.png +0 -0
- package/static/images/butterchurn-screenshots/Eo_S_ _ Zylot - skylight _Stained Glass Majesty mix_.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi - alien fish pond.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi - area 51.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi - infused with the spiral.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi - mindblob _shiny mix_.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi - mindblob mix.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi - predator-prey-spirals.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi - smashing fractals _acid etching mix_.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi - truly soft piece of software - this is generic texturing _Jelly_ .png +0 -0
- package/static/images/butterchurn-screenshots/Flexi _ Martin - astral projection.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi _ Martin - cascading decay swing.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi _ amandio c - piercing 05 - Kopie _2_ - Kopie.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi _ stahlregen - jelly showoff parade.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi_ fishbrain_ Geiss _ Martin - tokamak witchery.png +0 -0
- package/static/images/butterchurn-screenshots/Flexi_ martin _ geiss - dedicated to the sherwin maxawow.png +0 -0
- package/static/images/butterchurn-screenshots/Fumbling_Foo _ Flexi_ Martin_ Orb_ Unchained - Star Nova v7b.png +0 -0
- package/static/images/butterchurn-screenshots/Geiss - Cauldron - painterly 2 _saturation remix_.png +0 -0
- package/static/images/butterchurn-screenshots/Geiss - Reaction Diffusion 2.png +0 -0
- package/static/images/butterchurn-screenshots/Geiss - Spiral Artifact.png +0 -0
- package/static/images/butterchurn-screenshots/Geiss - Thumb Drum.png +0 -0
- package/static/images/butterchurn-screenshots/Geiss _ Flexi _ Martin - disconnected.png +0 -0
- package/static/images/butterchurn-screenshots/Geiss_ Flexi _ Stahlregen - Thumbdrum Tokamak _crossfiring aftermath jelly mashup_.png +0 -0
- package/static/images/butterchurn-screenshots/Goody - The Wild Vort.png +0 -0
- package/static/images/butterchurn-screenshots/Idiot - Star Of Annon.png +0 -0
- package/static/images/butterchurn-screenshots/Krash _ Illusion - Spiral Movement.png +0 -0
- package/static/images/butterchurn-screenshots/Martin - QBikal - Surface Turbulence IIb.png +0 -0
- package/static/images/butterchurn-screenshots/Martin - acid wiring.png +0 -0
- package/static/images/butterchurn-screenshots/Martin - charisma.png +0 -0
- package/static/images/butterchurn-screenshots/Martin - liquid arrows.png +0 -0
- package/static/images/butterchurn-screenshots/Milk Artist At our Best - FED - SlowFast Ft AdamFX n Martin - HD CosmoFX.png +0 -0
- package/static/images/butterchurn-screenshots/ORB - Waaa.png +0 -0
- package/static/images/butterchurn-screenshots/Phat_fiShbRaiN_Eo_S_Mandala_Chasers_remix.png +0 -0
- package/static/images/butterchurn-screenshots/Rovastar - Oozing Resistance.png +0 -0
- package/static/images/butterchurn-screenshots/Rovastar _ Loadus _ Geiss - FractalDrop _Triple Mix_.png +0 -0
- package/static/images/butterchurn-screenshots/TonyMilkdrop - Leonardo Da Vinci_s Balloon _Flexi - merry-go-round _ techstyle_.png +0 -0
- package/static/images/butterchurn-screenshots/TonyMilkdrop - Magellan_s Nebula _Flexi - you enter first _ multiverse_.png +0 -0
- package/static/images/butterchurn-screenshots/Unchained - Rewop.png +0 -0
- package/static/images/butterchurn-screenshots/Unchained - Unified Drag 2.png +0 -0
- package/static/images/butterchurn-screenshots/Unchained _ Rovastar - Wormhole Pillars _Hall of Shadows mix_.png +0 -0
- package/static/images/butterchurn-screenshots/Zylot - Paint Spill _Music Reactive Paint Mix_.png +0 -0
- package/static/images/butterchurn-screenshots/Zylot - Star Ornament.png +0 -0
- package/static/images/butterchurn-screenshots/Zylot - True Visionary _Final Mix_.png +0 -0
- package/static/images/butterchurn-screenshots/_Aderrasi - Wanderer in Curved Space - mash0000 - faclempt kibitzing meshuggana schmaltz _Geiss color mix_.png +0 -0
- package/static/images/butterchurn-screenshots/_Geiss - Artifact 01.png +0 -0
- package/static/images/butterchurn-screenshots/_Geiss - Desert Rose 2.png +0 -0
- package/static/images/butterchurn-screenshots/_Geiss - untitled.png +0 -0
- package/static/images/butterchurn-screenshots/_Mig_049.png +0 -0
- package/static/images/butterchurn-screenshots/_Mig_085.png +0 -0
- package/static/images/butterchurn-screenshots/_Rovastar _ Geiss - Hurricane Nightmare _Posterize Mix_.png +0 -0
- package/static/images/butterchurn-screenshots/___ Royal - Mashup _197_.png +0 -0
- package/static/images/butterchurn-screenshots/___ Royal - Mashup _220_.png +0 -0
- package/static/images/butterchurn-screenshots/___ Royal - Mashup _431_.png +0 -0
- package/static/images/butterchurn-screenshots/cope _ martin - mother-of-pearl.png +0 -0
- package/static/images/butterchurn-screenshots/fiShbRaiN _ Flexi - witchcraft 2_0.png +0 -0
- package/static/images/butterchurn-screenshots/flexi - bouncing balls _double mindblob neon mix_.png +0 -0
- package/static/images/butterchurn-screenshots/flexi - mom_ why the sky looks different today.png +0 -0
- package/static/images/butterchurn-screenshots/flexi - patternton_ district of media_ capitol of the united abstractions of fractopia.png +0 -0
- package/static/images/butterchurn-screenshots/flexi - swing out on the spiral.png +0 -0
- package/static/images/butterchurn-screenshots/flexi - what is the matrix.png +0 -0
- package/static/images/butterchurn-screenshots/flexi _ amandio c - organic _random mashup_.png +0 -0
- package/static/images/butterchurn-screenshots/flexi _ amandio c - organic12-3d-2_milk.png +0 -0
- package/static/images/butterchurn-screenshots/flexi _ fishbrain - neon mindblob grafitti.png +0 -0
- package/static/images/butterchurn-screenshots/flexi _ geiss - pogo cubes vs_ tokamak vs_ game of life _stahls jelly 4_5 finish_.png +0 -0
- package/static/images/butterchurn-screenshots/high-altitude basket unraveling - singh grooves nitrogen argon nz_.png +0 -0
- package/static/images/butterchurn-screenshots/martin - The Bridge of Khazad-Dum.png +0 -0
- package/static/images/butterchurn-screenshots/martin - angel flight.png +0 -0
- package/static/images/butterchurn-screenshots/martin - another kind of groove.png +0 -0
- package/static/images/butterchurn-screenshots/martin - bombyx mori.png +0 -0
- package/static/images/butterchurn-screenshots/martin - castle in the air.png +0 -0
- package/static/images/butterchurn-screenshots/martin - chain breaker.png +0 -0
- package/static/images/butterchurn-screenshots/martin - disco mix 4.png +0 -0
- package/static/images/butterchurn-screenshots/martin - extreme heat.png +0 -0
- package/static/images/butterchurn-screenshots/martin - frosty caves 2.png +0 -0
- package/static/images/butterchurn-screenshots/martin - fruit machine.png +0 -0
- package/static/images/butterchurn-screenshots/martin - ghost city.png +0 -0
- package/static/images/butterchurn-screenshots/martin - glass corridor.png +0 -0
- package/static/images/butterchurn-screenshots/martin - infinity _2010 update_.png +0 -0
- package/static/images/butterchurn-screenshots/martin - mandelbox explorer - high speed demo version.png +0 -0
- package/static/images/butterchurn-screenshots/martin - mucus cervix.png +0 -0
- package/static/images/butterchurn-screenshots/martin - reflections on black tiles.png +0 -0
- package/static/images/butterchurn-screenshots/martin - stormy sea _2010 update_.png +0 -0
- package/static/images/butterchurn-screenshots/martin - witchcraft reloaded.png +0 -0
- package/static/images/butterchurn-screenshots/martin _ flexi - diamond cutter _prismaticvortex_com_ - camille - i wish i wish i wish i was constrained.png +0 -0
- package/static/images/butterchurn-screenshots/martin _shadow harlequins shape code_ - fata morgana.png +0 -0
- package/static/images/butterchurn-screenshots/martin_ flexi_ fishbrain _ sto - enterstate _random mashup_.png +0 -0
- package/static/images/butterchurn-screenshots/sawtooth grin roam.png +0 -0
- package/static/images/butterchurn-screenshots/shifter - dark tides bdrv mix 2.png +0 -0
- package/static/images/butterchurn-screenshots/suksma - Rovastar - Sunflower Passion _Enlightment Mix__Phat_edit _ flexi und martin shaders - circumflex in character classes in regular expression.png +0 -0
- package/static/images/butterchurn-screenshots/suksma - heretical crosscut playpen.png +0 -0
- package/static/images/butterchurn-screenshots/suksma - uninitialized variabowl _hydroponic chronic_.png +0 -0
- package/static/images/butterchurn-screenshots/suksma - vector exp 1 - couldn_t not.png +0 -0
- package/static/images/butterchurn-screenshots/yin - 191 - Temporal singularities.png +0 -0
- package/static/images/logo-512.png +0 -0
- package/static/images/logo.png +0 -0
- package/static/loukai-logo.png +0 -0
- package/static/screenshot-generator.html +610 -0
|
@@ -0,0 +1,1200 @@
|
|
|
1
|
+
import { PlayerInterface } from './PlayerInterface.js';
|
|
2
|
+
import { MicrophoneEngine } from './microphoneEngine.js';
|
|
3
|
+
|
|
4
|
+
export class KAIPlayer extends PlayerInterface {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(); // Call PlayerInterface constructor
|
|
7
|
+
|
|
8
|
+
// Dual audio contexts for PA and IEM outputs
|
|
9
|
+
this.audioContexts = {
|
|
10
|
+
PA: null,
|
|
11
|
+
IEM: null,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// Device IDs for PA and IEM outputs
|
|
15
|
+
this.outputDevices = {
|
|
16
|
+
PA: 'default',
|
|
17
|
+
IEM: 'default',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// Note: this.isPlaying is inherited from PlayerInterface
|
|
21
|
+
this.currentPosition = 0;
|
|
22
|
+
this.songData = null;
|
|
23
|
+
this.audioBuffers = new Map();
|
|
24
|
+
|
|
25
|
+
// Separate gain nodes and sources for each output
|
|
26
|
+
this.outputNodes = {
|
|
27
|
+
PA: {
|
|
28
|
+
sourceNodes: new Map(),
|
|
29
|
+
gainNodes: new Map(),
|
|
30
|
+
masterGain: null,
|
|
31
|
+
analyser: null, // For butterchurn visualization
|
|
32
|
+
vocalsPAGain: null, // For backup:PA feature - vocals to PA routing
|
|
33
|
+
},
|
|
34
|
+
IEM: {
|
|
35
|
+
sourceNodes: new Map(),
|
|
36
|
+
gainNodes: new Map(),
|
|
37
|
+
masterGain: null,
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Track whether vocals should play through PA (for backup:PA lines)
|
|
42
|
+
this.vocalsPAEnabled = false;
|
|
43
|
+
|
|
44
|
+
this.startTime = 0;
|
|
45
|
+
this.pauseTime = 0;
|
|
46
|
+
|
|
47
|
+
// Note: this.onSongEndedCallback is inherited from PlayerInterface
|
|
48
|
+
|
|
49
|
+
// Microphone engine (handles all mic/auto-tune functionality)
|
|
50
|
+
this.micEngine = null; // Will be initialized after audio contexts are created
|
|
51
|
+
|
|
52
|
+
this.mixerState = {
|
|
53
|
+
// Simple 3-fader mixer
|
|
54
|
+
PA: {
|
|
55
|
+
gain: 0, // dB
|
|
56
|
+
muted: false,
|
|
57
|
+
},
|
|
58
|
+
IEM: {
|
|
59
|
+
gain: 0, // dB
|
|
60
|
+
muted: true, // Default muted - user must explicitly enable IEM monitoring
|
|
61
|
+
mono: true, // Always mono for single-ear monitoring
|
|
62
|
+
},
|
|
63
|
+
mic: {
|
|
64
|
+
gain: 0, // dB
|
|
65
|
+
muted: true, // Default muted - user must explicitly enable mic
|
|
66
|
+
},
|
|
67
|
+
// Per-song data (for internal use)
|
|
68
|
+
stems: [],
|
|
69
|
+
// Mic routing settings (deprecated - now in micEngine, kept for compatibility)
|
|
70
|
+
micToSpeakers: true,
|
|
71
|
+
enableMic: true,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// Note: this.stateReportInterval is inherited from PlayerInterface
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async initialize() {
|
|
78
|
+
try {
|
|
79
|
+
// Get available devices first for label-based matching
|
|
80
|
+
const availableDevices = await this.getAvailableOutputDevices();
|
|
81
|
+
|
|
82
|
+
// Load and resolve device preferences (matches by label, falls back to ID)
|
|
83
|
+
await this.loadDevicePreferences(availableDevices);
|
|
84
|
+
|
|
85
|
+
// Initialize PA audio context with resolved device
|
|
86
|
+
const paContextOptions = {};
|
|
87
|
+
if (this.outputDevices.PA !== 'default' && 'sinkId' in AudioContext.prototype) {
|
|
88
|
+
paContextOptions.sinkId = this.outputDevices.PA;
|
|
89
|
+
}
|
|
90
|
+
this.audioContexts.PA = new (window.AudioContext || window.webkitAudioContext)(
|
|
91
|
+
paContextOptions
|
|
92
|
+
);
|
|
93
|
+
this.outputNodes.PA.masterGain = this.audioContexts.PA.createGain();
|
|
94
|
+
this.outputNodes.PA.masterGain.connect(this.audioContexts.PA.destination);
|
|
95
|
+
// Apply saved PA gain (considering mute state)
|
|
96
|
+
const paGain = this.mixerState.PA.muted ? 0 : this.dbToLinear(this.mixerState.PA.gain);
|
|
97
|
+
this.outputNodes.PA.masterGain.gain.value = paGain;
|
|
98
|
+
|
|
99
|
+
// Create analyser for butterchurn visualization (before gain affects signal)
|
|
100
|
+
this.outputNodes.PA.analyser = this.audioContexts.PA.createAnalyser();
|
|
101
|
+
this.outputNodes.PA.analyser.fftSize = 2048;
|
|
102
|
+
this.outputNodes.PA.analyser.smoothingTimeConstant = 0.8;
|
|
103
|
+
|
|
104
|
+
// Create vocalsPAGain node for backup:PA feature (vocals to PA routing, muted by default)
|
|
105
|
+
this.outputNodes.PA.vocalsPAGain = this.audioContexts.PA.createGain();
|
|
106
|
+
this.outputNodes.PA.vocalsPAGain.gain.value = 0; // Muted by default
|
|
107
|
+
this.outputNodes.PA.vocalsPAGain.connect(this.outputNodes.PA.masterGain);
|
|
108
|
+
|
|
109
|
+
// Initialize IEM audio context with validated device
|
|
110
|
+
const iemContextOptions = {};
|
|
111
|
+
if (this.outputDevices.IEM !== 'default' && 'sinkId' in AudioContext.prototype) {
|
|
112
|
+
iemContextOptions.sinkId = this.outputDevices.IEM;
|
|
113
|
+
}
|
|
114
|
+
this.audioContexts.IEM = new (window.AudioContext || window.webkitAudioContext)(
|
|
115
|
+
iemContextOptions
|
|
116
|
+
);
|
|
117
|
+
this.outputNodes.IEM.masterGain = this.audioContexts.IEM.createGain();
|
|
118
|
+
this.outputNodes.IEM.masterGain.connect(this.audioContexts.IEM.destination);
|
|
119
|
+
// Apply saved IEM gain (considering mute state)
|
|
120
|
+
const iemGain = this.mixerState.IEM.muted ? 0 : this.dbToLinear(this.mixerState.IEM.gain);
|
|
121
|
+
this.outputNodes.IEM.masterGain.gain.value = iemGain;
|
|
122
|
+
|
|
123
|
+
// Initialize microphone engine
|
|
124
|
+
this.micEngine = new MicrophoneEngine(this.audioContexts.PA, this.outputNodes.PA.masterGain, {
|
|
125
|
+
getCurrentPosition: () => this.getCurrentPosition(),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Load auto-tune worklets
|
|
129
|
+
await this.micEngine.loadAutoTuneWorklet();
|
|
130
|
+
|
|
131
|
+
// Load mic settings
|
|
132
|
+
await this.loadMicSettings();
|
|
133
|
+
|
|
134
|
+
// Start microphone if enabled (after reinitialize, mic needs to be restarted)
|
|
135
|
+
if (this.micEngine.enableMic) {
|
|
136
|
+
await this.micEngine.startMicrophoneInput(this.micEngine.inputDevice);
|
|
137
|
+
|
|
138
|
+
// Apply saved mic gain and mute state
|
|
139
|
+
const linearGain = this.mixerState.mic.muted
|
|
140
|
+
? 0
|
|
141
|
+
: this.dbToLinear(this.mixerState.mic.gain);
|
|
142
|
+
this.micEngine.setMicrophoneGain(linearGain);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return true;
|
|
146
|
+
} catch (error) {
|
|
147
|
+
console.warn('⚠️ Audio initialization issue:', error.message);
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Find a device by matching label first, then ID as fallback
|
|
154
|
+
* @param {MediaDeviceInfo[]} availableDevices - List of available devices
|
|
155
|
+
* @param {Object} savedPref - Saved preference with id and name properties
|
|
156
|
+
* @param {string} busType - Bus type for logging (PA, IEM, input)
|
|
157
|
+
* @returns {string} Resolved device ID or 'default'
|
|
158
|
+
*/
|
|
159
|
+
resolveDeviceByLabel(availableDevices, savedPref, busType) {
|
|
160
|
+
if (!savedPref || (!savedPref.id && !savedPref.name)) {
|
|
161
|
+
return 'default';
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Skip if explicitly set to default
|
|
165
|
+
if (savedPref.deviceKind === 'default' || savedPref.id === '') {
|
|
166
|
+
return 'default';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Try to match by label/name first (most reliable across reconnections)
|
|
170
|
+
if (savedPref.name) {
|
|
171
|
+
const byLabel = availableDevices.find((d) => d.label === savedPref.name);
|
|
172
|
+
if (byLabel) {
|
|
173
|
+
if (byLabel.deviceId !== savedPref.id) {
|
|
174
|
+
console.log(`🎧 ${busType}: Matched "${savedPref.name}" by label (ID changed)`);
|
|
175
|
+
}
|
|
176
|
+
return byLabel.deviceId;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Fall back to ID match
|
|
181
|
+
if (savedPref.id) {
|
|
182
|
+
const byId = availableDevices.find((d) => d.deviceId === savedPref.id);
|
|
183
|
+
if (byId) {
|
|
184
|
+
return byId.deviceId;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Device not found - use default
|
|
189
|
+
console.warn(
|
|
190
|
+
`🎧 ${busType}: Saved device "${savedPref.name || savedPref.id}" not found, using default`
|
|
191
|
+
);
|
|
192
|
+
return 'default';
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async loadDevicePreferences(availableOutputDevices = []) {
|
|
196
|
+
try {
|
|
197
|
+
// Load device preferences from settingsAPI
|
|
198
|
+
if (window.kaiAPI.settings) {
|
|
199
|
+
const devicePrefs = await window.kaiAPI.settings.get('devicePreferences', null);
|
|
200
|
+
|
|
201
|
+
if (devicePrefs?.PA) {
|
|
202
|
+
this.outputDevices.PA = this.resolveDeviceByLabel(
|
|
203
|
+
availableOutputDevices,
|
|
204
|
+
devicePrefs.PA,
|
|
205
|
+
'PA'
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
if (devicePrefs?.IEM) {
|
|
209
|
+
this.outputDevices.IEM = this.resolveDeviceByLabel(
|
|
210
|
+
availableOutputDevices,
|
|
211
|
+
devicePrefs.IEM,
|
|
212
|
+
'IEM'
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
if (devicePrefs?.input) {
|
|
216
|
+
// For input devices, get input device list
|
|
217
|
+
const inputDevices = await this.getAvailableInputDevices();
|
|
218
|
+
this.inputDevice = this.resolveDeviceByLabel(inputDevices, devicePrefs.input, 'input');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Load mixer state from AppState
|
|
223
|
+
if (window.kaiAPI?.app) {
|
|
224
|
+
const appState = await window.kaiAPI.app.getState();
|
|
225
|
+
|
|
226
|
+
// Load mixer state from AppState
|
|
227
|
+
if (appState?.mixer) {
|
|
228
|
+
if (typeof appState.mixer.PA?.gain === 'number') {
|
|
229
|
+
this.mixerState.PA.gain = appState.mixer.PA.gain;
|
|
230
|
+
}
|
|
231
|
+
if (typeof appState.mixer.PA?.muted === 'boolean') {
|
|
232
|
+
this.mixerState.PA.muted = appState.mixer.PA.muted;
|
|
233
|
+
}
|
|
234
|
+
if (typeof appState.mixer.IEM?.gain === 'number') {
|
|
235
|
+
this.mixerState.IEM.gain = appState.mixer.IEM.gain;
|
|
236
|
+
}
|
|
237
|
+
if (typeof appState.mixer.IEM?.muted === 'boolean') {
|
|
238
|
+
this.mixerState.IEM.muted = appState.mixer.IEM.muted;
|
|
239
|
+
}
|
|
240
|
+
if (typeof appState.mixer.mic?.gain === 'number') {
|
|
241
|
+
this.mixerState.mic.gain = appState.mixer.mic.gain;
|
|
242
|
+
}
|
|
243
|
+
if (typeof appState.mixer.mic?.muted === 'boolean') {
|
|
244
|
+
this.mixerState.mic.muted = appState.mixer.mic.muted;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
} catch (error) {
|
|
249
|
+
console.warn('Failed to load device preferences:', error.message);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Get list of available audio output devices
|
|
255
|
+
* @returns {Promise<MediaDeviceInfo[]>}
|
|
256
|
+
*/
|
|
257
|
+
async getAvailableOutputDevices() {
|
|
258
|
+
try {
|
|
259
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
260
|
+
return devices.filter((d) => d.kind === 'audiooutput');
|
|
261
|
+
} catch (error) {
|
|
262
|
+
console.warn('Failed to enumerate output devices:', error.message);
|
|
263
|
+
return [];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Get list of available audio input devices
|
|
269
|
+
* @returns {Promise<MediaDeviceInfo[]>}
|
|
270
|
+
*/
|
|
271
|
+
async getAvailableInputDevices() {
|
|
272
|
+
try {
|
|
273
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
274
|
+
return devices.filter((d) => d.kind === 'audioinput');
|
|
275
|
+
} catch (error) {
|
|
276
|
+
console.warn('Failed to enumerate input devices:', error.message);
|
|
277
|
+
return [];
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async setOutputDevice(busType, deviceId) {
|
|
282
|
+
try {
|
|
283
|
+
if (!['PA', 'IEM'].includes(busType)) {
|
|
284
|
+
console.error('Invalid bus type:', busType);
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const wasPlaying = this.isPlaying;
|
|
289
|
+
|
|
290
|
+
// Stop current playback if running (pause() will update currentPosition to actual position)
|
|
291
|
+
if (wasPlaying) {
|
|
292
|
+
this.pause();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Capture position AFTER pause, which has the accurate current position
|
|
296
|
+
const currentPos = this.currentPosition;
|
|
297
|
+
|
|
298
|
+
// Store the device preference
|
|
299
|
+
this.outputDevices[busType] = deviceId;
|
|
300
|
+
|
|
301
|
+
// Close existing context for this bus
|
|
302
|
+
if (this.audioContexts[busType]) {
|
|
303
|
+
await this.audioContexts[busType].close();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Create new context with proper device
|
|
307
|
+
const contextOptions = {};
|
|
308
|
+
if (deviceId !== 'default' && 'sinkId' in AudioContext.prototype) {
|
|
309
|
+
contextOptions.sinkId = deviceId;
|
|
310
|
+
} else {
|
|
311
|
+
// Use default device - no additional config needed
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
this.audioContexts[busType] = new (window.AudioContext || window.webkitAudioContext)(
|
|
315
|
+
contextOptions
|
|
316
|
+
);
|
|
317
|
+
this.outputNodes[busType].masterGain = this.audioContexts[busType].createGain();
|
|
318
|
+
this.outputNodes[busType].masterGain.connect(this.audioContexts[busType].destination);
|
|
319
|
+
|
|
320
|
+
// Reapply saved gain (considering mute state)
|
|
321
|
+
const savedGain = this.mixerState[busType].muted
|
|
322
|
+
? 0
|
|
323
|
+
: this.dbToLinear(this.mixerState[busType].gain);
|
|
324
|
+
this.outputNodes[busType].masterGain.gain.value = savedGain;
|
|
325
|
+
|
|
326
|
+
// Create analyser for PA (for butterchurn visualization)
|
|
327
|
+
if (busType === 'PA') {
|
|
328
|
+
this.outputNodes.PA.analyser = this.audioContexts.PA.createAnalyser();
|
|
329
|
+
this.outputNodes.PA.analyser.fftSize = 2048;
|
|
330
|
+
this.outputNodes.PA.analyser.smoothingTimeConstant = 0.8;
|
|
331
|
+
|
|
332
|
+
// Recreate vocalsPAGain node for backup:PA feature
|
|
333
|
+
this.outputNodes.PA.vocalsPAGain = this.audioContexts.PA.createGain();
|
|
334
|
+
this.outputNodes.PA.vocalsPAGain.gain.value = this.vocalsPAEnabled
|
|
335
|
+
? this.dbToLinear(this.mixerState.IEM.gain)
|
|
336
|
+
: 0;
|
|
337
|
+
this.outputNodes.PA.vocalsPAGain.connect(this.outputNodes.PA.masterGain);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Clear old audio nodes
|
|
341
|
+
this.outputNodes[busType].sourceNodes.clear();
|
|
342
|
+
this.outputNodes[busType].gainNodes.clear();
|
|
343
|
+
|
|
344
|
+
// If PA context was recreated, update microphone engine
|
|
345
|
+
if (busType === 'PA' && this.micEngine) {
|
|
346
|
+
// console.log('[AutoTune] PA context recreated, updating microphone engine...');
|
|
347
|
+
this.micEngine.updateAudioContext(this.audioContexts.PA, this.outputNodes.PA.masterGain);
|
|
348
|
+
await this.micEngine.loadAutoTuneWorklet();
|
|
349
|
+
|
|
350
|
+
// Restart microphone if it was enabled
|
|
351
|
+
if (this.micEngine.enableMic) {
|
|
352
|
+
await this.micEngine.startMicrophoneInput(this.micEngine.inputDevice);
|
|
353
|
+
|
|
354
|
+
// Reapply saved mic gain and mute state
|
|
355
|
+
const linearGain = this.mixerState.mic.muted
|
|
356
|
+
? 0
|
|
357
|
+
: this.dbToLinear(this.mixerState.mic.gain);
|
|
358
|
+
this.micEngine.setMicrophoneGain(linearGain);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Reload audio buffers for the new context (audio buffers are context-specific)
|
|
363
|
+
if (this.songData) {
|
|
364
|
+
await this.reloadAudioBuffersForBus(busType);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Resume playback if it was playing
|
|
368
|
+
if (wasPlaying && this.songData) {
|
|
369
|
+
this.currentPosition = currentPos;
|
|
370
|
+
await this.play();
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return true;
|
|
374
|
+
} catch (error) {
|
|
375
|
+
console.error(`Failed to set ${busType} output device:`, error);
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
async loadSong(songData) {
|
|
381
|
+
this.songData = songData;
|
|
382
|
+
|
|
383
|
+
// Reset position using base class method
|
|
384
|
+
this.resetPosition();
|
|
385
|
+
|
|
386
|
+
// Reset engine-specific timing state
|
|
387
|
+
this.currentPosition = 0;
|
|
388
|
+
this.startTime = 0;
|
|
389
|
+
this.pauseTime = 0;
|
|
390
|
+
this.monitoringStartTime = null;
|
|
391
|
+
|
|
392
|
+
// Stop any existing song end monitoring
|
|
393
|
+
this.stopSongEndMonitoring();
|
|
394
|
+
|
|
395
|
+
// Keep stems array for tracking audio sources (for internal use only)
|
|
396
|
+
// Routing is automatic: vocals → IEM (mono), instrumental → PA, mic → PA
|
|
397
|
+
this.mixerState.stems = (songData.audio?.sources || []).map((source, index) => ({
|
|
398
|
+
id: source.name || source.filename,
|
|
399
|
+
name: source.name || source.filename,
|
|
400
|
+
gain: source.gain || 0, // Per-source gain (still useful for balancing)
|
|
401
|
+
index,
|
|
402
|
+
}));
|
|
403
|
+
|
|
404
|
+
await this.loadAudioBuffers(songData);
|
|
405
|
+
|
|
406
|
+
// Clear any previous pitch reference data
|
|
407
|
+
// Note: Pitch detection is now done in real-time from the vocal stem
|
|
408
|
+
if (this.micEngine) {
|
|
409
|
+
this.micEngine.clearPitchReference();
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// Report song loaded to main process
|
|
413
|
+
this.reportSongLoaded();
|
|
414
|
+
|
|
415
|
+
// Report initial playback state with position reset to 0
|
|
416
|
+
this.reportStateChange();
|
|
417
|
+
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Note: reportStateChange(), startStateReporting(), and stopStateReporting()
|
|
423
|
+
* are inherited from PlayerInterface base class
|
|
424
|
+
*/
|
|
425
|
+
|
|
426
|
+
reportSongLoaded() {
|
|
427
|
+
if (window.kaiAPI?.renderer && this.songData) {
|
|
428
|
+
const duration = this.getDuration();
|
|
429
|
+
window.kaiAPI.renderer.songLoaded({
|
|
430
|
+
path: this.songData.originalFilePath || this.songData.filePath,
|
|
431
|
+
title: this.songData.metadata?.title || 'Unknown',
|
|
432
|
+
artist: this.songData.metadata?.artist || 'Unknown',
|
|
433
|
+
duration: duration,
|
|
434
|
+
isLoading: false, // Song is fully loaded
|
|
435
|
+
format: 'kai',
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
// Report initial mixer state
|
|
439
|
+
this.reportMixerState();
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
reportMixerState() {
|
|
444
|
+
if (window.kaiAPI?.renderer && this.mixerState) {
|
|
445
|
+
window.kaiAPI.renderer.updateMixerState(this.mixerState);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
async loadAudioBuffers(songData) {
|
|
450
|
+
if (!songData.audio?.sources) {
|
|
451
|
+
console.warn('No audio sources found in song data');
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (!this.audioContexts.PA || !this.audioContexts.IEM) {
|
|
456
|
+
await this.initialize();
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
for (const source of songData.audio.sources) {
|
|
460
|
+
try {
|
|
461
|
+
if (source.audioData && source.audioData.length > 0) {
|
|
462
|
+
const arrayBuffer = source.audioData.buffer.slice(
|
|
463
|
+
source.audioData.byteOffset,
|
|
464
|
+
source.audioData.byteOffset + source.audioData.byteLength
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
// Sequential audio buffer decoding to avoid overwhelming WebAudio API
|
|
468
|
+
// eslint-disable-next-line no-await-in-loop
|
|
469
|
+
const decodedBuffer = await this.audioContexts.PA.decodeAudioData(arrayBuffer);
|
|
470
|
+
this.audioBuffers.set(source.name, decodedBuffer);
|
|
471
|
+
} else {
|
|
472
|
+
console.warn(`No audio data for source: ${source.name}`);
|
|
473
|
+
}
|
|
474
|
+
} catch (error) {
|
|
475
|
+
console.error(`Failed to decode audio for ${source.name}:`, error);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// Calculate the actual duration from the longest audio buffer
|
|
480
|
+
let maxDuration = 0;
|
|
481
|
+
for (const [_name, buffer] of this.audioBuffers) {
|
|
482
|
+
if (buffer.duration > maxDuration) {
|
|
483
|
+
maxDuration = buffer.duration;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Update the song metadata with the actual duration
|
|
488
|
+
if (maxDuration > 0) {
|
|
489
|
+
if (!this.songData.metadata) {
|
|
490
|
+
this.songData.metadata = {};
|
|
491
|
+
}
|
|
492
|
+
this.songData.metadata.duration = maxDuration;
|
|
493
|
+
} else {
|
|
494
|
+
console.warn('No audio buffers loaded, duration remains 0');
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
async reloadAudioBuffersForBus(busType) {
|
|
499
|
+
if (!this.songData?.audio?.sources || !this.audioContexts[busType]) {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// Audio buffers are shared between contexts, but we need to re-decode for new context
|
|
504
|
+
// The existing buffers in this.audioBuffers should still work, but let's make sure
|
|
505
|
+
// the new context has access to them by re-decoding if needed
|
|
506
|
+
|
|
507
|
+
for (const source of this.songData.audio.sources) {
|
|
508
|
+
if (source.audioData && source.audioData.length > 0) {
|
|
509
|
+
try {
|
|
510
|
+
// Check if we already have this buffer
|
|
511
|
+
if (!this.audioBuffers.has(source.name)) {
|
|
512
|
+
const arrayBuffer = source.audioData.buffer.slice(
|
|
513
|
+
source.audioData.byteOffset,
|
|
514
|
+
source.audioData.byteOffset + source.audioData.byteLength
|
|
515
|
+
);
|
|
516
|
+
|
|
517
|
+
// Sequential buffer decoding for new audio context to avoid WebAudio API errors
|
|
518
|
+
// eslint-disable-next-line no-await-in-loop
|
|
519
|
+
const decodedBuffer = await this.audioContexts[busType].decodeAudioData(arrayBuffer);
|
|
520
|
+
this.audioBuffers.set(source.name, decodedBuffer);
|
|
521
|
+
}
|
|
522
|
+
} catch (error) {
|
|
523
|
+
console.error(`Failed to reload audio buffer for ${busType} - ${source.name}:`, error);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
async play() {
|
|
530
|
+
if (!this.songData) {
|
|
531
|
+
console.error('No song loaded');
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (!this.audioContexts.PA || !this.audioContexts.IEM) {
|
|
536
|
+
console.error('Audio contexts not initialized');
|
|
537
|
+
return false;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (this.audioContexts.PA.state === 'suspended') {
|
|
541
|
+
await this.audioContexts.PA.resume();
|
|
542
|
+
}
|
|
543
|
+
if (this.audioContexts.IEM.state === 'suspended') {
|
|
544
|
+
await this.audioContexts.IEM.resume();
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
this.isPlaying = true;
|
|
548
|
+
|
|
549
|
+
this.stopAllSources();
|
|
550
|
+
this.createAudioGraph();
|
|
551
|
+
this.startAudioSources();
|
|
552
|
+
|
|
553
|
+
// Start song end monitoring
|
|
554
|
+
this.startSongEndMonitoring();
|
|
555
|
+
|
|
556
|
+
// Start state reporting
|
|
557
|
+
this.startStateReporting();
|
|
558
|
+
|
|
559
|
+
// Update microphone engine playing state
|
|
560
|
+
if (this.micEngine) {
|
|
561
|
+
this.micEngine.setPlaying(true);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Report immediate state change
|
|
565
|
+
this.reportStateChange();
|
|
566
|
+
|
|
567
|
+
return true;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
pause() {
|
|
571
|
+
// Save current playback position BEFORE setting isPlaying to false
|
|
572
|
+
// (getCurrentPosition uses isPlaying to calculate position)
|
|
573
|
+
this.currentPosition = this.getCurrentPosition();
|
|
574
|
+
|
|
575
|
+
this.isPlaying = false;
|
|
576
|
+
|
|
577
|
+
if (this.audioContexts.PA) {
|
|
578
|
+
this.pauseTime = this.audioContexts.PA.currentTime;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
this.stopAllSources();
|
|
582
|
+
|
|
583
|
+
// Stop song end monitoring
|
|
584
|
+
this.stopSongEndMonitoring();
|
|
585
|
+
|
|
586
|
+
// Stop state reporting
|
|
587
|
+
this.stopStateReporting();
|
|
588
|
+
|
|
589
|
+
// Update microphone engine playing state
|
|
590
|
+
if (this.micEngine) {
|
|
591
|
+
this.micEngine.setPlaying(false);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// Report immediate state change
|
|
595
|
+
this.reportStateChange();
|
|
596
|
+
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
seek(positionSec) {
|
|
601
|
+
this.currentPosition = positionSec;
|
|
602
|
+
|
|
603
|
+
if (this.isPlaying) {
|
|
604
|
+
this.stopAllSources();
|
|
605
|
+
this.startAudioSources();
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// Report immediate state change
|
|
609
|
+
this.reportStateChange();
|
|
610
|
+
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
stopAllSources() {
|
|
615
|
+
const _totalSources =
|
|
616
|
+
this.outputNodes.PA.sourceNodes.size + this.outputNodes.IEM.sourceNodes.size;
|
|
617
|
+
|
|
618
|
+
// Stop PA sources
|
|
619
|
+
this.outputNodes.PA.sourceNodes.forEach((source, _index) => {
|
|
620
|
+
try {
|
|
621
|
+
source.stop();
|
|
622
|
+
source.disconnect(); // Disconnect all connections
|
|
623
|
+
} catch {
|
|
624
|
+
// Source may already be stopped
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
this.outputNodes.PA.sourceNodes.clear();
|
|
628
|
+
|
|
629
|
+
// Stop IEM sources
|
|
630
|
+
this.outputNodes.IEM.sourceNodes.forEach((source, _index) => {
|
|
631
|
+
try {
|
|
632
|
+
source.stop();
|
|
633
|
+
source.disconnect(); // Disconnect all connections
|
|
634
|
+
} catch {
|
|
635
|
+
// Source may already be stopped
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
this.outputNodes.IEM.sourceNodes.clear();
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
createAudioGraph() {
|
|
642
|
+
if (!this.audioContexts.PA || !this.audioContexts.IEM) return;
|
|
643
|
+
|
|
644
|
+
// Clear existing gain nodes for both outputs
|
|
645
|
+
this.outputNodes.PA.gainNodes.clear();
|
|
646
|
+
this.outputNodes.IEM.gainNodes.clear();
|
|
647
|
+
|
|
648
|
+
this.mixerState.stems.forEach((stem) => {
|
|
649
|
+
// Create gain node for PA output
|
|
650
|
+
const paGainNode = this.audioContexts.PA.createGain();
|
|
651
|
+
paGainNode.connect(this.outputNodes.PA.masterGain);
|
|
652
|
+
this.outputNodes.PA.gainNodes.set(stem.name, paGainNode);
|
|
653
|
+
|
|
654
|
+
// Create gain node for IEM output
|
|
655
|
+
const iemGainNode = this.audioContexts.IEM.createGain();
|
|
656
|
+
|
|
657
|
+
// For vocal stems, add mono conversion if enabled
|
|
658
|
+
if (this.isVocalStem(stem.name) && this.mixerState.iemMonoVocals) {
|
|
659
|
+
// Create channel merger to convert stereo to mono
|
|
660
|
+
const channelMerger = this.audioContexts.IEM.createChannelMerger(1);
|
|
661
|
+
iemGainNode.connect(channelMerger);
|
|
662
|
+
channelMerger.connect(this.outputNodes.IEM.masterGain);
|
|
663
|
+
} else {
|
|
664
|
+
iemGainNode.connect(this.outputNodes.IEM.masterGain);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
this.outputNodes.IEM.gainNodes.set(stem.name, iemGainNode);
|
|
668
|
+
|
|
669
|
+
this.updateStemGain(stem);
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
startAudioSources() {
|
|
674
|
+
if (!this.audioContexts.PA || !this.audioContexts.IEM) return;
|
|
675
|
+
|
|
676
|
+
// Sync start time for both outputs - add small delay to ensure synchronization
|
|
677
|
+
const scheduleTime =
|
|
678
|
+
Math.max(this.audioContexts.PA.currentTime, this.audioContexts.IEM.currentTime) + 0.1;
|
|
679
|
+
this.startTime = scheduleTime;
|
|
680
|
+
|
|
681
|
+
this.mixerState.stems.forEach((stem) => {
|
|
682
|
+
// Skip mixdown stems - they contain the full mix and would overlap with individual stems
|
|
683
|
+
if (this.isMixdownStem(stem.name)) {
|
|
684
|
+
console.log(`⏭️ Skipping mixdown stem: ${stem.name}`);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
const audioBuffer = this.audioBuffers.get(stem.name);
|
|
689
|
+
const paGainNode = this.outputNodes.PA.gainNodes.get(stem.name);
|
|
690
|
+
const iemGainNode = this.outputNodes.IEM.gainNodes.get(stem.name);
|
|
691
|
+
|
|
692
|
+
if (audioBuffer && paGainNode && iemGainNode) {
|
|
693
|
+
try {
|
|
694
|
+
const offset = Math.min(this.currentPosition, audioBuffer.duration);
|
|
695
|
+
const isVocals = this.isVocalStem(stem.name);
|
|
696
|
+
|
|
697
|
+
// Proper karaoke routing: vocals to IEM only, music/backing tracks to PA only
|
|
698
|
+
// Exception: backup:PA feature routes vocals to PA when enabled
|
|
699
|
+
if (isVocals) {
|
|
700
|
+
// Vocals go to IEM (singer's ears)
|
|
701
|
+
const iemSource = this.audioContexts.IEM.createBufferSource();
|
|
702
|
+
iemSource.buffer = audioBuffer;
|
|
703
|
+
iemSource.connect(iemGainNode);
|
|
704
|
+
iemSource.start(scheduleTime, offset);
|
|
705
|
+
this.outputNodes.IEM.sourceNodes.set(stem.name, iemSource);
|
|
706
|
+
|
|
707
|
+
// Also create PA source for backup:PA feature (muted by default via vocalsPAGain)
|
|
708
|
+
if (this.outputNodes.PA.vocalsPAGain) {
|
|
709
|
+
const paSource = this.audioContexts.PA.createBufferSource();
|
|
710
|
+
paSource.buffer = audioBuffer; // Reuse the same decoded buffer
|
|
711
|
+
paSource.connect(this.outputNodes.PA.vocalsPAGain);
|
|
712
|
+
|
|
713
|
+
// Connect vocals to pitch detection for auto-tune reference
|
|
714
|
+
// This enables real-time pitch tracking from the vocal stem
|
|
715
|
+
if (this.micEngine) {
|
|
716
|
+
this.micEngine.connectMusicSource(paSource);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
paSource.start(scheduleTime, offset);
|
|
720
|
+
this.outputNodes.PA.sourceNodes.set(stem.name + '_vocalsPA', paSource);
|
|
721
|
+
}
|
|
722
|
+
} else {
|
|
723
|
+
// Backing tracks go to PA only (audience)
|
|
724
|
+
const paSource = this.audioContexts.PA.createBufferSource();
|
|
725
|
+
paSource.buffer = audioBuffer;
|
|
726
|
+
paSource.connect(paGainNode);
|
|
727
|
+
|
|
728
|
+
// Connect to analyser for butterchurn visualization (before gain affects signal)
|
|
729
|
+
if (this.outputNodes.PA.analyser) {
|
|
730
|
+
paSource.connect(this.outputNodes.PA.analyser);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// If this is a melodic stem, connect to microphone engine for pitch detection
|
|
734
|
+
if (this.isMelodicStem(stem.name) && this.micEngine) {
|
|
735
|
+
this.micEngine.connectMusicSource(paSource);
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
paSource.start(scheduleTime, offset);
|
|
739
|
+
this.outputNodes.PA.sourceNodes.set(stem.name, paSource);
|
|
740
|
+
|
|
741
|
+
// Add onended handler as backup to position monitoring
|
|
742
|
+
paSource.onended = () => {
|
|
743
|
+
if (this.isPlaying) {
|
|
744
|
+
// Let the position monitoring handle the cleanup
|
|
745
|
+
// This serves as a backup in case position monitoring misses it
|
|
746
|
+
setTimeout(() => this.checkForSongEnd(), 10);
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
} catch (error) {
|
|
751
|
+
console.error(`Failed to start source for ${stem.name}:`, error);
|
|
752
|
+
}
|
|
753
|
+
} else {
|
|
754
|
+
console.warn(`No audio buffer or gain nodes for stem: ${stem.name}`);
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
// Debug PA output routing (disabled)
|
|
759
|
+
// this.outputNodes.PA.gainNodes.forEach((gainNode, stemName) => {
|
|
760
|
+
// console.log('PA routing:', stemName, gainNode);
|
|
761
|
+
// });
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
isVocalStem(stemName) {
|
|
765
|
+
const vocalsKeywords = ['vocals', 'vocal', 'voice', 'lead', 'singing', 'vox'];
|
|
766
|
+
const lowerName = stemName.toLowerCase();
|
|
767
|
+
return vocalsKeywords.some((keyword) => lowerName.includes(keyword));
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
isMixdownStem(stemName) {
|
|
771
|
+
// Mixdown stems contain the full mix and should be skipped when individual stems are available
|
|
772
|
+
const mixdownKeywords = ['mixdown', 'mix', 'master', 'full mix', 'stereo mix'];
|
|
773
|
+
const lowerName = stemName.toLowerCase();
|
|
774
|
+
return mixdownKeywords.some(
|
|
775
|
+
(keyword) =>
|
|
776
|
+
lowerName === keyword ||
|
|
777
|
+
lowerName.includes(`_${keyword}`) ||
|
|
778
|
+
lowerName.includes(`${keyword}_`)
|
|
779
|
+
);
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
isMelodicStem(stemName) {
|
|
783
|
+
// Returns true for stems containing melodic instruments (typically "other")
|
|
784
|
+
// These are best for pitch detection as melody reference
|
|
785
|
+
const lowerName = stemName.toLowerCase();
|
|
786
|
+
|
|
787
|
+
// Explicitly melodic stems
|
|
788
|
+
if (
|
|
789
|
+
lowerName.includes('other') ||
|
|
790
|
+
lowerName.includes('music') ||
|
|
791
|
+
lowerName.includes('instrumental') ||
|
|
792
|
+
lowerName.includes('accompaniment') ||
|
|
793
|
+
lowerName.includes('melody')
|
|
794
|
+
) {
|
|
795
|
+
return true;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// Exclude non-melodic stems
|
|
799
|
+
if (this.isVocalStem(stemName)) return false;
|
|
800
|
+
if (lowerName.includes('drum') || lowerName.includes('percussion')) return false;
|
|
801
|
+
if (lowerName.includes('bass')) return false;
|
|
802
|
+
|
|
803
|
+
// Default to true for unknown stems (likely melodic)
|
|
804
|
+
return true;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
updateStemGain(stem) {
|
|
808
|
+
const paGainNode = this.outputNodes.PA.gainNodes.get(stem.name);
|
|
809
|
+
const iemGainNode = this.outputNodes.IEM.gainNodes.get(stem.name);
|
|
810
|
+
const isVocals = this.isVocalStem(stem.name);
|
|
811
|
+
|
|
812
|
+
if (!this.audioContexts.PA || !this.audioContexts.IEM) return;
|
|
813
|
+
|
|
814
|
+
// Convert stem gain from dB to linear (per-stem balancing)
|
|
815
|
+
const baseGain = Math.pow(10, stem.gain / 20);
|
|
816
|
+
|
|
817
|
+
// Simple routing: vocals to IEM, backing tracks to PA
|
|
818
|
+
// Master faders control overall output level
|
|
819
|
+
if (isVocals && iemGainNode) {
|
|
820
|
+
iemGainNode.gain.setValueAtTime(baseGain, this.audioContexts.IEM.currentTime);
|
|
821
|
+
} else if (!isVocals && paGainNode) {
|
|
822
|
+
paGainNode.gain.setValueAtTime(baseGain, this.audioContexts.PA.currentTime);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// New simple mixer controls
|
|
827
|
+
setMasterGain(bus, gainDb) {
|
|
828
|
+
if (!['PA', 'IEM', 'mic'].includes(bus)) return false;
|
|
829
|
+
|
|
830
|
+
this.mixerState[bus].gain = gainDb;
|
|
831
|
+
|
|
832
|
+
// Apply to audio node
|
|
833
|
+
if (bus === 'PA' && this.outputNodes.PA.masterGain) {
|
|
834
|
+
const linearGain = this.dbToLinear(gainDb);
|
|
835
|
+
this.outputNodes.PA.masterGain.gain.setValueAtTime(
|
|
836
|
+
linearGain,
|
|
837
|
+
this.audioContexts.PA.currentTime
|
|
838
|
+
);
|
|
839
|
+
} else if (bus === 'IEM' && this.outputNodes.IEM.masterGain) {
|
|
840
|
+
const linearGain = this.dbToLinear(gainDb);
|
|
841
|
+
this.outputNodes.IEM.masterGain.gain.setValueAtTime(
|
|
842
|
+
linearGain,
|
|
843
|
+
this.audioContexts.IEM.currentTime
|
|
844
|
+
);
|
|
845
|
+
} else if (bus === 'mic' && this.micEngine) {
|
|
846
|
+
// Apply mic gain (considering mute state)
|
|
847
|
+
const linearGain = this.mixerState.mic.muted ? 0 : this.dbToLinear(gainDb);
|
|
848
|
+
this.micEngine.setMicrophoneGain(linearGain);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
// Report to main process (which handles persistence via AppState)
|
|
852
|
+
this.reportMixerState();
|
|
853
|
+
return true;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
toggleMasterMute(bus) {
|
|
857
|
+
if (!['PA', 'IEM', 'mic'].includes(bus)) return false;
|
|
858
|
+
|
|
859
|
+
this.mixerState[bus].muted = !this.mixerState[bus].muted;
|
|
860
|
+
const muted = this.mixerState[bus].muted;
|
|
861
|
+
|
|
862
|
+
// Apply mute (set gain to 0 or restore)
|
|
863
|
+
if (bus === 'PA' && this.outputNodes.PA.masterGain) {
|
|
864
|
+
const gain = muted ? 0 : this.dbToLinear(this.mixerState.PA.gain);
|
|
865
|
+
this.outputNodes.PA.masterGain.gain.setValueAtTime(gain, this.audioContexts.PA.currentTime);
|
|
866
|
+
} else if (bus === 'IEM' && this.outputNodes.IEM.masterGain) {
|
|
867
|
+
const gain = muted ? 0 : this.dbToLinear(this.mixerState.IEM.gain);
|
|
868
|
+
this.outputNodes.IEM.masterGain.gain.setValueAtTime(gain, this.audioContexts.IEM.currentTime);
|
|
869
|
+
} else if (bus === 'mic' && this.micEngine) {
|
|
870
|
+
const gain = muted ? 0 : this.dbToLinear(this.mixerState.mic.gain);
|
|
871
|
+
this.micEngine.setMicrophoneGain(gain);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// Report to main process (which handles persistence via AppState)
|
|
875
|
+
this.reportMixerState();
|
|
876
|
+
return true;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
setMasterMute(bus, muted) {
|
|
880
|
+
if (!['PA', 'IEM', 'mic'].includes(bus)) return false;
|
|
881
|
+
|
|
882
|
+
this.mixerState[bus].muted = muted;
|
|
883
|
+
|
|
884
|
+
// Apply mute (set gain to 0 or restore)
|
|
885
|
+
if (bus === 'PA' && this.outputNodes.PA.masterGain) {
|
|
886
|
+
const gain = muted ? 0 : this.dbToLinear(this.mixerState.PA.gain);
|
|
887
|
+
this.outputNodes.PA.masterGain.gain.setValueAtTime(gain, this.audioContexts.PA.currentTime);
|
|
888
|
+
} else if (bus === 'IEM' && this.outputNodes.IEM.masterGain) {
|
|
889
|
+
const gain = muted ? 0 : this.dbToLinear(this.mixerState.IEM.gain);
|
|
890
|
+
this.outputNodes.IEM.masterGain.gain.setValueAtTime(gain, this.audioContexts.IEM.currentTime);
|
|
891
|
+
} else if (bus === 'mic' && this.micEngine) {
|
|
892
|
+
const gain = muted ? 0 : this.dbToLinear(this.mixerState.mic.gain);
|
|
893
|
+
this.micEngine.setMicrophoneGain(gain);
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// Don't report back to main - this was initiated by main/admin
|
|
897
|
+
return true;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* Enable/disable vocals routing to PA (for backup:PA feature)
|
|
902
|
+
* When a lyric line has singer="backup:PA", the original vocals should play through PA
|
|
903
|
+
* @param {boolean} enabled - Whether to route vocals to PA
|
|
904
|
+
* @param {number} fadeTime - Fade duration in seconds (default 50ms to avoid clicks)
|
|
905
|
+
*/
|
|
906
|
+
setVocalsPAEnabled(enabled, fadeTime = 0.05) {
|
|
907
|
+
if (!this.outputNodes.PA.vocalsPAGain || !this.audioContexts.PA) return;
|
|
908
|
+
|
|
909
|
+
// Avoid redundant changes
|
|
910
|
+
if (this.vocalsPAEnabled === enabled) return;
|
|
911
|
+
this.vocalsPAEnabled = enabled;
|
|
912
|
+
|
|
913
|
+
// Use IEM gain as reference for vocals volume (since that's where vocals normally go)
|
|
914
|
+
const targetGain = enabled ? this.dbToLinear(this.mixerState.IEM.gain) : 0;
|
|
915
|
+
|
|
916
|
+
// Smooth fade to avoid clicks/pops
|
|
917
|
+
const currentTime = this.audioContexts.PA.currentTime;
|
|
918
|
+
this.outputNodes.PA.vocalsPAGain.gain.cancelScheduledValues(currentTime);
|
|
919
|
+
this.outputNodes.PA.vocalsPAGain.gain.linearRampToValueAtTime(
|
|
920
|
+
targetGain,
|
|
921
|
+
currentTime + fadeTime
|
|
922
|
+
);
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
// Preset system removed - routing is now automatic with master faders
|
|
926
|
+
// Vocals → IEM (mono), Instrumental → PA, Mic → PA
|
|
927
|
+
|
|
928
|
+
getCurrentPosition() {
|
|
929
|
+
if (this.isPlaying && this.audioContexts.PA && this.startTime > 0) {
|
|
930
|
+
const elapsed = this.audioContexts.PA.currentTime - this.startTime;
|
|
931
|
+
const calculatedPosition = this.currentPosition + elapsed;
|
|
932
|
+
|
|
933
|
+
// Don't let position exceed song duration
|
|
934
|
+
const duration = this.getDuration();
|
|
935
|
+
const clampedPosition =
|
|
936
|
+
duration > 0 ? Math.min(calculatedPosition, duration) : calculatedPosition;
|
|
937
|
+
|
|
938
|
+
return clampedPosition;
|
|
939
|
+
}
|
|
940
|
+
return this.currentPosition;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
getCurrentTime() {
|
|
944
|
+
return this.getCurrentPosition();
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
getDuration() {
|
|
948
|
+
return this.songData?.metadata?.duration || 0;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
getMixerState() {
|
|
952
|
+
return {
|
|
953
|
+
PA: this.mixerState.PA,
|
|
954
|
+
IEM: this.mixerState.IEM,
|
|
955
|
+
mic: this.mixerState.mic,
|
|
956
|
+
stems: this.mixerState.stems, // For reference only
|
|
957
|
+
isPlaying: this.isPlaying,
|
|
958
|
+
position: this.getCurrentPosition(),
|
|
959
|
+
duration: this.getDuration(),
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
// Microphone/Auto-tune delegation methods (delegate to MicrophoneEngine)
|
|
964
|
+
|
|
965
|
+
async startMicrophoneInput(deviceId = 'default') {
|
|
966
|
+
if (this.micEngine) {
|
|
967
|
+
await this.micEngine.startMicrophoneInput(deviceId);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
stopMicrophoneInput() {
|
|
972
|
+
if (this.micEngine) {
|
|
973
|
+
this.micEngine.stopMicrophoneInput();
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
setAutoTuneSettings(settings) {
|
|
978
|
+
if (this.micEngine) {
|
|
979
|
+
this.micEngine.setAutoTuneSettings(settings);
|
|
980
|
+
|
|
981
|
+
// Reapply gain after auto-tune enable/disable reconnects the audio chain
|
|
982
|
+
if (this.micEngine.microphoneGain && Object.hasOwn(settings, 'enabled')) {
|
|
983
|
+
const linearGain = this.mixerState.mic.muted
|
|
984
|
+
? 0
|
|
985
|
+
: this.dbToLinear(this.mixerState.mic.gain);
|
|
986
|
+
this.micEngine.setMicrophoneGain(linearGain);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
setMicrophoneGain(gainValue) {
|
|
992
|
+
if (this.micEngine) {
|
|
993
|
+
this.micEngine.setMicrophoneGain(gainValue);
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
setIEMMonoVocals(enabled) {
|
|
998
|
+
this.mixerState.iemMonoVocals = enabled;
|
|
999
|
+
|
|
1000
|
+
// If playing, recreate audio graph to apply the change
|
|
1001
|
+
if (this.isPlaying) {
|
|
1002
|
+
this.stopAllSources();
|
|
1003
|
+
this.createAudioGraph();
|
|
1004
|
+
this.startAudioSources();
|
|
1005
|
+
} else {
|
|
1006
|
+
// Just recreate the graph for next playback
|
|
1007
|
+
this.createAudioGraph();
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
return true;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
async loadMicSettings() {
|
|
1014
|
+
try {
|
|
1015
|
+
if (window.kaiAPI.settings && this.micEngine) {
|
|
1016
|
+
const micToSpeakers = await window.kaiAPI.settings.get('micToSpeakers', false);
|
|
1017
|
+
const enableMic = await window.kaiAPI.settings.get('enableMic', true);
|
|
1018
|
+
const iemMonoVocals = await window.kaiAPI.settings.get('iemMonoVocals', true);
|
|
1019
|
+
|
|
1020
|
+
this.mixerState.micToSpeakers = micToSpeakers;
|
|
1021
|
+
this.mixerState.enableMic = enableMic;
|
|
1022
|
+
this.mixerState.iemMonoVocals = iemMonoVocals;
|
|
1023
|
+
|
|
1024
|
+
// Load settings into microphone engine
|
|
1025
|
+
this.micEngine.micToSpeakers = micToSpeakers;
|
|
1026
|
+
this.micEngine.enableMic = enableMic;
|
|
1027
|
+
|
|
1028
|
+
// Load auto-tune settings
|
|
1029
|
+
const autoTunePrefs = await window.kaiAPI.settings.get('autoTunePreferences', {});
|
|
1030
|
+
if (autoTunePrefs.enabled !== undefined) {
|
|
1031
|
+
this.micEngine.autotuneSettings.enabled = autoTunePrefs.enabled;
|
|
1032
|
+
}
|
|
1033
|
+
if (autoTunePrefs.strength !== undefined) {
|
|
1034
|
+
this.micEngine.autotuneSettings.strength = autoTunePrefs.strength;
|
|
1035
|
+
}
|
|
1036
|
+
if (autoTunePrefs.speed !== undefined) {
|
|
1037
|
+
this.micEngine.autotuneSettings.speed = autoTunePrefs.speed;
|
|
1038
|
+
}
|
|
1039
|
+
if (autoTunePrefs.preferVocals !== undefined) {
|
|
1040
|
+
this.micEngine.autotuneSettings.preferVocals = autoTunePrefs.preferVocals;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
// console.log('[AutoTune] Loaded settings:', this.micEngine.autotuneSettings);
|
|
1044
|
+
|
|
1045
|
+
// If auto-tune is enabled and mic is already running, apply it
|
|
1046
|
+
if (
|
|
1047
|
+
this.micEngine.autotuneSettings.enabled &&
|
|
1048
|
+
this.micEngine.microphoneGain &&
|
|
1049
|
+
this.micEngine.autoTuneWorkletsLoaded
|
|
1050
|
+
) {
|
|
1051
|
+
// console.log('[AutoTune] Applying enabled auto-tune from saved settings');
|
|
1052
|
+
this.micEngine.enableAutoTune();
|
|
1053
|
+
|
|
1054
|
+
// Reapply gain after auto-tune reconnects the audio chain
|
|
1055
|
+
const linearGain = this.mixerState.mic.muted
|
|
1056
|
+
? 0
|
|
1057
|
+
: this.dbToLinear(this.mixerState.mic.gain);
|
|
1058
|
+
this.micEngine.setMicrophoneGain(linearGain);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
} catch (error) {
|
|
1062
|
+
console.error('Failed to load mic/autotune settings:', error);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
setMicToSpeakers(enabled) {
|
|
1067
|
+
this.mixerState.micToSpeakers = enabled;
|
|
1068
|
+
if (this.micEngine) {
|
|
1069
|
+
this.micEngine.setMicToSpeakers(enabled);
|
|
1070
|
+
|
|
1071
|
+
// Reapply gain after mic routing changes reconnect the audio chain
|
|
1072
|
+
if (this.micEngine.microphoneGain) {
|
|
1073
|
+
const linearGain = this.mixerState.mic.muted
|
|
1074
|
+
? 0
|
|
1075
|
+
: this.dbToLinear(this.mixerState.mic.gain);
|
|
1076
|
+
this.micEngine.setMicrophoneGain(linearGain);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
async setEnableMic(enabled) {
|
|
1082
|
+
this.mixerState.enableMic = enabled;
|
|
1083
|
+
if (this.micEngine) {
|
|
1084
|
+
await this.micEngine.setEnableMic(enabled);
|
|
1085
|
+
|
|
1086
|
+
// Reapply saved mic gain and mute state after mic restarts
|
|
1087
|
+
if (enabled && this.micEngine.microphoneGain) {
|
|
1088
|
+
const linearGain = this.mixerState.mic.muted
|
|
1089
|
+
? 0
|
|
1090
|
+
: this.dbToLinear(this.mixerState.mic.gain);
|
|
1091
|
+
this.micEngine.setMicrophoneGain(linearGain);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
stop() {
|
|
1097
|
+
this.isPlaying = false;
|
|
1098
|
+
this.stopAllSources();
|
|
1099
|
+
this.stopMicrophoneInput();
|
|
1100
|
+
this.stopSongEndMonitoring();
|
|
1101
|
+
|
|
1102
|
+
if (this.audioContexts.PA) {
|
|
1103
|
+
this.audioContexts.PA.close();
|
|
1104
|
+
this.audioContexts.PA = null;
|
|
1105
|
+
}
|
|
1106
|
+
if (this.audioContexts.IEM) {
|
|
1107
|
+
this.audioContexts.IEM.close();
|
|
1108
|
+
this.audioContexts.IEM = null;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
this.audioBuffers.clear();
|
|
1112
|
+
this.outputNodes.PA.gainNodes.clear();
|
|
1113
|
+
this.outputNodes.IEM.gainNodes.clear();
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
async reinitialize() {
|
|
1117
|
+
this.stop();
|
|
1118
|
+
|
|
1119
|
+
// Wait for audio sources to fully stop and contexts to close
|
|
1120
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
1121
|
+
|
|
1122
|
+
await this.initialize();
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
setOnSongEndedCallback(callback) {
|
|
1126
|
+
this.onSongEndedCallback = callback;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
// Check if song has ended based on position
|
|
1130
|
+
checkForSongEnd() {
|
|
1131
|
+
const duration = this.getDuration();
|
|
1132
|
+
const currentPos = this.getCurrentPosition();
|
|
1133
|
+
const timeSinceMonitoringStarted = this.monitoringStartTime
|
|
1134
|
+
? this.audioContexts.PA.currentTime - this.monitoringStartTime
|
|
1135
|
+
: 0;
|
|
1136
|
+
|
|
1137
|
+
// Only trigger if:
|
|
1138
|
+
// 1. We've been monitoring for at least 2 seconds (prevents seek issues)
|
|
1139
|
+
// 2. Song duration > 3 seconds
|
|
1140
|
+
// 3. Current position is near the end
|
|
1141
|
+
if (
|
|
1142
|
+
this.isPlaying &&
|
|
1143
|
+
timeSinceMonitoringStarted > 2.0 &&
|
|
1144
|
+
duration > 3 &&
|
|
1145
|
+
currentPos >= duration - 0.2
|
|
1146
|
+
) {
|
|
1147
|
+
this.stopAllSources();
|
|
1148
|
+
this.stopSongEndMonitoring();
|
|
1149
|
+
|
|
1150
|
+
// Pause to properly clean up (sets isPlaying = false, updates renderer, etc.)
|
|
1151
|
+
this.pause();
|
|
1152
|
+
|
|
1153
|
+
// Use base class method for consistent song end handling (triggers callback)
|
|
1154
|
+
this._triggerSongEnd();
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
// Start monitoring for song end
|
|
1159
|
+
startSongEndMonitoring() {
|
|
1160
|
+
if (this.songEndMonitor) {
|
|
1161
|
+
clearInterval(this.songEndMonitor);
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// Track when monitoring actually started to prevent false positives
|
|
1165
|
+
this.monitoringStartTime = this.audioContexts.PA.currentTime;
|
|
1166
|
+
|
|
1167
|
+
this.songEndMonitor = setInterval(() => {
|
|
1168
|
+
this.checkForSongEnd();
|
|
1169
|
+
}, 250); // Check every 250ms for more responsive detection
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// Stop monitoring for song end
|
|
1173
|
+
stopSongEndMonitoring() {
|
|
1174
|
+
if (this.songEndMonitor) {
|
|
1175
|
+
clearInterval(this.songEndMonitor);
|
|
1176
|
+
this.songEndMonitor = null;
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
// Utility: Convert dB to linear gain
|
|
1181
|
+
dbToLinear(db) {
|
|
1182
|
+
return Math.pow(10, db / 20);
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
// Utility: Convert linear gain to dB
|
|
1186
|
+
linearToDb(linear) {
|
|
1187
|
+
return 20 * Math.log10(linear);
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
/**
|
|
1191
|
+
* Get the format type this player handles
|
|
1192
|
+
* @returns {string} Format name
|
|
1193
|
+
*/
|
|
1194
|
+
getFormat() {
|
|
1195
|
+
return 'kai';
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
// Export removed - KAIPlayer is instantiated by KaiPlayerApp in main.js
|
|
1200
|
+
// No longer attached to window global
|