summaryrefslogtreecommitdiff
path: root/src/main.clj
blob: fdb306fb7672b4fbb9b218524fe29025a26ff93c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns main
  (:require [missionary.core :as m]
            [clojure.set :refer [difference]]))

;; How many times per second are output continuous values sampled and turned
;; into events?
(def sample-rate (atom 1))

;; Temporary atom to explore the concept of note state as a continuous value
(def notes-on (atom #{}))
(def >notes-on (m/signal (m/watch notes-on)))

(defn play-note [v] (swap! notes-on conj v))

(defn stop-note [v] (swap! notes-on disj v))

(def clock
  (m/ap
   (loop []
     (m/amb
      (m/?
       (m/sleep (/ 1000 @sample-rate)))
      :tick
      (recur)))))

#_(play-note 1)
#_(stop-note 1)

;; convert the continuous time >notes-on flow to a series of discrete midi note on and off events
(def output
  (m/eduction (map (fn [{:keys [note-on note-off]}] {:note-on note-on :note-off note-off}))
              (m/reductions (fn [{:keys [active note-on note-off]} [curr _]]
                              {:note-on (difference (difference curr active) note-on)
                               :note-off (difference (difference active curr) note-off)
                               :active curr})
                            #{}
                            (m/sample
                             vector
                             >notes-on
                             clock))))

(def cancel
  ((m/reduce prn output) {} {}))

(cancel)