1191
|
1 # alsa API
|
|
2 type
|
|
3 OpenMode*{.size: sizeof(culong).} = enum
|
|
4 SND_PCM_BLOCK = 0x00000000 # added by semicongine, for clarity
|
|
5 SND_PCM_NONBLOCK = 0x00000001
|
|
6 StreamMode* {.size: sizeof(cint).} = enum
|
|
7 SND_PCM_STREAM_PLAYBACK = 0
|
|
8 AccessMode*{.size: sizeof(cint).} = enum
|
|
9 SND_PCM_ACCESS_RW_INTERLEAVED = 3
|
|
10 PCMFormat*{.size: sizeof(cint).} = enum
|
|
11 SND_PCM_FORMAT_S16_LE = 2
|
|
12 snd_pcm_p* = ptr object
|
|
13 snd_pcm_hw_params_p* = ptr object
|
|
14 snd_pcm_uframes_t* = culong
|
|
15 snd_pcm_sframes_t* = clong
|
|
16 {.pragma: alsafunc, importc, cdecl, dynlib: "libasound.so.2".}
|
|
17 proc snd_pcm_open*(pcm_ref: ptr snd_pcm_p, name: cstring, streamMode: StreamMode, openmode: OpenMode): cint {.alsafunc.}
|
|
18 proc snd_pcm_close*(pcm: snd_pcm_p): cint {.alsafunc.}
|
|
19 proc snd_pcm_hw_params_malloc*(hw_params_ptr: ptr snd_pcm_hw_params_p): cint {.alsafunc.}
|
|
20 proc snd_pcm_hw_params_free*(hw_params: snd_pcm_hw_params_p) {.alsafunc.}
|
|
21 proc snd_pcm_hw_params_any*(pcm: snd_pcm_p, params: snd_pcm_hw_params_p): cint {.alsafunc.}
|
|
22 proc snd_pcm_hw_params_set_access*(pcm: snd_pcm_p, params: snd_pcm_hw_params_p, mode: AccessMode): cint {.alsafunc.}
|
|
23 proc snd_pcm_hw_params_set_format*(pcm: snd_pcm_p, params: snd_pcm_hw_params_p, format: PCMFormat): cint {.alsafunc.}
|
|
24 proc snd_pcm_hw_params_set_channels*(pcm: snd_pcm_p, params: snd_pcm_hw_params_p, val: cuint): cint {.alsafunc.}
|
|
25 proc snd_pcm_hw_params_set_buffer_size*(pcm: snd_pcm_p, params: snd_pcm_hw_params_p, size: snd_pcm_uframes_t): cint {.alsafunc.}
|
|
26 proc snd_pcm_hw_params_set_rate*(pcm: snd_pcm_p, params: snd_pcm_hw_params_p, val: cuint, dir: cint): cint {.alsafunc.}
|
|
27 proc snd_pcm_hw_params*(pcm: snd_pcm_p, params: snd_pcm_hw_params_p): cint {.alsafunc.}
|
|
28 proc snd_pcm_writei*(pcm: snd_pcm_p, buffer: pointer, size: snd_pcm_uframes_t): snd_pcm_sframes_t {.alsafunc.}
|
|
29 proc snd_pcm_recover*(pcm: snd_pcm_p, err: cint, silent: cint): cint {.alsafunc.}
|
|
30
|
|
31 template checkAlsaResult(call: untyped) =
|
|
32 let value = call
|
|
33 if value < 0:
|
|
34 raise newException(Exception, "Alsa error: " & astToStr(call) &
|
|
35 " returned " & $value)
|
|
36
|
|
37 # required for engine:
|
|
38
|
|
39 type
|
|
40 NativeSoundDevice* = object
|
|
41 handle: snd_pcm_p
|
|
42 buffers: seq[ptr SoundData]
|
|
43
|
|
44 proc OpenSoundDevice*(sampleRate: uint32, buffers: seq[ptr SoundData]): NativeSoundDevice =
|
|
45 var hw_params: snd_pcm_hw_params_p = nil
|
|
46 checkAlsaResult snd_pcm_open(addr result.handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_BLOCK)
|
|
47
|
|
48 # hw parameters, quiet a bit of hardcoding here
|
|
49 checkAlsaResult snd_pcm_hw_params_malloc(addr hw_params)
|
|
50 checkAlsaResult snd_pcm_hw_params_any(result.handle, hw_params)
|
|
51 checkAlsaResult snd_pcm_hw_params_set_access(result.handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)
|
|
52 checkAlsaResult snd_pcm_hw_params_set_format(result.handle, hw_params, SND_PCM_FORMAT_S16_LE)
|
|
53 checkAlsaResult snd_pcm_hw_params_set_rate(result.handle, hw_params, sampleRate, 0)
|
|
54 checkAlsaResult snd_pcm_hw_params_set_channels(result.handle, hw_params, 2)
|
|
55 checkAlsaResult snd_pcm_hw_params_set_buffer_size(result.handle, hw_params, snd_pcm_uframes_t(buffers[0][].len))
|
|
56 checkAlsaResult snd_pcm_hw_params(result.handle, hw_params)
|
|
57 snd_pcm_hw_params_free(hw_params)
|
|
58 result.buffers = buffers
|
|
59
|
|
60 proc WriteSoundData*(soundDevice: NativeSoundDevice, buffer: int) =
|
|
61 var ret = snd_pcm_writei(soundDevice.handle, addr soundDevice.buffers[buffer][][0], snd_pcm_uframes_t(soundDevice.buffers[buffer][].len))
|
|
62 if ret < 0:
|
|
63 checkAlsaResult snd_pcm_recover(soundDevice.handle, cint(ret), 0)
|
|
64
|
|
65 proc CloseSoundDevice*(soundDevice: NativeSoundDevice) =
|
|
66 discard snd_pcm_close(soundDevice.handle)
|