我在GNU/Linux下使用的桌面环境工具组合(4)

;; Make sure active processes are killed before sawfish exits
(add-hook 'before-exit-hook
          (lambda ()
            (mapc stop-process (active-processes))))

3. 音量控制:目前音量控制绑定的快捷键如下

Win+]:运行volume-up-by-ratio,增加音量1%

Win+[:运行volume-down-by-ratio,减小音量1%

Win+\:运行toggle-volume-mute,在当静音与当前音量间切换

其所关联的Sawfish函数会调用amixer命令来完成指定的操作。具体可以查阅amixer相关帮助。Sawfish代码如下:

;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; System sound volume related variables and functions
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq max-volume (string->number (getenv "MAX_SOUND_VOLUME")))
(setq min-volume 0)
(setq current-left-volume 0)
(setq current-right-volume 0)
(setq left-volume-before-mute 0)
(setq right-volume-before-mute 0)
(setq volume-adjust-ratio-step 0.01)
(setq toggle-mute-flag nil)

;; Get volume for both left and right channels and store them into variables
(defun get-volume()
  "Get the volume string"
  (interactive)
  (let* ((res-stream (make-string-output-stream))
  (proc (make-process res-stream))
  (res-str)
  (res-str-splitted))
    (call-process proc nil "/usr/local/bin/scripts/get_volume.sh")
    (setq res-str (get-output-stream-string res-stream))
    (setq res-str-splitted (string-split "," (substring res-str 0 (- (length res-str) 1))))
    (setq current-left-volume (string->number (nth 0 res-str-splitted)))
    (setq current-right-volume (string->number (nth 1 res-str-splitted)))))
(get-volume)

;; Get volume percentage string
(defun get-volume-percentage-str (vol)
  "Get the percentage of the specified volume quantity in string format"
  (interactive)
  (concat (number->string (round (* (/ vol max-volume) 100))) "%"))

;; Set volume by ratio
(defun set-volume-by-ratio (lft-vol rt-vol)
  "Set volume by percentage"
  (interactive)
  (system (concat "amixer cset " (getenv "DEFAULT_SOUND") " " (number->string (* max-volume lft-vol)) "," (number->string (* max-volume rt-vol)) " &"))
  (get-volume))

;; Set volume by value
(defun set-volume-by-value (lft-vol rt-vol)
  "Set volume by value"
  (interactive)
  (system (concat "amixer cset " (getenv "DEFAULT_SOUND") " " (number->string lft-vol) "," (number->string rt-vol) " &"))
  (get-volume))

;; Increase volume by ratio
(defun volume-up-by-ratio (vol)
  "Increase volume by ratio"
  (interactive)
  (let ((tmp-lft-vol)
 (tmp-rt-vol))
    (get-volume)
    (setq tmp-lft-vol (+ current-left-volume (* max-volume vol)))
    (setq tmp-rt-vol (+ current-right-volume (* max-volume vol)))
    ;; Detect if the volume is larger than max-volume
    (if (> tmp-lft-vol max-volume)
 (setq tmp-lft-vol max-volume))
    (if (> tmp-rt-vol max-volume)
 (setq tmp-rt-vol max-volume))
   
    (set-volume-by-value tmp-lft-vol tmp-rt-vol)))

;; Decrease volume by ratio
(defun volume-down-by-ratio (vol)
  "Decrease volume by ratio"
  (interactive)
  (let ((tmp-lft-vol)
 (tmp-rt-vol))
    (get-volume)
    (setq tmp-lft-vol (- current-left-volume (* max-volume vol)))
    (setq tmp-rt-vol (- current-right-volume (* max-volume vol)))
    ;; Detect if the volume is smaller than min-volume
    (if (< tmp-lft-vol min-volume)
 (setq tmp-lft-vol min-volume))
    (if (< tmp-rt-vol min-volume)
 (setq tmp-rt-vol min-volume))
   
    (set-volume-by-value tmp-lft-vol tmp-rt-vol)))

;; Mute volume
(defun volume-mute ()
  "Mute system sound"
  (interactive)
  (set-volume-by-value min-volume min-volume))

;; Unmute volume
(defun volume-unmute ()
  "Unmute system sound"
  (interactive)
  (set-volume-by-value left-volume-before-mute right-volume-before-mute))

;; Full volume
(defun volume-max ()
  "Set to max sound volume"
  (interactive)
  (set-volume-by-value max-volume max-volume))

;; Toggle mute
(defun toggle-volume-mute ()
  "Toggle between current sound volume and mute"
  (interactive)
  (get-volume)
  (if (and (= current-left-volume min-volume) (= current-right-volume min-volume))
      (if toggle-mute-flag
  (progn
    (volume-unmute)
    (setq toggle-mute-flag nil)
    (display-close-message (concat "Sound restored to L:" (get-volume-percentage-str current-left-volume) " R:" (get-volume-percentage-str current-right-volume)) alert-msg-short-time alert-msg-attrib)))
    (progn
      (setq left-volume-before-mute current-left-volume)
      (setq right-volume-before-mute current-right-volume)
      (volume-mute)
      (setq toggle-mute-flag t)
      (display-close-message "Mute" alert-msg-short-time alert-msg-attrib))))

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/15885.html