fix: Serve spectrum analyzer JavaScript dynamically via API

Changed from static file generation to Radiance API endpoint.
Parenscript now generates JavaScript in-memory at runtime when
the /api/asteroid/spectrum-analyzer.js endpoint is requested.

This is the correct approach for Parenscript integration with Radiance.
This commit is contained in:
Glenn Thompson 2025-12-02 09:34:41 +03:00 committed by Brian O'Reilly
parent a1257af16f
commit 090e8e9898
3 changed files with 5 additions and 19 deletions

View File

@ -4,10 +4,3 @@
(:use #:cl #:radiance #:lass #:r-clip #:asteroid.app-utils)
(:domain "asteroid")
(:export #:-main))
;; Generate Parenscript files at compile time
(eval-when (:compile-toplevel :load-toplevel :execute)
(when (find-package :asteroid)
(let ((pkg (find-package :asteroid)))
(when (fboundp (find-symbol "WRITE-SPECTRUM-ANALYZER-JS" pkg))
(funcall (find-symbol "WRITE-SPECTRUM-ANALYZER-JS" pkg))))))

View File

@ -3,8 +3,9 @@
;;; Spectrum Analyzer - Parenscript Implementation
;;; Generates JavaScript for real-time audio visualization
(defun generate-spectrum-analyzer-js ()
"Generate JavaScript code for the spectrum analyzer using Parenscript"
(define-api asteroid/spectrum-analyzer.js () ()
"Serve the spectrum analyzer JavaScript generated from Parenscript"
(setf (content-type *response*) "application/javascript")
(ps:ps
(defvar *audio-context* nil)
(defvar *analyser* nil)
@ -85,12 +86,4 @@
(let ((audio-element (chain document (get-element-by-id "live-audio"))))
(when audio-element
(chain audio-element (add-event-listener "play" init-spectrum-analyzer))
(chain audio-element (add-event-listener "pause" stop-spectrum-analyzer)))))))))
(defun write-spectrum-analyzer-js ()
"Write the generated JavaScript to a file"
(with-open-file (stream (asdf:system-relative-pathname :asteroid "static/js/spectrum-analyzer.js")
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(write-string (generate-spectrum-analyzer-js) stream)))
(chain audio-element (add-event-listener "pause" stop-spectrum-analyzer))))))))

View File

@ -11,7 +11,7 @@
<script src="/asteroid/static/js/auth-ui.js"></script>
<script src="/asteroid/static/js/front-page.js"></script>
<script src="/asteroid/static/js/recently-played.js"></script>
<script src="/asteroid/static/js/spectrum-analyzer.js"></script>
<script src="/api/asteroid/spectrum-analyzer.js"></script>
</head>
<body>
<div class="container">