Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Mage sounds (#66482)

bryanb1ececbe3224
diff --git a/bb.edn b/bb.edn
index ecc59f1a60e..b9700e701df 100644
--- a/bb.edn
+++ b/bb.edn
@@ -203,9 +203,9 @@
:arg-schema [:sequential
{:details "If none are given, you will be prompted to select directories recursively from test/ and enterprise/backend/test/"}
[:string {:description "Files or directories (recursive) to test"}]]
- :requires [[mage.quick-test-runner :as qtr]]
+ :requires [[mage.quick-test-runner :as qtr]]
:task (task! (qtr/go parsed))}
-
+
affected-modules
{:doc "Prints information about which backend modules have changes compared to a Git ref e.g. `master`"
:examples [["./bin/mage affected-modules master"]
@@ -231,6 +231,20 @@
;; These tasks are more for internal tooling, e.g. for use from git hooks etc.
;; - hidden from `./bin/mage` listing and `bb tasks`, these all - start with a `-`
+ -play
+ {:doc "Play sounds from mage.sound"
+ :examples [["./bin/mage -play success" "play success sound"] ["./bin/mage -play s" "play success sound"]
+ ["./bin/mage -play error" "play error sound"] ["./bin/mage -play e" "play error sound"]
+ ["./bin/mage -play ping" "play ping sound"] ["./bin/mage -play p" "play ping sound"]
+ ["./bin/mage -play warning" "play warning sound"] ["./bin/mage -play w" "play warning sound"]]
+ :requires [[mage.sound :as sound]]
+ :arg-schema [:tuple [:enum "s" "success" "e" "error" "p" "ping" "w" "warning"]]
+ :task (task! (case (first (:arguments parsed))
+ ("s" "success") (sound/success)
+ ("e" "error") (sound/error)
+ ("p" "ping") (sound/ping)
+ ("w" "warning") (sound/warning)))}
+
-token-scan
{:doc "Security token scanner for detecting leaked API keys and secrets"
:examples [["./bin/mage -token-scan deps.edn bb.edn" "Scan specific files"]
diff --git a/mage/resources/sounds/error.ogg b/mage/resources/sounds/error.ogg
new file mode 100644
index 00000000000..94c086148f6
Binary files /dev/null and b/mage/resources/sounds/error.ogg differ
diff --git a/mage/resources/sounds/ping.ogg b/mage/resources/sounds/ping.ogg
new file mode 100644
index 00000000000..aaa9e474f4e
Binary files /dev/null and b/mage/resources/sounds/ping.ogg differ
diff --git a/mage/resources/sounds/success.ogg b/mage/resources/sounds/success.ogg
new file mode 100644
index 00000000000..dfcdf3c2d9e
Binary files /dev/null and b/mage/resources/sounds/success.ogg differ
diff --git a/mage/resources/sounds/warning.ogg b/mage/resources/sounds/warning.ogg
new file mode 100644
index 00000000000..5dbef578feb
Binary files /dev/null and b/mage/resources/sounds/warning.ogg differ
diff --git a/mage/src/mage/be_dev.clj b/mage/src/mage/be_dev.clj
index 0341e3650fe..ca8846a4f1b 100644
--- a/mage/src/mage/be_dev.clj
+++ b/mage/src/mage/be_dev.clj
@@ -72,8 +72,7 @@
{:read-cond :allow}
~(if (str/blank? code) "::loaded" code)))))
-(def ^{:private true
- :dynamic true
+(def ^{:dynamic true
:doc "Set this to true to suppress stdout output from nrepl-eval."}
*quiet-nrepl-eval* false)
@@ -89,10 +88,10 @@
_ (u/debug "Code:\n-----\n" code-str "\n-----")
_ (bencode/write-bencode out {:op "eval" :code code-str})
final-value (atom nil)
- safe-print (fn [& msg] (when-not *quiet-nrepl-eval* (apply print msg) (flush)))]
+ safe-print (fn [& msg] (when-not *quiet-nrepl-eval* (apply print (map #(str/replace % #"\n$" "") msg)) (flush)))]
(loop []
(let [response (->> (bencode/read-bencode in) (walk/postwalk consume))]
- (u/debug "Response:\n-----\n" (with-out-str (pp/pprint response)) "\n-----")
+ (u/debug "Response:\n-----\n" (with-out-str (pp/pprint response)) "-----")
(safe-print "\n")
(doseq [[k v] response]
(case k
@@ -106,7 +105,7 @@
(if (some #{"done"} (get response "status"))
(some->> @final-value
- (safe-print "\n=> \n"))
+ (safe-print "\n=>\n"))
(recur))))
@final-value))
diff --git a/mage/src/mage/quick_test_runner.clj b/mage/src/mage/quick_test_runner.clj
index 054f025cc65..78ded2ecaad 100644
--- a/mage/src/mage/quick_test_runner.clj
+++ b/mage/src/mage/quick_test_runner.clj
@@ -9,6 +9,7 @@
[mage.be-dev :as backend]
[mage.color :as c]
[mage.shell :as shell]
+ [mage.sound :as sound]
[mage.util :as u]))
(set! *warn-on-reflection* true)
@@ -73,6 +74,7 @@
(str/join "\n" (map #(str " - " %) test-dirs)))
(let [out (backend/nrepl-eval the-ns the-cmd)
elapsed (u/since-ms start)]
+ (println)
(try (u/pp (edn/read-string out)) (catch Exception _ #_:clj-kondo/ignore (prn out)))
(println (c/green (str "Tests completed in " elapsed " ms.\n")))
(when (u/env "MAGE_DEBUG" (constantly nil))
@@ -95,6 +97,7 @@
(prn out)))
exit-code (if (zero? (+ (:fail out-data) (:error out-data))) 0 1)]
(println (feedback-bar out-data))
+ (if (zero? exit-code) @(sound/success) @(sound/error))
(u/exit exit-code)))))
;; namespaces will be converted to their file paths, so this check will work.
@@ -167,20 +170,27 @@
(defn go
"Interactively select directories to run tests against."
[{:keys [arguments options] :as _parsed}]
- (let [tests (setup-test-files arguments options)]
- (if (and (backend/nrepl-open?)
- ;; No testing against the mage nrepl!
- (not= :bb (backend/nrepl-type)))
- (do
- (println "Running tests via ⏩🏎️✨" (c/green (c/bold "THE REPL")) "✨🏎️⏪.")
- (run-tests-over-nrepl tests options))
+ (let [tests (setup-test-files arguments options)
+ port (:port options)
+ nrepl-open? (backend/nrepl-open? port)
+ nrepl-type (backend/nrepl-type port)]
+ (u/debug (pr-str ["INFO" {:port port :nrepl-open? nrepl-open? :nrepl-type nrepl-type}]))
+ (cond
+ (not nrepl-open?)
(do
+ (println (c/red "Unable to find a backend nrepl."))
(println "Running via " (c/bold (c/magenta "the command line")) "."
- (c/red " This is " (c/bold "SLOW") " and " (c/bold "NOT RECCOMENDED!! "))
+ (c/red " This is " (c/bold "SLOW") " and " (c/bold "NOT RECOMMENDED!! "))
"Please consider starting a backend \nFor quicker test runs, use: " (c/magenta " clj -M:test:dev:ee:ee-dev:drivers:drivers-dev:dev-start"))
- (println "\n" (banner
- {:font bling.fonts.drippy/drippy
- :text "Please open a REPL!"
- :gradient-direction :to-top
- :gradient-colors [:magenta :red]}))
- (run-tests-cli tests)))))
+ (println "\n" (banner {:text "Please open a REPL!"
+ :font bling.fonts.drippy/drippy
+ :gradient-direction :to-right
+ :gradient-colors [:magenta :red]}))
+ (run-tests-cli tests))
+ (and nrepl-open? (= :clj nrepl-type))
+ (do
+ (println "Running tests via ⏩🏎️✨" (c/green (c/bold "THE REPL")) "✨🏎️⏪.")
+ (run-tests-over-nrepl tests options))
+ (and nrepl-open? (not= :clj nrepl-type))
+ (println (str "You have a non-clojure nrepl: " (c/yellow (name nrepl-type)) " at port " (c/yellow port) ".\n"
+ "Either use -p <backend port>, or run 'echo {server-nrepl-port} > .nrepl-port' to run tests via the backend repl.")))))
diff --git a/mage/src/mage/sound.clj b/mage/src/mage/sound.clj
new file mode 100644
index 00000000000..bc439774f4e
--- /dev/null
+++ b/mage/src/mage/sound.clj
@@ -0,0 +1,184 @@
+(ns mage.sound
+ "Cross-platform sound effects for mage tasks.
+
+ Plays notification sounds on macOS (afplay) and Linux (paplay/aplay).
+ Sounds are automatically disabled in CI environments or when MAGE_VOLUME is set to 0.
+
+ ## Volume Control
+
+ Set the MAGE_VOLUME environment variable to control volume (0-100, default: 25).
+ - MAGE_VOLUME=0 disables sound
+ - MAGE_VOLUME=50 plays at 50% volume
+ - MAGE_VOLUME=100 plays at full volume
+ - CI=1 also disables sound (no need to scare someone in a datacenter)
+
+ ## Available Sounds
+
+ All sound files are in OGG Vorbis format (CC0/public domain) in mage/resources/sounds/:
+
+ - success.ogg - Bright ascending tone with harmonics (C major, 0.2s)
+ - error.ogg - Harsh descending buzz with dissonance (400→200 Hz, 0.15s)
+ - warning.ogg - Double beep with overtones (800 Hz + harmonic, 0.28s)
+ - ping.ogg - Crisp high tone with overtone (1200/2400 Hz, 0.12s)
+
+ ## Usage
+
+ (require '[mage.sound :as sound])
+
+ ;; Convenience functions
+ (sound/success)
+ (sound/error)
+ (sound/warning)
+ (sound/ping)"
+ (:require
+ [babashka.fs :as fs]
+ [babashka.process :as process]
+ [mage.util :as u]))
+
+(set! *warn-on-reflection* true)
+
+(defn- get-volume
+ "Get volume level from MAGE_VOLUME env var (0-100, default 25)."
+ []
+ (let [volume-str (u/env "MAGE_VOLUME" (constantly nil))]
+ (if volume-str
+ (try
+ (let [v (Integer/parseInt volume-str)]
+ (max 0 (min 100 v)))
+ (catch Exception _
+ 25))
+ 25)))
+
+(defn- sound-disabled?
+ "Check if sounds should be disabled based on environment variables."
+ []
+ (or (u/env "CI" (constantly nil))
+ (zero? (get-volume))))
+
+(defn- get-os
+ "Detect the operating system."
+ []
+ (let [os-name (System/getProperty "os.name")]
+ (cond
+ (re-find #"(?i)mac|darwin" os-name) :macos
+ (re-find #"(?i)linux" os-name) :linux
+ :else :unknown)))
+
+(defn- get-sound-command
+ "Get the available sound command for the current OS."
+ []
+ (binding [u/*skip-warning* true]
+ (case (get-os)
+ :macos (when (u/can-run? "afplay") :afplay)
+ :linux (cond
+ (u/can-run? "paplay") :paplay
+ (u/can-run? "aplay") :aplay
+ :else nil)
+ nil)))
+
+(defn- sound-path
+ "Get the path to a bundled sound file."
+ [sound-name]
+ (let [sound-file (str sound-name ".ogg")
+ sound-path (str u/project-root-directory "/mage/resources/sounds/" sound-file)]
+ (when (fs/exists? sound-path)
+ sound-path)))
+
+(defn- play-sound
+ "Play a sound file using the appropriate command for the OS with volume control.
+ Returns a future that completes when the sound finishes playing."
+ [sound-path sound-cmd volume]
+ (if (and sound-path (fs/exists? sound-path))
+ (future
+ (try
+ (let [cmd (case sound-cmd
+ ;; macOS afplay uses -v with 0.0-1.0 range
+ :afplay (str "afplay -v " (/ volume 100.0) " " sound-path)
+ ;; Linux paplay uses --volume with 0-65536 range (65536 = 100%)
+ :paplay (str "paplay --volume=" (int (* volume 655.36)) " " sound-path)
+ ;; aplay doesn't have built-in volume control
+ :aplay (str "aplay -q " sound-path)
+ nil)]
+ (when cmd
+ (process/shell {:out :string :err :string :continue true} cmd)))
+ (catch Exception _
+ ;; Silently ignore sound playback errors
+ nil)))
+ ;; Return a completed future if sound is disabled/missing
+ (future nil)))
+
+(defn- play
+ "Play a sound effect by name. Accepts keywords or strings.
+ Returns a future that completes when the sound finishes playing.
+
+ Volume is controlled by MAGE_VOLUME environment variable (0-100, default 25).
+ Sounds are disabled when MAGE_VOLUME=0 or CI environment variable is set.
+
+ Looks for .ogg files in mage/resources/sounds/.
+
+ Common sound names:
+ - :success
+ - :error
+ - :warning
+ - :ping
+
+ Examples:
+ (play :success)
+ (play \"error\")
+ (play :warning)"
+ ([sound-name]
+ (play sound-name (get-volume)))
+ ([sound-name volume]
+ (if-not (sound-disabled?)
+ (if-let [sound-cmd (get-sound-command)]
+ (let [sound-str (cond-> sound-name (keyword? sound-name) name)
+ path (sound-path sound-str)]
+ (play-sound path sound-cmd volume))
+ ;; No sound command available
+ (future nil))
+ ;; Sound is disabled
+ (future nil))))
+
+(defn success
+ "Play a success sound. Returns a future that completes when the sound finishes."
+ []
+ (play :success))
+
+(defn error
+ "Play an error sound. Returns a future that completes when the sound finishes."
+ []
+ (play :error))
+
+(defn warning
+ "Play a warning sound. Returns a future that completes when the sound finishes."
+ []
+ (play :warning))
+
+(defn ping
+ "Play a ping sound. Returns a future that completes when the sound finishes."
+ []
+ (play :ping))
+
+(comment
+
+ ;; to regenerate, from mage/resources/sounds, run:
+ ;; These sounds are CC0 / public domain, created with ffmpeg sine waves, and are small: under 6KB each.
+ ;; ffmpeg -f lavfi -i "sine=f=900:d=0.10" -f lavfi -i "sine=f=1400:d=0.16" -f lavfi -i "sine=f=1800:d=0.06" -filter_complex "[0]volume=2.4[a];[1]adelay=120|120,volume=2.7[b];[2]adelay=240|240,volume=2.9[c];[a][b][c]amix=inputs=3,alimiter=limit=0.96,afade=t=in:d=0.005,afade=t=out:st=0.23:d=0.05" -c:a libvorbis -q:a 5 success.ogg -y
+ ;; ffmpeg -f lavfi -i "sine=f=200:d=0.15" -f lavfi -i "sine=f=400:d=0.15" -f lavfi -i "sine=f=200:d=0.15" -filter_complex "[0]volume=3.3[a];[1]volume=1.8[b];[2]volume=2.6[c];[a][b][c]amix=inputs=3,alimiter=limit=0.93,afade=t=in:d=0.01,afade=t=out:st=0.4:d=0.05" -c:a libvorbis -q:a 5 error.ogg -y
+ ;; ffmpeg -f lavfi -i "sine=f=400:d=0.10" -f lavfi -i "sine=f=550:d=0.10" -filter_complex "[0]volume=3.0,afade=t=in:d=0.005,afade=t=out:st=0.08:d=0.02[a];[1]adelay=150|150,volume=2.4,afade=t=in:d=0.005,afade=t=out:st=0.08:d=0.02[b];[a][b]amix=inputs=2,alimiter=limit=0.9" -c:a libvorbis -q:a 5 warning.ogg -y
+ ;; ffmpeg -f lavfi -i "sine=f=700:d=0.07" -filter_complex "volume=2.6,alimiter=limit=0.9,afade=t=in:d=0.005,afade=t=out:st=0.05:d=0.02" -c:a libvorbis -q:a 5 ping.ogg -y
+
+ (with-redefs [get-volume (fn [] 100)]
+ (success)
+ (Thread/sleep 300)
+ (error)
+ (Thread/sleep 300)
+ (warning)
+ (Thread/sleep 300)
+ (ping))
+
+;; Check environment
+ {:sound-disabled? (sound-disabled?)
+ :get-volume (get-volume)
+ :get-os (get-os)
+ :get-sound-command (get-sound-command)})