[Edu-sig] Python sound.

Robert Rickenbrode rkr_ii@yahoo.com
Tue, 18 Dec 2001 12:57:04 -0800 (PST)


Hey folks, after some late night oil, I wrote these
wrappers to Snack, to simplify the interface (my
students haven't yet reached GUI or event driven
programming).  I hope this helps someone.

(You will need Snack for Python here:
http://www.speech.kth.se/snack/)

Basically, there are 3 functions:
SoundStart, SoundStop, and Beep.  SoundStart wraps the
 initialization for Snack and Tkinter, and hides the
tk window.  SoundStop tries to clean-up after the
initialization.  Beep is exactly analogous to the
winsound.Beep call, except that the duration is
seconds rather than milliseconds.

Example:
import compsci

compsci.SoundStart(50)   #volume gain set to 50%
compsci.Beep(261.63, 10) #plays Middle-C for 10
seconds
compsci.SoundStop()  #clean up


Enjoy.

__________________________

import Tkinter
import tkSnack

def SoundStart(volume=50):
    """Initialize the Sound System"""
    
    global root, filt
    root = Tkinter.Tk()
    tkSnack.initializeSnack(root)
    if volume > 100:
        volume = 100
    elif volume < 0:
        volume = 0
    tkSnack.audio.play_gain(volume)
    root.withdraw()


def Beep(freq, duration):
    """Play a note of freq hertz for duration
seconds"""
    s = tkSnack.Sound()
    filt = tkSnack.Filter('generator', freq, 30000,
0.0, 'sine', int(11500*duration))
    s.stop()
    s.play(filter=filt, blocking=1)


def SoundStop():
    """Turn off the Sound System"""
    try:
        root = root.destroy()
        filt = None
    except:
        pass

__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com