# HG changeset patch
# User Robert McIntyre <rlm@mit.edu>
# Date 1335336999 18000
# Node ID 985c90ffa1fe9d878a990e40bcd218318070a866
# Parent  8e88366a81b92c48afde8186751b894529be4344
paramaterized do-message

diff -r 8e88366a81b9 -r 985c90ffa1fe clojure/com/aurellem/run/music.clj
--- a/clojure/com/aurellem/run/music.clj	Tue Apr 24 23:51:30 2012 -0500
+++ b/clojure/com/aurellem/run/music.clj	Wed Apr 25 01:56:39 2012 -0500
@@ -114,11 +114,11 @@
 
 (defn clear-music-registers []
   (flatten
-   [(store (Integer/parseInt "00000000" 2) 0xFF10)
-    (store (Integer/parseInt "00000000" 2) 0xFF11)
-    (store (Integer/parseInt "00000000" 2) 0xFF12)
-    (store (Integer/parseInt "00000000" 2) 0xFF13)
-    (store (Integer/parseInt "00000000" 2) 0xFF14)
+   [(store (Integer/parseInt "00000000" 2) 0xFF10) ;; sweep
+    (store (Integer/parseInt "00000000" 2) 0xFF11) ;; pattern duty
+    (store (Integer/parseInt "00000000" 2) 0xFF12) ;; volume
+    (store (Integer/parseInt "00000000" 2) 0xFF13) ;; frequency-low
+    (store (Integer/parseInt "00000000" 2) 0xFF14) ;; frequency-high
 
     (store (Integer/parseInt "00000000" 2) 0xFF16) ;; pattern duty 000000
     (store (Integer/parseInt "00000000" 2) 0xFF17) ;; volume 0000
@@ -131,10 +131,11 @@
     (store (Integer/parseInt "00000000" 2) 0xFF1D)
     (store (Integer/parseInt "00000000" 2) 0xFF1E)
 
-    (store (Integer/parseInt "00000000" 2) 0xFF20)
-    (store (Integer/parseInt "00000000" 2) 0xFF21)
-    (store (Integer/parseInt "00000000" 2) 0xFF22)
-    (store (Integer/parseInt "00000000" 2) 0xFF23)]))
+    (store (Integer/parseInt "00000000" 2) 0xFF20) ;; length
+    (store (Integer/parseInt "00000000" 2) 0xFF21) ;; volume
+    (store (Integer/parseInt "00000000" 2) 0xFF22) ;; noise-frequency
+    (store (Integer/parseInt "00000000" 2) 0xFF23) ;; control
+    ]))
 
 
 ;; mini-midi syntax
@@ -169,69 +170,68 @@
   "Read the message which starts at the current value of HL and do
    what it says. Duration is left in A, and HL is advanced
    appropraitely."
-  []
-  (let [switch
-        [0x2A ;; load message code into A, increment HL
-         
-         ;; switch on message
-         0xFE
-         note-code
-         
-         0x20
-         :note-length]
+  ([] (do-message 0x16))
+  ([sound-base-address]
+     (let [switch
+           [0x2A ;; load message code into A, increment HL
+            
+            ;; switch on message
+            0xFE
+            note-code
+            
+            0x20
+            :note-length]
 
-        play-note
-        [0x2A   ;; load volume/frequency-high info
-         0xF5   ;; push A
-         0xE6
-         (Integer/parseInt "11110000" 2) ;; volume mask
-         0xE0
-         0x17   ;; set volume
-         0xF1   ;; pop A
-         0xE6
-         (Integer/parseInt "00000111" 2) ;; frequency-high mask
-         0xE0   
-         0x19   ;; set frequency-high
-         
-         0x2A   ;; load frequency low-bits
-         0xE0
-         0x18   ;; set frequency-low-bits
-         
-         0x2A]]   ;; load duration
-    (replace
-     {:note-length (count play-note)}
-     (concat switch play-note))))
+           play-note
+           [0x2A   ;; load volume/frequency-high info
+            0xF5   ;; push A
+            0xE6
+            (Integer/parseInt "11110000" 2) ;; volume mask
+            0xE0
+            (inc sound-base-address) ;;0x17   ;; set volume
+            0xF1   ;; pop A
+            0xE6
+            (Integer/parseInt "00000111" 2) ;; frequency-high mask
+            0xE0   
+            (+ 3 sound-base-address) ;;0x19   ;; set frequency-high
+            
+            0x2A   ;; load frequency low-bits
+            0xE0
+            (+ 2 sound-base-address) ;;0x18   ;; set frequency-low-bits
+            0x2A]]   ;; load duration
+       (replace
+        {:note-length (count play-note)}
+        (concat switch play-note)))))
 
-(defn play-note
-  "Play the note referenced by HL in the appropiate channel.
-   Leaves desired-duration in A."
-  []
-  [0x2A   ;; load volume/frequency-high info
-   0xF5   ;; push A
-   0xE6
-   (Integer/parseInt "11110000" 2) ;; volume mask
-   0xE0
-   0x17   ;; set volume
-   0xF1   ;; pop A
-   0xE6
-   (Integer/parseInt "00000111" 2) ;; frequency-high mask
-   0xE0   
-   0x19   ;; set frequency-high
+;; (defn play-note
+;;   "Play the note referenced by HL in the appropiate channel.
+;;    Leaves desired-duration in A."
+
+;;   [0x2A   ;; load volume/frequency-high info
+;;    0xF5   ;; push A
+;;    0xE6
+;;    (Integer/parseInt "11110000" 2) ;; volume mask
+;;    0xE0
+;;    0x17   ;; set volume
+;;    0xF1   ;; pop A
+;;    0xE6
+;;    (Integer/parseInt "00000111" 2) ;; frequency-high mask
+;;    0xE0   
+;;    0x19   ;; set frequency-high
    
-   0x2A   ;; load frequency low-bits
-   0xE0
-   0x18   ;; set frequency-low-bits
+;;    0x2A   ;; load frequency low-bits
+;;    0xE0
+;;    0x18   ;; set frequency-low-bits
 
-   0x2A   ;; load duration
-   ]) 
+;;    0x2A   ;; load duration
+;;    ]) 
 
 (defn music-step []
   ;; C == current-ticks
   ;; A == desired-ticks
 
   (flatten
-   [
-    0xF5 ;; push A
+   [0xF5 ;; push A
     0xF0
     0x05 ;; load current ticks from 0xF005
     0xB8 ;; 
@@ -279,11 +279,10 @@
     0x00 ;; 0->C
     0x06
     0x00 ;; 0->B
+    0xAF ;; 0->A
 
 
-
-    0xAF ;; initialiaze A to zero
-
+    
 
     (music-step)
     0x18