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.
Files changed (290) hide show
  1. package/README.md +558 -0
  2. package/bin/loukai.js +32 -0
  3. package/package.json +243 -0
  4. package/src/main/appState.js +250 -0
  5. package/src/main/audioEngine.js +478 -0
  6. package/src/main/creator/conversionService.js +503 -0
  7. package/src/main/creator/downloadManager.js +1128 -0
  8. package/src/main/creator/ffmpegService.js +487 -0
  9. package/src/main/creator/installLogger.js +51 -0
  10. package/src/main/creator/keyDetection.js +212 -0
  11. package/src/main/creator/llmService.js +370 -0
  12. package/src/main/creator/lrclibService.js +340 -0
  13. package/src/main/creator/python/crepe_runner.py +189 -0
  14. package/src/main/creator/python/demucs_runner.py +158 -0
  15. package/src/main/creator/python/whisper_runner.py +172 -0
  16. package/src/main/creator/pythonRunner.js +268 -0
  17. package/src/main/creator/stemBuilder.js +491 -0
  18. package/src/main/creator/systemChecker.js +474 -0
  19. package/src/main/handlers/appHandlers.js +45 -0
  20. package/src/main/handlers/audioHandlers.js +33 -0
  21. package/src/main/handlers/autotuneHandlers.js +28 -0
  22. package/src/main/handlers/canvasHandlers.js +84 -0
  23. package/src/main/handlers/creatorHandlers.js +159 -0
  24. package/src/main/handlers/editorHandlers.js +98 -0
  25. package/src/main/handlers/effectsHandlers.js +100 -0
  26. package/src/main/handlers/fileHandlers.js +45 -0
  27. package/src/main/handlers/index.js +78 -0
  28. package/src/main/handlers/libraryHandlers.js +96 -0
  29. package/src/main/handlers/mixerHandlers.js +64 -0
  30. package/src/main/handlers/playerHandlers.js +39 -0
  31. package/src/main/handlers/preferencesHandlers.js +46 -0
  32. package/src/main/handlers/queueHandlers.js +81 -0
  33. package/src/main/handlers/rendererHandlers.js +63 -0
  34. package/src/main/handlers/settingsHandlers.js +42 -0
  35. package/src/main/handlers/webServerHandlers.js +105 -0
  36. package/src/main/main.js +2351 -0
  37. package/src/main/preload.js +252 -0
  38. package/src/main/settingsManager.js +139 -0
  39. package/src/main/statePersistence.js +193 -0
  40. package/src/main/utils/pathValidator.js +112 -0
  41. package/src/main/webServer.js +2535 -0
  42. package/src/native/autotune.js +417 -0
  43. package/src/renderer/adapters/ElectronBridge.js +677 -0
  44. package/src/renderer/canvas.html +80 -0
  45. package/src/renderer/components/App.jsx +303 -0
  46. package/src/renderer/components/AppRoot.jsx +37 -0
  47. package/src/renderer/components/AudioDeviceSettings.jsx +145 -0
  48. package/src/renderer/components/EffectsPanelWrapper.jsx +267 -0
  49. package/src/renderer/components/MixerTab.jsx +233 -0
  50. package/src/renderer/components/MixerTabWrapper.jsx +31 -0
  51. package/src/renderer/components/PortalSelect.jsx +239 -0
  52. package/src/renderer/components/QueueTab.jsx +116 -0
  53. package/src/renderer/components/RequestsListWrapper.jsx +78 -0
  54. package/src/renderer/components/ServerTab.jsx +472 -0
  55. package/src/renderer/components/SongInfoBarWrapper.jsx +77 -0
  56. package/src/renderer/components/StatusBar.jsx +92 -0
  57. package/src/renderer/components/TabNavigation.jsx +77 -0
  58. package/src/renderer/components/TransportControlsWrapper.jsx +69 -0
  59. package/src/renderer/components/creator/CreateTab.jsx +1236 -0
  60. package/src/renderer/dist/assets/kaiPlayer-CoMx__a_.js +2 -0
  61. package/src/renderer/dist/assets/kaiPlayer-CoMx__a_.js.map +1 -0
  62. package/src/renderer/dist/assets/microphoneEngine-BaCUhhQc.js +2 -0
  63. package/src/renderer/dist/assets/microphoneEngine-BaCUhhQc.js.map +1 -0
  64. package/src/renderer/dist/assets/player-DVrqp7N5.js +3 -0
  65. package/src/renderer/dist/assets/player-DVrqp7N5.js.map +1 -0
  66. package/src/renderer/dist/assets/songLoaders-BaTgGib4.js +2 -0
  67. package/src/renderer/dist/assets/songLoaders-BaTgGib4.js.map +1 -0
  68. package/src/renderer/dist/assets/webrtcManager-BhCHWceK.js +2 -0
  69. package/src/renderer/dist/assets/webrtcManager-BhCHWceK.js.map +1 -0
  70. package/src/renderer/dist/js/autoTuneWorklet.js +224 -0
  71. package/src/renderer/dist/js/micPitchDetectorWorklet.js +137 -0
  72. package/src/renderer/dist/js/musicAnalysisWorklet.js +216 -0
  73. package/src/renderer/dist/js/phaseVocoderWorklet.js +341 -0
  74. package/src/renderer/dist/js/soundtouch-worklet.js +1395 -0
  75. package/src/renderer/dist/renderer.css +1 -0
  76. package/src/renderer/dist/renderer.js +62 -0
  77. package/src/renderer/dist/renderer.js.map +1 -0
  78. package/src/renderer/dist/renderer.woff2 +0 -0
  79. package/src/renderer/hooks/useKeyboardShortcuts.js +154 -0
  80. package/src/renderer/index.html +24 -0
  81. package/src/renderer/index.html.backup +372 -0
  82. package/src/renderer/js/PlayerInterface.js +267 -0
  83. package/src/renderer/js/autoTuneWorklet.js +224 -0
  84. package/src/renderer/js/butterchurnVerify.js +46 -0
  85. package/src/renderer/js/canvas-app.js +114 -0
  86. package/src/renderer/js/cdgPlayer.js +685 -0
  87. package/src/renderer/js/kaiPlayer.js +1200 -0
  88. package/src/renderer/js/karaokeRenderer.js +3392 -0
  89. package/src/renderer/js/micPitchDetectorWorklet.js +137 -0
  90. package/src/renderer/js/microphoneEngine.js +656 -0
  91. package/src/renderer/js/musicAnalysisWorklet.js +216 -0
  92. package/src/renderer/js/phaseVocoderWorklet.js +341 -0
  93. package/src/renderer/js/player.js +232 -0
  94. package/src/renderer/js/referencePitchTracker.js +130 -0
  95. package/src/renderer/js/songLoaders.js +334 -0
  96. package/src/renderer/js/soundtouch-worklet.js +1395 -0
  97. package/src/renderer/js/webrtcManager.js +511 -0
  98. package/src/renderer/lib/butterchurn.min.js +6739 -0
  99. package/src/renderer/lib/butterchurnPresets.min.js +1 -0
  100. package/src/renderer/lib/cdgraphics-wrapper.js +16 -0
  101. package/src/renderer/lib/cdgraphics.js +299 -0
  102. package/src/renderer/public/js/autoTuneWorklet.js +224 -0
  103. package/src/renderer/public/js/micPitchDetectorWorklet.js +137 -0
  104. package/src/renderer/public/js/musicAnalysisWorklet.js +216 -0
  105. package/src/renderer/public/js/phaseVocoderWorklet.js +341 -0
  106. package/src/renderer/public/js/soundtouch-worklet.js +1395 -0
  107. package/src/renderer/react-entry.jsx +44 -0
  108. package/src/renderer/styles/tailwind.css +106 -0
  109. package/src/renderer/utils/qrCodeGenerator.js +98 -0
  110. package/src/renderer/vite.config.js +31 -0
  111. package/src/shared/adapters/BridgeInterface.js +195 -0
  112. package/src/shared/components/EffectsPanel.jsx +177 -0
  113. package/src/shared/components/LibraryPanel.jsx +701 -0
  114. package/src/shared/components/LineDetailCanvas.jsx +167 -0
  115. package/src/shared/components/LyricLine.jsx +505 -0
  116. package/src/shared/components/LyricRejection.jsx +84 -0
  117. package/src/shared/components/LyricSuggestion.jsx +80 -0
  118. package/src/shared/components/LyricsEditorCanvas.jsx +271 -0
  119. package/src/shared/components/MixerPanel.jsx +94 -0
  120. package/src/shared/components/PlayerControls.jsx +206 -0
  121. package/src/shared/components/PortalSelect.jsx +239 -0
  122. package/src/shared/components/QueueList.jsx +365 -0
  123. package/src/shared/components/QuickSearch.jsx +126 -0
  124. package/src/shared/components/RequestsList.jsx +121 -0
  125. package/src/shared/components/SongEditor.jsx +1362 -0
  126. package/src/shared/components/SongInfoBar.jsx +81 -0
  127. package/src/shared/components/ThemeToggle.jsx +106 -0
  128. package/src/shared/components/Toast.jsx +30 -0
  129. package/src/shared/components/VisualizationSettings.jsx +243 -0
  130. package/src/shared/constants.js +95 -0
  131. package/src/shared/context/BridgeContext.jsx +32 -0
  132. package/src/shared/contexts/AudioContext.jsx +37 -0
  133. package/src/shared/contexts/PlayerContext.jsx +66 -0
  134. package/src/shared/contexts/SettingsContext.jsx +50 -0
  135. package/src/shared/defaults.js +158 -0
  136. package/src/shared/formatUtils.js +59 -0
  137. package/src/shared/formatUtils.test.js +207 -0
  138. package/src/shared/hooks/useAppState.js +97 -0
  139. package/src/shared/hooks/useAudioEngine.js +264 -0
  140. package/src/shared/hooks/usePlayer.js +89 -0
  141. package/src/shared/hooks/useSettingsPersistence.js +74 -0
  142. package/src/shared/hooks/useWebRTC.js +118 -0
  143. package/src/shared/ipcContracts.js +299 -0
  144. package/src/shared/package.json +3 -0
  145. package/src/shared/services/creatorService.js +373 -0
  146. package/src/shared/services/creatorService.test.js +413 -0
  147. package/src/shared/services/editorService.js +213 -0
  148. package/src/shared/services/editorService.test.js +219 -0
  149. package/src/shared/services/effectsService.js +271 -0
  150. package/src/shared/services/effectsService.test.js +418 -0
  151. package/src/shared/services/libraryService.js +438 -0
  152. package/src/shared/services/libraryService.test.js +474 -0
  153. package/src/shared/services/mixerService.js +172 -0
  154. package/src/shared/services/mixerService.test.js +399 -0
  155. package/src/shared/services/playerService.js +221 -0
  156. package/src/shared/services/playerService.test.js +357 -0
  157. package/src/shared/services/preferencesService.js +219 -0
  158. package/src/shared/services/queueService.js +226 -0
  159. package/src/shared/services/queueService.test.js +430 -0
  160. package/src/shared/services/requestsService.js +155 -0
  161. package/src/shared/services/requestsService.test.js +362 -0
  162. package/src/shared/services/serverSettingsService.js +151 -0
  163. package/src/shared/services/settingsService.js +257 -0
  164. package/src/shared/services/settingsService.test.js +295 -0
  165. package/src/shared/state/StateManager.js +263 -0
  166. package/src/shared/utils/audio.js +42 -0
  167. package/src/shared/utils/format.js +32 -0
  168. package/src/shared/utils/lyricsUtils.js +162 -0
  169. package/src/test/setup.js +40 -0
  170. package/src/utils/cdgLoader.js +180 -0
  171. package/src/utils/m4aLoader.js +333 -0
  172. package/src/web/App.jsx +578 -0
  173. package/src/web/adapters/WebBridge.js +428 -0
  174. package/src/web/components/PlayerSettingsPanel.jsx +231 -0
  175. package/src/web/components/SongSearch.jsx +180 -0
  176. package/src/web/dist/assets/index-0H-RnRrV.js +51 -0
  177. package/src/web/dist/assets/index-0H-RnRrV.js.map +1 -0
  178. package/src/web/dist/assets/index-DYW2zB0u.css +1 -0
  179. package/src/web/dist/index.html +15 -0
  180. package/src/web/index.html +14 -0
  181. package/src/web/main.jsx +10 -0
  182. package/src/web/package-lock.json +1765 -0
  183. package/src/web/pages/SongRequestPage.jsx +619 -0
  184. package/src/web/styles/tailwind.css +68 -0
  185. package/src/web/vite.config.js +27 -0
  186. package/static/fonts/material-icons.woff2 +0 -0
  187. package/static/images/butterchurn-screenshots/Aderrasi - Potion of Spirits.png +0 -0
  188. package/static/images/butterchurn-screenshots/Aderrasi - Songflower _Moss Posy_.png +0 -0
  189. package/static/images/butterchurn-screenshots/Aderrasi - Storm of the Eye _Thunder_ - mash0000 - quasi pseudo meta concentrics.png +0 -0
  190. package/static/images/butterchurn-screenshots/Aderrasi _ Geiss - Airhandler _Kali Mix_ - Canvas Mix.png +0 -0
  191. 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
  192. package/static/images/butterchurn-screenshots/Cope - The Neverending Explosion of Red Liquid Fire.png +0 -0
  193. proton lights __Krash_s beat code_ _Phat_remix02b.png +0 -0
  194. package/static/images/butterchurn-screenshots/Eo_S_ _ Phat - cubetrace - v2.png +0 -0
  195. package/static/images/butterchurn-screenshots/Eo_S_ _ Zylot - skylight _Stained Glass Majesty mix_.png +0 -0
  196. package/static/images/butterchurn-screenshots/Flexi - alien fish pond.png +0 -0
  197. package/static/images/butterchurn-screenshots/Flexi - area 51.png +0 -0
  198. package/static/images/butterchurn-screenshots/Flexi - infused with the spiral.png +0 -0
  199. package/static/images/butterchurn-screenshots/Flexi - mindblob _shiny mix_.png +0 -0
  200. package/static/images/butterchurn-screenshots/Flexi - mindblob mix.png +0 -0
  201. package/static/images/butterchurn-screenshots/Flexi - predator-prey-spirals.png +0 -0
  202. package/static/images/butterchurn-screenshots/Flexi - smashing fractals _acid etching mix_.png +0 -0
  203. package/static/images/butterchurn-screenshots/Flexi - truly soft piece of software - this is generic texturing _Jelly_ .png +0 -0
  204. package/static/images/butterchurn-screenshots/Flexi _ Martin - astral projection.png +0 -0
  205. package/static/images/butterchurn-screenshots/Flexi _ Martin - cascading decay swing.png +0 -0
  206. package/static/images/butterchurn-screenshots/Flexi _ amandio c - piercing 05 - Kopie _2_ - Kopie.png +0 -0
  207. package/static/images/butterchurn-screenshots/Flexi _ stahlregen - jelly showoff parade.png +0 -0
  208. package/static/images/butterchurn-screenshots/Flexi_ fishbrain_ Geiss _ Martin - tokamak witchery.png +0 -0
  209. package/static/images/butterchurn-screenshots/Flexi_ martin _ geiss - dedicated to the sherwin maxawow.png +0 -0
  210. package/static/images/butterchurn-screenshots/Fumbling_Foo _ Flexi_ Martin_ Orb_ Unchained - Star Nova v7b.png +0 -0
  211. package/static/images/butterchurn-screenshots/Geiss - Cauldron - painterly 2 _saturation remix_.png +0 -0
  212. package/static/images/butterchurn-screenshots/Geiss - Reaction Diffusion 2.png +0 -0
  213. package/static/images/butterchurn-screenshots/Geiss - Spiral Artifact.png +0 -0
  214. package/static/images/butterchurn-screenshots/Geiss - Thumb Drum.png +0 -0
  215. package/static/images/butterchurn-screenshots/Geiss _ Flexi _ Martin - disconnected.png +0 -0
  216. package/static/images/butterchurn-screenshots/Geiss_ Flexi _ Stahlregen - Thumbdrum Tokamak _crossfiring aftermath jelly mashup_.png +0 -0
  217. package/static/images/butterchurn-screenshots/Goody - The Wild Vort.png +0 -0
  218. package/static/images/butterchurn-screenshots/Idiot - Star Of Annon.png +0 -0
  219. package/static/images/butterchurn-screenshots/Krash _ Illusion - Spiral Movement.png +0 -0
  220. package/static/images/butterchurn-screenshots/Martin - QBikal - Surface Turbulence IIb.png +0 -0
  221. package/static/images/butterchurn-screenshots/Martin - acid wiring.png +0 -0
  222. package/static/images/butterchurn-screenshots/Martin - charisma.png +0 -0
  223. package/static/images/butterchurn-screenshots/Martin - liquid arrows.png +0 -0
  224. package/static/images/butterchurn-screenshots/Milk Artist At our Best - FED - SlowFast Ft AdamFX n Martin - HD CosmoFX.png +0 -0
  225. package/static/images/butterchurn-screenshots/ORB - Waaa.png +0 -0
  226. package/static/images/butterchurn-screenshots/Phat_fiShbRaiN_Eo_S_Mandala_Chasers_remix.png +0 -0
  227. package/static/images/butterchurn-screenshots/Rovastar - Oozing Resistance.png +0 -0
  228. package/static/images/butterchurn-screenshots/Rovastar _ Loadus _ Geiss - FractalDrop _Triple Mix_.png +0 -0
  229. package/static/images/butterchurn-screenshots/TonyMilkdrop - Leonardo Da Vinci_s Balloon _Flexi - merry-go-round _ techstyle_.png +0 -0
  230. package/static/images/butterchurn-screenshots/TonyMilkdrop - Magellan_s Nebula _Flexi - you enter first _ multiverse_.png +0 -0
  231. package/static/images/butterchurn-screenshots/Unchained - Rewop.png +0 -0
  232. package/static/images/butterchurn-screenshots/Unchained - Unified Drag 2.png +0 -0
  233. package/static/images/butterchurn-screenshots/Unchained _ Rovastar - Wormhole Pillars _Hall of Shadows mix_.png +0 -0
  234. package/static/images/butterchurn-screenshots/Zylot - Paint Spill _Music Reactive Paint Mix_.png +0 -0
  235. package/static/images/butterchurn-screenshots/Zylot - Star Ornament.png +0 -0
  236. package/static/images/butterchurn-screenshots/Zylot - True Visionary _Final Mix_.png +0 -0
  237. package/static/images/butterchurn-screenshots/_Aderrasi - Wanderer in Curved Space - mash0000 - faclempt kibitzing meshuggana schmaltz _Geiss color mix_.png +0 -0
  238. package/static/images/butterchurn-screenshots/_Geiss - Artifact 01.png +0 -0
  239. package/static/images/butterchurn-screenshots/_Geiss - Desert Rose 2.png +0 -0
  240. package/static/images/butterchurn-screenshots/_Geiss - untitled.png +0 -0
  241. package/static/images/butterchurn-screenshots/_Mig_049.png +0 -0
  242. package/static/images/butterchurn-screenshots/_Mig_085.png +0 -0
  243. package/static/images/butterchurn-screenshots/_Rovastar _ Geiss - Hurricane Nightmare _Posterize Mix_.png +0 -0
  244. package/static/images/butterchurn-screenshots/___ Royal - Mashup _197_.png +0 -0
  245. package/static/images/butterchurn-screenshots/___ Royal - Mashup _220_.png +0 -0
  246. package/static/images/butterchurn-screenshots/___ Royal - Mashup _431_.png +0 -0
  247. package/static/images/butterchurn-screenshots/cope _ martin - mother-of-pearl.png +0 -0
  248. package/static/images/butterchurn-screenshots/fiShbRaiN _ Flexi - witchcraft 2_0.png +0 -0
  249. package/static/images/butterchurn-screenshots/flexi - bouncing balls _double mindblob neon mix_.png +0 -0
  250. package/static/images/butterchurn-screenshots/flexi - mom_ why the sky looks different today.png +0 -0
  251. package/static/images/butterchurn-screenshots/flexi - patternton_ district of media_ capitol of the united abstractions of fractopia.png +0 -0
  252. package/static/images/butterchurn-screenshots/flexi - swing out on the spiral.png +0 -0
  253. package/static/images/butterchurn-screenshots/flexi - what is the matrix.png +0 -0
  254. package/static/images/butterchurn-screenshots/flexi _ amandio c - organic _random mashup_.png +0 -0
  255. package/static/images/butterchurn-screenshots/flexi _ amandio c - organic12-3d-2_milk.png +0 -0
  256. package/static/images/butterchurn-screenshots/flexi _ fishbrain - neon mindblob grafitti.png +0 -0
  257. package/static/images/butterchurn-screenshots/flexi _ geiss - pogo cubes vs_ tokamak vs_ game of life _stahls jelly 4_5 finish_.png +0 -0
  258. package/static/images/butterchurn-screenshots/high-altitude basket unraveling - singh grooves nitrogen argon nz_.png +0 -0
  259. package/static/images/butterchurn-screenshots/martin - The Bridge of Khazad-Dum.png +0 -0
  260. package/static/images/butterchurn-screenshots/martin - angel flight.png +0 -0
  261. package/static/images/butterchurn-screenshots/martin - another kind of groove.png +0 -0
  262. package/static/images/butterchurn-screenshots/martin - bombyx mori.png +0 -0
  263. package/static/images/butterchurn-screenshots/martin - castle in the air.png +0 -0
  264. package/static/images/butterchurn-screenshots/martin - chain breaker.png +0 -0
  265. package/static/images/butterchurn-screenshots/martin - disco mix 4.png +0 -0
  266. package/static/images/butterchurn-screenshots/martin - extreme heat.png +0 -0
  267. package/static/images/butterchurn-screenshots/martin - frosty caves 2.png +0 -0
  268. package/static/images/butterchurn-screenshots/martin - fruit machine.png +0 -0
  269. package/static/images/butterchurn-screenshots/martin - ghost city.png +0 -0
  270. package/static/images/butterchurn-screenshots/martin - glass corridor.png +0 -0
  271. package/static/images/butterchurn-screenshots/martin - infinity _2010 update_.png +0 -0
  272. package/static/images/butterchurn-screenshots/martin - mandelbox explorer - high speed demo version.png +0 -0
  273. package/static/images/butterchurn-screenshots/martin - mucus cervix.png +0 -0
  274. package/static/images/butterchurn-screenshots/martin - reflections on black tiles.png +0 -0
  275. package/static/images/butterchurn-screenshots/martin - stormy sea _2010 update_.png +0 -0
  276. package/static/images/butterchurn-screenshots/martin - witchcraft reloaded.png +0 -0
  277. package/static/images/butterchurn-screenshots/martin _ flexi - diamond cutter _prismaticvortex_com_ - camille - i wish i wish i wish i was constrained.png +0 -0
  278. package/static/images/butterchurn-screenshots/martin _shadow harlequins shape code_ - fata morgana.png +0 -0
  279. package/static/images/butterchurn-screenshots/martin_ flexi_ fishbrain _ sto - enterstate _random mashup_.png +0 -0
  280. package/static/images/butterchurn-screenshots/sawtooth grin roam.png +0 -0
  281. package/static/images/butterchurn-screenshots/shifter - dark tides bdrv mix 2.png +0 -0
  282. 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
  283. package/static/images/butterchurn-screenshots/suksma - heretical crosscut playpen.png +0 -0
  284. package/static/images/butterchurn-screenshots/suksma - uninitialized variabowl _hydroponic chronic_.png +0 -0
  285. package/static/images/butterchurn-screenshots/suksma - vector exp 1 - couldn_t not.png +0 -0
  286. package/static/images/butterchurn-screenshots/yin - 191 - Temporal singularities.png +0 -0
  287. package/static/images/logo-512.png +0 -0
  288. package/static/images/logo.png +0 -0
  289. package/static/loukai-logo.png +0 -0
  290. package/static/screenshot-generator.html +610 -0
