;;; -*- Emacs-Lisp -*-
;;; An experimental add on to mpg123.el - an emacs mpeg playing mode.
;;; $Id: mpg123-mods.el,v 1.3 2002/02/19 04:15:57 priestdo Exp $
;;; 

;;[Commentary]
;;	
;;      This package is an add on to mpg123.el.  It allows access to two
;;      parameters of the mpg123 program that control playback speed
;;      from within emacs while using mpg123 mode.  See the man page for
;;      mpg123 for a description of --doublespeed and --halfspeed
;;      parameters to learn what this can do for you.
;;	
;;[Requirement]
;;	
;;      All requirements for mpg123.el must be met.  See mpg123.el
;;	
;;[Installation]
;;	
;;      After installing mpg123.el, load this file.  As this is for
;;      testing purposes, I suggest you load it manualy with M-x
;;      load-file.  But if you realy like it, you could add it to your
;;      .emacs after you load mpg123.el.
;;
;;[How to Use]
;;
;;      This hack adds the two variables to mpg123 mode,
;;      mpg123-speed-double and mpg123-speed-half.  These allow the
;;      setting of the --doublespeed and --halfspeed parameters of
;;      mpg123 from withing emacs.  To play back at double speed
;;      change the value of mpg123-speed-double to "2".  This can be
;;      done with the emacs command set-variable.
;;


(defvar mpg123-speed-double "1"
  "*modifier string to set playback speed multiplier.
modifier string to set playback speed multiplier.")
(defvar mpg123-speed-half "1"
  "*modifier string to set playback speed divider.
modifier string to set playback speed divider.")

(defun mpg123:play (&optional startframe)
  "Play mp3 on current line."
  (save-excursion
    (set-buffer (get-buffer-create mpg123*info-buffer))
    (buffer-disable-undo)
    (erase-buffer))
  (beginning-of-line)
  (if (and mpg123*cur-play-marker
	   (markerp mpg123*cur-play-marker))
      (set-marker mpg123*cur-play-marker nil))
  (setq mpg123*cur-play-marker (point-marker))
  (skip-chars-forward " ")
  (if (or (not (looking-at "[0-9]"))
	  (not (mpg123:in-music-list-p)))
      nil ;;if not on music line, then exit
    (let ((last mpg123*cur-music-number) music p)
      (setq mpg123*cur-music-number (mpg123:get-music-number))
      (skip-chars-forward "^ ")
      (skip-chars-forward " ")
      (cond
       (startframe (setq mpg123*cur-start-frame startframe))
       ((or (looking-at mpg123*default-time-string)
	    ;;(not (equal last mpg123*cur-music-number))
	      )
	(setq mpg123*cur-start-frame "0"))
       (t
	(let ((time (buffer-substring
		     (point)
		     (progn (skip-chars-forward "^ /")(point)))))
	  (if (and (string= time mpg123*cur-playtime) mpg123*cur-playframe)
	      (setq mpg123*cur-start-frame mpg123*cur-playframe)
	    (setq mpg123*cur-start-frame (mpg123:time2frame time))))))
      (setq music (mpg123:get-music-info mpg123*cur-music-number 'filename))
      (if (fboundp 'code-convert-string)
	  (setq music (code-convert-string
		       music mpg123-process-coding-system *internal*)))
      (setq mpg123*time-setting-mode nil)
      (set-process-filter
       (setq p (start-process "mpg123"
			      (current-buffer)
			      mpg123-command
			      "-v"
			      "-k" mpg123*cur-start-frame
			      "-d" mpg123-speed-double
			      "-h" mpg123-speed-half
			      music
			      ))
       (if (mpg123:get-music-info mpg123*cur-music-number 'length)
	   'mpg123:filter
	 'mpg123:initial-filter))
      (message "%s %s.."
	       mpg123*cur-music-number
	       (mpg123:get-music-info mpg123*cur-music-number 'name))
      (set-process-sentinel p 'mpg123:sentinel))))
