File: Sound.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (95 lines) | stat: -rw-r--r-- 3,004 bytes parent folder | download | duplicates (2)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python

import wx
import wx.adv
from Main import opj

#----------------------------------------------------------------------

class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        wx.Panel.__init__(self, parent, -1)
        self.log = log

        b = wx.Button(self, -1, "Play Sound 1 (sync)", (25, 25))
        self.Bind(wx.EVT_BUTTON, self.OnButton1, b)

        b = wx.Button(self, -1, "Play Sound 2 (async)", (25, 65))
        self.Bind(wx.EVT_BUTTON, self.OnButton2, b)

        b = wx.Button(self, -1, "Select .WAV file", (25, 105))
        self.Bind(wx.EVT_BUTTON, self.OnSelectSound, b)


    def OnButton1(self, evt):
        try:
            sound = wx.adv.Sound(opj('data/anykey.wav'))
            self.log.write("before Play...\n")
            sound.Play(wx.adv.SOUND_SYNC)
            self.log.write("...after Play\n")
        except NotImplementedError as v:
            wx.MessageBox(str(v), "Exception Message")


    def OnButton2(self, evt):
        try:
            if True:
                sound = wx.adv.Sound(opj('data/plan.wav'))
            else:
                # sounds can also be loaded from a buffer object
                with open(opj('data/plan.wav'), 'rb') as fid:
                    data = fid.read()
                sound = wx.SoundFromData(data)

            self.log.write("before Play...\n")
            sound.Play(wx.adv.SOUND_ASYNC)
            self.sound = sound  # save a reference (This shouldn't be needed, but there seems to be a bug...)
            # wx.YieldIfNeeded()
            self.log.write("...after Play\n")
        except NotImplementedError as v:
            wx.MessageBox(str(v), "Exception Message")


    def OnSelectSound(self, evt):
        dlg = wx.FileDialog(wx.GetTopLevelParent(self),
                            "Choose a sound file",
                            wildcard="WAV files (*.wav)|*.wav",
                            style=wx.FD_OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            try:
                #sound = wx.adv.Sound(dlg.GetPath(), wx.SOUND_SYNC)
                #sound.Play()

                # another way to do it.
                wx.adv.Sound.PlaySound(dlg.GetPath(), wx.adv.SOUND_SYNC)

            except NotImplementedError as v:
                wx.MessageBox(str(v), "Exception Message")
        dlg.Destroy()


#----------------------------------------------------------------------

def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win

#----------------------------------------------------------------------


overview = """<html><body>
<h2>Sound</h2>
This class represents a short wave file, in Windows WAV format, that can
be stored in memory and played.
<p>
This demo offers two examples, both driven by buttons, but obviously the event
that drives the playing of the sound can come from anywhere.

</body></html>
"""


if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])