annotate semiconginev2/audio/platform/windows.nim @ 1262:66956794ce6d compiletime-tests

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