@kairotic/app 0.0.0-stage → 0.2.1
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 +9 -2
- package/index.html +1101 -0
- package/kairotic/api.cljs +362 -0
- package/kairotic/assistant.cljs +505 -0
- package/kairotic/backends/gun-backend.js +371 -0
- package/kairotic/canvas.cljs +751 -0
- package/kairotic/code.cljs +108 -0
- package/kairotic/components.cljs +838 -0
- package/kairotic/core.cljs +96 -0
- package/kairotic/corefns.cljs +473 -0
- package/kairotic/docs.mjs +2 -0
- package/kairotic/engine.cljs +482 -0
- package/kairotic/examples.mjs +96 -0
- package/kairotic/faust.cljs +225 -0
- package/kairotic/help.cljs +1036 -0
- package/kairotic/hiccup.cljs +566 -0
- package/kairotic/input.cljs +271 -0
- package/kairotic/kfile.mjs +138 -0
- package/kairotic/library.cljs +214 -0
- package/kairotic/links.cljs +41 -0
- package/kairotic/mt.cljs +1604 -0
- package/kairotic/mtboxes.cljs +276 -0
- package/kairotic/packages/analysis.mjs +64 -0
- package/kairotic/packages/controls.mjs +28 -0
- package/kairotic/packages/data.mjs +22 -0
- package/kairotic/packages/faust-units/cmaes.mjs +82 -0
- package/kairotic/packages/faust-units/graph.mjs +212 -0
- package/kairotic/packages/faust-units/listen.mjs +125 -0
- package/kairotic/packages/faust-units/notation.mjs +139 -0
- package/kairotic/packages/faust-units/parse.mjs +439 -0
- package/kairotic/packages/faust-units/recipe.mjs +195 -0
- package/kairotic/packages/faust-units/render.mjs +69 -0
- package/kairotic/packages/faust-units/units.mjs +130 -0
- package/kairotic/packages/faust-units/view.mjs +353 -0
- package/kairotic/packages/faust-units.mjs +9 -0
- package/kairotic/packages/io.mjs +58 -0
- package/kairotic/packages/net.mjs +28 -0
- package/kairotic/packages/patterns.mjs +34 -0
- package/kairotic/packages/spatial.mjs +22 -0
- package/kairotic/packages/spectral/shift.mjs +169 -0
- package/kairotic/packages/spectral.mjs +16 -0
- package/kairotic/packages/synth.mjs +28 -0
- package/kairotic/packages/theory/counterpoint.mjs +798 -0
- package/kairotic/packages/theory/mirror.mjs +221 -0
- package/kairotic/packages/theory.mjs +46 -0
- package/kairotic/patterns.cljs +538 -0
- package/kairotic/presets.cljs +227 -0
- package/kairotic/quantize.cljs +782 -0
- package/kairotic/record.cljs +169 -0
- package/kairotic/release.cljs +90 -0
- package/kairotic/ring.cljs +129 -0
- package/kairotic/ringaudio.cljs +232 -0
- package/kairotic/ringcore.mjs +270 -0
- package/kairotic/score.cljs +176 -0
- package/kairotic/shapes.cljs +283 -0
- package/kairotic/signal.cljs +327 -0
- package/kairotic/sounds.cljs +565 -0
- package/kairotic/sounds.mjs +108 -0
- package/kairotic/span.cljs +129 -0
- package/kairotic/spell.cljs +281 -0
- package/kairotic/structure.cljs +145 -0
- package/kairotic/talk.cljs +115 -0
- package/kairotic/tempo.cljs +226 -0
- package/kairotic/treecanvas.cljs +299 -0
- package/kairotic/tutorials.mjs +1283 -0
- package/kairotic/vendor/codemirror.mjs +23 -0
- package/package.json +23 -4
- package/page/00-work.cljs +862 -0
- package/page/01-commands.cljs +1219 -0
- package/page/02-patch-view.cljs +1051 -0
- package/page/03-history-view.cljs +481 -0
- package/page/04-panels.cljs +448 -0
- package/page/05-gestures.cljs +1247 -0
- package/page/06-connection.cljs +219 -0
- package/page/07-claude.cljs +430 -0
- package/page/08-sessions.cljs +423 -0
- package/page/09-learn.cljs +602 -0
- package/page/10-embed.cljs +469 -0
- package/page/11-startup.cljs +61 -0
- package/page/12-feedback.cljs +172 -0
- package/page/13-perform.cljs +325 -0
- package/page/14-editor.cljs +656 -0
- package/page/15-presets.cljs +230 -0
- package/page/16-release.cljs +242 -0
- package/page/17-sounds.cljs +147 -0
- package/page/18-lines.cljs +594 -0
- package/page/19-files.cljs +247 -0
- package/page/19-html.cljs +152 -0
- package/page/20-talk.cljs +527 -0
- package/page/21-net.cljs +110 -0
- package/page/22-learn.cljs +303 -0
- package/page/23-ids.cljs +233 -0
- package/page/24-clipboard.cljs +136 -0
- package/page/25-tidy.cljs +74 -0
- package/page/26-probes.cljs +111 -0
- package/sources.mjs +2 -0
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · sounds
|
|
3
|
+
;; Recorded and loaded sound. Its samples are too big for the history of a work: they are kept in this
|
|
4
|
+
;; browser (IndexedDB) under the hash of the samples, and the history keeps only a description,
|
|
5
|
+
;; {:audio "5a2c…" :sr 48000 :frames 96000 :channels 1 :peaks [[-0.4 0.38] …]}
|
|
6
|
+
;; whose peaks are enough to draw it anywhere. A work opened in another browser shows the sound and
|
|
7
|
+
;; says it is not there; (load-sound! :bN) brings a file in. A sound can also come from a URL,
|
|
8
|
+
;; {:url "https://…/voice.ogg" …} or only {:url "…"}
|
|
9
|
+
;; and then every browser that opens the work fetches it by itself (once: then it is kept like the rest,
|
|
10
|
+
;; under a key made from the URL), which is how a shared session or a test has the same sounds
|
|
11
|
+
;; everywhere. Two sources need no CORS of their own:
|
|
12
|
+
;; github:owner/repo[@ref]/path.wav a file of a public GitHub repository, served by jsDelivr
|
|
13
|
+
;; freesound:12345 a sound of Freesound (its preview), with the API key of whoever
|
|
14
|
+
;; opens the work, kept only in their browser: (freesound-key! "…")
|
|
15
|
+
;; A Freesound sound brings its credit ({:name :author :license :page}), kept beside the description.
|
|
16
|
+
;; The loops over samples are JavaScript (kairotic/sounds.mjs).
|
|
17
|
+
;;
|
|
18
|
+
;; sample [seconds] a buffer of sound: what reaches its left inlet is recorded on `rec` until `stop`
|
|
19
|
+
;; (or until it has `seconds`, 60 by default). Its right outlet is the buffer for a
|
|
20
|
+
;; reader~, which plays it at the position on its left (seconds; 0…1 with norm: a phasor~); a bang sends its length in
|
|
21
|
+
;; seconds on the left, and so does every new recording.
|
|
22
|
+
|
|
23
|
+
(ns kairotic.sounds
|
|
24
|
+
(:require [clojure.string :as str]
|
|
25
|
+
[kairotic.components :as comp]
|
|
26
|
+
[kairotic.api :as api]
|
|
27
|
+
[kairotic.engine :as eng]))
|
|
28
|
+
|
|
29
|
+
;; ───────── the store ─────────
|
|
30
|
+
|
|
31
|
+
(defonce lib* (atom nil))
|
|
32
|
+
(defn lib
|
|
33
|
+
"Promise of the native part (kairotic/sounds.mjs), loaded the first time a sound is needed."
|
|
34
|
+
[]
|
|
35
|
+
(or @lib* (reset! lib* ((.-kairoticImport js/window) "kairotic/sounds.mjs"))))
|
|
36
|
+
|
|
37
|
+
(defonce cache (atom {})) ; key → {:sr n :chans #js [Float32Array …]}
|
|
38
|
+
(defonce status (atom {})) ; key → :here | :missing | :checking | :loading | :failed
|
|
39
|
+
(defonce local (atom {})) ; key → {:sr :frames :channels :peaks} of a sound this browser has
|
|
40
|
+
(defonce failures (atom {})) ; key → why its URL could not be loaded
|
|
41
|
+
|
|
42
|
+
(def peak-columns 120)
|
|
43
|
+
|
|
44
|
+
;; ───────── sources: where a URL of a sound points ─────────
|
|
45
|
+
|
|
46
|
+
(defn github-spec
|
|
47
|
+
"github:owner/repo[@ref]/path → {:owner :repo :ref :path}, or nil. ref is a tag, a commit or a branch
|
|
48
|
+
without slashes (without it, jsDelivr serves the default branch, which can change: pin it)."
|
|
49
|
+
[u]
|
|
50
|
+
(when-let [[_ owner repo ref path] (and (string? u)
|
|
51
|
+
(re-matches #"github:([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)(?:@([A-Za-z0-9_.-]+))?/(.+)" u))]
|
|
52
|
+
(when-not (re-find #"\.\." path)
|
|
53
|
+
{:owner owner :repo repo :ref ref :path path})))
|
|
54
|
+
|
|
55
|
+
(defn github-url
|
|
56
|
+
"The jsDelivr URL of a github: sound (it serves public repositories with CORS open)."
|
|
57
|
+
[{:keys [owner repo ref path]}]
|
|
58
|
+
(str "https://cdn.jsdelivr.net/gh/" owner "/" repo (when ref (str "@" ref)) "/"
|
|
59
|
+
(str/join "/" (map js/encodeURIComponent (str/split path #"/")))))
|
|
60
|
+
|
|
61
|
+
(defn freesound-id "freesound:12345 → 12345, or nil." [u]
|
|
62
|
+
(when-let [[_ n] (and (string? u) (re-matches #"freesound:(\d{1,10})" u))] (js/parseInt n 10)))
|
|
63
|
+
|
|
64
|
+
(defn url?
|
|
65
|
+
"A place a sound can be fetched from: an http(s) URL, a path of this page, a data: or blob: URL, a
|
|
66
|
+
github:owner/repo/path or a freesound:id. Never javascript:, no spaces, at most 2048 characters."
|
|
67
|
+
[u]
|
|
68
|
+
(boolean
|
|
69
|
+
(and (string? u) (<= 1 (count u) 2048) (not (re-find #"\s" u))
|
|
70
|
+
(not (re-find #"(?i)^javascript:" u))
|
|
71
|
+
(or (re-find #"(?i)^(https?|data|blob):" u)
|
|
72
|
+
(github-spec u)
|
|
73
|
+
(freesound-id u)
|
|
74
|
+
;; a path of this page: no scheme, and a dot or a slash (sounds/voice.ogg, ./a.wav)
|
|
75
|
+
(and (not (re-find #"^[A-Za-z][A-Za-z0-9+.-]*:" u)) (re-find #"[./]" u))))))
|
|
76
|
+
|
|
77
|
+
;; Freesound: every person uses their own API key (freesound.org/apiv2/apply), kept in their browser and
|
|
78
|
+
;; never in a work, a link or a room
|
|
79
|
+
(def freesound-api "https://freesound.org/apiv2/")
|
|
80
|
+
(def key-storage "kairotic.freesound-key")
|
|
81
|
+
|
|
82
|
+
(defn freesound-key []
|
|
83
|
+
(try (let [k (.getItem js/localStorage key-storage)] (when (seq k) k)) (catch :default _ nil)))
|
|
84
|
+
|
|
85
|
+
(defn set-freesound-key! [k]
|
|
86
|
+
(try (if (seq k) (.setItem js/localStorage key-storage k) (.removeItem js/localStorage key-storage)) (catch :default _ nil))
|
|
87
|
+
(boolean (seq k)))
|
|
88
|
+
|
|
89
|
+
(defn license-label
|
|
90
|
+
"A short name for a license URL: CC0, CC BY, CC BY-NC, Sampling+…"
|
|
91
|
+
[u]
|
|
92
|
+
(let [u (str/lower-case (str u))]
|
|
93
|
+
(cond (re-find #"publicdomain/zero|cc0" u) "CC0"
|
|
94
|
+
(re-find #"by-nc" u) "CC BY-NC"
|
|
95
|
+
(re-find #"/by/|attribution" u) "CC BY"
|
|
96
|
+
(re-find #"sampling" u) "Sampling+"
|
|
97
|
+
(seq u) "see license"
|
|
98
|
+
:else "")))
|
|
99
|
+
|
|
100
|
+
(defn freesound-get
|
|
101
|
+
"Promise of the JSON (as a map) of a Freesound API path, with the key of this browser."
|
|
102
|
+
[path params]
|
|
103
|
+
(if-let [k (freesound-key)]
|
|
104
|
+
(-> (js/fetch (str freesound-api path "?"
|
|
105
|
+
(str/join "&" (for [[a b] (assoc params :token k)] (str (name a) "=" (js/encodeURIComponent (str b)))))))
|
|
106
|
+
(.then (fn [r] (if (.-ok r) (.json r) (throw (js/Error. (str "Freesound answered " (.-status r)
|
|
107
|
+
(when (= 401 (.-status r)) ": the API key is not valid")))))))
|
|
108
|
+
(.then (fn [j] (js->clj j :keywordize-keys true))))
|
|
109
|
+
(js/Promise.reject (js/Error. "Freesound needs an API key, kept only in this browser: (freesound-key! \"…\") — get one at freesound.org/apiv2/apply"))))
|
|
110
|
+
|
|
111
|
+
(defn freesound-credit [j]
|
|
112
|
+
{:name (str (:name j)) :author (str (:username j)) :license (license-label (:license j))
|
|
113
|
+
:page (str (or (:url j) (str "https://freesound.org/s/" (:id j) "/")))})
|
|
114
|
+
|
|
115
|
+
(defn github-hrefs
|
|
116
|
+
"Where to look for a github: sound, in order. Without @ref: @main, @master, then what jsDelivr serves
|
|
117
|
+
without a version — the latest semver tag of the repository, not its default branch, which may not
|
|
118
|
+
have the file."
|
|
119
|
+
[spec]
|
|
120
|
+
(if (:ref spec)
|
|
121
|
+
[(github-url spec)]
|
|
122
|
+
(mapv (fn [ref] (github-url (assoc spec :ref ref))) ["main" "master" nil])))
|
|
123
|
+
|
|
124
|
+
(defn resolve-url
|
|
125
|
+
"Promise of {:hrefs [url-to-fetch …] :credit {…}?} for a URL of a sound: the hrefs are tried in order
|
|
126
|
+
while they answer 404."
|
|
127
|
+
[u]
|
|
128
|
+
(cond
|
|
129
|
+
(github-spec u) (js/Promise.resolve {:hrefs (github-hrefs (github-spec u))})
|
|
130
|
+
(freesound-id u)
|
|
131
|
+
(.then (freesound-get (str "sounds/" (freesound-id u) "/") {:fields "id,name,username,license,url,previews"})
|
|
132
|
+
(fn [j]
|
|
133
|
+
(let [pv (:previews j)
|
|
134
|
+
href (or (get pv :preview-hq-mp3) (get pv :preview-hq-ogg) (get pv :preview-lq-mp3))]
|
|
135
|
+
(when-not href (throw (js/Error. "Freesound gave no preview for that sound")))
|
|
136
|
+
{:hrefs [href] :credit (freesound-credit j)})))
|
|
137
|
+
:else (js/Promise.resolve {:hrefs [u]})))
|
|
138
|
+
|
|
139
|
+
(defn fetch-first
|
|
140
|
+
"Promise of the bytes of the first of hrefs that answers; if one fails the next is tried (jsDelivr
|
|
141
|
+
answers a file it does not have without the CORS header, so a missing file fails like a refused one).
|
|
142
|
+
The error says why, in words: not found (and for github: where it looked), refused by the site (CORS)
|
|
143
|
+
or unreachable."
|
|
144
|
+
[u hrefs]
|
|
145
|
+
(let [github? (github-spec u)
|
|
146
|
+
not-found (fn [] (js/Error. (if github?
|
|
147
|
+
(str "not found: it is not in "
|
|
148
|
+
(if (:ref github?) (str "@" (:ref github?)) "@main, @master or the latest tag")
|
|
149
|
+
" of " (:owner github?) "/" (:repo github?) " — check the path, or add @branch or @commit")
|
|
150
|
+
"not found (404)")))]
|
|
151
|
+
(letfn [(go [[href & more]]
|
|
152
|
+
(-> (js/fetch href)
|
|
153
|
+
(.then (fn [r]
|
|
154
|
+
(if (.-ok r)
|
|
155
|
+
(.arrayBuffer r)
|
|
156
|
+
(throw (if (= 404 (.-status r)) (not-found) (js/Error. (str "the site answered " (.-status r))))))))
|
|
157
|
+
(.catch (fn [e]
|
|
158
|
+
(cond
|
|
159
|
+
(seq more) (go more)
|
|
160
|
+
;; fetch itself fails (a TypeError) when the site refuses the page or cannot be reached
|
|
161
|
+
(instance? js/TypeError e)
|
|
162
|
+
(throw (if github? (not-found)
|
|
163
|
+
(js/Error. "the site does not let this page load it (CORS), or cannot be reached")))
|
|
164
|
+
:else (throw e))))))]
|
|
165
|
+
(go hrefs))))
|
|
166
|
+
|
|
167
|
+
(defn as-url
|
|
168
|
+
"A URL from what reaches a sample: a string, or a word as a msg box sends it (msg https://…: a name,
|
|
169
|
+
or msg \"https://…\": a string; a keyword too). nil if it is not one."
|
|
170
|
+
[v]
|
|
171
|
+
(let [t (cond (string? v) v
|
|
172
|
+
;; the whole text of the keyword: name and namespace split it at its first slash
|
|
173
|
+
(keyword? v) (subs (str v) 1)
|
|
174
|
+
(symbol? v) (str v)
|
|
175
|
+
:else nil)
|
|
176
|
+
t (when t (str/trim t))
|
|
177
|
+
t (if (and t (re-matches #"\".*\"|'.*'" t)) (subs t 1 (dec (count t))) t)]
|
|
178
|
+
(when (url? t) t)))
|
|
179
|
+
|
|
180
|
+
(defn url-key
|
|
181
|
+
"The key a sound from a URL is kept under: 16 hex characters from the URL (two FNV-1a hashes)."
|
|
182
|
+
[u]
|
|
183
|
+
(let [h (fn [seed] (reduce (fn [h c] (unsigned-bit-shift-right (js/Math.imul (bit-xor h (.charCodeAt c 0)) 16777619) 0))
|
|
184
|
+
seed (seq u)))
|
|
185
|
+
hex (fn [x] (.padStart (.toString x 16) 8 "0"))]
|
|
186
|
+
(str (hex (h 2166136261)) (hex (h 3735928559)))))
|
|
187
|
+
|
|
188
|
+
(defn sample?
|
|
189
|
+
"A description of a sound (the data of a sample box), not an empty one."
|
|
190
|
+
[d] (and (map? d) (string? (:audio d)) (or (pos? (or (:frames d) 0)) (url? (:url d)))))
|
|
191
|
+
|
|
192
|
+
(defn about
|
|
193
|
+
"What is known of the sound of d: its description, completed with what this browser learnt when it
|
|
194
|
+
loaded it (a description that is only a URL learns its length and peaks so)."
|
|
195
|
+
[d] (merge (get @local (:audio d)) (select-keys d [:sr :frames :channels :peaks :credit])))
|
|
196
|
+
|
|
197
|
+
(defn seconds [d]
|
|
198
|
+
(let [{:keys [frames sr]} (when (sample? d) (about d))]
|
|
199
|
+
(if (and (number? frames) (number? sr) (pos? sr)) (/ frames sr) 0)))
|
|
200
|
+
|
|
201
|
+
(defn sane
|
|
202
|
+
"The description of a sound as it may be kept in the history, or {} if it is not one. A URL alone is
|
|
203
|
+
enough: its key is made from it."
|
|
204
|
+
[d]
|
|
205
|
+
(let [n (fn [x] (and (number? x) (js/isFinite x) (pos? x)))
|
|
206
|
+
url (when (and (map? d) (url? (:url d))) (:url d))
|
|
207
|
+
credit (when (map? (:credit d))
|
|
208
|
+
(let [c (into {} (for [k [:name :author :license :page] :let [v (get-in d [:credit k])]
|
|
209
|
+
:when (and (string? v) (seq v))]
|
|
210
|
+
[k (subs v 0 (min 300 (count v)))]))]
|
|
211
|
+
(when (seq c) c)))]
|
|
212
|
+
(cond
|
|
213
|
+
(and (map? d) (string? (:audio d)) (re-matches #"[0-9a-f]{8,64}" (:audio d)) (n (:sr d)) (n (:frames d)))
|
|
214
|
+
(cond-> {:audio (:audio d) :sr (:sr d) :frames (js/Math.floor (:frames d))
|
|
215
|
+
:channels (max 1 (min 32 (js/Math.floor (or (:channels d) 1))))
|
|
216
|
+
:peaks (vec (for [p (take 2000 (:peaks d)) :when (and (sequential? p) (= 2 (count p)) (every? number? p))]
|
|
217
|
+
(mapv (fn [x] (comp/clamp x -1 1)) p)))}
|
|
218
|
+
url (assoc :url url)
|
|
219
|
+
credit (assoc :credit credit))
|
|
220
|
+
url (cond-> {:audio (url-key url) :url url} credit (assoc :credit credit))
|
|
221
|
+
:else {})))
|
|
222
|
+
|
|
223
|
+
(defn sound
|
|
224
|
+
"Promise of the sound kept under hash h ({:sr :chans}), or nil if this browser does not have it."
|
|
225
|
+
[h]
|
|
226
|
+
(if-let [s (get @cache h)]
|
|
227
|
+
(js/Promise.resolve s)
|
|
228
|
+
(-> (lib)
|
|
229
|
+
(.then (fn [L] (.then (.get L h) (fn [r] [L r]))))
|
|
230
|
+
(.then (fn [[L r]]
|
|
231
|
+
(if r
|
|
232
|
+
(let [chans (.-chans r)
|
|
233
|
+
s {:sr (.-sr r) :chans chans}]
|
|
234
|
+
(swap! cache assoc h s)
|
|
235
|
+
;; what it is (length, picture), as when it was stored: a sound kept in an earlier
|
|
236
|
+
;; session is described again, so a description that is only a URL learns it too
|
|
237
|
+
(when-not (:peaks (get @local h))
|
|
238
|
+
(swap! local update h merge
|
|
239
|
+
{:sr (.-sr r) :frames (.-length (aget chans 0)) :channels (.-length chans)
|
|
240
|
+
:peaks (js->clj (.peaks L chans peak-columns))}))
|
|
241
|
+
(swap! status assoc h :here)
|
|
242
|
+
s)
|
|
243
|
+
(do (swap! status assoc h :missing) nil)))))))
|
|
244
|
+
|
|
245
|
+
(defn store!
|
|
246
|
+
"Keeps a sound under key h in this browser (memory and IndexedDB). Promise of its description."
|
|
247
|
+
[h sr chans]
|
|
248
|
+
(.then (lib)
|
|
249
|
+
(fn [L]
|
|
250
|
+
(.then (.put L h sr chans)
|
|
251
|
+
(fn [_]
|
|
252
|
+
(let [d {:audio h :sr sr :frames (.-length (aget chans 0)) :channels (.-length chans)
|
|
253
|
+
:peaks (js->clj (.peaks L chans peak-columns))}]
|
|
254
|
+
(swap! cache assoc h {:sr sr :chans chans})
|
|
255
|
+
(swap! local assoc h (dissoc d :audio))
|
|
256
|
+
(swap! status assoc h :here)
|
|
257
|
+
d))))))
|
|
258
|
+
|
|
259
|
+
(defn keep!
|
|
260
|
+
"Keeps a sound (sample rate, a JS array of Float32Array, one per channel) in this browser, under the
|
|
261
|
+
hash of its samples. Promise of its description, the data of a sample box."
|
|
262
|
+
[sr chans]
|
|
263
|
+
(.then (lib) (fn [L] (.then (.hash L chans sr) (fn [h] (store! h sr chans))))))
|
|
264
|
+
|
|
265
|
+
(defn decode
|
|
266
|
+
"Promise of a sound ({:sr :chans}, up to 8 channels) from the bytes of a file, decoded at the sample
|
|
267
|
+
rate of the engine."
|
|
268
|
+
[buf]
|
|
269
|
+
(.then (.decodeAudioData (eng/ctx) buf)
|
|
270
|
+
(fn [ab] {:sr (.-sampleRate ab)
|
|
271
|
+
:chans (into-array (for [c (range (min 8 (.-numberOfChannels ab)))]
|
|
272
|
+
(js/Float32Array. (.getChannelData ab c))))})))
|
|
273
|
+
|
|
274
|
+
(defonce fetching (atom {})) ; key → Promise
|
|
275
|
+
|
|
276
|
+
(defn fetch!
|
|
277
|
+
"Promise of the sound at url, fetched, decoded and kept under key h (once, whoever asks first)."
|
|
278
|
+
[h url]
|
|
279
|
+
(or (get @fetching h)
|
|
280
|
+
(let [credit (atom nil)
|
|
281
|
+
p (-> (resolve-url url)
|
|
282
|
+
(.then (fn [{:keys [hrefs] c :credit}]
|
|
283
|
+
(reset! credit c)
|
|
284
|
+
(fetch-first url hrefs)))
|
|
285
|
+
(.then (fn [buf] (.catch (decode buf)
|
|
286
|
+
(fn [e] (throw (js/Error. (str "it is not a sound this browser can decode (" (or (.-message e) e) ")")))))))
|
|
287
|
+
(.then (fn [{:keys [sr chans]}] (store! h sr chans)))
|
|
288
|
+
(.then (fn [_]
|
|
289
|
+
(when-let [c @credit] (swap! local assoc-in [h :credit] c))
|
|
290
|
+
(get @cache h)))
|
|
291
|
+
(.catch (fn [e]
|
|
292
|
+
(swap! status assoc h :failed)
|
|
293
|
+
(swap! failures assoc h (str (or (.-message e) e)))
|
|
294
|
+
(swap! fetching dissoc h)
|
|
295
|
+
(throw (js/Error. (str "could not load " url ": " (or (.-message e) e)))))))]
|
|
296
|
+
(swap! status assoc h :loading)
|
|
297
|
+
(swap! fetching assoc h p)
|
|
298
|
+
p)))
|
|
299
|
+
|
|
300
|
+
(defn sound-for
|
|
301
|
+
"Promise of the sound of a description ({:sr :chans}): from this browser, or fetched from its URL if
|
|
302
|
+
it has one; nil if neither."
|
|
303
|
+
[d]
|
|
304
|
+
(let [h (:audio d)]
|
|
305
|
+
(.then (sound h) (fn [s] (if (or s (not (url? (:url d)))) s (fetch! h (:url d)))))))
|
|
306
|
+
|
|
307
|
+
(defn check!
|
|
308
|
+
"Finds out (once) whether this browser has the sound of d, fetching it if it comes from a URL:
|
|
309
|
+
(get @status (:audio d)) then says it."
|
|
310
|
+
[d]
|
|
311
|
+
(let [h (:audio d)]
|
|
312
|
+
(when (and (string? h) (nil? (get @status h)))
|
|
313
|
+
(swap! status assoc h :checking)
|
|
314
|
+
;; why it could not be loaded is said once, in the REPL
|
|
315
|
+
(.catch (sound-for d) (fn [e] (eng/print! (eng/now) (str "sample: " (or (.-message e) e))))))))
|
|
316
|
+
|
|
317
|
+
;; ───────── playing: the worklet a reader~ uses for a sound ─────────
|
|
318
|
+
|
|
319
|
+
(def player-source
|
|
320
|
+
"class KairoticPlayer extends AudioWorkletProcessor {
|
|
321
|
+
// Plays a sound at the position of its input — seconds, or 0…1 from the start to the end with norm —,
|
|
322
|
+
// interpolating between samples: output channel c plays channel c of the sound (or its last one); after V
|
|
323
|
+
// value channels come G gate channels, 1 while it has a sound. A message {chans, sr, norm} gives it the sound.
|
|
324
|
+
constructor(o) {
|
|
325
|
+
super();
|
|
326
|
+
const p = (o && o.processorOptions) || {};
|
|
327
|
+
this.V = p.V || 1; this.G = p.G || 0; this.chans = null; this.sr = sampleRate; this.norm = true;
|
|
328
|
+
this.port.onmessage = (e) => {
|
|
329
|
+
const d = e.data;
|
|
330
|
+
this.chans = d.chans && d.chans.length ? d.chans : null;
|
|
331
|
+
if (d.sr) this.sr = d.sr;
|
|
332
|
+
this.norm = d.norm !== false;
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
process(inputs, outputs) {
|
|
336
|
+
const inp = inputs[0] && inputs[0][0], out = outputs[0], ch = this.chans;
|
|
337
|
+
const L = out[0].length, n = ch ? ch[0].length : 0;
|
|
338
|
+
for (let i = 0; i < L; i++) {
|
|
339
|
+
let p = inp ? inp[i] : 0;
|
|
340
|
+
if (!this.norm && n > 1) p = p * this.sr / (n - 1);
|
|
341
|
+
p = p < 0 ? 0 : (p > 1 ? 1 : p);
|
|
342
|
+
const f = p * (n - 1), k = Math.floor(f), r = f - k;
|
|
343
|
+
for (let c = 0; c < out.length; c++) {
|
|
344
|
+
if (c < this.V) {
|
|
345
|
+
if (n < 2) { out[c][i] = 0; continue; }
|
|
346
|
+
const s = ch[Math.min(c, ch.length - 1)];
|
|
347
|
+
out[c][i] = k + 1 < n ? s[k] + (s[k + 1] - s[k]) * r : s[n - 1];
|
|
348
|
+
} else out[c][i] = n ? 1 : 0;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
registerProcessor('kairotic-player', KairoticPlayer);
|
|
355
|
+
")
|
|
356
|
+
|
|
357
|
+
(defn player
|
|
358
|
+
"Promise of a player worklet with V value channels and G gate channels (the outlets of a reader~)."
|
|
359
|
+
[ac V G]
|
|
360
|
+
(api/worklet-node ac "kairotic-player" player-source
|
|
361
|
+
{:numberOfInputs 1 :numberOfOutputs 1 :outputChannelCount [(+ V G)]
|
|
362
|
+
:channelCount 1 :channelCountMode "explicit" :processorOptions {:V V :G G}}))
|
|
363
|
+
|
|
364
|
+
(defn give!
|
|
365
|
+
"Gives a player the sound s ({:chans}), or none (nil)."
|
|
366
|
+
([pl s] (give! pl s true))
|
|
367
|
+
([pl s norm]
|
|
368
|
+
(.postMessage (.-port pl) #js {:chans (if s (:chans s) #js []) :sr (when s (:sr s)) :norm (boolean norm)})))
|
|
369
|
+
|
|
370
|
+
;; ───────── the sample box ─────────
|
|
371
|
+
|
|
372
|
+
(def block 4096)
|
|
373
|
+
|
|
374
|
+
(defn sample-seconds [x] (comp/clamp (or (comp/num-of x) 60) 0.1 600))
|
|
375
|
+
|
|
376
|
+
(defn splice
|
|
377
|
+
"The channels of an old sound (a JS array of Float32Array, or nil) until frame n0 — silence where it is
|
|
378
|
+
shorter —, then the new samples s, in every channel; with after, what the old sound had after the new
|
|
379
|
+
samples end follows them (punch-in/out)."
|
|
380
|
+
([old n0 s] (splice old n0 s false))
|
|
381
|
+
([old n0 s after]
|
|
382
|
+
(let [C (if old (max 1 (.-length old)) 1)
|
|
383
|
+
n1 (+ n0 (.-length s))
|
|
384
|
+
len (if (and after old) (max n1 (.-length (aget old 0))) n1)]
|
|
385
|
+
(into-array
|
|
386
|
+
(for [c (range C)]
|
|
387
|
+
(let [out (js/Float32Array. len)
|
|
388
|
+
o (when old (aget old c))]
|
|
389
|
+
(when o (.set out (.subarray o 0 (min n0 (.-length o)))))
|
|
390
|
+
(.set out s n0)
|
|
391
|
+
(when (and after o (> (.-length o) n1)) (.set out (.subarray o n1) n1))
|
|
392
|
+
out))))))
|
|
393
|
+
|
|
394
|
+
(defonce takes (atom [])) ; what the last takes did, step by step (for debugging and tests)
|
|
395
|
+
(defn step! [x] (swap! takes (fn [t] (vec (take-last 30 (conj t x))))))
|
|
396
|
+
|
|
397
|
+
(defn finish!
|
|
398
|
+
"The end of a recording: its blocks become one sound, kept in this browser and written as the data
|
|
399
|
+
of the box (through the page, an event of the history); the box hears it on inlet :audio."
|
|
400
|
+
[node id]
|
|
401
|
+
(when-let [{:keys [blocks sr max from base ovr]} @(:rec node)]
|
|
402
|
+
(reset! (:rec node) nil)
|
|
403
|
+
(step! [:finish id (count blocks) from])
|
|
404
|
+
(-> (lib)
|
|
405
|
+
(.then (fn [L]
|
|
406
|
+
(let [s (.concat L (clj->js blocks) max)]
|
|
407
|
+
(step! [:joined id (.-length s)])
|
|
408
|
+
(cond
|
|
409
|
+
(zero? (.-length s)) (js/Promise.reject (js/Error. "nothing was recorded"))
|
|
410
|
+
;; rec t: the sound it had until t, then the take (punch-in)
|
|
411
|
+
(some? from)
|
|
412
|
+
(.then (if (sample? base) (sound-for base) (js/Promise.resolve nil))
|
|
413
|
+
(fn [old]
|
|
414
|
+
(step! [:old id (some? old) (:sr old)])
|
|
415
|
+
(cond
|
|
416
|
+
(and (sample? base) (nil? old))
|
|
417
|
+
(throw (js/Error. "the sound to record over is not in this browser"))
|
|
418
|
+
(and old (not= (:sr old) sr))
|
|
419
|
+
(throw (js/Error. (str "the sound to record over is at " (:sr old) " Hz, the engine at " sr " Hz")))
|
|
420
|
+
:else (keep! sr (splice (:chans old) (js/Math.floor (* from sr)) s ovr)))))
|
|
421
|
+
:else (keep! sr #js [s])))))
|
|
422
|
+
(.then (fn [d]
|
|
423
|
+
(step! [:kept id (:frames d)])
|
|
424
|
+
(api/effect! id [:sample [:set d]])
|
|
425
|
+
(api/report! id {:done d} nil)))
|
|
426
|
+
(.catch (fn [e]
|
|
427
|
+
(step! [:failed id (str (or (.-message e) e))])
|
|
428
|
+
(js/console.warn "sample:" e)
|
|
429
|
+
(api/report! id {:error (str "sample: " (or (.-message e) e))} nil))))))
|
|
430
|
+
|
|
431
|
+
(defn start!
|
|
432
|
+
"rec: a new take (unless one is going on); the box hears that it started. With {:from t :base d} the
|
|
433
|
+
take goes after the first t seconds of the sound d (punch-in)."
|
|
434
|
+
[node at opts]
|
|
435
|
+
(when-not @(:rec node)
|
|
436
|
+
(let [sr (.-sampleRate (:ac node))]
|
|
437
|
+
(reset! (:rec node) (merge {:blocks [] :n 0 :sr sr :max (js/Math.floor (* sr @(:seconds node)))}
|
|
438
|
+
(when (map? opts) (select-keys opts [:from :base :ovr]))))
|
|
439
|
+
(when-let [w @(:tap node)] (api/retap w {:active true}))
|
|
440
|
+
(api/report! (:id node) {:started true} at))))
|
|
441
|
+
|
|
442
|
+
(defn stop!
|
|
443
|
+
"stop: what was heard until now (the worklet hands its last samples at once) becomes the take."
|
|
444
|
+
[node]
|
|
445
|
+
(step! [:stop (:id node) (some? @(:rec node)) (some? @(:tap node))])
|
|
446
|
+
(when @(:rec node)
|
|
447
|
+
(if-let [w @(:tap node)]
|
|
448
|
+
(api/retap w {:flush true :active false})
|
|
449
|
+
(finish! node (:id node)))))
|
|
450
|
+
|
|
451
|
+
(defn on-block!
|
|
452
|
+
"A block heard while recording: kept; at the end (stop, or the length of the box) the take is finished."
|
|
453
|
+
[node data {:keys [last]}]
|
|
454
|
+
(when-let [r @(:rec node)]
|
|
455
|
+
(let [b (first data)
|
|
456
|
+
r (swap! (:rec node) (fn [r] (-> r (update :blocks conj b) (update :n + (.-length b)))))
|
|
457
|
+
full (>= (:n r) (:max r))]
|
|
458
|
+
(when full (when-let [w @(:tap node)] (api/retap w {:active false})))
|
|
459
|
+
(if (or last full)
|
|
460
|
+
(finish! node (:id node))
|
|
461
|
+
(api/report! (:id node) {:progress (+ (or (:from r) 0) (/ (:n r) (:sr r))) :level (when-let [L @(:L node)] (.peak L b))} nil)))))
|
|
462
|
+
|
|
463
|
+
(defn wave-points
|
|
464
|
+
"The waveform of a description: its peaks as a zigzag from x0 to x1 around the line at y mid."
|
|
465
|
+
[peaks x0 x1 mid half]
|
|
466
|
+
(let [k (count peaks)]
|
|
467
|
+
(vec (for [i (range k) :let [[lo hi] (nth peaks i)
|
|
468
|
+
x (+ x0 (* (- x1 x0) (/ i (max 1 (dec k)))))]
|
|
469
|
+
y [(- mid (* hi half)) (- mid (* lo half))]]
|
|
470
|
+
[x y]))))
|
|
471
|
+
|
|
472
|
+
(defn fmt-s [s] (str (comp/nice s) " s"))
|
|
473
|
+
|
|
474
|
+
(comp/define! "sample"
|
|
475
|
+
{:doc "A buffer of sound. What reaches its left inlet is recorded on `:rec` until `:stop` (or until it has seconds, 60 by default), replacing what it had; :rec t (msg :rec 2.5) keeps its first t seconds and records from there (punch-in); :ovr t (msg :ovr 2.5) replaces only what the take covers and keeps what comes after (punch-in/out); `:clear` empties it. Its right outlet is the buffer for a reader~, which plays it at the position on its left, in seconds (or with reader~ norm from 0 to 1: a phasor~); a bang sends its length in seconds on the left, and so does every new recording. The sound stays in this browser; the work keeps its picture. A URL (a string on the left, or (load-sound! :bN \"https://…\")) loads a sound that every browser opening the work fetches by itself. (load-sound! :bN) brings in a file, (save-sound :bN) saves it as WAV."
|
|
476
|
+
:usage "sample [seconds]"
|
|
477
|
+
:license "MPL-2.0"
|
|
478
|
+
:io [1 2]
|
|
479
|
+
:size [240 72]
|
|
480
|
+
:args [[:seconds 60]]
|
|
481
|
+
:model {:empty {}
|
|
482
|
+
:step (fn [d [op x]] (case op :set (sane x) :clear {} d))
|
|
483
|
+
:load sane
|
|
484
|
+
:code (fn [id d] (when (sample? d) [(str "(sample! " id " " (pr-str d) ")")]))}
|
|
485
|
+
:receive (fn [{:keys [inlet value state data]}]
|
|
486
|
+
(let [rec? (:rec state)]
|
|
487
|
+
(cond
|
|
488
|
+
(= inlet :audio)
|
|
489
|
+
(cond
|
|
490
|
+
(:started value) {:state (assoc state :rec true) :show {:rec 0}}
|
|
491
|
+
(:progress value) (if rec? {:show {:rec (:progress value) :level (:level value)}} {})
|
|
492
|
+
(:done value) {:state (dissoc state :rec) :show nil :out [[0 (seconds (:done value))]]}
|
|
493
|
+
(:error value) {:state (dissoc state :rec) :show nil :effects [[:print (:error value)]]}
|
|
494
|
+
:else {})
|
|
495
|
+
;; the node says when it starts (only while ▶ is on: without it there is no sound to record)
|
|
496
|
+
(= value :rec) {:effects [[:rec true]]}
|
|
497
|
+
;; rec t (msg :rec 2.5): the sound until t stays, the take replaces what comes after (punch-in)
|
|
498
|
+
(and (vector? value) (= :rec (first value)) (number? (second value)) (>= (second value) 0))
|
|
499
|
+
{:effects [[:rec {:from (second value) :base (when (sample? data) data)}]]}
|
|
500
|
+
;; ovr t (msg :ovr 2.5): only what the take covers is replaced, what comes after it stays (punch-in/out)
|
|
501
|
+
(or (= value :ovr) (and (vector? value) (= :ovr (first value)) (number? (second value)) (>= (second value) 0)))
|
|
502
|
+
{:effects [[:rec {:from (if (vector? value) (second value) 0) :base (when (sample? data) data) :ovr true}]]}
|
|
503
|
+
(= value :stop) {:effects [[:rec false]]}
|
|
504
|
+
(= value :bang) (if (sample? data) {:out [[0 (seconds data)]]} {})
|
|
505
|
+
(= value :clear) {:effects [[:sample [:clear]]]}
|
|
506
|
+
;; a URL (a string, or [:url "…"]): the page loads it and writes it as the data of the box
|
|
507
|
+
;; msg https://… (or msg "https://…", or msg :url "…") arrives as a name or a string: as-url reads it
|
|
508
|
+
(as-url value) {:effects [[:sample [:url (as-url value)]]]}
|
|
509
|
+
(and (sequential? value) (= :url (first value)) (as-url (second value)))
|
|
510
|
+
{:effects [[:sample [:url (as-url (second value))]]]}
|
|
511
|
+
(and (sequential? value) (= :url (first value)))
|
|
512
|
+
{:effects [[:print (str "sample: " (pr-str (second value)) " is not a URL a sound can be loaded from")]]}
|
|
513
|
+
(or (string? value) (keyword? value) (symbol? value))
|
|
514
|
+
{:effects [[:print (str "sample: " (pr-str value) " is neither :rec, :stop, :clear nor a URL (https://…, github:…, freesound:…)")]]}
|
|
515
|
+
:else {})))
|
|
516
|
+
;; drawing it is also when this browser looks for its sound (and fetches it, if it comes from a URL)
|
|
517
|
+
:draw (fn [{:keys [data shown w h]}]
|
|
518
|
+
(let [w (or w 240) h (or h 72) mid (/ (- h 12) 2) d (when (sample? data) (merge data (about data)))
|
|
519
|
+
here (when d (get @status (:audio d)))]
|
|
520
|
+
(when d (check! data))
|
|
521
|
+
[[:rect {:x 2 :y 2 :w (- w 4) :h (- h 4) :r 4 :stroke :grid :fill :bg}]
|
|
522
|
+
[:line {:x1 6 :y1 (+ 2 mid) :x2 (- w 6) :y2 (+ 2 mid) :stroke :grid}]
|
|
523
|
+
(when (seq (:peaks d)) [:path {:points (wave-points (:peaks d) 6 (- w 6) (+ 2 mid) (- mid 4))
|
|
524
|
+
:stroke (if (#{:missing :failed} here) :soft :accent) :weight 1}])
|
|
525
|
+
(if (:rec shown)
|
|
526
|
+
[:text {:x 8 :y (- h 7) :size 10 :fill :kairos} (str "● rec " (fmt-s (:rec shown)))]
|
|
527
|
+
[:text {:x 8 :y (- h 7) :size 9 :fill :soft}
|
|
528
|
+
(cond
|
|
529
|
+
(and d (= here :loading)) "loading…"
|
|
530
|
+
(and d (= here :failed))
|
|
531
|
+
(if (re-find #"API key" (str (get @failures (:audio d)))) "needs a Freesound API key" "could not load its URL")
|
|
532
|
+
(and d (number? (:sr d)))
|
|
533
|
+
(str (fmt-s (seconds d)) " · " (comp/nice (/ (:sr d) 1000)) " kHz"
|
|
534
|
+
(when (> (or (:channels d) 1) 1) (str " · " (:channels d) " ch"))
|
|
535
|
+
(if-let [c (:credit d)]
|
|
536
|
+
(str " · " (:author c) (when (seq (:license c)) (str " · " (:license c))))
|
|
537
|
+
(when (:url d) " · URL"))
|
|
538
|
+
(when (= here :missing) " · not in this browser"))
|
|
539
|
+
d "…"
|
|
540
|
+
:else "empty · rec records what reaches it")])
|
|
541
|
+
(when (and (:rec shown) (number? (:level shown)))
|
|
542
|
+
[:rect {:x (- w 70) :y (- h 13) :w (* 60 (comp/clamp (:level shown) 0 1)) :h 4 :r 1 :stroke nil :fill :kairos}])]))
|
|
543
|
+
:audio {:create (fn [ac b _]
|
|
544
|
+
(let [in (.createGain ac)
|
|
545
|
+
secs (sample-seconds (:seconds (comp/args-for (comp/lookup "sample") (:text b))))
|
|
546
|
+
node {:ac ac :id (:id b) :in in :seconds (atom secs) :tap (atom nil) :L (atom nil) :rec (atom nil)
|
|
547
|
+
:alive (atom true) :inlets [[in 0]] :outlets []}]
|
|
548
|
+
(.then (lib) (fn [L] (reset! (:L node) L)))
|
|
549
|
+
(-> (api/tap ac {:mode :frames :size block :hop block :active false}
|
|
550
|
+
(fn [data _ _ flags] (on-block! node data flags)))
|
|
551
|
+
(.then (fn [w]
|
|
552
|
+
(if @(:alive node)
|
|
553
|
+
(do (.connect in w) (reset! (:tap node) w)
|
|
554
|
+
(when @(:rec node) (api/retap w {:active true})))
|
|
555
|
+
(try (.disconnect w) (catch :default _ nil)))))
|
|
556
|
+
(.catch (fn [e] (js/console.error "sample" e))))
|
|
557
|
+
node))
|
|
558
|
+
:update (fn [node b _]
|
|
559
|
+
(reset! (:seconds node) (sample-seconds (:seconds (comp/args-for (comp/lookup "sample") (:text b))))))
|
|
560
|
+
:effects {:rec (fn [node on at] (if on (start! node at on) (stop! node)))}
|
|
561
|
+
;; ■ while recording keeps what was heard until then
|
|
562
|
+
:dispose (fn [node]
|
|
563
|
+
(reset! (:alive node) false)
|
|
564
|
+
(when @(:rec node) (finish! node (:id node)))
|
|
565
|
+
(try (.disconnect (:in node)) (catch :default _ nil)))}})
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
// Kairotic · sounds (the native part of kairotic/sounds.cljs)
|
|
3
|
+
// Recorded or loaded sound is too big for the history: it is kept in this browser (IndexedDB), by the
|
|
4
|
+
// hash of its samples, and the history keeps only {:audio hash :sr :frames :channels :peaks}. The loops
|
|
5
|
+
// over samples live here, in JavaScript, not in the interpreted ClojureScript of the page.
|
|
6
|
+
|
|
7
|
+
const DB = "kairotic-sounds", STORE = "sounds";
|
|
8
|
+
|
|
9
|
+
/** One Float32Array from blocks (Float32Arrays) in order, at most `max` samples. */
|
|
10
|
+
function concat(blocks, max) {
|
|
11
|
+
let n = 0;
|
|
12
|
+
for (const b of blocks) n += b.length;
|
|
13
|
+
if (typeof max === "number" && max >= 0) n = Math.min(n, max);
|
|
14
|
+
const out = new Float32Array(n);
|
|
15
|
+
let o = 0;
|
|
16
|
+
for (const b of blocks) {
|
|
17
|
+
if (o >= n) break;
|
|
18
|
+
const k = Math.min(b.length, n - o);
|
|
19
|
+
out.set(k === b.length ? b : b.subarray(0, k), o);
|
|
20
|
+
o += k;
|
|
21
|
+
}
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The largest absolute value of a Float32Array. */
|
|
26
|
+
function peak(f) {
|
|
27
|
+
let p = 0;
|
|
28
|
+
for (let i = 0; i < f.length; i++) { const a = f[i] < 0 ? -f[i] : f[i]; if (a > p) p = a; }
|
|
29
|
+
return p;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** n columns [min, max] over every channel, rounded to 2 decimals: the picture of a sound. */
|
|
33
|
+
function peaks(chans, n) {
|
|
34
|
+
const frames = chans.length ? chans[0].length : 0, out = [];
|
|
35
|
+
if (!frames) return out;
|
|
36
|
+
n = Math.max(1, Math.min(n | 0 || 200, frames));
|
|
37
|
+
for (let k = 0; k < n; k++) {
|
|
38
|
+
const a = Math.floor(k * frames / n), b = Math.max(a + 1, Math.floor((k + 1) * frames / n));
|
|
39
|
+
let lo = Infinity, hi = -Infinity;
|
|
40
|
+
for (const c of chans) for (let i = a; i < b; i++) { const x = c[i]; if (x < lo) lo = x; if (x > hi) hi = x; }
|
|
41
|
+
out.push([Math.round(lo * 100) / 100, Math.round(hi * 100) / 100]);
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Promise of the hash of a sound (its sample rate and samples): 32 hex characters of SHA-256. */
|
|
47
|
+
async function hash(chans, sr) {
|
|
48
|
+
const head = new Float64Array([sr, chans.length]);
|
|
49
|
+
const parts = [new Uint8Array(head.buffer)];
|
|
50
|
+
for (const c of chans) parts.push(new Uint8Array(c.buffer, c.byteOffset, c.byteLength));
|
|
51
|
+
const total = parts.reduce((s, p) => s + p.length, 0), bytes = new Uint8Array(total);
|
|
52
|
+
let o = 0;
|
|
53
|
+
for (const p of parts) { bytes.set(p, o); o += p.length; }
|
|
54
|
+
const d = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
55
|
+
return Array.from(d.subarray(0, 16), (x) => x.toString(16).padStart(2, "0")).join("");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let opening = null;
|
|
59
|
+
function db() {
|
|
60
|
+
if (!opening) opening = new Promise((resolve, reject) => {
|
|
61
|
+
if (typeof indexedDB === "undefined") { reject(new Error("this browser keeps no sounds (no IndexedDB)")); return; }
|
|
62
|
+
const r = indexedDB.open(DB, 1);
|
|
63
|
+
r.onupgradeneeded = () => r.result.createObjectStore(STORE);
|
|
64
|
+
r.onsuccess = () => resolve(r.result);
|
|
65
|
+
r.onerror = () => { opening = null; reject(r.error); };
|
|
66
|
+
});
|
|
67
|
+
return opening;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Keeps a sound under its hash. Promise of true. */
|
|
71
|
+
async function put(h, sr, chans) {
|
|
72
|
+
const d = await db();
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const t = d.transaction(STORE, "readwrite");
|
|
75
|
+
t.objectStore(STORE).put({ sr, chans }, h);
|
|
76
|
+
t.oncomplete = () => resolve(true);
|
|
77
|
+
t.onerror = () => reject(t.error);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Promise of the sound kept under a hash ({sr, chans}), or null if this browser does not have it. */
|
|
82
|
+
async function get(h) {
|
|
83
|
+
const d = await db();
|
|
84
|
+
return new Promise((resolve, reject) => {
|
|
85
|
+
const r = d.transaction(STORE, "readonly").objectStore(STORE).get(h);
|
|
86
|
+
r.onsuccess = () => resolve(r.result || null);
|
|
87
|
+
r.onerror = () => reject(r.error);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A WAV file (16-bit PCM) of a sound, as a Blob. */
|
|
92
|
+
function wav(chans, sr) {
|
|
93
|
+
const C = chans.length, n = C ? chans[0].length : 0, bytes = 44 + n * C * 2;
|
|
94
|
+
const v = new DataView(new ArrayBuffer(bytes));
|
|
95
|
+
const s = (o, t) => { for (let i = 0; i < t.length; i++) v.setUint8(o + i, t.charCodeAt(i)); };
|
|
96
|
+
s(0, "RIFF"); v.setUint32(4, bytes - 8, true); s(8, "WAVE"); s(12, "fmt ");
|
|
97
|
+
v.setUint32(16, 16, true); v.setUint16(20, 1, true); v.setUint16(22, C, true); v.setUint32(24, sr, true);
|
|
98
|
+
v.setUint32(28, sr * C * 2, true); v.setUint16(32, C * 2, true); v.setUint16(34, 16, true);
|
|
99
|
+
s(36, "data"); v.setUint32(40, n * C * 2, true);
|
|
100
|
+
let o = 44;
|
|
101
|
+
for (let i = 0; i < n; i++) for (let c = 0; c < C; c++) {
|
|
102
|
+
const x = Math.max(-1, Math.min(1, chans[c][i]));
|
|
103
|
+
v.setInt16(o, x < 0 ? x * 0x8000 : x * 0x7fff, true); o += 2;
|
|
104
|
+
}
|
|
105
|
+
return new Blob([v.buffer], { type: "audio/wav" });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export default { concat, peak, peaks, hash, put, get, wav };
|