common-lispccl

Common Lisp: Write to external program input


I want to write a simple wrapper for mpg123 console mp3 player in CL/CCL and wonder how to write to mpg123 which exists as a spawned process:

(let* ((p (run-program "mpg123" '("-R") :input :stream :output :stream :wait nil))
       (s (external-process-input-stream p)))

  (write "LOAD /path/to/file.mp3" :stream s)
  (write-char #\return s))

But this code is not working and I am bit stuck. Any ideas?


Solution

  • Caveat emptor: I don't have a Clozure instance handy, and I'm assuming that this run-program is from the CCL extensions.

    Almost certainly, though, mpg123 is probably not receiving your output “yet,” because on all mainstream systems I/O is (by default) buffered until you hit some buffer-size-limit. Adding a (finish-output s) after #'write-char will probably do the trick.

    See http://clhs.lisp.se/Body/f_finish.htmforce-output may be more appropriate if you weren't concerned about immediately reading any reply (eg, if you were queueing up a long playlist in a loop), but in practice, they're somewhat interchangeable.