def play(self):  # 播放文件
     f = open(self.file, 'rb')  # 打开文件
     header = f.read(WAV_HEADER_SIZE)  # 读取WAV文件头
     (riff, riffsize, wave, fmt, fmtsize,
      format, nchannels, samplespersecond,
      datarate, blockalign, bitspersample,
      data, size) =\
      struct.unpack('<4sl4s4slhhllhh4sl', header)    # 获取参数值
     if riff != 'RIFF' or fmtsize != 16 or fmt != 'fmt ' or data != 'data':  # 判断文件格式
         raise 'Data Error'
     wfx = pywintypes.WAVEFORMATEX()  # 创建WAVEFORMATEX结构
     wfx.wFormatTag = format
     wfx.nChannels = nchannels
     wfx.nSamplesPerSec = samplespersecond
     wfx.nAvgBytesPerSec = datarate
     wfx.nBlockAlign = blockalign
     wfx.wBitsPerSample = bitspersample
     d = directsound.DirectSoundCreate(None, None)  # 使用DirectSound播放声音
     d.SetCooperativeLevel(None, directsound.DSSCL_PRIORITY)
     sdesc = directsound.DSBUFFERDESC()
     sdesc.dwFlags = (directsound.DSBCAPS_STICKYFOCUS
                      | directsound.DSBCAPS_CTRLPOSITIONNOTIFY)
     sdesc.dwBufferBytes = size
     sdesc.lpwfxFormat = wfx
     self.buffer = buffer = d.CreateSoundBuffer(sdesc, None)
     buffer.Update(0, f.read(size))
     buffer.Play(0)
Exemple #2
0
 def play(self):
     f = open(self.file, 'rb')
     hdr = f.read(WAV_HEADER_SIZE)
     wfx, size = self.wav_header_unpack(hdr)
     d = ds.DirectSoundCreate(None, None)
     d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY)
     sdesc = ds.DSBUFFERDESC()
     sdesc.dwFlags = (ds.DSBCAPS_STICKYFOCUS
                      | ds.DSBCAPS_CTRLPOSITIONNOTIFY)
     sdesc.dwBufferBytes = size
     sdesc.lpwfxFormat = wfx
     self.currentbuffer = buffer = d.CreateSoundBuffer(sdesc, None)
     buffer.Update(0, f.read(size))
     buffer.Play(0)
Exemple #3
0
 def add(self):
     self.file = filedialog.askopenfilename(title='Python DirectSound',
                                            filetypes=[('WAV', '*.wav')])
     f = open(self.file, 'rb')
     hdr = f.read(WAV_HEADER_SIZE)
     wfx, size = self.wav_header_unpack(hdr)
     d = ds.DirectSoundCreate(None, None)
     d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY)
     sdesc = ds.DSBUFFERDESC()
     sdesc.dwFlags = (ds.DSBCAPS_STICKYFOCUS
                      | ds.DSBCAPS_CTRLPOSITIONNOTIFY)
     sdesc.dwBufferBytes = size
     sdesc.lpwfxFormat = wfx
     self.buffer = buffer = d.CreateSoundBuffer(sdesc, None)
     buffer.Update(0, f.read(size))
Exemple #4
0
    def testPlay(self):
        """Mesdames et Messieurs, la cour de Devin Dazzle"""
        # look for the test file in various places
        candidates = [
            os.path.dirname(__file__),
            os.path.dirname(sys.argv[0]),
            # relative to 'testall.py' in the win32com test suite.
            os.path.join(
                os.path.dirname(sys.argv[0]), "../../win32comext/directsound/test"
            ),
            ".",
        ]
        for candidate in candidates:
            fname = os.path.join(candidate, "01-Intro.wav")
            if os.path.isfile(fname):
                break
        else:
            raise TestSkipped("Can't find test .wav file to play")

        with open(fname, "rb") as f:
            hdr = f.read(WAV_HEADER_SIZE)
            wfx, size = wav_header_unpack(hdr)

            d = ds.DirectSoundCreate(None, None)
            d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY)

            sdesc = ds.DSBUFFERDESC()
            sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY
            sdesc.dwBufferBytes = size
            sdesc.lpwfxFormat = wfx

            buffer = d.CreateSoundBuffer(sdesc, None)

            event = win32event.CreateEvent(None, 0, 0, None)
            notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify)

            notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event))

            buffer.Update(0, f.read(size))

            buffer.Play(0)

            win32event.WaitForSingleObject(event, -1)
    def test_2_Attr(self):
        'DSBUFFERDESC attribute access'
        c = ds.DSBUFFERDESC()
        c.dwFlags = 1
        c.dwBufferBytes = 2
        c.lpwfxFormat = pywintypes.WAVEFORMATEX()
        c.lpwfxFormat.wFormatTag = pywintypes.WAVE_FORMAT_PCM
        c.lpwfxFormat.nChannels = 2
        c.lpwfxFormat.nSamplesPerSec = 44100
        c.lpwfxFormat.nAvgBytesPerSec = 176400
        c.lpwfxFormat.nBlockAlign = 4
        c.lpwfxFormat.wBitsPerSample = 16

        self.failUnless(c.dwFlags == 1)
        self.failUnless(c.dwBufferBytes == 2)
        self.failUnless(c.lpwfxFormat.wFormatTag == 1)
        self.failUnless(c.lpwfxFormat.nChannels == 2)
        self.failUnless(c.lpwfxFormat.nSamplesPerSec == 44100)
        self.failUnless(c.lpwfxFormat.nAvgBytesPerSec == 176400)
        self.failUnless(c.lpwfxFormat.nBlockAlign == 4)
        self.failUnless(c.lpwfxFormat.wBitsPerSample == 16)
