From f5853bf37d45bf9d606ec51bbb38be4ff11c3ec3 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sun, 5 Aug 2018 22:52:54 -0400 Subject: [PATCH] Implement (dur) --- project.clj | 4 +++- src/music.clj | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/project.clj b/project.clj index e04e52b..99e952a 100644 --- a/project.clj +++ b/project.clj @@ -1,2 +1,4 @@ (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"]]) diff --git a/src/music.clj b/src/music.clj index 13c97c4..9a6b28e 100644 --- a/src/music.clj +++ b/src/music.clj @@ -1,4 +1,5 @@ -(ns music) +(ns music + (:require [clojure.core.match :refer [match]])) ;; an octave is an int, e.g. 4 ;; a pitch class is a keyword from :Abb to G## @@ -109,3 +110,16 @@ (if (> (abs-pitch p) (abs-pitch max-ps)) p 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))) +