blob: 0a74458c9ee567c6c1193a3e2e635b6e1290b6c2 [file] [log] [blame]
Guido van Rossum157e3f81992-05-18 14:49:07 +00001#! /usr/local/python
Guido van Rossumf62e1dd1992-05-15 15:39:56 +00002
Guido van Rossum157e3f81992-05-18 14:49:07 +00003# XXX This only works on SGIs running IRIX 4.0 or higher
Guido van Rossumf62e1dd1992-05-15 15:39:56 +00004
5# JUKEBOX: browse directories full of sampled sound files.
6#
7# One or more "list windows" display the files and subdirectories of
8# the arguments. Double-clicking on a subdirectory opens a new window
9# displaying its contents (and so on recursively). Double clicking
10# on a file plays it as a sound file (assuming it is one).
11#
Guido van Rossum157e3f81992-05-18 14:49:07 +000012# Playing is asynchronous: the application keeps listening for events
13# while the sample is playing, so you can cancel playing or start a
14# new sample right away. Synchronous playing is available through the
15# -s option.
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000016#
Guido van Rossum157e3f81992-05-18 14:49:07 +000017# The control window displays a "stop button" that cancel the current
18# play request.
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000019#
Guido van Rossum157e3f81992-05-18 14:49:07 +000020# Most sound file formats recognized by SOX or SFPLAY are recognized.
21# Since conversion is costly, converted files are cached in
Guido van Rossumef963591992-05-19 13:47:37 +000022# /usr/tmp/@j* until the user quits or changes the sampling rate via
23# the Rate menu.
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000024
25import commands
26import getopt
27import os
Guido van Rossumef963591992-05-19 13:47:37 +000028from stat import *
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000029import rand
30import stdwin
31from stdwinevents import *
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000032import sys
33import tempfile
Guido van Rossumef963591992-05-19 13:47:37 +000034import sndhdr
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000035
36from WindowParent import WindowParent
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000037from Buttons import PushButton
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000038
39# Pathnames
40
Guido van Rossum157e3f81992-05-18 14:49:07 +000041DEF_DB = '/usr/local/sounds' # Default directory of sounds
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000042SOX = '/usr/local/sox' # Sound format conversion program
43SFPLAY = '/usr/sbin/sfplay' # Sound playing program
44
45
46# Global variables
47
48class struct(): pass # Class to define featureless structures
49
Guido van Rossum157e3f81992-05-18 14:49:07 +000050G = struct() # oHlds writable global variables
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000051
52
53# Main program
54
55def main():
56 G.synchronous = 0 # If set, use synchronous audio.write()
57 G.debug = 0 # If set, print debug messages
58 G.busy = 0 # Set while asynchronous playing is active
59 G.windows = [] # List of open windows, except control
60 G.mode = '' # File type (default any that sfplay knows)
61 G.rate = 0 # Sampling rate (default " " " ")
62 G.tempprefix = tempfile.mktemp()
63 #
64 try:
65 optlist, args = getopt.getopt(sys.argv[1:], 'dr:st:')
66 except getopt.error, msg:
67 sys.stdout = sys.stderr
68 print msg
69 print 'usage: jukebox [-d] [-s] [-t type] [-r rate]'
Guido van Rossum157e3f81992-05-18 14:49:07 +000070 print ' -d debugging (-dd event debugging)'
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000071 print ' -s synchronous playing'
72 print ' -t type file type'
73 print ' -r rate sampling rate'
74 sys.exit(2)
75 #
76 for optname, optarg in optlist:
77 if optname == '-d':
Guido van Rossum157e3f81992-05-18 14:49:07 +000078 G.debug = G.debug + 1
Guido van Rossumf62e1dd1992-05-15 15:39:56 +000079 elif optname == '-r':
80 G.rate = int(eval(optarg))
81 elif optname == '-s':
82 G.synchronous = 1
83 elif optname == '-t':
84 G.mode = optarg
85 #
86 if not args:
87 args = [DEF_DB]
88 #
89 G.cw = opencontrolwindow()
90 for dirname in args:
91 G.windows.append(openlistwindow(dirname))
92 #
93 #
94 try:
95 maineventloop()
96 finally:
97 clearcache()
98 killchild()
99
Guido van Rossum157e3f81992-05-18 14:49:07 +0000100# Entries in Rate menu:
Guido van Rossumef963591992-05-19 13:47:37 +0000101rates = ['default', '7350', \
Guido van Rossum157e3f81992-05-18 14:49:07 +0000102 '8000', '11025', '16000', '22050', '32000', '41000', '48000']
103
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000104def maineventloop():
105 mouse_events = WE_MOUSE_DOWN, WE_MOUSE_MOVE, WE_MOUSE_UP
106 while G.windows:
Guido van Rossumef963591992-05-19 13:47:37 +0000107 try:
108 type, w, detail = event = stdwin.getevent()
109 except KeyboardInterrupt:
110 killchild()
111 continue
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000112 if w == G.cw.win:
113 if type == WE_CLOSE:
114 return
115 if type == WE_TIMER:
116 checkchild()
117 if G.busy:
118 G.cw.win.settimer(1)
Guido van Rossum157e3f81992-05-18 14:49:07 +0000119 elif type == WE_MENU:
120 menu, item = detail
121 if menu is G.ratemenu:
122 clearcache()
123 if item == 0:
124 G.rate = 0
125 else:
126 G.rate = eval(rates[item])
127 for i in range(len(rates)):
128 menu.check(i, (i == item))
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000129 else:
130 G.cw.dispatch(event)
131 else:
132 if type == WE_DRAW:
133 w.drawproc(w, detail)
134 elif type in mouse_events:
135 w.mouse(w, type, detail)
136 elif type == WE_CLOSE:
137 w.close(w)
138 del w, event
139 else:
Guido van Rossum157e3f81992-05-18 14:49:07 +0000140 if G.debug > 1: print type, w, detail
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000141
142def checkchild():
143 if G.busy:
144 waitchild(1)
145
146def killchild():
147 if G.busy:
148 os.kill(G.busy, 9)
149 waitchild(0)
150
151def waitchild(options):
152 pid, sts = os.wait(G.busy, options)
153 if pid == G.busy:
154 G.busy = 0
155 G.stop.enable(0)
156
157
158# Control window -- to set gain and cancel play operations in progress
159
160def opencontrolwindow():
161 stdwin.setdefscrollbars(0, 0)
162 cw = WindowParent().create('Jukebox', (0, 0))
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000163 #
Guido van Rossum157e3f81992-05-18 14:49:07 +0000164 stop = PushButton().definetext(cw, ' Stop ')
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000165 stop.hook = stop_hook
166 stop.enable(0)
167 G.stop = stop
168 #
169 cw.realize()
Guido van Rossum157e3f81992-05-18 14:49:07 +0000170 #
171 G.ratemenu = cw.win.menucreate('Rate')
172 for r in rates:
173 G.ratemenu.additem(r)
174 if G.rate == 0:
175 G.ratemenu.check(0, 1)
176 else:
177 for i in len(range(rates)):
178 if rates[i] == `G.rate`:
179 G.ratemenu.check(i, 1)
180 #
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000181 return cw
182
183def stop_hook(self):
184 killchild()
185
186
187# List windows -- to display list of files and subdirectories
188
189def openlistwindow(dirname):
190 list = os.listdir(dirname)
191 list.sort()
192 i = 0
193 while i < len(list):
Guido van Rossumef963591992-05-19 13:47:37 +0000194 if list[i][0] == '.':
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000195 del list[i]
196 else:
197 i = i+1
198 for i in range(len(list)):
Guido van Rossumef963591992-05-19 13:47:37 +0000199 fullname = os.path.join(dirname, list[i])
200 if os.path.isdir(fullname):
201 info = '/'
202 else:
203 try:
204 size = os.stat(fullname)[ST_SIZE]
205 info = `(size + 1023)/1024` + 'k'
206 except IOError:
207 info = '???'
208 info = '(' + info + ')'
209 list[i] = list[i], info
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000210 width = maxwidth(list)
211 # width = width + stdwin.textwidth(' ') # XXX X11 stdwin bug workaround
212 height = len(list) * stdwin.lineheight()
213 stdwin.setdefwinsize(width, min(height, 500))
214 stdwin.setdefscrollbars(0, 1)
215 w = stdwin.open(dirname)
216 stdwin.setdefwinsize(0, 0)
217 w.setdocsize(width, height)
218 w.drawproc = drawlistwindow
219 w.mouse = mouselistwindow
220 w.close = closelistwindow
221 w.dirname = dirname
222 w.list = list
223 w.selected = -1
224 return w
225
226def maxwidth(list):
227 width = 1
Guido van Rossumef963591992-05-19 13:47:37 +0000228 for name, info in list:
229 w = stdwin.textwidth(name + ' ' + info)
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000230 if w > width: width = w
231 return width
232
233def drawlistwindow(w, area):
234## (left, top), (right, bottom) = area
235 d = w.begindrawing()
236 d.erase((0, 0), (1000, 10000))
237 lh = d.lineheight()
238 h, v = 0, 0
Guido van Rossumef963591992-05-19 13:47:37 +0000239 for name, info in w.list:
240 if info == '/':
241 text = name + '/'
242 else:
243 text = name + ' ' + info
244 d.text((h, v), text)
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000245 v = v + lh
246 showselection(w, d)
247 d.close()
248
249def hideselection(w, d):
250 if w.selected >= 0:
251 invertselection(w, d)
252
253def showselection(w, d):
254 if w.selected >= 0:
255 invertselection(w, d)
256
257def invertselection(w, d):
258 lh = d.lineheight()
259 h1, v1 = p1 = 0, w.selected*lh
260 h2, v2 = p2 = 1000, v1 + lh
261 d.invert(p1, p2)
262
263def mouselistwindow(w, type, detail):
264 (h, v), clicks, button = detail[:3]
265 d = w.begindrawing()
266 lh = d.lineheight()
267 if 0 <= v < lh*len(w.list):
268 i = v / lh
269 else:
270 i = -1
271 if w.selected <> i:
272 hideselection(w, d)
273 w.selected = i
274 showselection(w, d)
275 d.close()
276 if type == WE_MOUSE_DOWN and clicks >= 2 and i >= 0:
277 setcursors('watch')
Guido van Rossumef963591992-05-19 13:47:37 +0000278 name, info = w.list[i]
279 fullname = os.path.join(w.dirname, name)
280 if info == '/':
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000281 if clicks == 2:
Guido van Rossumef963591992-05-19 13:47:37 +0000282 G.windows.append(openlistwindow(fullname))
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000283 else:
Guido van Rossumef963591992-05-19 13:47:37 +0000284 playfile(fullname)
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000285 setcursors('cross')
286
287def closelistwindow(w):
Guido van Rossumef963591992-05-19 13:47:37 +0000288 G.windows.remove(w)
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000289
290def setcursors(cursor):
291 for w in G.windows:
292 w.setwincursor(cursor)
293 G.cw.win.setwincursor(cursor)
294
295
296# Playing tools
297
298cache = {}
299
300def clearcache():
301 for x in cache.keys():
Guido van Rossum157e3f81992-05-18 14:49:07 +0000302 cmd = 'rm -f ' + cache[x]
303 if G.debug: print cmd
304 sts = os.system(cmd)
305 if sts:
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000306 print cmd
Guido van Rossum157e3f81992-05-18 14:49:07 +0000307 print 'Exit status', sts
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000308 del cache[x]
309
Guido van Rossum157e3f81992-05-18 14:49:07 +0000310validrates = (8000, 11025, 16000, 22050, 32000, 44100, 48000)
311
312def playfile(filename):
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000313 killchild()
Guido van Rossum157e3f81992-05-18 14:49:07 +0000314 tuple = sndhdr.what(filename)
315 raw = 0
316 if tuple:
317 mode, rate = tuple[:2]
318 if rate == 0:
319 rate = G.rate
320 if rate == 0:
321 rate = 8000
322 else:
323 mode = G.mode
324 rate = G.rate
325 if G.debug: print 'mode =', mode, 'rate =', rate
326 if mode in ('au', 'aiff', 'wav', 'aifc', 'ul', 'ub', 'sb') and \
327 rate in validrates:
328 tempname = filename
329 if mode in ('ul', 'ub', 'sb'):
330 raw = 1
331 elif cache.has_key(filename):
332 tempname = cache[filename]
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000333 else:
334 tempname = G.tempprefix + `rand.rand()` + '.aiff'
335 cmd = SOX
Guido van Rossum157e3f81992-05-18 14:49:07 +0000336 if G.debug:
337 cmd = cmd + ' -V'
338 if mode <> '':
339 cmd = cmd + ' -t ' + mode
340 cmd = cmd + ' ' + commands.mkarg(filename)
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000341 cmd = cmd + ' -t aiff'
Guido van Rossum157e3f81992-05-18 14:49:07 +0000342 if rate not in validrates:
343 rate = 32000
344 if rate:
345 cmd = cmd + ' -r ' + `rate`
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000346 cmd = cmd + ' ' + tempname
347 if G.debug: print cmd
348 sts = os.system(cmd)
349 if sts:
350 print cmd
351 print 'Exit status', sts
352 stdwin.fleep()
Guido van Rossumef963591992-05-19 13:47:37 +0000353 try:
354 os.unlink(tempname)
355 except:
356 pass
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000357 return
Guido van Rossum157e3f81992-05-18 14:49:07 +0000358 cache[filename] = tempname
359 if raw:
360 pid = sfplayraw(tempname, tuple)
361 else:
362 pid = sfplay(tempname, [])
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000363 if G.synchronous:
364 sts = os.wait(pid, 0)
365 else:
366 G.busy = pid
367 G.stop.enable(1)
368 G.cw.win.settimer(1)
369
Guido van Rossum157e3f81992-05-18 14:49:07 +0000370def sfplayraw(filename, tuple):
Guido van Rossum157e3f81992-05-18 14:49:07 +0000371 args = ['-i']
372 type, rate, channels, frames, bits = tuple
373 if type == 'ul':
374 args.append('mulaw')
375 elif type == 'ub':
376 args = args + ['integer', '8', 'unsigned']
377 elif type == 'sb':
378 args = args + ['integer', '8', '2scomp']
379 else:
380 print 'sfplayraw: warning: unknown type in', tuple
381 if channels > 1:
382 args = args + ['channels', `channels`]
383 if not rate:
384 rate = G.rate
385 if rate:
386 args = args + ['rate', `rate`]
387 args.append('end')
388 return sfplay(filename, args)
389
390def sfplay(filename, args):
391 if G.debug:
392 args = ['-p'] + args
393 args = [SFPLAY, '-r'] + args + [filename]
394 if G.debug: print 'sfplay:', args
395 pid = os.fork()
396 if pid == 0:
397 # Child
398 os.exec(SFPLAY, args)
399 # NOTREACHED
400 else:
401 # Parent
402 return pid
403
Guido van Rossumf62e1dd1992-05-15 15:39:56 +0000404main()