Coverage report: /home/ellis/comp/core/ffi/alsa/pkg.lisp

KindCoveredAll%
expression0100 0.0
branch032 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; alsa.lisp --- low-level bindings to ALSA
2
 
3
 ;;; Commentary:
4
 
5
 ;;; Code:
6
 (defpackage :alsa
7
   (:use :cl :std :sb-alien)
8
   (:import-from :sb-unix :off-t)
9
   (:export :load-asound
10
            :snd-pcm-state
11
            :snd-pcm-format
12
            :snd-pcm-access
13
            :snd-pcm-stream
14
            :snd-pcm-open
15
            :snd-pcm-close
16
            :snd-strerror
17
            :snd-pcm-set-params
18
            :snd-pcm-recover
19
            :snd-pcm-writei
20
            :snd-pcm-dump
21
            :alsa-element-type
22
            :alsa-format-type))
23
 
24
 (in-package :alsa)
25
 
26
 (define-alien-loader asound "/usr/lib/")
27
 
28
 (define-alien-enum (snd-pcm-class int)
29
   :generic 0
30
   :multi 1
31
   :modem 2
32
   :digitizer 3)
33
 
34
 (define-alien-enum (snd-pcm-stream int)
35
   :playback 0
36
   :capture 1)
37
 
38
 (define-alien-enum (snd-pcm-access int)
39
   :mmap-interleaved 0
40
   :mmap-noninterleaved 1
41
   :mmap-complex 2
42
   :rw-interleaved 3
43
   :rw-noninterleaved 4)
44
 
45
 ;; incomplete list of formats
46
 (define-alien-enum (snd-pcm-format int)
47
   :unknown -1
48
   :s8 0
49
   :u8 1
50
   :s16-le 2
51
   :s16-be 3
52
   :u16-le 4
53
   :u16-be 5
54
   :s24-le 6
55
   :s24-be 7
56
   :u24-le 8
57
   :u24-be 9
58
   :s32-le 10
59
   :s32-be 11
60
   :u32-le 12
61
   :u32-be 13
62
   :float-le 14
63
   :float-be 15
64
   :float64-le 16
65
   :float64-be 17
66
   :iec958-subframe-le 18
67
   :iec958-subframe-be 19
68
   :mu-law 20
69
   :a-law 21
70
   :ima-adpcm 22
71
   :mpeg 23
72
   :gsm 24
73
   :special 31
74
   :s24-3le 32
75
   :s24-3be 33
76
   :u24-3le 34
77
   :u24-3be 35)
78
 
79
 (define-alien-enum (snd-pcm-state int)
80
   :open 0
81
   :setup 1
82
   :prepared 2
83
   :running 3
84
   :xrun 4
85
   :draining 5
86
   :paused 6
87
   :suspended 7
88
   :disconnected 8)
89
 
90
 (defconstant %seek-set 0)
91
 (defconstant %seek-cur 1)
92
 (defconstant %seek-end 2)
93
 
94
 (define-alien-type snd-pcm (* t))
95
 (define-alien-type snd-output (* t))
96
 
97
 (define-alien-type snd-pcm-stream int)
98
 (define-alien-type snd-pcm-mode int)
99
 
100
 (defar snd-pcm-open int (pcm (* snd-pcm)) (name c-string) (ty snd-pcm-stream) (mode snd-pcm-mode))
101
 
102
 (defar snd-pcm-close int (pcm snd-pcm))
103
 
104
 (defar snd-strerror c-string (errnum int))
105
 
106
 ;; TODO
107
 
108
 (defar snd-pcm-set-params int
109
   (pcm snd-pcm)
110
   (format snd-pcm-format)
111
   (access snd-pcm-access)
112
   (channels unsigned-int)
113
   (rate unsigned-int)
114
   (soft-resample int)
115
   (latency unsigned-int))
116
 
117
 (defar snd-pcm-recover int
118
   (pcm snd-pcm)
119
   (err int)
120
   (silent int))
121
 
122
 (define-alien-type snd-pcm-sframes long)
123
 (define-alien-type snd-pcm-uframes unsigned-long)
124
 
125
 (defar snd-pcm-writei snd-pcm-sframes
126
   (pcm snd-pcm)
127
   (buffer (* t))
128
   (size snd-pcm-uframes))
129
 
130
 (defar snd-output-stdio-attach int
131
   (outputp (* snd-output))
132
   (file (* t))
133
   (close int))
134
 
135
 (defar snd-pcm-dump int
136
   (pcm snd-pcm)
137
   (out snd-output))
138
 
139
 (define-alien-variable stdout (* t))
140
 
141
 ;;; Utils
142
 (defun alsa-element-type (type)
143
   (cond ((equalp type '(signed-byte 16)) :int16)
144
         ((eql type 'single-float) :float)
145
         ((eql type 'double-float) :double)
146
         ((equalp type '(unsigned-byte 8)) :uint8)
147
         ((equalp type '(signed-byte 8)) :int8)
148
         ((equalp type '(unsigned-byte 16)) :uint16)
149
         ((equalp type '(unsigned-byte 32)) :uint32)
150
         ((equalp type '(signed-byte 32)) :int32)
151
         (t (error "Invalid base type ~A" type))))
152
 
153
 (defun alsa-format-type (element-type)
154
   (cond ((eql element-type 'single-float) :float-le)
155
         ((eql element-type 'double-float) :float64-le)
156
         ((equalp element-type '(unsigned-byte 8)) :u8)
157
         ((equalp element-type '(signed-byte 8)) :8)
158
         ((equalp element-type '(unsigned-byte 16)) :u16-le)
159
         ((equalp element-type '(signed-byte 16)) :s16-le)
160
         ((equalp element-type '(unsigned-byte 32)) :u32-le)
161
         ((equalp element-type '(signed-byte 32)) :s32-le)
162
         (t (error "Invalid base type ~A" element-type))))