comparison clojure/com/aurellem/run/music.clj @ 430:c709f4857fa9

correctly handles notes longer than 1 second.
author Robert McIntyre <rlm@mit.edu>
date Mon, 23 Apr 2012 09:49:24 -0500
parents a69c4d0c1a3b
children b73cb1b937d5
comparison
equal deleted inserted replaced
429:a69c4d0c1a3b 430:c709f4857fa9
306 (/ (- (* 2048 frequency) 131072) frequency))) 306 (/ (- (* 2048 frequency) 131072) frequency)))
307 0x00 2048)) 307 0x00 2048))
308 308
309 (defn note-codes [frequency volume duration] 309 (defn note-codes [frequency volume duration]
310 (assert (<= 0 volume 0xF)) 310 (assert (<= 0 volume 0xF))
311 (loop [current-duration duration 311 (if (<= duration 0xFF)
312 notes []] 312 (let [frequency-code
313 313 (frequency->frequency-code frequency)
314 (if (<= 0 duration 0xFF) 314 volume&high-frequency
315 (let [frequency-code 315 (+ (bit-shift-left volume 4)
316 (frequency->frequency-code frequency) 316 (bit-shift-right frequency-code 8))
317 volume&high-frequency 317 low-frequency
318 (+ (bit-shift-left volume 4) 318 (bit-and 0xFF frequency-code)]
319 (bit-shift-right frequency-code 8)) 319 [note-code
320 low-frequency 320 volume&high-frequency
321 (bit-and 0xFF frequency-code)] 321 low-frequency
322 [note-code 322 duration])
323 volume&high-frequency 323 (vec
324 low-frequency 324 (flatten
325 duration]) 325 [(note-codes frequency volume 0xFF)
326 (recur (- current-duration 0xFF) 326 (note-codes frequency volume (- duration 0xFF))]))))
327 (conj notes (note-codes frequency volume 0xFF)))))) 327
328
329 328
330 (defn midi-code->frequency 329 (defn midi-code->frequency
331 [midi-code] 330 [midi-code]
332 (* 8.1757989156 331 (* 8.1757989156
333 (Math/pow 2 (* (float (/ 12)) midi-code)))) 332 (Math/pow 2 (* (float (/ 12)) midi-code))))
409 (def G4 (partial note-codes 392)) 408 (def G4 (partial note-codes 392))
410 (def A4 (partial note-codes 440)) 409 (def A4 (partial note-codes 440))
411 (def B4 (partial note-codes 493.88)) 410 (def B4 (partial note-codes 493.88))
412 (def C5 (partial note-codes 523.3)) 411 (def C5 (partial note-codes 523.3))
413 412
414 ;; (def scale 413 (def scale
415 ;; (flatten 414 (flatten
416 ;; [(C4 0xF 0x40) 415 [(C4 0xF 0x40)
417 ;; (D4 0xF 0x40) 416 (D4 0xF 0x40)
418 ;; (E4 0xF 0x40) 417 (E4 0xF 0x40)
419 ;; (F4 0xF 0x40) 418 (F4 0xF 0x40)
420 ;; (G4 0xF 0x40) 419 (G4 0xF 0x40)
421 ;; (A4 0xF 0x40) 420 (A4 0xF 0x40)
422 ;; (B4 0xF 0x40) 421 (B4 0xF 0x40)
423 ;; (C5 0xF 0x40)])) 422 (C5 0xF 0x40)]))
424 423
425 (defn play-music [music-bytes] 424 (defn play-music [music-bytes]
426 (let [program-target 0xC000 425 (let [program-target 0xC000
427 music-target 0xD000] 426 music-target 0xD000]
428 (-> (set-memory-range (second (music-base)) 427 (-> (set-memory-range (second (music-base))