I'm trying to make a .wav file generator in Emacs Lisp. I have some binary data, as a sequence of bytes:
(setq binary-data (list 62 236 60))
I would like to insert that data into a file. I expect that file to be three bytes long.
If I try to insert it into a buffer, then write that buffer to a file, I get a file that consists of four bytes:
(with-temp-buffer
(seq-doseq (char binary-data)
(insert char))
(write-region nil nil "~/char-inserted.wav"))
$ hd ~/char-inserted.wav
00000000 3e c3 ac 3c |>..<|
00000004
So it seems like I'm having problems with inserting that single byte. I've also tried converting the data into a unibyte string, but that still gives me a four-byte file:
(with-temp-buffer
(seq-doseq (char (apply #'unibyte-string binary-data))
(insert char))
(write-region nil nil "~/unibyte-string.wav"))
$ hd ~/unibyte-string.wav
00000000 3e c3 ac 3c |>..<|
00000004
I've also converted the unibyte string to a multibyte string:
(with-temp-buffer
(seq-doseq (char (string-as-multibyte (apply #'unibyte-string binary-data)))
(insert char))
(write-region nil nil "~/multibyte-string.wav"))
But this gives me an error:
> These default coding systems were tried to encode text
in the buffer ‘ *temp*-110101’:
(utf-8-unix (2 . 4194284))
However, each of them encountered characters it couldn’t encode:
utf-8-unix cannot encode these:
It suggests I select one of the coding systems "raw-text" or "no-conversion", but I'm unsure how to do that, or if it will even help.
How do I save these three bytes into a file that ends up as a three-byte file? What's going on here?