Created
October 10, 2014 00:46
-
-
Save whacked/c0f124f5c6589b44d4b5 to your computer and use it in GitHub Desktop.
Glenn's ringtone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ringtone.core | |
(:use [overtone.live] | |
[overtone.inst.piano])) | |
(def state | |
(atom {:measure-number 0 | |
:is-running true})) | |
(let [ | |
;; milliseconds for each tick | |
increment 100 | |
music-list (map-indexed | |
(fn [index pitch] | |
{:onset (* index increment) | |
:data [{:duration increment | |
:pitch pitch}]} | |
) | |
;; list of pitches, transpose by 2 octaves | |
(map #(+ 24 %) [68 69 72 | |
68 69 72 | |
68 69 72 | |
68 69 72 | |
65 66 62])) | |
start-time (now) | |
;; not useful now... to find the total duration of the entire jingle | |
;; max-duration (+ (apply max (map :onset music-list)) | |
;; (apply max (map :duration (last (map :data music-list))))) | |
;; call this on every timer tick (per increment) | |
playfunction (fn [] | |
(if (< (@state :measure-number) (count music-list)) | |
(let [current-measure (nth music-list (@state :measure-number)) | |
current-time (now)] | |
(when (< (+ start-time (:onset current-measure)) current-time) | |
;;(piano (:pitch (first (:data current-measure)))) | |
(doseq [pitch (map :pitch (:data current-measure))] | |
(piano pitch)) | |
(swap! state assoc :measure-number (inc (@state :measure-number))) | |
) | |
) | |
(swap! state assoc :is-running false) | |
) | |
) | |
tasktimer (java.util.Timer.)] | |
(.scheduleAtFixedRate tasktimer | |
(proxy [java.util.TimerTask] [] | |
(run [] | |
(if (@state :is-running) | |
(do | |
(.start | |
(Thread. playfunction))) | |
(do | |
(.cancel tasktimer))))) | |
0 ;; first run delay | |
increment ;; thereafter | |
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject ringtone "0.1.0-SNAPSHOT" | |
:description "FIXME: write description" | |
:url "http://example.com/FIXME" | |
:license {:name "Eclipse Public License" | |
:url "http://www.eclipse.org/legal/epl-v10.html"} | |
:dependencies [[org.clojure/clojure "1.4.0"] | |
[overtone "0.8.1"]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment