ds_test.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import unittest
  2. import struct
  3. import sys
  4. import os
  5. import pywintypes
  6. import win32event, win32api
  7. import os
  8. from pywin32_testutil import TestSkipped
  9. import win32com.directsound.directsound as ds
  10. import pythoncom
  11. # next two lines are for for debugging:
  12. # import win32com
  13. # import directsound as ds
  14. WAV_FORMAT_PCM = 1
  15. WAV_HEADER_SIZE = struct.calcsize('<4sl4s4slhhllhh4sl')
  16. def wav_header_unpack(data):
  17. (riff, riffsize, wave, fmt, fmtsize, format, nchannels, samplespersecond,
  18. datarate, blockalign, bitspersample, data, datalength) \
  19. = struct.unpack('<4sl4s4slhhllhh4sl', data)
  20. if riff != b'RIFF':
  21. raise ValueError('invalid wav header')
  22. if fmtsize != 16 or fmt != b'fmt ' or data != b'data':
  23. # fmt chuck is not first chunk, directly followed by data chuck
  24. # It is nowhere required that they are, it is just very common
  25. raise ValueError('cannot understand wav header')
  26. wfx = pywintypes.WAVEFORMATEX()
  27. wfx.wFormatTag = format
  28. wfx.nChannels = nchannels
  29. wfx.nSamplesPerSec = samplespersecond
  30. wfx.nAvgBytesPerSec = datarate
  31. wfx.nBlockAlign = blockalign
  32. wfx.wBitsPerSample = bitspersample
  33. return wfx, datalength
  34. def wav_header_pack(wfx, datasize):
  35. return struct.pack('<4sl4s4slhhllhh4sl', b'RIFF', 36 + datasize,
  36. b'WAVE', b'fmt ', 16,
  37. wfx.wFormatTag, wfx.nChannels, wfx.nSamplesPerSec,
  38. wfx.nAvgBytesPerSec, wfx.nBlockAlign,
  39. wfx.wBitsPerSample, b'data', datasize);
  40. class WAVEFORMATTest(unittest.TestCase):
  41. def test_1_Type(self):
  42. 'WAVEFORMATEX type'
  43. w = pywintypes.WAVEFORMATEX()
  44. self.assertTrue(type(w) == pywintypes.WAVEFORMATEXType)
  45. def test_2_Attr(self):
  46. 'WAVEFORMATEX attribute access'
  47. # A wav header for a soundfile from a CD should look like this...
  48. w = pywintypes.WAVEFORMATEX()
  49. w.wFormatTag = pywintypes.WAVE_FORMAT_PCM
  50. w.nChannels = 2
  51. w.nSamplesPerSec = 44100
  52. w.nAvgBytesPerSec = 176400
  53. w.nBlockAlign = 4
  54. w.wBitsPerSample = 16
  55. self.assertTrue(w.wFormatTag == 1)
  56. self.assertTrue(w.nChannels == 2)
  57. self.assertTrue(w.nSamplesPerSec == 44100)
  58. self.assertTrue(w.nAvgBytesPerSec == 176400)
  59. self.assertTrue(w.nBlockAlign == 4)
  60. self.assertTrue(w.wBitsPerSample == 16)
  61. class DSCAPSTest(unittest.TestCase):
  62. def test_1_Type(self):
  63. 'DSCAPS type'
  64. c = ds.DSCAPS()
  65. self.assertTrue(type(c) == ds.DSCAPSType)
  66. def test_2_Attr(self):
  67. 'DSCAPS attribute access'
  68. c = ds.DSCAPS()
  69. c.dwFlags = 1
  70. c.dwMinSecondarySampleRate = 2
  71. c.dwMaxSecondarySampleRate = 3
  72. c.dwPrimaryBuffers = 4
  73. c.dwMaxHwMixingAllBuffers = 5
  74. c.dwMaxHwMixingStaticBuffers = 6
  75. c.dwMaxHwMixingStreamingBuffers = 7
  76. c.dwFreeHwMixingAllBuffers = 8
  77. c.dwFreeHwMixingStaticBuffers = 9
  78. c.dwFreeHwMixingStreamingBuffers = 10
  79. c.dwMaxHw3DAllBuffers = 11
  80. c.dwMaxHw3DStaticBuffers = 12
  81. c.dwMaxHw3DStreamingBuffers = 13
  82. c.dwFreeHw3DAllBuffers = 14
  83. c.dwFreeHw3DStaticBuffers = 15
  84. c.dwFreeHw3DStreamingBuffers = 16
  85. c.dwTotalHwMemBytes = 17
  86. c.dwFreeHwMemBytes = 18
  87. c.dwMaxContigFreeHwMemBytes = 19
  88. c.dwUnlockTransferRateHwBuffers = 20
  89. c.dwPlayCpuOverheadSwBuffers = 21
  90. self.assertTrue(c.dwFlags == 1)
  91. self.assertTrue(c.dwMinSecondarySampleRate == 2)
  92. self.assertTrue(c.dwMaxSecondarySampleRate == 3)
  93. self.assertTrue(c.dwPrimaryBuffers == 4)
  94. self.assertTrue(c.dwMaxHwMixingAllBuffers == 5)
  95. self.assertTrue(c.dwMaxHwMixingStaticBuffers == 6)
  96. self.assertTrue(c.dwMaxHwMixingStreamingBuffers == 7)
  97. self.assertTrue(c.dwFreeHwMixingAllBuffers == 8)
  98. self.assertTrue(c.dwFreeHwMixingStaticBuffers == 9)
  99. self.assertTrue(c.dwFreeHwMixingStreamingBuffers == 10)
  100. self.assertTrue(c.dwMaxHw3DAllBuffers == 11)
  101. self.assertTrue(c.dwMaxHw3DStaticBuffers == 12)
  102. self.assertTrue(c.dwMaxHw3DStreamingBuffers == 13)
  103. self.assertTrue(c.dwFreeHw3DAllBuffers == 14)
  104. self.assertTrue(c.dwFreeHw3DStaticBuffers == 15)
  105. self.assertTrue(c.dwFreeHw3DStreamingBuffers == 16)
  106. self.assertTrue(c.dwTotalHwMemBytes == 17)
  107. self.assertTrue(c.dwFreeHwMemBytes == 18)
  108. self.assertTrue(c.dwMaxContigFreeHwMemBytes == 19)
  109. self.assertTrue(c.dwUnlockTransferRateHwBuffers == 20)
  110. self.assertTrue(c.dwPlayCpuOverheadSwBuffers == 21)
  111. class DSBCAPSTest(unittest.TestCase):
  112. def test_1_Type(self):
  113. 'DSBCAPS type'
  114. c = ds.DSBCAPS()
  115. self.assertTrue(type(c) == ds.DSBCAPSType)
  116. def test_2_Attr(self):
  117. 'DSBCAPS attribute access'
  118. c = ds.DSBCAPS()
  119. c.dwFlags = 1
  120. c.dwBufferBytes = 2
  121. c.dwUnlockTransferRate = 3
  122. c.dwPlayCpuOverhead = 4
  123. self.assertTrue(c.dwFlags == 1)
  124. self.assertTrue(c.dwBufferBytes == 2)
  125. self.assertTrue(c.dwUnlockTransferRate == 3)
  126. self.assertTrue(c.dwPlayCpuOverhead == 4)
  127. class DSCCAPSTest(unittest.TestCase):
  128. def test_1_Type(self):
  129. 'DSCCAPS type'
  130. c = ds.DSCCAPS()
  131. self.assertTrue(type(c) == ds.DSCCAPSType)
  132. def test_2_Attr(self):
  133. 'DSCCAPS attribute access'
  134. c = ds.DSCCAPS()
  135. c.dwFlags = 1
  136. c.dwFormats = 2
  137. c.dwChannels = 4
  138. self.assertTrue(c.dwFlags == 1)
  139. self.assertTrue(c.dwFormats == 2)
  140. self.assertTrue(c.dwChannels == 4)
  141. class DSCBCAPSTest(unittest.TestCase):
  142. def test_1_Type(self):
  143. 'DSCBCAPS type'
  144. c = ds.DSCBCAPS()
  145. self.assertTrue(type(c) == ds.DSCBCAPSType)
  146. def test_2_Attr(self):
  147. 'DSCBCAPS attribute access'
  148. c = ds.DSCBCAPS()
  149. c.dwFlags = 1
  150. c.dwBufferBytes = 2
  151. self.assertTrue(c.dwFlags == 1)
  152. self.assertTrue(c.dwBufferBytes == 2)
  153. class DSBUFFERDESCTest(unittest.TestCase):
  154. def test_1_Type(self):
  155. 'DSBUFFERDESC type'
  156. c = ds.DSBUFFERDESC()
  157. self.assertTrue(type(c) == ds.DSBUFFERDESCType)
  158. def test_2_Attr(self):
  159. 'DSBUFFERDESC attribute access'
  160. c = ds.DSBUFFERDESC()
  161. c.dwFlags = 1
  162. c.dwBufferBytes = 2
  163. c.lpwfxFormat = pywintypes.WAVEFORMATEX()
  164. c.lpwfxFormat.wFormatTag = pywintypes.WAVE_FORMAT_PCM
  165. c.lpwfxFormat.nChannels = 2
  166. c.lpwfxFormat.nSamplesPerSec = 44100
  167. c.lpwfxFormat.nAvgBytesPerSec = 176400
  168. c.lpwfxFormat.nBlockAlign = 4
  169. c.lpwfxFormat.wBitsPerSample = 16
  170. self.assertTrue(c.dwFlags == 1)
  171. self.assertTrue(c.dwBufferBytes == 2)
  172. self.assertTrue(c.lpwfxFormat.wFormatTag == 1)
  173. self.assertTrue(c.lpwfxFormat.nChannels == 2)
  174. self.assertTrue(c.lpwfxFormat.nSamplesPerSec == 44100)
  175. self.assertTrue(c.lpwfxFormat.nAvgBytesPerSec == 176400)
  176. self.assertTrue(c.lpwfxFormat.nBlockAlign == 4)
  177. self.assertTrue(c.lpwfxFormat.wBitsPerSample == 16)
  178. def invalid_format(self, c):
  179. c.lpwfxFormat = 17
  180. def test_3_invalid_format(self):
  181. 'DSBUFFERDESC invalid lpwfxFormat assignment'
  182. c = ds.DSBUFFERDESC()
  183. self.assertRaises(ValueError, self.invalid_format, c)
  184. class DSCBUFFERDESCTest(unittest.TestCase):
  185. def test_1_Type(self):
  186. 'DSCBUFFERDESC type'
  187. c = ds.DSCBUFFERDESC()
  188. self.assertTrue(type(c) == ds.DSCBUFFERDESCType)
  189. def test_2_Attr(self):
  190. 'DSCBUFFERDESC attribute access'
  191. c = ds.DSCBUFFERDESC()
  192. c.dwFlags = 1
  193. c.dwBufferBytes = 2
  194. c.lpwfxFormat = pywintypes.WAVEFORMATEX()
  195. c.lpwfxFormat.wFormatTag = pywintypes.WAVE_FORMAT_PCM
  196. c.lpwfxFormat.nChannels = 2
  197. c.lpwfxFormat.nSamplesPerSec = 44100
  198. c.lpwfxFormat.nAvgBytesPerSec = 176400
  199. c.lpwfxFormat.nBlockAlign = 4
  200. c.lpwfxFormat.wBitsPerSample = 16
  201. self.assertTrue(c.dwFlags == 1)
  202. self.assertTrue(c.dwBufferBytes == 2)
  203. self.assertTrue(c.lpwfxFormat.wFormatTag == 1)
  204. self.assertTrue(c.lpwfxFormat.nChannels == 2)
  205. self.assertTrue(c.lpwfxFormat.nSamplesPerSec == 44100)
  206. self.assertTrue(c.lpwfxFormat.nAvgBytesPerSec == 176400)
  207. self.assertTrue(c.lpwfxFormat.nBlockAlign == 4)
  208. self.assertTrue(c.lpwfxFormat.wBitsPerSample == 16)
  209. def invalid_format(self, c):
  210. c.lpwfxFormat = 17
  211. def test_3_invalid_format(self):
  212. 'DSCBUFFERDESC invalid lpwfxFormat assignment'
  213. c = ds.DSCBUFFERDESC()
  214. self.assertRaises(ValueError, self.invalid_format, c)
  215. class DirectSoundTest(unittest.TestCase):
  216. # basic tests - mostly just exercise the functions
  217. def testEnumerate(self):
  218. '''DirectSoundEnumerate() sanity tests'''
  219. devices = ds.DirectSoundEnumerate()
  220. # this might fail on machines without a sound card
  221. self.assertTrue(len(devices))
  222. # if we have an entry, it must be a tuple of size 3
  223. self.assertTrue(len(devices[0]) == 3)
  224. def testCreate(self):
  225. '''DirectSoundCreate()'''
  226. try:
  227. d = ds.DirectSoundCreate(None, None)
  228. except pythoncom.com_error as exc:
  229. if exc.hresult != ds.DSERR_NODRIVER:
  230. raise
  231. raise TestSkipped(exc)
  232. def testPlay(self):
  233. '''Mesdames et Messieurs, la cour de Devin Dazzle'''
  234. # look for the test file in various places
  235. candidates = [
  236. os.path.dirname(__file__),
  237. os.path.dirname(sys.argv[0]),
  238. # relative to 'testall.py' in the win32com test suite.
  239. os.path.join(os.path.dirname(sys.argv[0]),
  240. '../../win32comext/directsound/test'),
  241. '.',
  242. ]
  243. for candidate in candidates:
  244. fname=os.path.join(candidate, "01-Intro.wav")
  245. if os.path.isfile(fname):
  246. break
  247. else:
  248. raise TestSkipped("Can't find test .wav file to play")
  249. with open(fname, 'rb') as f:
  250. hdr = f.read(WAV_HEADER_SIZE)
  251. wfx, size = wav_header_unpack(hdr)
  252. d = ds.DirectSoundCreate(None, None)
  253. d.SetCooperativeLevel(None, ds.DSSCL_PRIORITY)
  254. sdesc = ds.DSBUFFERDESC()
  255. sdesc.dwFlags = ds.DSBCAPS_STICKYFOCUS | ds.DSBCAPS_CTRLPOSITIONNOTIFY
  256. sdesc.dwBufferBytes = size
  257. sdesc.lpwfxFormat = wfx
  258. buffer = d.CreateSoundBuffer(sdesc, None)
  259. event = win32event.CreateEvent(None, 0, 0, None)
  260. notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify)
  261. notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event))
  262. buffer.Update(0, f.read(size))
  263. buffer.Play(0)
  264. win32event.WaitForSingleObject(event, -1)
  265. class DirectSoundCaptureTest(unittest.TestCase):
  266. # basic tests - mostly just exercise the functions
  267. def testEnumerate(self):
  268. '''DirectSoundCaptureEnumerate() sanity tests'''
  269. devices = ds.DirectSoundCaptureEnumerate()
  270. # this might fail on machines without a sound card
  271. self.assertTrue(len(devices))
  272. # if we have an entry, it must be a tuple of size 3
  273. self.assertTrue(len(devices[0]) == 3)
  274. def testCreate(self):
  275. '''DirectSoundCreate()'''
  276. try:
  277. d = ds.DirectSoundCaptureCreate(None, None)
  278. except pythoncom.com_error as exc:
  279. if exc.hresult != ds.DSERR_NODRIVER:
  280. raise
  281. raise TestSkipped(exc)
  282. def testRecord(self):
  283. try:
  284. d = ds.DirectSoundCaptureCreate(None, None)
  285. except pythoncom.com_error as exc:
  286. if exc.hresult != ds.DSERR_NODRIVER:
  287. raise
  288. raise TestSkipped(exc)
  289. sdesc = ds.DSCBUFFERDESC()
  290. sdesc.dwBufferBytes = 352800 # 2 seconds
  291. sdesc.lpwfxFormat = pywintypes.WAVEFORMATEX()
  292. sdesc.lpwfxFormat.wFormatTag = pywintypes.WAVE_FORMAT_PCM
  293. sdesc.lpwfxFormat.nChannels = 2
  294. sdesc.lpwfxFormat.nSamplesPerSec = 44100
  295. sdesc.lpwfxFormat.nAvgBytesPerSec = 176400
  296. sdesc.lpwfxFormat.nBlockAlign = 4
  297. sdesc.lpwfxFormat.wBitsPerSample = 16
  298. buffer = d.CreateCaptureBuffer(sdesc)
  299. event = win32event.CreateEvent(None, 0, 0, None)
  300. notify = buffer.QueryInterface(ds.IID_IDirectSoundNotify)
  301. notify.SetNotificationPositions((ds.DSBPN_OFFSETSTOP, event))
  302. buffer.Start(0)
  303. win32event.WaitForSingleObject(event, -1)
  304. event.Close()
  305. data = buffer.Update(0, 352800)
  306. fname=os.path.join(win32api.GetTempPath(), 'test_directsound_record.wav')
  307. f = open(fname, 'wb')
  308. f.write(wav_header_pack(sdesc.lpwfxFormat, 352800))
  309. f.write(data)
  310. f.close()
  311. if __name__ == '__main__':
  312. unittest.main()