1 ;;; sound.el --- Loading sound files in SXEmacs
3 ;; Copyright (C) 1985, 1986, 1992, 1993, 1994 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
5 ;; Copyright (C) 2006 Sebastian Freundt
7 ;; Maintainer: SXEmacs Development Team
10 ;; This file is part of SXEmacs.
12 ;; SXEmacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; SXEmacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Synched up with: Not in FSF.
31 "Configure SXEmacs sounds and properties"
34 (defcustom sound-default-alist
35 '((default :sound bass)
36 (undefined-key :sound drum)
37 (undefined-click :sound drum)
38 ;; beginning-of-buffer or end-of-buffer errors.
39 (buffer-bound :sound drum)
40 ;; buffer-read-only error
41 (read-only :sound drum)
42 ;; non-interactive function or lambda called
43 (command-error :sound bass)
44 (y-or-n-p :sound quiet)
45 (yes-or-no-p :sound quiet)
46 (auto-save-error :sound whip :volume 100)
47 (no-completion :sound whip)
48 (isearch-failed :sound quiet)
49 (isearch-quit :sound bass)
50 ;; QUIT: sound generated by ^G and its variants.
51 (quit :sound quiet :volume 75)
52 ;; READY: time-consuming task has completed... compile,
55 ;; WARP: XEmacs has changed the selected-window or frame
56 ;; asynchronously... Especially when it's done by an
57 ;; asynchronous process filter. Perhaps by a debugger breakpoint
59 (warp :sound yeep :volume 75)
60 ;; ALARM: used for reminders...
61 (alarm :sound cuckoo :volume 100)
63 "The alist of sounds and associated error symbols.
65 Used to set sound-alist in load-default-sounds."
68 (group (symbol :tag "Name")
72 (const :format "" :value :sound)
73 (symbol :tag "Sound"))
75 (const :format "" :value :volume)
76 (integer :tag "Volume"))
78 (const :format "" :value :pitch)
79 (integer :tag "Pitch"))
81 (const :format "" :value :duration)
82 (integer :tag "Duration"))))))
84 (defcustom sound-load-list
85 '((load-sound-file "drum-beep" 'drum)
86 (load-sound-file "quiet-beep" 'quiet)
87 (load-sound-file "bass-snap" 'bass 80)
88 (load-sound-file "whip" 'whip 70)
89 (load-sound-file "cuckoo" 'cuckoo)
90 (load-sound-file "yeep" 'yeep)
91 (load-sound-file "hype" 'hype 100)
93 "A list of calls to load-sound-file to be processed by load-default-sounds.
95 Reference load-sound-file for more information."
98 :type '(repeat (sexp :tag "Sound")
101 (defcustom default-sound-directory (locate-data-directory "sounds")
102 "Default directory to load a sound file from."
107 ;; #### This should really be a list. --hniksic
108 ;; #### It is now :) -hroptatyr
109 (defcustom sound-extension-list (cond ((eq system-type 'linux)
110 '(".wav" ".au" ".mp3" ".mka" ".ogg"
111 ".aac" ".ac3" ".mp4" ".flac"
115 "List of filename extensions to complete sound file name with."
119 (defcustom default-sound-directory-list (locate-data-directory-list "sounds")
120 "List of directories which to search for sound files"
122 :type '(repeat directory )
125 (defvar default-media-stream-volume nil
126 "*The default volume to assign to media streams
127 when an explicit volume parameter is omitted.")
131 ;; these should be silent until sounds are loaded
132 (setq sound-alist '((ready nil) (warp nil))))
134 (eval-when-compile (defvar file))
136 (defun locate-sound-file (filename)
137 "Search for FILENAME in `default-sound-directory-list'
138 with respect to the extensions given by `sound-extension-list'."
139 (let ((exts (cond ((listp sound-extension-list)
140 sound-extension-list)
141 ((stringp sound-extension-list)
142 (split-string sound-extension-list ":"))
145 (cond ((file-exists-p filename)
146 (expand-file-name filename))
147 ((file-name-absolute-p filename)
148 ;; For absolute file names, we don't have on choice on the
149 ;; location, but sound extensions however can still be tried
150 (setq file (locate-file filename
151 (list (file-name-directory filename))
154 (setq file (locate-file filename
155 default-sound-directory-list
158 (defun make-sound-alist-item (filename sound-name &optional volume)
159 "Return an item suitable for `sound-alist'."
160 (let* ((file (locate-sound-file filename))
161 ;; let's create media-streams
162 (stream (make-media-stream :file file))
166 (error "Couldn't load sound file %s" filename))
168 ;; DEPRECATED as of merge of hrop-feat-MM series
171 ;; (set-buffer (setq buf (get-buffer-create " *sound-tmp*")))
172 ;; (buffer-disable-undo (current-buffer))
174 ;; (let ((coding-system-for-read 'binary))
175 ;; (insert-file-contents file))
176 ;; (setq data (buffer-string))
178 ;; (and buf (kill-buffer buf)))
180 (nconc (list sound-name)
181 (if (and volume (not (eq 0 volume)))
182 (list ':volume volume))
185 (list ':stream stream))))
188 (defun load-sound-file (filename sound-name &optional volume)
189 "Read in an audio-file and add it to the sound-alist.
190 The cached sound can be referenced later by SOUND-NAME.
192 FILENAME can either be absolute or relative, in which case the file will
193 be searched in the directories given by `default-sound-directory-list'.
194 When looking for the file, the extensions given by `sound-extension-list' are
195 also tried in the given order."
196 (interactive "fSound file name: \n\
197 SSymbol to name this sound: \n\
198 nVolume (0 for default): ")
199 (unless (symbolp sound-name)
200 (error "sound-name not a symbol"))
201 (unless (or (null volume) (integerp volume))
202 (error "volume not an integer or nil"))
204 (let ((item (make-sound-alist-item filename sound-name volume))
205 (old (assq sound-name sound-alist)))
207 ;; some conses in sound-alist might have been dumped with emacs.
209 (setq sound-alist (delq old (copy-sequence sound-alist))))
211 (setq sound-alist (cons item sound-alist)))
215 (defun load-default-sounds ()
216 "Load and install some sound files as beep-types, using
217 `load-sound-file'. This only works if you specify `default-audio-device'."
219 ;; #### - this should do NOTHING if the sounds can't be played.
220 (message "Loading sounds...")
221 (setq sound-alist nil)
222 ;; this is where the calls to load-sound-file get done
223 (mapc 'eval sound-load-list)
225 (append sound-default-alist
227 (message "Loading sounds...done")
232 (defun play-media-stream (stream &optional device sentinel volume)
233 "Play the media stream STREAM on an audio device.
234 STREAM must be a valid media-stream object as created by
236 Return a media-thread object which can be used to interact with
237 the worker thread which handles STREAM, which of course is only
238 useful in asynchronous mode.
240 This function uses the value of `synchronous-sounds' to decide
241 if a stream is played synchronously or asynchronously.
242 See `play-media-stream-synchronously' and
243 `play-media-stream&'.
245 Optional second argument DEVICE must be an audio device created
246 by `make-audio-device'.
247 If omitted DEVICE defaults to the value of `default-audio-device'.
249 Optional third argument SENTINEL specifies a lisp function to be
250 called after the stream playback finishes. The function should
251 take one argument (STREAM) where STREAM is bound to the
252 media stream which finished. See `set-media-thread-sentinel'.
254 Optional fourth argument VOLUME specifies an initial value for
258 Depending on the value of `synchronous-sounds' this function will
259 decide whether to play either asynchronously or synchronously.
261 If `synchronous-sounds' is `nil' AND threads are supported,
262 streams will be passed to the `play-media-stream&'
264 In that case return a media-thread object which can be used to
265 interact with the worker thread which handles STREAM.
267 If `synchronous-sounds' is non-`nil' OR threads are not supported,
268 streams will be passed to the `play-media-stream-synchronously'
270 In that case return non-`nil' if STREAM was played successfully,
273 See `play-media-stream-synchronously' and
274 `play-media-stream&'."
275 (let* ((vol (or volume default-media-stream-volume)))
276 (if (and (fboundp #'play-media-stream&)
277 (not synchronous-sounds))
278 (play-media-stream& stream device sentinel vol)
279 (play-media-stream-synchronously stream device sentinel vol))))
282 (defun play-sound (sound &optional volume device sentinel)
283 "Play the sound SOUND from `sound-alist'.
285 The sound is played at the specified VOLUME \(0-100, default
286 specified by the `bell-volume' variable\).
288 With no further media drivers, the sound file must be in the
289 Sun/NeXT U-LAW format. Under Linux WAV files are also supported.
291 DEVICE can be any device created by `make-audio-device' and
292 defaults to `default-audio-device', or, if that is `nil',
293 to the selected device.
295 Optional argument SENTINEL specifies a lisp function to be
296 called after the stream playback finishes. The function should
297 take two arguments (STREAM STATE) where STREAM is bound to the
298 media stream which finished and STATE is a symbol \(currently the
299 only valid symbol is 'finished\). See `set-media-thread-sentinel'."
300 (let ((data (cdr-safe (assq sound sound-alist))))
301 (when (and data default-audio-device)
302 (let ((ms (or (plist-get data :stream)
303 (let ((s (plist-get data :sound)))
305 (make-media-stream :data s)))))
307 (plist-get data :volume
308 (or volume default-media-stream-volume bell-volume))))
310 (play-media-stream ms device sentinel vol))))))
312 ;;; sound.el ends here.