Yet another sound module
Kare Sjolander
kare at speech.kth.se
Tue Aug 8 12:28:04 EDT 2000
Hello Pythonians,
I include an initial attempt at interfacing the Snack Sound Toolkit with
Python. Hopefully, somebody knowledgeable in Python/Tkinter can tell me
if I'm on the right track.
Snack currently supports Linux, Solaris, HP-UX, IRIX, NetBSD, Macintosh,
and Windows95/98/NT/2K, see http://www.speech.kth.se/snack/
The included code is for a simple playback/record application which also
shows a waveform. Only tested on Linux. Build Snack according to the
instructions in the distribution,.
I started learning Python at lunch time, so I realize the code can be
improved on ;-)
I would be very thankful to suggestions about the API. More specifically
I would like to be able to avoid using this syntax
s=Sound(master=root)
in favor of this
s=Sound()
in the code below.
Regards
Kare Sjolander
from Tkinter import *
from Tkinter import _default_root
class Sound:
def __init__(self, name=None, cnf={}, master=None, **kw):
self.name = None
if not master:
global _default_root
if not _default_root:
_default_root = Tk()
master = _default_root
if not master:
raise RuntimeError, 'Tk not initialized'
self.tk = master.tk
if kw and cnf: cnf = _cnfmerge((cnf, kw))
elif kw: cnf = kw
options = ()
for k, v in cnf.items():
if callable(v):
v = self._register(v)
options = options + ('-'+k, v)
if not name:
self.name = self.tk.call(('sound',) + options)
else:
self.name = self.tk.call(('sound', name,) + options)
def read(self, filename):
return self.tk.call(self.name, 'read', filename)
def info(self):
return self.tk.call(self.name, 'info')
def sample(self, index):
return self.tk.call(self.name, 'sample', index)
def play(self):
return self.tk.call(self.name, 'play')
def record(self):
return self.tk.call(self.name, 'record')
def stop(self):
return self.tk.call(self.name, 'stop')
class SnackCanvas(Canvas):
def create_waveform(self, *args, **kw):
return self._create('waveform', args, kw)
root = Tk()
root.tk.call('eval', 'package require snack')
s=Sound(master=root)
def load():
file = root.tk.call('eval', 'snack::getOpenFile')
s.read(file)
def play():
s.play()
def stop():
s.stop()
def record():
s.record()
def info():
print s.info()
c=SnackCanvas(root,height=100)
c.pack()
c.create_waveform(0,0,sound=s.name)
root.tk.call('snack::createIcons')
a=Button(root,image='snackOpen',command=load)
a.pack(side='left')
b=Button(root,bitmap='snackPlay',command=play)
b.pack(side='left')
d=Button(root,bitmap='snackRecord',fg='red',command=record)
d.pack(side='left')
e=Button(root,bitmap='snackStop',command=stop)
e.pack(side='left')
f=Button(root,text='Info',command=info)
f.pack(side='left')
g=Button(root,text='Quit',command=root.quit)
g.pack(side='left')
root.tk.mainloop()
More information about the Python-list
mailing list