annotate semiconginev2/platform/windows/audio.nim @ 1218:56781cc0fc7c compiletime-tests

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