R: [Tutor] snack classes
Michael Lange
klappnase at freenet.de
Sun Nov 23 19:25:54 EST 2003
On Sun, 23 Nov 2003 11:34:28 +0100
"Andrea Valle" <marta_andrea at libero.it> wrote:
>
> As you can see, I'd like to use the Sig class derived from the Sound class
> to make some sample manipulation, i.e. the function sine create a sinusoidal
> signal, etc.. The sample method of Sound is really powerful.
Hi Andrea,
I put your code in a little script to try what happens:
from Tkinter import *
from tkSnack import *
from math import *
class Sig(Sound):
def __init__(self):
self.sec=2
self.length=44100*self.sec
def sine(self):
l=self.length
for x in range(l):
y=int(10000*sin(float(x)*50))
self.sample(x,y)
return self
def test():
root=Tk()
initializeSnack(root)
s = Sig()
s.sine()
root.mainloop()
test()
Running this in IDLE leads to:
Traceback (most recent call last):
File "/usr/local/share/test.py", line 28, in -toplevel-
test()
File "/usr/local/share/test.py", line 25, in test
s.sine()
File "/usr/local/share/test.py", line 18, in sine
self.sample(x,y)
File "/usr/lib/python2.2/site-packages/tkSnack.py", line 288, in sample
return _cast(self.tk.call((self.name, 'sample', index) + opts))
AttributeError: Sig instance has no attribute 'tk'
Well, of course there's no attribute "tk" in Sig, look at __init__:
self.sec=2
self.length=44100*self.sec
there are in fact only two integers there.
So let's change this a little:
from Tkinter import *
from tkSnack import *
from math import *
class Sig(Sound):
def __init__(self):
Sound.__init__(self)
self.sec=2
self.length=44100*self.sec
def sine(self):
l=self.length
for x in range(l):
y=int(10000*sin(float(x)*50))
self.sample(x,y)
return self
def test():
root=Tk()
initializeSnack(root)
s = Sig()
s.sine()
root.mainloop()
test()
Running in IDLE results in:
Traceback (most recent call last):
File "/usr/local/share/test.py", line 27, in -toplevel-
test()
File "/usr/local/share/test.py", line 24, in test
s.sine()
File "/usr/local/share/test.py", line 17, in sine
self.sample(x,y)
File "/usr/lib/python2.2/site-packages/tkSnack.py", line 288, in sample
return _cast(self.tk.call((self.name, 'sample', index) + opts))
TclError: Index out of bounds
Ah, we've come farther :)
"Index out of bounds" seems to tell us that we were trying to access data that don't exist -
of course our SIG object doesn't contain any data yet.
So let's feed some data to Sig:
from Tkinter import *
from tkSnack import *
from math import *
class Sig(Sound):
def __init__(self):
Sound.__init__(self)
self.sec=2
self.length=44100*self.sec
self.load('/home/pingu/phonoripper/test_2.wav')
def sine(self):
l=self.length
for x in range(l):
y=int(10000*sin(float(x)*50))
self.sample(x,y)
return self
def test():
root=Tk()
initializeSnack(root)
s = Sig()
s.sine()
root.mainloop()
test()
No more error messages! However except of computing a while not much happens,
so let's finally do it something:
from Tkinter import *
from tkSnack import *
from math import *
class Sig(Sound):
def __init__(self):
Sound.__init__(self)
self.sec=2
self.length=44100*self.sec
self.load('/home/pingu/phonoripper/test_2.wav')
def sine(self):
l=self.length
for x in range(l):
y=int(10000*sin(float(x)*50))
self.sample(x,y)
return self
def test():
root=Tk()
initializeSnack(root)
s = Sig()
s2 = s.sine()
s2.play()
s2.write('/home/pingu/phonoripper/sig_test.wav')
root.mainloop()
test()
Success, it computes a while, beeps 2 seconds and writes a file that when opened beeps 2
seconds again.
I'm afraid I'm not one of the "gurus" here, so I can't explain about class definitions very well,
but I hope this helped anyway.
Cheers
Michael
More information about the Tutor
mailing list