comparison semiconginev2/audio/platform/windows.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
children
comparison
equal deleted inserted replaced
1223:55896320c8bf 1224:a3fa15c25026
1 import ./thirdparty/winim/winim/inc/mmsystem
2
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)