Implement (dur)

This commit is contained in:
Jeremy Dormitzer 2018-08-05 22:52:54 -04:00
parent 52caab4a39
commit f5853bf37d
2 changed files with 18 additions and 2 deletions

View File

@ -1,2 +1,4 @@
(defproject clojure-school-of-music "0.1.0" (defproject clojure-school-of-music "0.1.0"
:dependencies [[overtone "0.10.3"]]) :dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/core.match "0.3.0-alpha5"]
[overtone "0.10.3"]])

View File

@ -1,4 +1,5 @@
(ns music) (ns music
(:require [clojure.core.match :refer [match]]))
;; an octave is an int, e.g. 4 ;; an octave is an int, e.g. 4
;; a pitch class is a keyword from :Abb to G## ;; a pitch class is a keyword from :Abb to G##
@ -109,3 +110,16 @@
(if (> (abs-pitch p) (abs-pitch max-ps)) (if (> (abs-pitch p) (abs-pitch max-ps))
p p
max-ps)))) max-ps))))
(defn dur
"Computes the duration of the music value"
[music]
(match music
{:duration d} d
[:+ m & ms] (+ (dur m) (dur (into [:+] ms)))
[:+] 0
[:= & ms] (apply max (map dur ms))
[:=] 0
[:modify {:tempo r} m] (/ (dur m) r)
[:modify _ m] (dur m)))