| 1 | ;;; emms-bookmarks.el --- Bookmarks for Emms. -*- lexical-binding: t; -*- |
| 2 | |
| 3 | ;; Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Yoni Rabkin <yrk@gnu.org> |
| 6 | ;; Keywords: emms, bookmark |
| 7 | |
| 8 | ;; This file is part of EMMS. |
| 9 | |
| 10 | ;; EMMS is free software; you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation; either version 3, or (at your option) |
| 13 | ;; any later version. |
| 14 | ;; |
| 15 | ;; EMMS is distributed in the hope that it will be useful, |
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ;; GNU General Public License for more details. |
| 19 | ;; |
| 20 | ;; You should have received a copy of the GNU General Public License |
| 21 | ;; along with EMMS; if not, write to the Free Software Foundation, |
| 22 | ;; Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | |
| 24 | ;;; Commentary: |
| 25 | ;; |
| 26 | ;; You can use this to add ``temporal bookmarks'' (term by Lucas |
| 27 | ;; Bonnet) to your media files. The interesting functions here are |
| 28 | ;; `emms-bookmarks-next', `emms-bookmarks-prev', `emms-bookmarks-add' |
| 29 | ;; (which pauses the player while you describe the bookmark) and |
| 30 | ;; `emms-bookmarks-clear'. All of which do exactly what you think |
| 31 | ;; they do. |
| 32 | |
| 33 | ;;; Code: |
| 34 | |
| 35 | |
| 36 | ;; dependencies |
| 37 | (require 'emms) |
| 38 | (require 'emms-playing-time) |
| 39 | |
| 40 | (defvar emms-bookmarks-prev-overshoot 5 |
| 41 | "Time in seconds for skipping a previous bookmark.") |
| 42 | |
| 43 | (defun emms-bookmarks-reset (track) |
| 44 | "Remove all the bookmarks from TRACK." |
| 45 | (emms-track-set track 'bookmarks nil)) |
| 46 | |
| 47 | (defun emms-bookmarks-straight-insertion-sort (item l acc) |
| 48 | "Insert ITEM into the already sorted L, ACC should be nil." |
| 49 | (if (null l) |
| 50 | (append acc (list item)) |
| 51 | (cond ((< (cdr item) (cdr (car l))) (append acc (list item (car l)) (cdr l))) |
| 52 | (t (emms-bookmarks-straight-insertion-sort item (cdr l) (append acc (list (car l)))))))) |
| 53 | |
| 54 | (defun emms-bookmarks-get (track) |
| 55 | "Return the bookmark property from TRACK." |
| 56 | (emms-track-get track 'bookmarks)) |
| 57 | |
| 58 | (defun emms-bookmarks-set (track desc time) |
| 59 | "Set bookmark property for TRACK, text DESC at TIME seconds." |
| 60 | (let ((old-bookmarks (emms-track-get track 'bookmarks)) |
| 61 | (new-bookmarks nil)) |
| 62 | (setq new-bookmarks (emms-bookmarks-straight-insertion-sort (cons desc time) old-bookmarks nil)) |
| 63 | (emms-track-set track 'bookmarks new-bookmarks))) |
| 64 | |
| 65 | (defun emms-bookmarks-set-current (desc) |
| 66 | "Set bookmark property for the current track with text DESC." |
| 67 | (emms-bookmarks-set (emms-playlist-current-selected-track) desc emms-playing-time)) |
| 68 | |
| 69 | (defun emms-bookmarks-search (time track test) |
| 70 | "Return a bookmark based on heuristics. |
| 71 | |
| 72 | TIME should be a reference point in seconds. |
| 73 | TRACK should be an Emms track. |
| 74 | TEST should be a numerical comparator predicate." |
| 75 | (let* ((s (append (list (cons "time" time)) |
| 76 | (copy-sequence (emms-bookmarks-get track)))) |
| 77 | (s (sort s #'(lambda (a b) (funcall test (cdr a) (cdr b)))))) |
| 78 | (while (not (= time (cdar s))) |
| 79 | (setq s (cdr s))) |
| 80 | (when (cdr s) |
| 81 | (car (cdr s))))) |
| 82 | |
| 83 | (defun emms-bookmarks-next-1 (time track) |
| 84 | "Return the bookmark after TIME for TRACK, otherwise return nil." |
| 85 | (emms-bookmarks-search time track #'<)) |
| 86 | |
| 87 | (defun emms-bookmarks-prev-1 (time track) |
| 88 | "Return the bookmark before TIME for TRACK, otherwise return nil." |
| 89 | (emms-bookmarks-search (- time emms-bookmarks-prev-overshoot) track #'>)) |
| 90 | |
| 91 | (defun emms-bookmarks-goto (search-f track failure-message) |
| 92 | "Seek the player to a bookmark. |
| 93 | |
| 94 | SEARCH-F should be a function which returns a bookmark. |
| 95 | TRACK should be an Emms track. |
| 96 | FAILURE-MESSAGE should be a string." |
| 97 | ;; note that when emms is paused then `emms-player-playing-p' => t |
| 98 | (when (not emms-player-playing-p) |
| 99 | (emms-start)) |
| 100 | (let ((m (funcall search-f emms-playing-time track))) |
| 101 | (if m |
| 102 | (progn |
| 103 | (emms-player-seek-to (cdr m)) |
| 104 | (message "%s" (car m))) |
| 105 | (message "%s" failure-message)))) |
| 106 | |
| 107 | |
| 108 | ;; entry points |
| 109 | |
| 110 | (defun emms-bookmarks-next () |
| 111 | "Seek to the next bookmark in the current track." |
| 112 | (interactive) |
| 113 | (emms-bookmarks-goto #'emms-bookmarks-next-1 |
| 114 | (emms-playlist-current-selected-track) |
| 115 | "No next bookmark")) |
| 116 | |
| 117 | (defun emms-bookmarks-prev () |
| 118 | "Seek to the previous bookmark in the current track." |
| 119 | (interactive) |
| 120 | (emms-bookmarks-goto #'emms-bookmarks-prev-1 |
| 121 | (emms-playlist-current-selected-track) |
| 122 | "No previous bookmark")) |
| 123 | |
| 124 | (defmacro emms-bookmarks-with-paused-player (&rest body) |
| 125 | "Eval BODY with player paused." |
| 126 | `(progn |
| 127 | (when (not emms-player-paused-p) (emms-pause)) |
| 128 | ,@body |
| 129 | (when emms-player-paused-p (emms-pause)))) |
| 130 | |
| 131 | ;; can't use `interactive' to promt the user here because we want to |
| 132 | ;; pause the player before the prompt appears. |
| 133 | (defun emms-bookmarks-add () |
| 134 | "Add a new bookmark to the current track. |
| 135 | |
| 136 | This function pauses the player while prompting the user for a |
| 137 | description of the bookmark. The function resumes the player |
| 138 | after the prompt." |
| 139 | (interactive) |
| 140 | (emms-bookmarks-with-paused-player |
| 141 | (let ((desc (read-string "Description: "))) |
| 142 | (if (emms-playlist-current-selected-track) |
| 143 | (emms-bookmarks-set-current desc) |
| 144 | (error "No current track to bookmark"))))) |
| 145 | |
| 146 | (defun emms-bookmarks-clear () |
| 147 | "Remove all the bookmarks from the current track." |
| 148 | (interactive) |
| 149 | (let ((this (emms-playlist-current-selected-track))) |
| 150 | (when this (emms-bookmarks-reset this)))) |
| 151 | |
| 152 | (provide 'emms-bookmarks) |
| 153 | |
| 154 | ;;; emms-bookmarks.el ends here |