Mercurial > vba-clojure
comparison clojure/com/aurellem/run/music.clj @ 429:a69c4d0c1a3b
investigating infinite loop in note-codes.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 23 Apr 2012 09:40:11 -0500 |
parents | 476f7da175a4 |
children | c709f4857fa9 |
comparison
equal
deleted
inserted
replaced
428:476f7da175a4 | 429:a69c4d0c1a3b |
---|---|
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 (assert (<= 0 duration 0xFF)) | 311 (loop [current-duration duration |
312 (let [frequency-code | 312 notes []] |
313 (frequency->frequency-code frequency) | 313 |
314 volume&high-frequency | 314 (if (<= 0 duration 0xFF) |
315 (+ (bit-shift-left volume 4) | 315 (let [frequency-code |
316 (bit-shift-right frequency-code 8)) | 316 (frequency->frequency-code frequency) |
317 low-frequency | 317 volume&high-frequency |
318 (bit-and 0xFF frequency-code)] | 318 (+ (bit-shift-left volume 4) |
319 [note-code | 319 (bit-shift-right frequency-code 8)) |
320 volume&high-frequency | 320 low-frequency |
321 low-frequency | 321 (bit-and 0xFF frequency-code)] |
322 duration])) | 322 [note-code |
323 volume&high-frequency | |
324 low-frequency | |
325 duration]) | |
326 (recur (- current-duration 0xFF) | |
327 (conj notes (note-codes frequency volume 0xFF)))))) | |
328 | |
323 | 329 |
324 (defn midi-code->frequency | 330 (defn midi-code->frequency |
325 [midi-code] | 331 [midi-code] |
326 (* 8.1757989156 | 332 (* 8.1757989156 |
327 (Math/pow 2 (* (float (/ 12)) midi-code)))) | 333 (Math/pow 2 (* (float (/ 12)) midi-code)))) |
394 (:volume note-event) | 400 (:volume note-event) |
395 (int (* (:duration note-event) 0x100)))) | 401 (int (* (:duration note-event) 0x100)))) |
396 notes-with-silence))) | 402 notes-with-silence))) |
397 | 403 |
398 | 404 |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | |
405 | |
406 | |
407 | |
408 | |
409 (def C4 (partial note-codes 261.63)) | 405 (def C4 (partial note-codes 261.63)) |
410 (def D4 (partial note-codes 293.66)) | 406 (def D4 (partial note-codes 293.66)) |
411 (def E4 (partial note-codes 329.63)) | 407 (def E4 (partial note-codes 329.63)) |
412 (def F4 (partial note-codes 349.23)) | 408 (def F4 (partial note-codes 349.23)) |
413 (def G4 (partial note-codes 392)) | 409 (def G4 (partial note-codes 392)) |
414 (def A4 (partial note-codes 440)) | 410 (def A4 (partial note-codes 440)) |
415 (def B4 (partial note-codes 493.88)) | 411 (def B4 (partial note-codes 493.88)) |
416 (def C5 (partial note-codes 523.3)) | 412 (def C5 (partial note-codes 523.3)) |
417 | 413 |
418 (def scale | 414 ;; (def scale |
419 (flatten | 415 ;; (flatten |
420 [(C4 0xF 0x40) | 416 ;; [(C4 0xF 0x40) |
421 (D4 0xF 0x40) | 417 ;; (D4 0xF 0x40) |
422 (E4 0xF 0x40) | 418 ;; (E4 0xF 0x40) |
423 (F4 0xF 0x40) | 419 ;; (F4 0xF 0x40) |
424 (G4 0xF 0x40) | 420 ;; (G4 0xF 0x40) |
425 (A4 0xF 0x40) | 421 ;; (A4 0xF 0x40) |
426 (B4 0xF 0x40) | 422 ;; (B4 0xF 0x40) |
427 (C5 0xF 0x40)])) | 423 ;; (C5 0xF 0x40)])) |
428 | 424 |
429 (defn play-music [music-bytes] | 425 (defn play-music [music-bytes] |
430 (let [program-target 0xC000 | 426 (let [program-target 0xC000 |
431 music-target 0xD000] | 427 music-target 0xD000] |
432 (-> (set-memory-range (second (music-base)) | 428 (-> (set-memory-range (second (music-base)) |