Typewriter sounds for emacs
Asked Answered
H

3

9

I would like to set up emacs so that it plays a typewriter sounds when typing text into the buffer, as well as a carriage return sound when hitting enter (similar to the Q10 editor on windows). Does anyone have any suggestions for how I might go about this? Is there a hook that I could use?

I currently use aquamacs and emacs 22, but am not averse to upgrading.

EDIT: In case anyone is interested, the vim version of this question was asked here: How can I make VIM play typewriter sound when I write a letter?

Hampden answered 26/6, 2012 at 11:15 Comment(0)
C
11

First you must establish some way to play sound:

    (defun play-typewriter-sound ()
      (let ((data-directory "~/Dowloads/Sounds"))
        (play-sound `(sound :file "key1.wav"))))

...doesn't work on Mac OSX Emacs for example since it's not compiled with sound support. There are workarounds though, see for example http://www.emacswiki.org/emacs/ErcSound

  • Then, you can use advice on any Emacsen

    (defadvice self-insert-command (after play-a-sound activate)
      (play-typewriter-sound))
    

    You could also advise newline-and-indent.

  • On Emacs24 you now have post-self-insert-hook

    (add-hook 'post-self-insert-hook 'play-typewriter-sound)
    
  • If you don't like defadvice you can use post-command-hook and check the name of this-command there:

    (add-hook 'post-command-hook #'play-typewriter-sound-maybe)
    
    (defun play-typewriter-sound-maybe ()
      (if (eq this-command 'self-insert-command)
          (play-typewriter-sound)))
    
Crashing answered 26/6, 2012 at 18:53 Comment(2)
Thanks, that's a great answer. I figured out one way to play sound on a mac was to call the afplay utility using start-process-shell-command.Hampden
For this use case, it sounds like compiling Emacs with sound support would be worth looking into. Then you could provide your typewriter noise in string format (see C-h C-i g (elisp) Sound Output) and have Emacs play it without needing to access the file system or an external process each time.Complacent
P
2

If someone need this using afplay here whats I use

(defun play (audio-name)
    (interactive)
    (let* (buf (get-buffer-create "playnoise"))
        (start-process-shell-command 
         "play" buf (concat (format "afplay /Users/foo/audios/%s" audio-name) ".mp3"))))

(play "wrong")
Pipage answered 16/5, 2015 at 16:27 Comment(0)
P
1

This is quite an old question now, but in case somebody stumbles into this, there's selectric-mode for emacs in MELPA now.

Punkie answered 2/10, 2022 at 1:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.