Skip to content

Commit

Permalink
Merge tunes.cljs back into music.cljs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chouser committed Sep 7, 2013
1 parent 1cd6dd8 commit 43ea5f9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 52 deletions.
20 changes: 11 additions & 9 deletions src/clj/joy/macro_tunes.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns joy.macro-tunes
"Functions for computing tunes full of notes")
"Functions for computing tunes full of notes at compile time")

(defn pair-to-note
"Return a note map for the given tone and duration"
Expand Down Expand Up @@ -29,13 +29,15 @@
(map #(update-in % [:delay] (comp double /) bps))
(map #(update-in % [:duration] (comp double /) bps)))))

(defmacro magical-theme
(defn magical-theme
"A sequence of notes for a magical theme"
[]
(vec
(notes
(concat
[[11 2] [16 3] [19 1] [18 2] [16 4] [23 2]]
[[21 6] [18 6] [16 3] [19 1] [18 2] [14 4] [17 2] [11 10]]
[[11 2] [16 3] [19 1] [18 2] [16 4] [23 2]]
[[26 4] [25 2] [24 4] [20 2] [24 3] [23 1] [22 2] [10 4] [19 2] [16 10]]))))
(notes
(concat
[[11 2] [16 3] [19 1] [18 2] [16 4] [23 2]]
[[21 6] [18 6] [16 3] [19 1] [18 2] [14 4] [17 2] [11 10]]
[[11 2] [16 3] [19 1] [18 2] [16 4] [23 2]]
[[26 4] [25 2] [24 4] [20 2] [24 3] [23 1] [22 2] [10 4] [19 2] [16 10]])))

(defmacro magical-theme-macro []
(vec (magical-theme)))
47 changes: 43 additions & 4 deletions src/cljs/joy/music.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns joy.music
"Functions for interoperating with the Web Audio API"
(:require [joy.tunes :as tunes])
(:require-macros [joy.macro-tunes :as mtunes]))

;; == Functions for interoperating with the Web Audio API ==

(defn soft-attack
"Return a gain node that goes from silent at time <delay> up to
<volume> in 50 milliseconds, then ramps back down to silent after
Expand Down Expand Up @@ -62,12 +62,51 @@
{:cent 1400, :duration 1, :delay 0.2, :volume 0.4}
{:cent 1800, :duration 1, :delay 0.4, :volume 0.4}])

;; == Functions for computing tunes full of notes ==

(defn pair-to-note
"Return a note map for the given tone and duration"
[[tone duration]]
{:cent (* 100 tone)
:duration duration
:volume 0.4})

(defn consecutive-notes
"Take a sequences of note maps that have no :delay, and return them
with correct :delay's so that they will play in the order given."
[notes]
(reductions (fn [{:keys [delay duration]} note]
(assoc note
:delay (+ delay duration)))
notes))

(defn notes [tone-pairs]
"Returns a sequence of note maps at moderate tempo for the given
sequence of tone-pairs."
(let [bpm 360
bps (/ bpm 60)]
(->> tone-pairs
(map pair-to-note)
consecutive-notes
(map #(update-in % [:delay] / bps))
(map #(update-in % [:duration] / bps)))))

(defn magical-theme
"A sequence of notes for a magical theme"
[]
(notes
(concat
[[11 2] [16 3] [19 1] [18 2] [16 4] [23 2]]
[[21 6] [18 6] [16 3] [19 1] [18 2] [14 4] [17 2] [11 10]]
[[11 2] [16 3] [19 1] [18 2] [16 4] [23 2]]
[[26 4] [25 2] [24 4] [20 2] [24 3] [23 1] [22 2] [10 4] [19 2] [16 10]])))

(defn ^:export go []
(play! woo (tunes/magical-theme)))
(play! woo (magical-theme)))

;; Uncomment to use macro version of tunes
;; (defn ^:export go []
;; (play! woo (mtunes/magical-theme)))
;; (play! woo (mtunes/magical-theme-macro)))

;; Uncomment to begin playing on page load
;; (go)
39 changes: 0 additions & 39 deletions src/cljs/joy/tunes.cljs

This file was deleted.

0 comments on commit 43ea5f9

Please sign in to comment.