comparison 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
comparison
equal deleted inserted replaced
424:7bd806c4dbb6 425:df4e03672b05
85 ;; change-duty-format 85 ;; change-duty-format
86 ;; 2 bytes 86 ;; 2 bytes
87 ;; [change-duty-code (0x01)] 87 ;; [change-duty-code (0x01)]
88 ;; [new-duty] 88 ;; [new-duty]
89 89
90 (def note-code 0x00)
91 (def change-duty-code 0x01)
92 (def silence-code 0x02)
93
94
90 (defn do-message 95 (defn do-message
91 "Read the message which starts at the current value of HL and do 96 "Read the message which starts at the current value of HL and do
92 what it says. Duration is left in A, and HL is advanced 97 what it says. Duration is left in A, and HL is advanced
93 appropraitely." 98 appropraitely."
94 [] 99 []
95 100 (let [switch
96 ) 101 [0x2A ;; load message code into A, increment HL
97 102
98 103 ;; switch on message
99 104 0xFE
100 105 note-code
101 106
107 0x20
108 :note-length]
109
110 play-note
111 [0x2A ;; load volume/frequency-high info
112 0xF5 ;; push A
113 0xE6
114 (Integer/parseInt "11110000" 2) ;; volume mask
115 0xE0
116 0x17 ;; set volume
117 0xF1 ;; pop A
118 0xE6
119 (Integer/parseInt "00000111" 2) ;; frequency-high mask
120 0xE0
121 0x19 ;; set frequency-high
122
123 0x2A ;; load frequency low-bits
124 0xE0
125 0x18 ;; set frequency-low-bits
126
127 0x2A]] ;; load duration
128 (replace
129 {:note-length (count play-note)}
130 (concat switch play-note))))
102 131
103 (defn play-note 132 (defn play-note
104 "Play the note referenced by HL in the appropiate channel. 133 "Play the note referenced by HL in the appropiate channel.
105 Leaves desired-duration in A." 134 Leaves desired-duration in A."
106 [] 135 []
146 175
147 ;; if desired-ticks = current ticks 176 ;; if desired-ticks = current ticks
148 ;; go to next note ; set current set ticks to 0. 177 ;; go to next note ; set current set ticks to 0.
149 178
150 0x20 179 0x20
151 (+ (count (play-note)) 2) 180 (+ (count (do-message)) 2)
152 181
153 (play-note) 182 (do-message)
154 183
155 0x0E 184 0x0E
156 0x00])) ;; 0->C (current-ticks) 185 0x00])) ;; 0->C (current-ticks)
157 186
158 (defn music-kernel [] 187 (defn music-kernel []
183 212
184 (music-step) 213 (music-step)
185 0x18 214 0x18
186 (->signed-8-bit (+ (- (count (music-step))) 215 (->signed-8-bit (+ (- (count (music-step)))
187 -2))])) 216 -2))]))
188
189 (def one-note
190 [0xA0 0x00 0xFF])
191
192 (def many-notes
193 (flatten (repeat 10 one-note)))
194
195 (def increasing-notes
196 [0xA0 0x00 0x55
197 0xA1 0x00 0x55
198 0xA2 0x00 0x55
199 0xA3 0x00 0x55
200 0xA4 0x00 0x55
201 0xA5 0x00 0x55
202 0xA6 0x00 0x55
203 0xA6 0x55 0xFF
204 0xA6 0x55 0xFF
205 0xA6 0x55 0xFF
206 0x00 0x00 0xFF
207 ])
208 217
209 (defn frequency-code->frequency 218 (defn frequency-code->frequency
210 [code] 219 [code]
211 (assert (<= 0 code 2047)) 220 (assert (<= 0 code 2047))
212 (/ 131072 (- 2048 code))) 221 (/ 131072 (- 2048 code)))
232 volume&high-frequency 241 volume&high-frequency
233 (+ (bit-shift-left volume 4) 242 (+ (bit-shift-left volume 4)
234 (bit-shift-right frequency-code 8)) 243 (bit-shift-right frequency-code 8))
235 low-frequency 244 low-frequency
236 (bit-and 0xFF frequency-code)] 245 (bit-and 0xFF frequency-code)]
237 [volume&high-frequency 246 [note-code
247 volume&high-frequency
238 low-frequency 248 low-frequency
239 duration])) 249 duration]))
240 250
241 (def C4 (partial note-codes 261.63)) 251 (def C4 (partial note-codes 261.63))
242 (def D4 (partial note-codes 293.66)) 252 (def D4 (partial note-codes 293.66))