My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
;; play-sound.lisp
;; status: feature spartan and no real attempt to manage memory but works
;; 11/30/08 Alexander Repenning
;;; updated: 05/13/09
;;; todo: support parallel sound: new NSsound instance when old one is still playing sound

(in-package :lui)


(defparameter *Sounds* (make-hash-table :test #'equal) "Sound handles")


(defvar *Secondary-Sound-File-Directory-Hook* nil "lambda () -> directory-pathname")


(defgeneric PLAY-SOUND (Name &key Loops)
(:documentation "Play sound in resources/sounds/ folder"))


(defgeneric STOP-SOUND (Name)
(:documentation "Stop playing sound"))


(defgeneric SET-VOLUME (Name Volume)
(:documentation "Set volume of sound <Name> playing to <volume>. <volume> in range 0.0 ..1.0"))


(defgeneric SHUT-UP-SOUNDS ()
(:documentation "Stop all sounds from playing"))


(defmethod PLAY-SOUND ((Name string) &key Loops (path nil))
#+cocotron (declare (ignore Loops))
(ccl::with-autorelease-pool
(let ((Sound (or (gethash Name *Sounds*)
(let ((New-Sound
(#/initWithContentsOfFile:byReference:
(#/alloc ns:ns-sound)
(native-string (if path path (native-path "lui:resources;sounds;" Name)))
#$YES)))
;; (print "loading sound")
(unless (%null-ptr-p New-Sound)
(setf (gethash Name *Sounds*) New-Sound))))))
(unless Sound (return-from play-sound (error "sound \"~A\" is missing" Name)))
#-cocotron
(#/setLoops: Sound (if Loops #$YES #$NO))
(#/play Sound))))


(defmethod SET-VOLUME ((Name string) Volume)
#+cocotron (Declare (ignore volume))
#-cocotron
(let ((Sound (gethash Name *Sounds*)))
(when Sound (#/setVolume: Sound Volume))))


(defmethod SHUT-UP-SOUNDS ()
(maphash
#'(lambda (Name Sound)
(declare (ignore Sound))
(stop-sound Name))
*Sounds*))


(defmethod STOP-SOUND (Name)
(let ((Sound (gethash Name *Sounds*)))
(when Sound (#/stop Sound))))


(defun SOUND-FILES-IN-SOUND-FILE-DIRECTORY () "
out: Sound-Files lisp of pathname"
(directory "lui:resources;sounds;*.*"))




#| Examples:

(play-sound "Radar-beep.mp3")

(play-sound "Inhale.mp3")

(play-sound "Explode.mp3")

(play-sound "RuinsMystique.mp3")

(play-sound "missing-sound.mp3")

(play-sound "Inhale.mp3" :loops t)

(play-sound "whiteNoise.mp3" :loops t)

(shut-up-sounds)


(progn
(play-sound "whiteNoise.mp3" :loops t)
(sleep 1.0)
(set-volume "whiteNoise.mp3" 0.5)
(sleep 1.0)
(set-volume "whiteNoise.mp3" 0.1)
(sleep 1.0)
(stop-sound "whiteNoise.mp3"))



|#

Change log

r1658 by pokermike2012 on Jan 31, 2012   Diff
Play-sound now optionally takes a path
Go to: 
Project members, sign in to write a code review

Older revisions

r1547 by pokermike2012 on Nov 16, 2011   Diff
fixing warnings on Windows

r1437 by aiolida on Oct 5, 2011   Diff
changed "warn" to error in play-sound
r1332 by pokermike2012 on Sep 7, 2011   Diff
Play-sound was causing a memory leak
when called from the inflation-jog-
slider, so I surrounded it with an
autorelease-pool
All revisions of this file

File info

Size: 2675 bytes, 107 lines
Powered by Google Project Hosting