Spaces:
Running
Running
File size: 12,961 Bytes
10865e1 |
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
import os
import struct
import sys
import unittest
import pythoncom
import pywintypes
import win32api
import win32com.directsound.directsound as ds
import win32event
from pywin32_testutil import TestSkipped, find_test_fixture
# next two lines are for for debugging:
# import win32com
# import directsound as ds
WAV_FORMAT_PCM = 1
WAV_HEADER_SIZE = struct.calcsize("<4sl4s4slhhllhh4sl")
def wav_header_unpack(data):
(
riff,
riffsize,
wave,
fmt,
fmtsize,
format,
nchannels,
samplespersecond,
datarate,
blockalign,
bitspersample,
data,
datalength,
) = struct.unpack("<4sl4s4slhhllhh4sl", data)
if riff != b"RIFF":
raise ValueError("invalid wav header")
if fmtsize != 16 or fmt != b"fmt " or data != b"data":
# fmt chuck is not first chunk, directly followed by data chuck
# It is nowhere required that they are, it is just very common
raise ValueError("cannot understand wav header")
wfx = pywintypes.WAVEFORMATEX()
wfx.wFormatTag = format
wfx.nChannels = nchannels
wfx.nSamplesPerSec = samplespersecond
wfx.nAvgBytesPerSec = datarate
wfx.nBlockAlign = blockalign
wfx.wBitsPerSample = bitspersample
return wfx, datalength
def wav_header_pack(wfx, datasize):
return struct.pack(
"<4sl4s4slhhllhh4sl",
b"RIFF",
36 + datasize,
b"WAVE",
b"fmt ",
16,
wfx.wFormatTag,
wfx.nChannels,
wfx.nSamplesPerSec,
wfx.nAvgBytesPerSec,
wfx.nBlockAlign,
wfx.wBitsPerSample,
b"data",
datasize,
)
class WAVEFORMATTest(unittest.TestCase):
def test_1_Type(self):
"WAVEFORMATEX type"
w = pywintypes.WAVEFORMATEX()
self.assertTrue(type(w) == pywintypes.WAVEFORMATEXType)
def test_2_Attr(self):
"WAVEFORMATEX attribute access"
# A wav header for a soundfile from a CD should look like this...
w = pywintypes.WAVEFORMATEX()
w.wFormatTag = pywintypes.WAVE_FORMAT_PCM
w.nChannels = 2
w.nSamplesPerSec = 44100
w.nAvgBytesPerSec = 176400
w.nBlockAlign = 4
w.wBitsPerSample = 16
self.assertTrue(w.wFormatTag == 1)
self.assertTrue(w.nChannels == 2)
self.assertTrue(w.nSamplesPerSec == 44100)
self.assertTrue(w.nAvgBytesPerSec == 176400)
self.assertTrue(w.nBlockAlign == 4)
self.assertTrue(w.wBitsPerSample == 16)
class DSCAPSTest(unittest.TestCase):
def test_1_Type(self):
"DSCAPS type"
c = ds.DSCAPS()
self.assertTrue(type(c) == ds.DSCAPSType)
def test_2_Attr(self):
"DSCAPS attribute access"
c = ds.DSCAPS()
c.dwFlags = 1
c.dwMinSecondarySampleRate = 2
c.dwMaxSecondarySampleRate = 3
c.dwPrimaryBuffers = 4
c.dwMaxHwMixingAllBuffers = 5
c.dwMaxHwMixingStaticBuffers = 6
c.dwMaxHwMixingStreamingBuffers = 7
c.dwFreeHwMixingAllBuffers = 8
c.dwFreeHwMixingStaticBuffers = 9
c.dwFreeHwMixingStreamingBuffers = 10
c.dwMaxHw3DAllBuffers = 11
c.dwMaxHw3DStaticBuffers = 12
c.dwMaxHw3DStreamingBuffers = 13
c.dwFreeHw3DAllBuffers = 14
c.dwFreeHw3DStaticBuffers = 15
c.dwFreeHw3DStreamingBuffers = 16
c.dwTotalHwMemBytes = 17
c.dwFreeHwMemBytes = 18
c.dwMaxContigFreeHwMemBytes = 19
c.dwUnlockTransferRateHwBuffers = 20
c.dwPlayCpuOverheadSwBuffers = 21
self.assertTrue(c.dwFlags == 1)
self.assertTrue(c.dwMinSecondarySampleRate == 2)
self.assertTrue(c.dwMaxSecondarySampleRate == 3)
self.assertTrue(c.dwPrimaryBuffers == 4)
self.assertTrue(c.dwMaxHwMixingAllBuffers == 5)
self.assertTrue(c.dwMaxHwMixingStaticBuffers == 6)
self.assertTrue(c.dwMaxHwMixingStreamingBuffers == 7)
self.assertTrue(c.dwFreeHwMixingAllBuffers == 8)
self.assertTrue(c.dwFreeHwMixingStaticBuffers == 9)
self.assertTrue(c.dwFreeHwMixingStreamingBuffers == 10)
self.assertTrue(c.dwMaxHw3DAllBuffers == 11)
self.assertTrue(c.dwMaxHw3DStaticBuffers == 12)
self.assertTrue(c.dwMaxHw3DStreamingBuffers == 13)
self.assertTrue(c.dwFreeHw3DAllBuffers == 14)
self.assertTrue(c.dwFreeHw3DStaticBuffers == 15)
self.assertTrue(c.dwFreeHw3DStreamingBuffers == 16)
self.assertTrue(c.dwTotalHwMemBytes == 17)
self.assertTrue(c.dwFreeHwMemBytes == 18)
self.assertTrue(c.dwMaxContigFreeHwMemBytes == 19)
self.assertTrue(c.dwUnlockTransferRateHwBuffers == 20)
self.assertTrue(c.dwPlayCpuOverheadSwBuffers == 21)
class DSBCAPSTest(unittest.TestCase):
def test_1_Type(self):
"DSBCAPS type"
c = ds.DSBCAPS()
self.assertTrue(type(c) == ds.DSBCAPSType)
def test_2_Attr(self):
"DSBCAPS attribute access"
c = ds.DSBCAPS()
c.dwFlags = 1
c.dwBufferBytes = 2
c.dwUnlockTransferRate = 3
c.dwPlayCpuOverhead = 4
self.assertTrue(c.dwFlags == 1)
self.assertTrue(c.dwBufferBytes == 2)
self.assertTrue(c.dwUnlockTransferRate == 3)
self.assertTrue(c.dwPlayCpuOverhead == 4)
class DSCCAPSTest(unittest.TestCase):
def test_1_Type(self):
"DSCCAPS type"
c = ds.DSCCAPS()
self.assertTrue(type(c) == ds.DSCCAPSType)
def test_2_Attr(self):
"DSCCAPS attribute access"
c = ds.DSCCAPS()
c.dwFlags = 1
c.dwFormats = 2
c.dwChannels = 4
self.assertTrue(c.dwFlags == 1)
self.assertTrue(c.dwFormats == 2)
self.assertTrue(c.dwChannels == 4)
class DSCBCAPSTest(unittest.TestCase):
def test_1_Type(self):
"DSCBCAPS type"
c = ds.DSCBCAPS()
self.assertTrue(type(c) == ds.DSCBCAPSType)
def test_2_Attr(self):
"DSCBCAPS attribute access"
c = ds.DSCBCAPS()
c.dwFlags = 1
c.dwBufferBytes = 2
self.assertTrue(c.dwFlags == 1)
self.assertTrue(c.dwBufferBytes == 2)
class DSBUFFERDESCTest(unittest.TestCase):
def test_1_Type(self):
"DSBUFFERDESC type"
c = ds.DSBUFFERDESC()
self.assertTrue(type(c) == ds.DSBUFFERDESCType)
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.assertTrue(c.dwFlags == 1)
self.assertTrue(c.dwBufferBytes == 2)
self.assertTrue(c.lpwfxFormat.wFormatTag == 1)
self.assertTrue(c.lpwfxFormat.nChannels == 2)
self.assertTrue(c.lpwfxFormat.nSamplesPerSec == 44100)
self.assertTrue(c.lpwfxFormat.nAvgBytesPerSec == 176400)
self.assertTrue(c.lpwfxFormat.nBlockAlign == 4)
self.assertTrue(c.lpwfxFormat.wBitsPerSample == 16)
def invalid_format(self, c):
c.lpwfxFormat = 17
def test_3_invalid_format(self):
"DSBUFFERDESC invalid lpwfxFormat assignment"
c = ds.DSBUFFERDESC()
self.assertRaises(ValueError, self.invalid_format, c)
class DSCBUFFERDESCTest(unittest.TestCase):
def test_1_Type(self):
"DSCBUFFERDESC type"
c = ds.DSCBUFFERDESC()
self.assertTrue(type(c) == ds.DSCBUFFERDESCType)
def test_2_Attr(self):
"DSCBUFFERDESC attribute access"
c = ds.DSCBUFFERDESC()
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.assertTrue(c.dwFlags == 1)
self.assertTrue(c.dwBufferBytes == 2)
self.assertTrue(c.lpwfxFormat.wFormatTag == 1)
self.assertTrue(c.lpwfxFormat.nChannels == 2)
self.assertTrue(c.lpwfxFormat.nSamplesPerSec == 44100)
self.assertTrue(c.lpwfxFormat.nAvgBytesPerSec == 176400)
self.assertTrue(c.lpwfxFormat.nBlockAlign == 4)
self.assertTrue(c.lpwfxFormat.wBitsPerSample == 16)
def invalid_format(self, c):
c.lpwfxFormat = 17
def test_3_invalid_format(self):
"DSCBUFFERDESC invalid lpwfxFormat assignment"
c = ds.DSCBUFFERDESC()
self.assertRaises(ValueError, self.invalid_format, c)
class DirectSoundTest(unittest.TestCase):
# basic tests - mostly just exercise the functions
def testEnumerate(self):
"""DirectSoundEnumerate() sanity tests"""
devices = ds.DirectSoundEnumerate()
# this might fail on machines without a sound card
self.assertTrue(len(devices))
# if we have an entry, it must be a tuple of size 3
self.assertTrue(len(devices[0]) == 3)
def testCreate(self):
"""DirectSoundCreate()"""
try:
d = ds.DirectSoundCreate(None, None)
except pythoncom.com_error as exc:
if exc.hresult != ds.DSERR_NODRIVER:
raise
raise TestSkipped(exc)
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)
class DirectSoundCaptureTest(unittest.TestCase):
# basic tests - mostly just exercise the functions
def testEnumerate(self):
"""DirectSoundCaptureEnumerate() sanity tests"""
devices = ds.DirectSoundCaptureEnumerate()
# this might fail on machines without a sound card
self.assertTrue(len(devices))
# if we have an entry, it must be a tuple of size 3
self.assertTrue(len(devices[0]) == 3)
def testCreate(self):
"""DirectSoundCreate()"""
try:
d = ds.DirectSoundCaptureCreate(None, None)
except pythoncom.com_error as exc:
if exc.hresult != ds.DSERR_NODRIVER:
raise
raise TestSkipped(exc)
def testRecord(self):
try:
d = ds.DirectSoundCaptureCreate(None, None)
except pythoncom.com_error as exc:
if exc.hresult != ds.DSERR_NODRIVER:
raise
raise TestSkipped(exc)
sdesc = ds.DSCBUFFERDESC()
sdesc.dwBufferBytes = 352800 # 2 seconds
sdesc.lpwfxFormat = pywintypes.WAVEFORMATEX()
sdesc.lpwfxFormat.wFormatTag = pywintypes.WAVE_FORMAT_PCM
sdesc.lpwfxFormat.nChannels = 2
sdesc.lpwfxFormat.nSamplesPerSec = 44100
sdesc.lpwfxFormat.nAvgBytesPerSec = 176400
sdesc.lpwfxFormat.nBlockAlign = 4
sdesc.lpwfxFormat.wBitsPerSample = 16
buffer = d.CreateCaptureBuffer(sdesc)
event = win32event.CreateEvent(None, 0, 0, None)
notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify)
notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event))
buffer.Start(0)
win32event.WaitForSingleObject(event, -1)
event.Close()
data = buffer.Update(0, 352800)
fname = os.path.join(win32api.GetTempPath(), "test_directsound_record.wav")
f = open(fname, "wb")
f.write(wav_header_pack(sdesc.lpwfxFormat, 352800))
f.write(data)
f.close()
if __name__ == "__main__":
unittest.main()
|