Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tick sound more stable using background long audio playing #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions org-pomodoro.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ finishes the pomodoro and enters the break period."
:group 'org-pomodoro
:type 'string)

(defcustom org-pomodoro-audio-player-buffer "*org-pomodoro-audio-player*"
"Buffer which is play audio sounds."
:group 'org-pomodoro
:type 'string)

(defcustom org-pomodoro-audio-player (or (executable-find "aplay")
(executable-find "afplay"))
(defcustom org-pomodoro-audio-player (or (executable-find "mpv")
(executable-find "mplayer")
(executable-find "ffplay")
(executable-find "play"))
"Music player used to play sounds."
:group 'org-pomodoro
:type 'string)
Expand Down Expand Up @@ -228,9 +234,16 @@ Use `org-pomodoro-long-break-sound' to determine what sound that should be."
:group 'org-pomodoro
:type 'boolean)

(defcustom org-pomodoro-ticking-sound-long nil
"Determines whether ticking sounds are long or not."
:group 'org-pomodoro
:type 'boolean)

(defcustom org-pomodoro-ticking-sound (when load-file-name
(concat (file-name-directory load-file-name)
"resources/tick.wav"))
(if org-pomodoro-ticking-sound-long
"resources/tick_long.mp3"
"resources/tick.wav")))
"The path to a sound file that´s to be played while a pomodoro is running."
:group 'org-pomodoro
:type 'file)
Expand All @@ -245,7 +258,7 @@ Use `org-pomodoro-long-break-sound' to determine what sound that should be."
:group 'org-pomodoro
:type 'list)

(defcustom org-pomodoro-ticking-frequency 1
(defcustom org-pomodoro-ticking-frequency (if org-pomodoro-ticking-sound-long 10 1)
"The frequency at which to playback the ticking sound."
:group 'org-pomodoro
:type 'list)
Expand Down Expand Up @@ -406,26 +419,35 @@ or :break when starting a break.")
(:tick org-pomodoro-ticking-sound-args)
(t (error "Unknown org-pomodoro sound: %S" type))))

(defun org-pomodoro-stop-sound ()
"Stop an playing audio file now"
(let ((kill-buffer-query-functions nil))
(when (get-buffer org-pomodoro-audio-player-buffer)
(kill-buffer org-pomodoro-audio-player-buffer))))

(defun org-pomodoro-play-sound (type)
"Play an audio file specified by TYPE (:pomodoro, :short-break, :long-break)."
(let ((sound (org-pomodoro-sound type))
(args (org-pomodoro-sound-args type)))
(args (concat (org-pomodoro-sound-args type)
(when (string-equal org-pomodoro-audio-player (executable-find "ffplay"))
" -nodisp -autoexit"))))
(cond ((and (fboundp 'sound-wav-play)
org-pomodoro-play-sounds
sound)
(sound-wav-play sound))
((and org-pomodoro-audio-player
org-pomodoro-play-sounds
sound)
(start-process-shell-command
"org-pomodoro-audio-player" nil
(mapconcat 'identity
`(,org-pomodoro-audio-player
,@(delq nil (list args (shell-quote-argument (expand-file-name sound)))))
" "))))))
org-pomodoro-play-sounds
sound)
(sound-wav-play sound))
((and org-pomodoro-audio-player
org-pomodoro-play-sounds
sound)
(start-process-shell-command
"org-pomodoro-audio-player" org-pomodoro-audio-player-buffer
(mapconcat 'identity
`(,org-pomodoro-audio-player
,@(delq nil (list args (shell-quote-argument (expand-file-name sound)))))
" "))))))

(defun org-pomodoro-maybe-play-sound (type)
"Play an audio file specified by TYPE."
(org-pomodoro-stop-sound)
(when (org-pomodoro-sound-p type)
(org-pomodoro-play-sound type)))

Expand Down Expand Up @@ -485,7 +507,7 @@ invokes the handlers for finishing."
(run-hooks 'org-pomodoro-tick-hook)
(org-pomodoro-update-mode-line)
(when (and (member org-pomodoro-state org-pomodoro-ticking-sound-states)
(equal (mod (truncate (org-pomodoro-remaining-seconds))
(equal (mod (truncate (1+ (org-pomodoro-remaining-seconds)))
org-pomodoro-ticking-frequency)
0))
(org-pomodoro-maybe-play-sound :tick))))
Expand Down
Binary file added resources/tick_long.mp3
Binary file not shown.