guile-clipboard-speaker

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit daf585bdcb7244da68f61bad68ec3068f4464873
parent 98afd4758f417d3f1c3908a91346463e774d704d
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Wed, 12 Jul 2023 18:02:04 +0300

Pipe clipboard contents into espeak-ng.

Diffstat:
Mclipboard-speaker.scm | 54++++++++++++++++++++++++++++++++++--------------------
1 file changed, 34 insertions(+), 20 deletions(-)

diff --git a/clipboard-speaker.scm b/clipboard-speaker.scm @@ -200,8 +200,7 @@ ;; Read lines from xsel port and for each line replace ;; the newlines in it with spaces. - (define xsel-output ((compose (cut string-append <> "\n") - (cut string-join <> " ") + (define xsel-output ((compose (cut string-join <> " ") (cut map string-trim-both <>) (cut string-split <> #\newline) get-string-all) @@ -237,23 +236,31 @@ (define (espeak-loop fifo-read-port words-per-minute) - (define text-to-speak - (string-append (get-line fifo-read-port) - "\n")) + (define espeak-ng-input-pipe (pipe)) (set! espeak-ng-pid - (spawn "espeak-ng" `("espeak-ng" - "-s" - ,(number->string words-per-minute) - "--" ; TODO: XXX: Avoid treating texts as options? Maybe? - ,text-to-speak))) + (spawn "espeak-ng" + (list "espeak-ng" + "-s" + (number->string words-per-minute)) + #:input (car espeak-ng-input-pipe))) - (waitpid espeak-ng-pid) + (let loop ((text-to-speak (get-line fifo-read-port))) + (format #t "Server received: ~A\n" text-to-speak) - (set! espeak-ng-pid #f) + (put-string (cdr espeak-ng-input-pipe) + text-to-speak) + ;; XXX: These ports are line buffered(?). Always write to ports + ;; with a newline at their end if you want the other side to read + ;; them now. + (put-char (cdr espeak-ng-input-pipe) + #\newline) - (espeak-loop fifo-read-port - words-per-minute)) + ;; XXX: You also have you flush the port, otherwise the TTS keeps + ;; silent. Please someone explain this to me. + (force-output (cdr espeak-ng-input-pipe)) + + (loop (get-line fifo-read-port)))) (define (make-pipes buffering-type) @@ -273,13 +280,13 @@ (cond ((number? pid) (kill pid SIGTERM) - (format #t "Killed ~s.~%" pid) + (format #t "Killed ~S.~%" pid) (exit EXIT_SUCCESS)) (else (format #t "clipboard-speaker is not running.~%") (exit EXIT_FAILURE))))) (else - (format #t "No pid file: ~a~%" (config:option-ref options 'lock-file-path)) + (format #t "No pid file: ~S~%" (config:option-ref options 'lock-file-path)) (exit EXIT_FAILURE)))) @@ -298,13 +305,20 @@ (read-clipboard (config:option-ref options 'clipboard-type))) - (format #t "Clipboard: ~s~%" clipboard-output) + (format #t "Client sends: ~A~%" clipboard-output) - ;; Write lines into fifo write port and flush. + ;; Write clipboard contents into the FIFO file for the TTS server to + ;; read. (put-string (cdr fifo-r+w) clipboard-output) - - (flush-all-ports) + ;; XXX: These ports are line buffered. Always write to ports with + ;; a newline at their end if you want the other side to read them + ;; now. + (put-char (cdr fifo-r+w) #\newline) + + ;; XXX: We don't need to write to FIFO anymore. This will make sure + ;; the message passes through. Please someone explain this to me. + (close-port (cdr fifo-r+w)) ;; Try to lock the lock file. (define lock-file-port (open-file-locked (config:option-ref options