@@ -0,0 +1,417 @@
1
+ const EventEmitter = require('events');
2
+
3
+ class AutotuneEngine extends EventEmitter {
4
+ constructor(sampleRate = 48000) {
5
+ super();
6
+ this.sampleRate = sampleRate;
7
+ this.enabled = false;
8
+
9
+ this.settings = {
10
+ strength: 50,
11
+ speed: 20,
12
+ maxCorrection: 100,
13
+ scale: 'chromatic',
14
+ formantPreserve: true,
15
+ key: 'C',
16
+ mode: 'major',
17
+ };
18
+
19
+ this.pitchDetector = new PitchDetector(sampleRate);
20
+ this.pitchShifter = new PitchShifter(sampleRate);
21
+
22
+ this.targetNotes = this.generateScaleNotes();
23
+
24
+ this.processingBuffer = new Float32Array(1024);
25
+ this.outputBuffer = new Float32Array(1024);
26
+
27
+ this.initialized = false;
28
+ }
29
+
30
+ initialize() {
31
+ try {
32
+ this.pitchDetector.initialize();
33
+ this.pitchShifter.initialize();
34
+ this.initialized = true;
35
+ console.log('Autotune engine initialized');
36
+ return true;
37
+ } catch (error) {
38
+ console.error('Failed to initialize autotune engine:', error);
39
+ return false;
40
+ }
41
+ }
42
+
43
+ setEnabled(enabled) {
44
+ this.enabled = enabled;
45
+ this.emit('enabledChanged', enabled);
46
+ }
47
+
48
+ setSettings(settings) {
49
+ this.settings = { ...this.settings, ...settings };
50
+
51
+ if (settings.key || settings.mode) {
52
+ this.targetNotes = this.generateScaleNotes();
53
+ }
54
+
55
+ this.emit('settingsChanged', this.settings);
56
+ }
57
+
58
+ generateScaleNotes() {
59
+ const baseFreq = this.keyToFrequency(this.settings.key);
60
+ const intervals = this.getScaleIntervals(this.settings.mode);
61
+
62
+ const notes = [];
63
+
64
+ for (let octave = 2; octave <= 6; octave++) {
65
+ const octaveMultiplier = Math.pow(2, octave - 4);
66
+
67
+ intervals.forEach((interval) => {
68
+ const frequency = baseFreq * octaveMultiplier * Math.pow(2, interval / 12);
69
+ notes.push(frequency);
70
+ });
71
+ }
72
+
73
+ return notes.sort((a, b) => a - b);
74
+ }
75
+
76
+ keyToFrequency(key) {
77
+ const keyMap = {
78
+ C: 261.63,
79
+ 'C#': 277.18,
80
+ Db: 277.18,
81
+ D: 293.66,
82
+ 'D#': 311.13,
83
+ Eb: 311.13,
84
+ E: 329.63,
85
+ F: 349.23,
86
+ 'F#': 369.99,
87
+ Gb: 369.99,
88
+ G: 392.0,
89
+ 'G#': 415.3,
90
+ Ab: 415.3,
91
+ A: 440.0,
92
+ 'A#': 466.16,
93
+ Bb: 466.16,
94
+ B: 493.88,
95
+ };
96
+
97
+ return keyMap[key] || 261.63;
98
+ }
99
+
100
+ getScaleIntervals(mode) {
101
+ const scales = {
102
+ major: [0, 2, 4, 5, 7, 9, 11],
103
+ minor: [0, 2, 3, 5, 7, 8, 10],
104
+ dorian: [0, 2, 3, 5, 7, 9, 10],
105
+ mixolydian: [0, 2, 4, 5, 7, 9, 10],
106
+ chromatic: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
107
+ };
108
+
109
+ return scales[mode] || scales.major;
110
+ }
111
+
112
+ process(inputBuffer, outputBuffer) {
113
+ if (!this.initialized || !this.enabled || !inputBuffer || !outputBuffer) {
114
+ if (inputBuffer && outputBuffer) {
115
+ outputBuffer.set(inputBuffer);
116
+ }
117
+ return;
118
+ }
119
+
120
+ try {
121
+ const frameSize = Math.min(inputBuffer.length, outputBuffer.length);
122
+
123
+ for (let i = 0; i < frameSize; i++) {
124
+ this.processingBuffer[i] = inputBuffer[i];
125
+ }
126
+
127
+ const pitch = this.pitchDetector.detectPitch(this.processingBuffer);
128
+
129
+ if (pitch.frequency > 80 && pitch.frequency < 1000 && pitch.confidence > 0.5) {
130
+ const targetFreq = this.findClosestTargetNote(pitch.frequency);
131
+ const correction = this.calculateCorrection(pitch.frequency, targetFreq);
132
+
133
+ if (Math.abs(correction) > 5) {
134
+ this.pitchShifter.shiftPitch(
135
+ this.processingBuffer,
136
+ this.outputBuffer,
137
+ correction,
138
+ frameSize
139
+ );
140
+
141
+ outputBuffer.set(this.outputBuffer.subarray(0, frameSize));
142
+ } else {
143
+ outputBuffer.set(this.processingBuffer.subarray(0, frameSize));
144
+ }
145
+
146
+ this.emit('pitchDetected', {
147
+ detected: pitch.frequency,
148
+ target: targetFreq,
149
+ correction: correction,
150
+ confidence: pitch.confidence,
151
+ });
152
+ } else {
153
+ outputBuffer.set(this.processingBuffer.subarray(0, frameSize));
154
+ }
155
+ } catch (error) {
156
+ console.error('Autotune processing error:', error);
157
+ if (inputBuffer && outputBuffer) {
158
+ outputBuffer.set(inputBuffer);
159
+ }
160
+ }
161
+ }
162
+
163
+ findClosestTargetNote(frequency) {
164
+ if (this.settings.scale === 'chromatic') {
165
+ const baseFreq = 440;
166
+ const semitone = Math.round(12 * Math.log2(frequency / baseFreq));
167
+ return baseFreq * Math.pow(2, semitone / 12);
168
+ }
169
+
170
+ let closest = this.targetNotes[0];
171
+ let minDiff = Math.abs(frequency - closest);
172
+
173
+ for (const note of this.targetNotes) {
174
+ const diff = Math.abs(frequency - note);
175
+ if (diff < minDiff) {
176
+ minDiff = diff;
177
+ closest = note;
178
+ }
179
+ }
180
+
181
+ return closest;
182
+ }
183
+
184
+ calculateCorrection(detected, target) {
185
+ const cents = 1200 * Math.log2(target / detected);
186
+ const maxCorrection = this.settings.maxCorrection;
187
+ const strength = this.settings.strength / 100;
188
+ const speed = this.settings.speed / 100;
189
+
190
+ let correction = cents * strength;
191
+ correction = Math.max(-maxCorrection, Math.min(maxCorrection, correction));
192
+
193
+ correction *= speed;
194
+
195
+ return correction;
196
+ }
197
+
198
+ destroy() {
199
+ this.enabled = false;
200
+
201
+ if (this.pitchDetector) {
202
+ this.pitchDetector.destroy();
203
+ }
204
+
205
+ if (this.pitchShifter) {
206
+ this.pitchShifter.destroy();
207
+ }
208
+
209
+ this.initialized = false;
210
+ console.log('Autotune engine destroyed');
211
+ }
212
+ }
213
+
214
+ class PitchDetector {
215
+ constructor(sampleRate) {
216
+ this.sampleRate = sampleRate;
217
+ this.bufferSize = 1024;
218
+ this.hopSize = 256;
219
+
220
+ this.window = this.createHannWindow(this.bufferSize);
221
+ this.fftBuffer = new Float32Array(this.bufferSize);
222
+ this.spectrum = new Float32Array(this.bufferSize / 2);
223
+
224
+ this.autocorrelationBuffer = new Float32Array(this.bufferSize);
225
+ this.inputHistory = new Float32Array(this.bufferSize * 2);
226
+ this.historyIndex = 0;
227
+ }
228
+
229
+ initialize() {
230
+ console.log('Pitch detector initialized');
231
+ }
232
+
233
+ createHannWindow(size) {
234
+ const window = new Float32Array(size);
235
+ for (let i = 0; i < size; i++) {
236
+ window[i] = 0.5 * (1 - Math.cos((2 * Math.PI * i) / (size - 1)));
237
+ }
238
+ return window;
239
+ }
240
+
241
+ detectPitch(buffer) {
242
+ for (let i = 0; i < buffer.length; i++) {
243
+ this.inputHistory[this.historyIndex] = buffer[i];
244
+ this.historyIndex = (this.historyIndex + 1) % this.inputHistory.length;
245
+ }
246
+
247
+ const startIndex =
248
+ (this.historyIndex - this.bufferSize + this.inputHistory.length) % this.inputHistory.length;
249
+
250
+ for (let i = 0; i < this.bufferSize; i++) {
251
+ const historyIndex = (startIndex + i) % this.inputHistory.length;
252
+ this.fftBuffer[i] = this.inputHistory[historyIndex] * this.window[i];
253
+ }
254
+
255
+ const frequency = this.autocorrelationMethod(this.fftBuffer);
256
+ const confidence = this.calculateConfidence(this.fftBuffer, frequency);
257
+
258
+ return { frequency, confidence };
259
+ }
260
+
261
+ autocorrelationMethod(buffer) {
262
+ const minPeriod = Math.floor(this.sampleRate / 800);
263
+ const maxPeriod = Math.floor(this.sampleRate / 80);
264
+
265
+ this.autocorrelationBuffer.fill(0);
266
+
267
+ for (let lag = minPeriod; lag < maxPeriod && lag < buffer.length; lag++) {
268
+ let correlation = 0;
269
+ for (let i = 0; i < buffer.length - lag; i++) {
270
+ correlation += buffer[i] * buffer[i + lag];
271
+ }
272
+ this.autocorrelationBuffer[lag] = correlation;
273
+ }
274
+
275
+ let maxCorrelation = 0;
276
+ let bestLag = minPeriod;
277
+
278
+ for (let lag = minPeriod; lag < maxPeriod; lag++) {
279
+ if (this.autocorrelationBuffer[lag] > maxCorrelation) {
280
+ maxCorrelation = this.autocorrelationBuffer[lag];
281
+ bestLag = lag;
282
+ }
283
+ }
284
+
285
+ if (maxCorrelation > 0.3) {
286
+ return this.sampleRate / bestLag;
287
+ }
288
+
289
+ return 0;
290
+ }
291
+
292
+ calculateConfidence(buffer, frequency) {
293
+ if (frequency === 0) return 0;
294
+
295
+ const rms = Math.sqrt(buffer.reduce((sum, val) => sum + val * val, 0) / buffer.length);
296
+
297
+ if (rms < 0.01) return 0;
298
+
299
+ const period = this.sampleRate / frequency;
300
+ const periodInt = Math.round(period);
301
+
302
+ if (periodInt >= buffer.length) return 0;
303
+
304
+ let correlation = 0;
305
+ let energy1 = 0;
306
+ let energy2 = 0;
307
+
308
+ const compareLength = Math.min(periodInt, buffer.length - periodInt);
309
+
310
+ for (let i = 0; i < compareLength; i++) {
311
+ const val1 = buffer[i];
312
+ const val2 = buffer[i + periodInt];
313
+
314
+ correlation += val1 * val2;
315
+ energy1 += val1 * val1;
316
+ energy2 += val2 * val2;
317
+ }
318
+
319
+ const normalizedCorrelation = correlation / Math.sqrt(energy1 * energy2);
320
+ return Math.max(0, Math.min(1, normalizedCorrelation));
321
+ }
322
+
323
+ destroy() {
324
+ console.log('Pitch detector destroyed');
325
+ }
326
+ }
327
+
328
+ class PitchShifter {
329
+ constructor(sampleRate) {
330
+ this.sampleRate = sampleRate;
331
+ this.frameSize = 1024;
332
+ this.overlapFactor = 4;
333
+ this.hopSize = this.frameSize / this.overlapFactor;
334
+
335
+ this.window = this.createHannWindow(this.frameSize);
336
+ this.inputBuffer = new Float32Array(this.frameSize * 2);
337
+ this.outputBuffer = new Float32Array(this.frameSize * 2);
338
+ this.grainBuffer = new Float32Array(this.frameSize);
339
+
340
+ this.inputIndex = 0;
341
+ this.outputIndex = 0;
342
+
343
+ this.grains = [];
344
+ for (let i = 0; i < this.overlapFactor; i++) {
345
+ this.grains.push({
346
+ buffer: new Float32Array(this.frameSize),
347
+ position: 0,
348
+ active: false,
349
+ });
350
+ }
351
+ this.currentGrain = 0;
352
+ }
353
+
354
+ initialize() {
355
+ console.log('Pitch shifter initialized');
356
+ }
357
+
358
+ createHannWindow(size) {
359
+ const window = new Float32Array(size);
360
+ for (let i = 0; i < size; i++) {
361
+ window[i] = 0.5 * (1 - Math.cos((2 * Math.PI * i) / (size - 1)));
362
+ }
363
+ return window;
364
+ }
365
+
366
+ shiftPitch(input, output, centsShift, length) {
367
+ const pitchRatio = Math.pow(2, centsShift / 1200);
368
+ const timeStretchRatio = 1.0 / pitchRatio;
369
+
370
+ for (let i = 0; i < length; i++) {
371
+ this.inputBuffer[this.inputIndex] = input[i];
372
+ this.inputIndex = (this.inputIndex + 1) % this.inputBuffer.length;
373
+
374
+ if (this.inputIndex % this.hopSize === 0) {
375
+ this.processGrain(timeStretchRatio);
376
+ }
377
+
378
+ let outputSample = 0;
379
+ for (const grain of this.grains) {
380
+ if (grain.active && grain.position < this.frameSize) {
381
+ outputSample +=
382
+ grain.buffer[Math.floor(grain.position)] * this.window[Math.floor(grain.position)];
383
+ grain.position += pitchRatio;
384
+
385
+ if (grain.position >= this.frameSize) {
386
+ grain.active = false;
387
+ }
388
+ }
389
+ }
390
+
391
+ output[i] = outputSample * 0.5;
392
+ }
393
+ }
394
+
395
+ processGrain(timeStretchRatio) {
396
+ const grain = this.grains[this.currentGrain];
397
+
398
+ const startIndex =
399
+ (this.inputIndex - this.frameSize + this.inputBuffer.length) % this.inputBuffer.length;
400
+
401
+ for (let i = 0; i < this.frameSize; i++) {
402
+ const inputIdx = (startIndex + Math.floor(i * timeStretchRatio)) % this.inputBuffer.length;
403
+ grain.buffer[i] = this.inputBuffer[inputIdx];
404
+ }
405
+
406
+ grain.position = 0;
407
+ grain.active = true;
408
+
409
+ this.currentGrain = (this.currentGrain + 1) % this.grains.length;
410
+ }
411
+
412
+ destroy() {
413
+ console.log('Pitch shifter destroyed');
414
+ }
415
+ }
416
+
417
+ module.exports = { AutotuneEngine, PitchDetector, PitchShifter };