diff clojure/com/aurellem/run/music.clj @ 466:b31cd6651375

working on noise.
author Robert McIntyre <rlm@mit.edu>
date Fri, 04 May 2012 04:13:13 -0500
parents 34bf4b64d9d1
children ac0ed5c1a1c4
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/music.clj	Fri May 04 03:55:00 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/music.clj	Fri May 04 04:13:13 2012 -0500
     1.3 @@ -215,6 +215,17 @@
     1.4          {:note-length (count play-note)}
     1.5          (concat switch play-note)))))
     1.6  
     1.7 +(defn play-noise
     1.8 +  "read [noise-code, duration] and play the noise. Duration is left in
     1.9 +   A, and HL is advanced appropraitely."
    1.10 +  ([]
    1.11 +     [0x2A  ;; load noise-code into A
    1.12 +      0xE0
    1.13 +      0x22  ;; write noise-code
    1.14 +      0x2A] ;; load duration into A
    1.15 +     ))
    1.16 +
    1.17 +
    1.18  ;; (defn play-note
    1.19  ;;   "Play the note referenced by HL in the appropiate channel.
    1.20  ;;    Leaves desired-duration in A."
    1.21 @@ -238,7 +249,7 @@
    1.22  ;;    0x2A   ;; load duration
    1.23  ;;    ]) 
    1.24  
    1.25 -(defn music-step [sound-base-address wave-duty]
    1.26 +(defn music-step [sound-base-address wave-duty noise?]
    1.27    ;; C == current-ticks
    1.28    ;; A == desired-ticks
    1.29    
    1.30 @@ -266,10 +277,14 @@
    1.31      ;; if desired-ticks = current ticks
    1.32      ;;   go to next note ; set current set ticks to 0.
    1.33  
    1.34 -    0x20
    1.35 -    (+ (count (do-message 0 0)) 2)
    1.36 -
    1.37 -    (do-message sound-base-address wave-duty)
    1.38 +    (if noise?
    1.39 +      [0x20
    1.40 +       (+ 2 (count (play-noise)))
    1.41 +       (play-noise)]
    1.42 +    
    1.43 +      [0x20
    1.44 +       (+ (count (do-message 0 0)) 2)
    1.45 +       (do-message sound-base-address wave-duty)])
    1.46      
    1.47      0x0E
    1.48      0x00 ;; 0->C (current-ticks)
    1.49 @@ -325,26 +340,38 @@
    1.50      0xE5 ;; push HL
    1.51  
    1.52  
    1.53 +    ;; initialize frame 3 (noise)
    1.54 +    0x21
    1.55 +    0x00
    1.56 +    0xA9 ;; 0xA9OO -> HL
    1.57 +
    1.58 +    0xF5 ;; push AF
    1.59 +    0xC5 ;; push CB
    1.60 +    0xE5 ;; push HL
    1.61 +
    1.62      ;; main music loop
    1.63  
    1.64 -    0xE8 ;; SP + 6; activate frame 1
    1.65 -    6
    1.66 -    (music-step music-1 wave-duty-1)
    1.67 -    ;;(repeat (count (music-step music-1)) 0x00)
    1.68 +    0xE8 ;; SP + 12; activate frame 1
    1.69 +    12
    1.70 +    (music-step music-1 wave-duty-1 false)
    1.71      
    1.72      0xE8 ;; SP - 6; activate frame 2
    1.73      (->signed-8-bit -6)
    1.74 -    ;;(repeat (count (music-step music-2)) 0x00)
    1.75 -    (music-step music-2 wave-duty-2)
    1.76 -    
    1.77 +    (music-step music-2 wave-duty-2 false)
    1.78 +
    1.79 +    0xE8 ;; SP - 6;  activate frame 3
    1.80 +    (->signed-8-bit -6)
    1.81 +    (music-step nil nil true)
    1.82  
    1.83      0x18
    1.84      (->signed-8-bit (+
    1.85                       ;; two music-steps
    1.86 -                     (- (* 2 (count (music-step 0 0))))
    1.87 +                     (- (* 2 (count (music-step 0 0 false))))
    1.88 +                     (- (count (music-step nil nil true)))
    1.89                       -2 ;; this jump instruction
    1.90                       -2 ;; activate frame 1
    1.91                       -2 ;; activate frame 2
    1.92 +                     -2 ;; activate frame 3
    1.93                       ))]))
    1.94  
    1.95  (defn frequency-code->frequency
    1.96 @@ -402,6 +429,11 @@
    1.97     :duration length
    1.98     :volume 0})
    1.99  
   1.100 +(defn commands
   1.101 +  "return all events where #(= (:command %) command)"
   1.102 +  [command s]
   1.103 +  (filter #(= command (:command %)) s))
   1.104 +
   1.105  (defn track-info [#^File midi-file]
   1.106    (let [events (parse-midi midi-file)
   1.107          track-titles (commands :Title_t events)
   1.108 @@ -420,11 +452,6 @@
   1.109                  (vals track-data))
   1.110          channel-nums (map (comp :channel track-order) (range 3))]
   1.111      channel-nums))
   1.112 -        
   1.113 -(defn commands
   1.114 -  "return all events where #(= (:command %) command)"
   1.115 -  [command s]
   1.116 -  (filter #(= command (:command %)) s))
   1.117  
   1.118  (defn midi-track->mini-midi [#^File midi-file track-num]
   1.119    (let [midi-events (parse-midi midi-file)
   1.120 @@ -508,6 +535,8 @@
   1.121  
   1.122          voice-2 (flatten (:voice-2 mini-midi))
   1.123          wave-duty-2 ((:duty mini-midi) 1 0)
   1.124 +
   1.125 +        noise (flatten (:noise mini-midi))
   1.126          ]
   1.127      
   1.128      (-> (second (music-base))