Skip to content

Commit

Permalink
fake waveform
Browse files Browse the repository at this point in the history
  • Loading branch information
samharad committed Jan 6, 2025
1 parent 409eb78 commit 9d8f102
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 81 deletions.
9 changes: 0 additions & 9 deletions src/dev/practaid/mock_events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,6 @@
(rf/dispatch [:practaid.looper/confirm-refresh-recently-played
{:items [{:track mock-item}]}])

(and (= method :get)
(str/starts-with? uri "https://api.spotify.com/v1/audio-analysis"))
(rf/dispatch [:practaid.looper/confirm-track-analysis
{:segments (->> (range 20)
(map #(identity {:duration 1000
:start (* % 1000)
:loudness_max (- (rand-int 30)
30)})))}])

:else (real-handler req))))))

(mock-fx
Expand Down
6 changes: 3 additions & 3 deletions src/practaid/auth.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"user-modify-playback-state"
;; Determine whether the user has Spotify premium
"user-read-private"
;; Including this prevents a seemingly benign console error, but
;; we don't actually need it
#_"user-read-email"]))
;; I think this is required
"user-read-email"
,]))



Expand Down
1 change: 0 additions & 1 deletion src/practaid/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
:practaid.looper/state {:loop-start-ms nil
:loop-end-ms nil
:loop-timeout-id nil
:track-analysis nil
:album-colors nil}})

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
33 changes: 0 additions & 33 deletions src/practaid/looper.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
(s/def ::loop-start-ms (s/nilable integer?))
(s/def ::loop-end-ms(s/nilable integer?))
(s/def ::loop-timeout-id (s/nilable any?))
(s/def ::track-analysis (s/nilable any?))
(s/def ::album-colors (s/nilable (s/coll-of
(s/coll-of integer?))))

(s/def ::state (s/keys :req-un [::loop-start-ms
::loop-end-ms
::loop-timeout-id
::track-analysis
::album-colors]))

(rf/reg-event-fx
Expand All @@ -45,8 +43,6 @@

{:db db
:fx [(when is-different-track [:dispatch [::clear-looper]])
;; TODO where does this one belong...
(when is-different-track [:dispatch [::refresh-track-analysis]])
(when clear-loop-timeout
[:practaid.common/clear-timeout loop-timeout-id])
(when set-loop-timeout
Expand Down Expand Up @@ -206,34 +202,6 @@



(rf/reg-event-fx
::refresh-track-analysis
[inject-store
check-db-spec-interceptor]
(fn [{:keys [db store]} _]
(let [{player-state ::player/state} db
{:keys [external-playback-state]} player-state
access-token (:access-token store)
track (db/playback-track db)
track-id (or (:id track)
(get-in external-playback-state [:item :id]))]
(when track-id
{:fx [[:http-xhrio {:method :get
:uri (str "https://api.spotify.com/v1/audio-analysis/" track-id)
:headers {"Authorization" (str "Bearer " access-token)
"Content-Type" "application/json"}
:response-format (ajax/json-response-format {:keywords? true})
:on-success [::confirm-track-analysis]
:on-failure [:practaid.common/http-request-failure]}]]}))))

(rf/reg-event-db
::confirm-track-analysis
[check-db-spec-interceptor]
(fn [db [_ track-analysis]]
(assoc-in db [::state :track-analysis] track-analysis)))



(rf/reg-event-fx
::album-cover-img-loaded
[check-db-spec-interceptor]
Expand Down Expand Up @@ -283,5 +251,4 @@
;; TODO: store the outcome ID
[:practaid.common/set-interval {:f #(rf/dispatch [:practaid.player/refresh-playback-state])
:interval-ms 5000}]
[:dispatch [:practaid.looper/refresh-track-analysis]]
[:dispatch [:practaid.player/refresh-recently-played]]]}))
39 changes: 4 additions & 35 deletions src/practaid/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
(fn [db]
(get-in db [::player/state :recently-played])))

(rf/reg-sub
::track-analysis
(fn [db]
(get-in db [::looper/state :track-analysis])))

(rf/reg-sub
::loop-start-ms
(fn [db]
Expand Down Expand Up @@ -139,36 +134,10 @@

(rf/reg-sub
::loudness-samples
:<- [::track-analysis]
(fn [{:keys [segments]} [_ opts]]
(when (not-empty segments)
(let [{:keys [num-samples decibel-floor decibel-ceil]}
(merge {:num-samples 300 :decibel-floor -35 :decibel-ceil 0} opts)
total-duration (->> segments
(map :duration)
(reduce +))
regularized (loop [[seg & more-segs :as segments] segments
acc []]
(if (= (count acc) num-samples)
acc
(let [{:keys [start duration]} seg
mark (/ (count acc) num-samples)
seg-start (/ start total-duration)
seg-end (/ (+ start duration) total-duration)]
(if (and (<= seg-start mark)
(< mark seg-end))
(recur segments (conj acc (dissoc seg :start :duration)))
(recur more-segs acc)))))
clamped (map (fn [seg]
(let [clamped (min decibel-ceil
(max decibel-floor (:loudness_max seg)))]
(- 1 (/ clamped decibel-floor))))
regularized)
max-loudness (reduce #(max %1 %2) clamped)]
(map (fn [loudness]
(/ (js/Math.round (* (/ loudness max-loudness) 100))
100))
clamped)))))
(fn [& _]
(repeatedly 300 rand)))



(rf/reg-sub
::most-recently-played-track
Expand Down

0 comments on commit 9d8f102

Please # to comment.