annotate semiconginev2/platform/windows/audio.nim @ 1225:27cd1c21290e compiletime-tests

did: refactor resources
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 22:20:59 +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: 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
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
3 template CheckWinMMResult*(call: untyped) =
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
4 let value = call
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
5 if value < 0:
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
6 raise newException(Exception, "Windows multimedia error: " & astToStr(call) &
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
7 " returned " & $value)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
8 type
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
9 NativeSoundDevice* = object
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
10 handle: HWAVEOUT
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
11 buffers: seq[WAVEHDR]
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
12
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
13 proc OpenSoundDevice*(sampleRate: uint32, buffers: seq[ptr SoundData]): NativeSoundDevice =
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
14 var format = WAVEFORMATEX(
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
15 wFormatTag: WAVE_FORMAT_PCM,
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
16 nChannels: 2,
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
17 nSamplesPerSec: DWORD(sampleRate),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
18 nAvgBytesPerSec: DWORD(sampleRate) * 4,
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
19 nBlockAlign: 4,
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
20 wBitsPerSample: 16,
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
21 cbSize: 0,
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 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
24
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
25 for i in 0 ..< buffers.len:
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
26 result.buffers.add WAVEHDR(
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
27 lpData: cast[cstring](addr buffers[i][][0]),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
28 dwBufferLength: DWORD(buffers[i][].len * sizeof(Sample)),
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
29 dwLoops: 1,
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
30 )
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
31 for i in 0 ..< result.buffers.len:
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
32 CheckWinMMResult waveOutPrepareHeader(result.handle, addr result.buffers[i], UINT(sizeof(WAVEHDR)))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
33 CheckWinMMResult waveOutWrite(result.handle, addr result.buffers[i], UINT(sizeof(WAVEHDR)))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
34
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
35 proc WriteSoundData*(soundDevice: var NativeSoundDevice, buffer: int) =
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
36 while (soundDevice.buffers[buffer].dwFlags and WHDR_DONE) == 0:
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
37 sleep(1)
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
38 CheckWinMMResult waveOutWrite(soundDevice.handle, addr soundDevice.buffers[buffer], UINT(sizeof(WAVEHDR)))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
39
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
40 proc CloseSoundDevice*(soundDevice: var NativeSoundDevice) =
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
41 for i in 0 ..< soundDevice.buffers.len:
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
42 discard waveOutUnprepareHeader(soundDevice.handle, addr soundDevice.buffers[i], UINT(sizeof(WAVEHDR)))
239adab121a3 sync from bedroom to office
sam <sam@basx.dev>
parents:
diff changeset
43 waveOutClose(soundDevice.handle)