|
1 | 1 | (ns alda.cli
|
2 |
| - (:require [boot.cli :refer (defclifn)] |
3 |
| - [boot.core :refer (merge-env!)] |
4 |
| - [alda.parser :refer (parse-input)] |
5 |
| - [alda.repl])) |
| 2 | + (:require [boot.cli :refer (defclifn)] |
| 3 | + [boot.core :refer (merge-env!)] |
| 4 | + [clojure.string :as str] |
| 5 | + [alda.parser :refer (parse-input)] |
| 6 | + [alda.repl] |
| 7 | + [alda.version :refer (-version-)])) |
6 | 8 |
|
7 | 9 | (defn fluid-r3!
|
8 | 10 | "Fetches FluidR3 dependency and returns the input stream handle."
|
|
13 | 15 | (require '[midi.soundfont.fluid-r3 :as fluid-r3])
|
14 | 16 | fluid-r3/sf2)))
|
15 | 17 |
|
16 |
| -(defclifn parse |
| 18 | +(defclifn ^:alda-task parse |
17 | 19 | "Parse some Alda code and print the results to the console."
|
18 |
| - [f file FILE str "The path to a file containing Alda code." |
19 |
| - c code CODE str "A string of Alda code." |
| 20 | + [f file FILE str "The path to a file containing Alda code to parse." |
| 21 | + c code CODE str "The string of Alda code to parse." |
20 | 22 | l lisp bool "Parse into alda.lisp code."
|
21 | 23 | m map bool "Evaluate the score and show the resulting instruments/events map."]
|
22 |
| - (let [alda-lisp-code (parse-input (if code code (slurp file)))] |
23 |
| - (when lisp |
24 |
| - (prn alda-lisp-code)) |
25 |
| - (when map |
26 |
| - (require 'alda.lisp) |
27 |
| - (println) |
28 |
| - (prn (eval alda-lisp-code))))) |
| 24 | + (if-not (or file code) |
| 25 | + (parse "--help") |
| 26 | + (let [alda-lisp-code (parse-input (if code code (slurp file)))] |
| 27 | + (when lisp |
| 28 | + (prn alda-lisp-code)) |
| 29 | + (when map |
| 30 | + (require 'alda.lisp) |
| 31 | + (println) |
| 32 | + (prn (eval alda-lisp-code)))))) |
29 | 33 |
|
30 |
| -(defclifn play |
| 34 | +(defclifn ^:alda-task play |
31 | 35 | "Parse some Alda code and play the resulting score."
|
32 |
| - [f file FILE str "The path to a file containing Alda code." |
33 |
| - c code CODE str "A string of Alda code." |
| 36 | + [f file FILE str "The path to a file containing Alda code to play." |
| 37 | + c code CODE str "The string of Alda code to play." |
34 | 38 | ; TODO: implement smart buffering and remove the buffer options
|
35 | 39 | p pre-buffer MS int "The number of milliseconds of lead time for buffering. (default: 0)"
|
36 | 40 | P post-buffer MS int "The number of milliseconds to keep the synth open after the score ends. (default: 1000)"
|
|
42 | 46 | alda.sound/*play-opts* {:pre-buffer (or pre-buffer 0)
|
43 | 47 | :post-buffer (or post-buffer 1000)
|
44 | 48 | :one-off? true}]
|
45 |
| - (let [parsed (parse-input (if code code (slurp file)))] |
46 |
| - (if (instaparse.core/failure? parsed) |
47 |
| - (prn parsed) |
48 |
| - (alda.sound/play! (eval parsed))) |
49 |
| - identity))) |
| 49 | + (if-not (or file code) |
| 50 | + (play "--help") |
| 51 | + (let [parsed (parse-input (if code code (slurp file)))] |
| 52 | + (if (instaparse.core/failure? parsed) |
| 53 | + (prn parsed) |
| 54 | + (alda.sound/play! (eval parsed))) |
| 55 | + identity)))) |
50 | 56 |
|
51 |
| -(defclifn alda-repl |
| 57 | +(defclifn ^:alda-task repl |
52 | 58 | "Starts an Alda Read-Evaluate-Play-Loop."
|
53 | 59 | [p pre-buffer MS int "The number of milliseconds of lead time for buffering. (default: 0)"
|
54 | 60 | P post-buffer MS int "The number of milliseconds to wait after the score ends. (default: 0)"
|
|
61 | 67 |
|
62 | 68 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
63 | 69 |
|
| 70 | +(def alda-tasks |
| 71 | + (-> (into {} |
| 72 | + (for [[sym var] (ns-publics *ns*) |
| 73 | + :when (:alda-task (meta var)) |
| 74 | + :let [doc (:doc (meta var)) |
| 75 | + help-blurb (apply str (take-while (partial not= \newline) doc))]] |
| 76 | + [sym help-blurb])) |
| 77 | + (merge {'help "Display this help text." |
| 78 | + 'version "Display Alda version number."}))) |
| 79 | + |
64 | 80 | (def help-text
|
65 |
| - "alda help text - TODO") |
| 81 | + (format (str "alda v%s\n\nUsage:\n\n alda <task> <options>\n\n" |
| 82 | + "To see options for a task:\n\n alda <task> --help\n\n" |
| 83 | + "Tasks:\n\n%s") |
| 84 | + -version- |
| 85 | + (str/join \newline |
| 86 | + (for [[task blurb] alda-tasks] |
| 87 | + (str " " task \tab blurb))))) |
66 | 88 |
|
67 | 89 | (defn- delegate
|
68 | 90 | [cmd args]
|
69 | 91 | (if (empty? args)
|
70 |
| - (cmd "--help") |
| 92 | + (cmd "") |
71 | 93 | (apply cmd args)))
|
72 | 94 |
|
73 | 95 | (defn -main [& [cmd & args]]
|
74 | 96 | (case cmd
|
75 |
| - nil (println help-text) |
76 |
| - "help" (println help-text) |
77 |
| - "--help" (println help-text) |
78 |
| - "parse" (delegate parse args) |
79 |
| - "play" (delegate play args) |
80 |
| - "repl" (delegate alda-repl args) |
| 97 | + nil (println help-text) |
| 98 | + "help" (println help-text) |
| 99 | + "--help" (println help-text) |
| 100 | + "-h" (println help-text) |
| 101 | + "version" (printf "alda v%s\n" -version-) |
| 102 | + "--version" (printf "alda v%s\n" -version-) |
| 103 | + "-v" (printf "alda v%s\n" -version-) |
| 104 | + "parse" (delegate parse args) |
| 105 | + "play" (delegate play args) |
| 106 | + "repl" (delegate repl args) |
81 | 107 | (printf "[alda] Invalid command '%s'.\n\n%s\n" cmd)))
|
0 commit comments