diff clojure/com/aurellem/run/music.clj @ 429:a69c4d0c1a3b

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