Skip to content

Instantly share code, notes, and snippets.

@g000001
Created December 11, 2022 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g000001/5e269f747a2884c15a6770b2bddacb0d to your computer and use it in GitHub Desktop.
Save g000001/5e269f747a2884c15a6770b2bddacb0d to your computer and use it in GitHub Desktop.
code generation
今回のような場合、マクロでコード生成する(楽をしたい)のであれば関数単位というよりは全体を生成することになるかと思いました。
(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