diff clojure/com/aurellem/run/music.clj @ 425:df4e03672b05

implemented note-code message.
author Robert McIntyre <rlm@mit.edu>
date Mon, 23 Apr 2012 05:45:25 -0500
parents 7bd806c4dbb6
children c03f28aa98d9
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/music.clj	Mon Apr 23 04:45:55 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/music.clj	Mon Apr 23 05:45:25 2012 -0500
     1.3 @@ -87,18 +87,47 @@
     1.4  ;;  [change-duty-code (0x01)]
     1.5  ;;  [new-duty]
     1.6  
     1.7 +(def note-code 0x00)
     1.8 +(def change-duty-code 0x01)
     1.9 +(def silence-code 0x02)
    1.10 +  
    1.11 +
    1.12  (defn do-message
    1.13    "Read the message which starts at the current value of HL and do
    1.14     what it says. Duration is left in A, and HL is advanced
    1.15     appropraitely."
    1.16    []
    1.17 +  (let [switch
    1.18 +        [0x2A ;; load message code into A, increment HL
    1.19 +         
    1.20 +         ;; switch on message
    1.21 +         0xFE
    1.22 +         note-code
    1.23 +         
    1.24 +         0x20
    1.25 +         :note-length]
    1.26  
    1.27 -  )
    1.28 -
    1.29 -
    1.30 -
    1.31 -
    1.32 -
    1.33 +        play-note
    1.34 +        [0x2A   ;; load volume/frequency-high info
    1.35 +         0xF5   ;; push A
    1.36 +         0xE6
    1.37 +         (Integer/parseInt "11110000" 2) ;; volume mask
    1.38 +         0xE0
    1.39 +         0x17   ;; set volume
    1.40 +         0xF1   ;; pop A
    1.41 +         0xE6
    1.42 +         (Integer/parseInt "00000111" 2) ;; frequency-high mask
    1.43 +         0xE0   
    1.44 +         0x19   ;; set frequency-high
    1.45 +         
    1.46 +         0x2A   ;; load frequency low-bits
    1.47 +         0xE0
    1.48 +         0x18   ;; set frequency-low-bits
    1.49 +         
    1.50 +         0x2A]]   ;; load duration
    1.51 +    (replace
    1.52 +     {:note-length (count play-note)}
    1.53 +     (concat switch play-note))))
    1.54  
    1.55  (defn play-note
    1.56    "Play the note referenced by HL in the appropiate channel.
    1.57 @@ -148,9 +177,9 @@
    1.58      ;;   go to next note ; set current set ticks to 0.
    1.59  
    1.60      0x20
    1.61 -    (+ (count (play-note)) 2)
    1.62 +    (+ (count (do-message)) 2)
    1.63  
    1.64 -    (play-note)
    1.65 +    (do-message)
    1.66      
    1.67      0x0E
    1.68      0x00])) ;; 0->C (current-ticks)
    1.69 @@ -186,26 +215,6 @@
    1.70      (->signed-8-bit (+ (- (count (music-step)))
    1.71                         -2))]))
    1.72  
    1.73 -(def one-note
    1.74 -  [0xA0 0x00 0xFF])
    1.75 -
    1.76 -(def many-notes
    1.77 -  (flatten (repeat 10 one-note)))
    1.78 -
    1.79 -(def increasing-notes
    1.80 -  [0xA0 0x00 0x55
    1.81 -   0xA1 0x00 0x55
    1.82 -   0xA2 0x00 0x55
    1.83 -   0xA3 0x00 0x55
    1.84 -   0xA4 0x00 0x55
    1.85 -   0xA5 0x00 0x55
    1.86 -   0xA6 0x00 0x55
    1.87 -   0xA6 0x55 0xFF
    1.88 -   0xA6 0x55 0xFF
    1.89 -   0xA6 0x55 0xFF
    1.90 -   0x00 0x00 0xFF
    1.91 -   ])
    1.92 -
    1.93  (defn frequency-code->frequency
    1.94    [code]
    1.95    (assert (<= 0 code 2047))
    1.96 @@ -234,7 +243,8 @@
    1.97             (bit-shift-right frequency-code 8))
    1.98          low-frequency
    1.99          (bit-and 0xFF frequency-code)]
   1.100 -    [volume&high-frequency
   1.101 +    [note-code
   1.102 +     volume&high-frequency
   1.103       low-frequency
   1.104       duration]))
   1.105