File: sound.py

package info (click to toggle)
pythoncard 0.8.1-8.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 5,352 kB
  • ctags: 4,594
  • sloc: python: 42,401; makefile: 55; sh: 22
file content (46 lines) | stat: -rw-r--r-- 1,305 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
__version__ = "$Revision: 1.6 $"
__date__ = "$Date: 2004/04/14 00:17:43 $"
"""

import wx
import sndhdr

class Sound:
    """
    Feeble beginnings of a class for playing sounds.
    The goal is to be able to provide a filename and then be able
    to play the sound without worrying about sound formats or particular
    OS capabilities. I haven't investigated the Python Standard Libraries
    much yet.
    """
    
    def __init__(self, filename) :
        self._filename = filename
        self._sndType = sndhdr.what(filename)
        if self._sndType:
            self._sndType = self._sndType[0]
        if self._sndType in ['wav']:
            try:
                self._sound = wx.Sound(filename)    # support resources?
            except:
                self._sndType = None
        else:
            self._sndType = None
        if not self._sndType:
            raise

    def play(self, async=True, looped=False) :
        if self._sndType:
            if async:
                if looped:
                    flags = wx.SOUND_ASYNC | wx.SOUND_LOOP
                else:
                    flags = wx.SOUND_ASYNC
            else:
                flags = wx.SOUND_SYNC
            self._sound.Play(flags)

    def stop(self):
        if self._sndType:
            self._sound.Stop()