Created
December 11, 2022 06:12
-
-
Save g000001/5e269f747a2884c15a6770b2bddacb0d to your computer and use it in GitHub Desktop.
code generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
今回のような場合、マクロでコード生成する(楽をしたい)のであれば関数単位というよりは全体を生成することになるかと思いました。 | |
(defmacro doit () | |
`(progn | |
;; data chunk | |
(write-chars "data" out-stream) | |
(write-32le (* nsmpl nch (/ nbits 8)) out-stream) ; size of data chunk | |
(cond | |
,@(mapcan (lambda (nch) | |
(mapcar (lambda (n) | |
`((and (= nbits ,n) ,nch) | |
(loop for k from 0 to (1- nsmpl) do | |
(,(intern (concatenate 'string (string '#:write-sample-) (princ-to-string n))) | |
(aref snd k) out-stream)))) | |
'(8 16 24 32))) | |
'((= nch 1) (> nch 1))) | |
;; neither | |
(t (error "Unsupported WAV format."))) | |
;; add trailing zero to make the file length even | |
(if (oddp (* nsmpl nch (/ nbits 8))) | |
(write-byte 0 out-stream))))))) | |
(doit) | |
write-sample-nのようなAPIで、ということであれば、個人的にはそのまま関数で書くかなと思います。 | |
(defun write-sample-n (n val out-stream) | |
(etypecase n | |
((integer 8) | |
(write-byte (truncate (+ 128 (scale-float val 7))) out-stream)) | |
((integer 16) | |
(write-byte (truncate (scale-float val 15)) out-stream)) | |
((integer 24) | |
(write-24le (truncate (scale-float val 23)) out-stream)) | |
((integer 32) | |
(write-32le (ieee-floats:encode-float32 val) out-stream)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment