[Tkinter-discuss] Drawing waveform on a Canvas

Mohammad Tayseer m_tayseer82 at yahoo.com
Wed Jan 18 20:41:26 CET 2006


Michael Lange <klappnase at web.de> wrote:
> Hello list,
>
> I am trying to draw a graphical representation of a soundfile 
> (a.k.a. waveform) onto a Canvas,

You have two problems
1. Reading a sound file
2. drawing the waveform

I solved the 2nd problem only. This is a code that draws a random waveform.
---------------------------------------------------------------------------------
from Tkinter import *
import random
import math
import time

def fun(zoom=.1):
    t1 = time.time()
    global c
    # remove the old shapes.
    for item in c.find_all():
        c.delete(item)
    # calculate a random amplitude
    amplitude = random.random() * int(c.winfo_height()) / 2
    points = []
    for x in range(0, c.winfo_width()):
        y = math.sin(x * math.pi / (zoom*180)) * amplitude + int(c.winfo_height()) / 2
        points.append(x)
        points.append(y)
    #  now points should be of form [x0, y0, x1, y1, ...]
    # not [(x0, y0), ...]
    c.create_line(smooth=1, *points)
    c.update_idletasks() # to draw the shapes immediately.
    print 'Timing:', time.time() - t1
    
root = Tk()
c = Canvas(root, bg='beige')
c.pack(expand=1, fill=BOTH)
Button(root, text='Do waveform', command=fun).pack()
mainloop()
---------------------------------------------------------------------------------
It took 0.0310001373291 second to execute fun() in full-screen mode. 
I don't think customizing this will be a problem, but you know you can always ask.




			
---------------------------------
Yahoo! Photos
 Got holiday prints? See all the ways to get quality prints in your hands ASAP.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tkinter-discuss/attachments/20060118/c98a0b48/attachment.html 


More information about the Tkinter-discuss mailing list