diff clojure/com/aurellem/run/music.clj @ 430:c709f4857fa9

correctly handles notes longer than 1 second.
author Robert McIntyre <rlm@mit.edu>
date Mon, 23 Apr 2012 09:49:24 -0500
parents a69c4d0c1a3b
children b73cb1b937d5
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/music.clj	Mon Apr 23 09:40:11 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/music.clj	Mon Apr 23 09:49:24 2012 -0500
     1.3 @@ -308,24 +308,23 @@
     1.4      
     1.5  (defn note-codes [frequency volume duration]
     1.6    (assert (<= 0 volume 0xF))
     1.7 -  (loop [current-duration duration
     1.8 -         notes []]
     1.9 -
    1.10 -    (if (<= 0 duration 0xFF)
    1.11 -      (let [frequency-code
    1.12 -            (frequency->frequency-code frequency)
    1.13 -            volume&high-frequency
    1.14 -            (+ (bit-shift-left volume 4)
    1.15 -               (bit-shift-right frequency-code 8))
    1.16 -            low-frequency
    1.17 -            (bit-and 0xFF frequency-code)]
    1.18 -        [note-code
    1.19 -         volume&high-frequency
    1.20 -         low-frequency
    1.21 -         duration])
    1.22 -      (recur (- current-duration 0xFF)
    1.23 -             (conj notes (note-codes frequency volume 0xFF))))))
    1.24 -    
    1.25 +  (if (<= duration 0xFF)
    1.26 +    (let [frequency-code
    1.27 +          (frequency->frequency-code frequency)
    1.28 +          volume&high-frequency
    1.29 +          (+ (bit-shift-left volume 4)
    1.30 +             (bit-shift-right frequency-code 8))
    1.31 +          low-frequency
    1.32 +          (bit-and 0xFF frequency-code)]
    1.33 +      [note-code
    1.34 +       volume&high-frequency
    1.35 +       low-frequency
    1.36 +       duration])
    1.37 +    (vec
    1.38 +     (flatten 
    1.39 +      [(note-codes frequency volume 0xFF)
    1.40 +       (note-codes frequency volume (- duration 0xFF))]))))
    1.41 +  
    1.42  
    1.43  (defn midi-code->frequency
    1.44    [midi-code]
    1.45 @@ -411,16 +410,16 @@
    1.46  (def B4 (partial note-codes 493.88))
    1.47  (def C5 (partial note-codes 523.3))
    1.48  
    1.49 -;; (def scale
    1.50 -;;   (flatten
    1.51 -;;    [(C4 0xF 0x40)
    1.52 -;;     (D4 0xF 0x40)
    1.53 -;;     (E4 0xF 0x40)
    1.54 -;;     (F4 0xF 0x40)
    1.55 -;;     (G4 0xF 0x40)
    1.56 -;;     (A4 0xF 0x40)
    1.57 -;;     (B4 0xF 0x40)
    1.58 -;;     (C5 0xF 0x40)]))
    1.59 +(def scale
    1.60 +  (flatten
    1.61 +   [(C4 0xF 0x40)
    1.62 +    (D4 0xF 0x40)
    1.63 +    (E4 0xF 0x40)
    1.64 +    (F4 0xF 0x40)
    1.65 +    (G4 0xF 0x40)
    1.66 +    (A4 0xF 0x40)
    1.67 +    (B4 0xF 0x40)
    1.68 +    (C5 0xF 0x40)]))
    1.69                   
    1.70  (defn play-music [music-bytes]
    1.71    (let [program-target 0xC000