Exemple #6
0
    def testPlay(self):
        """Mesdames et Messieurs, la cour de Devin Dazzle"""
        # relative to 'testall.py' in the win32com test suite.
        extra = os.path.join(
            os.path.dirname(sys.argv[0]), "../../win32comext/directsound/test"
        )

        fname = find_test_fixture("01-Intro.wav", extra)

        with open(fname, "rb") as f:
            hdr = f.read(WAV_HEADER_SIZE)
            wfx, size = wav_header_unpack(hdr)

            try:
                d = ds.DirectSoundCreate(None, None)
            except pythoncom.com_error as exc:
                if exc.hresult != ds.DSERR_NODRIVER:
                    raise
                raise TestSkipped(exc)
            d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY)

            sdesc = ds.DSBUFFERDESC()
            sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY
            sdesc.dwBufferBytes = size
            sdesc.lpwfxFormat = wfx

            buffer = d.CreateSoundBuffer(sdesc, None)

            event = win32event.CreateEvent(None, 0, 0, None)
            notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify)

            notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event))

            buffer.Update(0, f.read(size))

            buffer.Play(0)

            win32event.WaitForSingleObject(event, -1)
Exemple #7
0
def loadwav(wavefname):
    d = ds.DirectSoundCreate(None, None)
    d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY)

    w = wave.open(wavefname, "rb")

    sdesc = ds.DSBUFFERDESC()
    sdesc.lpwfxFormat = pywintypes.WAVEFORMATEX()
    sdesc.lpwfxFormat.wFormatTag = pywintypes.WAVE_FORMAT_PCM
    sdesc.lpwfxFormat.nChannels = w.getnchannels()
    sdesc.lpwfxFormat.nSamplesPerSec = w.getframerate()
    sdesc.lpwfxFormat.nAvgBytesPerSec = w.getnchannels() * w.getsampwidth(
    ) * w.getframerate()
    sdesc.lpwfxFormat.nBlockAlign = w.getnchannels() * w.getsampwidth()
    sdesc.lpwfxFormat.wBitsPerSample = w.getsampwidth() * 8
    sdesc.dwBufferBytes = w.getnframes() * w.getnchannels() * w.getsampwidth()
    sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY

    buffer = d.CreateSoundBuffer(sdesc, None)

    buffer.Update(0, w.readframes(w.getnframes()))
    w.close()
    return buffer
 def test_3_invalid_format(self):
     'DSBUFFERDESC invalid lpwfxFormat assignment'
     c = ds.DSBUFFERDESC()
     self.failUnlessRaises(ValueError, self.invalid_format, c)
 def test_1_Type(self):
     'DSBUFFERDESC type'
     c = ds.DSBUFFERDESC()
     self.failUnless(type(c) == ds.DSBUFFERDESCType)
Exemple #10
0
 def test_3_invalid_format(self):
     "DSBUFFERDESC invalid lpwfxFormat assignment"
     c = ds.DSBUFFERDESC()
     self.assertRaises(ValueError, self.invalid_format, c)
Exemple #11
0
 def test_1_Type(self):
     "DSBUFFERDESC type"
     c = ds.DSBUFFERDESC()
     self.assertTrue(type(c) == ds.DSBUFFERDESCType)
Exemple #12
0
    wfx.wBitsPerSample = bitspersample
    print("Bits per sample: " + str(bitspersample))
    return wfx, datalength


sound_file = sys.argv[1]
fname = os.path.join(os.path.dirname(__file__), sound_file)
print("You want to play file: " + fname)
f = open(fname, 'rb')

#read wav header and unpack
hdr = f.read(WAV_HEADER_SIZE)
wfx, size = wav_header_unpack(hdr)

d = ds.DirectSoundCreate(None, None)
d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY)

sdesc = ds.DSBUFFERDESC()
sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY
sdesc.dwBufferBytes = size
sdesc.lpwfxFormat = wfx

buffer = d.CreateSoundBuffer(sdesc, None)
event = win32event.CreateEvent(None, 0, 0, None)
notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify)
notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event))
buffer.Update(0, f.read(size))
buffer.Play(0)

win32event.WaitForSingleObject(event, -1)
Exemple #13
0
 def test_1_Type(self):
     'DSBUFFERDESC type'
     c = ds.DSBUFFERDESC()
     self.assertTrue(isinstance(c, ds.DSBUFFERDESCType))