Mercurial > games > semicongine
annotate semiconginev2/platform/windows/audio.nim @ 1224:a3fa15c25026 compiletime-tests
did: cleanup, add audio, change platform-dependent structure
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 22:02:11 +0700 |
parents | 56781cc0fc7c |
children |
rev | line source |
---|---|
1224
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
1218
diff
changeset
|
1 include ./thirdparty/winim/winim/inc/mmsystem |
a3fa15c25026
did: cleanup, add audio, change platform-dependent structure
sam <sam@basx.dev>
parents:
1218
diff
changeset
|
2 |
1191 | 3 template CheckWinMMResult*(call: untyped) = |
4 let value = call | |
5 if value < 0: | |
6 raise newException(Exception, "Windows multimedia error: " & astToStr(call) & | |
7 " returned " & $value) | |
8 type | |
9 NativeSoundDevice* = object | |
10 handle: HWAVEOUT | |
11 buffers: seq[WAVEHDR] | |
12 | |
13 proc OpenSoundDevice*(sampleRate: uint32, buffers: seq[ptr SoundData]): NativeSoundDevice = | |
14 var format = WAVEFORMATEX( | |
15 wFormatTag: WAVE_FORMAT_PCM, | |
16 nChannels: 2, | |
17 nSamplesPerSec: DWORD(sampleRate), | |
18 nAvgBytesPerSec: DWORD(sampleRate) * 4, | |
19 nBlockAlign: 4, | |
20 wBitsPerSample: 16, | |
21 cbSize: 0, | |
22 ) | |
23 CheckWinMMResult waveOutOpen(addr result.handle, WAVE_MAPPER, addr format, DWORD_PTR(0), DWORD_PTR(0), CALLBACK_NULL) | |
24 | |
25 for i in 0 ..< buffers.len: | |
26 result.buffers.add WAVEHDR( | |
27 lpData: cast[cstring](addr buffers[i][][0]), | |
28 dwBufferLength: DWORD(buffers[i][].len * sizeof(Sample)), | |
29 dwLoops: 1, | |
30 ) | |
31 for i in 0 ..< result.buffers.len: | |
32 CheckWinMMResult waveOutPrepareHeader(result.handle, addr result.buffers[i], UINT(sizeof(WAVEHDR))) | |
33 CheckWinMMResult waveOutWrite(result.handle, addr result.buffers[i], UINT(sizeof(WAVEHDR))) | |
34 | |
35 proc WriteSoundData*(soundDevice: var NativeSoundDevice, buffer: int) = | |
36 while (soundDevice.buffers[buffer].dwFlags and WHDR_DONE) == 0: | |
37 sleep(1) | |
38 CheckWinMMResult waveOutWrite(soundDevice.handle, addr soundDevice.buffers[buffer], UINT(sizeof(WAVEHDR))) | |
39 | |
40 proc CloseSoundDevice*(soundDevice: var NativeSoundDevice) = | |
41 for i in 0 ..< soundDevice.buffers.len: | |
42 discard waveOutUnprepareHeader(soundDevice.handle, addr soundDevice.buffers[i], UINT(sizeof(WAVEHDR))) | |
43 waveOutClose(soundDevice.handle) |