audiodef.h revision 1.2 1 /* $NetBSD: audiodef.h,v 1.2 2019/05/08 13:40:17 isaki Exp $ */
2
3 /*
4 * Copyright (C) 2017 Tetsuya Isaki. All rights reserved.
5 * Copyright (C) 2017 Y.Sugahara (moveccr). All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #ifndef _SYS_DEV_AUDIO_AUDIODEF_H_
30 #define _SYS_DEV_AUDIO_AUDIODEF_H_
31
32 /* Number of HW buffer's blocks. */
33 #define NBLKHW (3)
34
35 /* Number of track output buffer's blocks. Must be > NBLKHW */
36 #define NBLKOUT (4)
37
38 /* Minimum number of usrbuf's blocks. */
39 #define AUMINNOBLK (3)
40
41 /*
42 * Hardware blocksize in msec.
43 * We use 40 msec as default. (1 / 40ms) = 25 = 5^2.
44 * In this case, the number of frames in a block can be an integer
45 * even if the frequency is a multiple of 100 (44100, 48000, etc),
46 * or even if 15625Hz (vs(4)).
47 */
48 #if !defined(AUDIO_BLK_MS)
49 #define AUDIO_BLK_MS 40
50 #endif
51
52 /*
53 * Whether the playback mixer use single buffer mode.
54 * It reduces the latency one block but needs machine power.
55 * In case of the double buffer (as default), it increases the latency
56 * but can be expected to stabilize even on slower machines.
57 */
58 /* #define AUDIO_HW_SINGLE_BUFFER */
59
60 /*
61 * Whether supports per-track volume.
62 * For now, there are no user interfaces to get/set it.
63 */
64 /* #define AUDIO_SUPPORT_TRACK_VOLUME */
65
66 /*
67 * Whether use C language's "implementation defined" behavior (note that
68 * it's not "undefined" behavior). It improves performance well.
69 */
70 #define AUDIO_USE_C_IMPLEMENTATION_DEFINED_BEHAVIOR
71
72 /* conversion stage */
73 typedef struct {
74 audio_ring_t srcbuf;
75 audio_ring_t *dst;
76 audio_filter_t filter;
77 audio_filter_arg_t arg;
78 } audio_stage_t;
79
80 typedef enum {
81 AUDIO_STATE_CLEAR, /* no data, no need to drain */
82 AUDIO_STATE_RUNNING, /* need to drain */
83 AUDIO_STATE_DRAINING, /* now draining */
84 } audio_state_t;
85
86 typedef struct audio_track {
87 /*
88 * AUMODE_PLAY for playback track, or
89 * AUMODE_RECORD for recoding track.
90 * Note that AUMODE_PLAY_ALL is maintained by file->mode, not here.
91 */
92 int mode;
93
94 audio_ring_t usrbuf; /* user i/o buffer */
95 u_int usrbuf_blksize; /* usrbuf block size in bytes */
96 struct uvm_object *uobj;
97 bool mmapped; /* device is mmap()-ed */
98 u_int usrbuf_stamp; /* transferred bytes from/to stage */
99 u_int usrbuf_stamp_last; /* last stamp */
100 u_int usrbuf_usedhigh;/* high water mark in bytes */
101 u_int usrbuf_usedlow; /* low water mark in bytes */
102
103 /*
104 * Track input format. It means usrbuf.fmt for playback, or
105 * mixer->trackfmt for recording.
106 */
107 audio_format2_t inputfmt;
108
109 /*
110 * Pointer to track (conversion stage's) input buffer.
111 * Must be protected by track lock (only for recording track).
112 */
113 audio_ring_t *input;
114 /*
115 * Track (conversion stage's) output buffer.
116 * Must be protected by track lock (only for playback track).
117 */
118 audio_ring_t outbuf;
119
120 audio_stage_t codec; /* encoding conversion stage */
121 audio_stage_t chvol; /* channel volume stage */
122 audio_stage_t chmix; /* channel mix stage */
123 audio_stage_t freq; /* frequency conversion stage */
124
125 /* Work area for frequency conversion. */
126 u_int freq_step; /* src/dst ratio */
127 u_int freq_current; /* counter */
128 u_int freq_leap; /* correction counter per block */
129 aint_t freq_prev[AUDIO_MAX_CHANNELS]; /* previous values */
130 aint_t freq_curr[AUDIO_MAX_CHANNELS]; /* current values */
131
132 /* Per-channel volumes (0..256) */
133 uint16_t ch_volume[AUDIO_MAX_CHANNELS];
134 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
135 /* Track volume (0..256) */
136 u_int volume;
137 #endif
138
139 audio_trackmixer_t *mixer; /* connected track mixer */
140
141 /* Sequence number picked up by track mixer. */
142 uint64_t seq;
143
144 audio_state_t pstate; /* playback state */
145 bool is_pause;
146
147 /* Statistic counters. */
148 uint64_t inputcounter; /* # of frames input to track */
149 uint64_t outputcounter; /* # of frames output from track */
150 uint64_t useriobytes; /* # of bytes xfer to/from userland */
151 uint64_t dropframes; /* # of frames dropped */
152 int eofcounter; /* count of zero-sized write */
153
154 /*
155 * Non-zero if the track is in use.
156 * Must access atomically.
157 */
158 volatile uint lock;
159
160 int id; /* track id for debug */
161 } audio_track_t;
162
163 struct audio_file {
164 struct audio_softc *sc;
165 dev_t dev;
166
167 /*
168 * Playback and recording track, or NULL if the track is unavailable.
169 */
170 audio_track_t *ptrack;
171 audio_track_t *rtrack;
172
173 /*
174 * Indicates the operation mode of this file.
175 * AUMODE_PLAY means playback is requested.
176 * AUMODE_RECORD means recording is requested.
177 * AUMODE_PLAY_ALL affects nothing but can be get/set for backward
178 * compatibility.
179 */
180 int mode;
181
182 /* process who wants audio SIGIO. */
183 pid_t async_audio;
184
185 /*
186 * Non-zero if some thread context is using this file structure
187 * (including ptrack and rtrack) now.
188 * Must be protected by sc_lock.
189 */
190 volatile int lock;
191
192 SLIST_ENTRY(audio_file) entry;
193 };
194
195 struct audio_trackmixer {
196 struct audio_softc *sc;
197
198 int mode; /* AUMODE_PLAY or AUMODE_RECORD */
199 audio_format2_t track_fmt; /* track <-> trackmixer format */
200
201 int frames_per_block; /* number of frames in a block */
202
203 u_int volume; /* software master volume (0..256) */
204
205 audio_format2_t mixfmt;
206 void *mixsample; /* mixing buf in double-sized int */
207
208 /*
209 * true if trackmixer does LE<->BE conversion.
210 * Generally an encoding conversion should be done by each hardware
211 * driver but for most modern little endian drivers which support
212 * only linear PCM it's troublesome issue to consider about big endian
213 * arch. Therefore, we do this conversion here only if the hardware
214 * format is SLINEAR_OE:16.
215 */
216 bool swap_endian;
217
218 audio_filter_t codec; /* hardware codec */
219 audio_filter_arg_t codecarg; /* and its argument */
220 audio_ring_t codecbuf; /* also used for wide->int conversion */
221
222 audio_ring_t hwbuf; /* HW I/O buf */
223
224 void *sih; /* softint cookie */
225
226 /* Must be protected by sc_lock. */
227 kcondvar_t outcv;
228
229 uint64_t mixseq; /* seq# currently being mixed */
230 uint64_t hwseq; /* seq# HW output completed */
231
232 /* initial blktime n/d = AUDIO_BLK_MS / 1000 */
233 int blktime_n; /* blk time numerator */
234 int blktime_d; /* blk time denominator */
235
236 /* XXX */
237 uint64_t hw_complete_counter;
238 };
239
240 /*
241 * Audio Ring Buffer.
242 */
243
244 #ifdef DIAGNOSTIC
245 #define DIAGNOSTIC_ring(ring) audio_diagnostic_ring(__func__, (ring))
246 extern void audio_diagnostic_ring(const char *, const audio_ring_t *);
247 #else
248 #define DIAGNOSTIC_ring(ring)
249 #endif
250
251 /*
252 * Convert number of frames to number of bytes.
253 */
254 static __inline int
255 frametobyte(const audio_format2_t *fmt, int frames)
256 {
257 return frames * fmt->channels * fmt->stride / NBBY;
258 }
259
260 /*
261 * Return the number of frames per block.
262 */
263 static __inline int
264 frame_per_block(const audio_trackmixer_t *mixer, const audio_format2_t *fmt)
265 {
266 return (fmt->sample_rate * mixer->blktime_n + mixer->blktime_d - 1) /
267 mixer->blktime_d;
268 }
269
270 /*
271 * Round idx. idx must be non-negative and less than 2 * capacity.
272 */
273 static __inline int
274 auring_round(const audio_ring_t *ring, int idx)
275 {
276 DIAGNOSTIC_ring(ring);
277 KASSERT(idx >= 0);
278 KASSERT(idx < ring->capacity * 2);
279
280 if (idx < ring->capacity) {
281 return idx;
282 } else {
283 return idx - ring->capacity;
284 }
285 }
286
287 /*
288 * Return ring's tail (= head + used) position.
289 * This position indicates next frame of the last valid frames.
290 */
291 static __inline int
292 auring_tail(const audio_ring_t *ring)
293 {
294 return auring_round(ring, ring->head + ring->used);
295 }
296
297 /*
298 * Return ring's head pointer.
299 * This function can be used only if the stride of the 'ring' is equal to
300 * the internal stride. Don't use this for hw buffer.
301 */
302 static __inline aint_t *
303 auring_headptr_aint(const audio_ring_t *ring)
304 {
305 KASSERT(ring->fmt.stride == sizeof(aint_t) * NBBY);
306
307 return (aint_t *)ring->mem + ring->head * ring->fmt.channels;
308 }
309
310 /*
311 * Return ring's tail (= head + used) pointer.
312 * This function can be used only if the stride of the 'ring' is equal to
313 * the internal stride. Don't use this for hw buffer.
314 */
315 static __inline aint_t *
316 auring_tailptr_aint(const audio_ring_t *ring)
317 {
318 KASSERT(ring->fmt.stride == sizeof(aint_t) * NBBY);
319
320 return (aint_t *)ring->mem + auring_tail(ring) * ring->fmt.channels;
321 }
322
323 /*
324 * Return ring's head pointer.
325 * This function can be used even if the stride of the 'ring' is equal to
326 * or not equal to the internal stride.
327 */
328 static __inline uint8_t *
329 auring_headptr(const audio_ring_t *ring)
330 {
331 return (uint8_t *)ring->mem +
332 ring->head * ring->fmt.channels * ring->fmt.stride / NBBY;
333 }
334
335 /*
336 * Return ring's tail pointer.
337 * It points the next position of the last valid frames.
338 * This function can be used even if the stride of the 'ring' is equal to
339 * or not equal to the internal stride.
340 */
341 static __inline uint8_t *
342 auring_tailptr(audio_ring_t *ring)
343 {
344 return (uint8_t *)ring->mem +
345 auring_tail(ring) * ring->fmt.channels * ring->fmt.stride / NBBY;
346 }
347
348 /*
349 * Return ring's capacity in bytes.
350 */
351 static __inline int
352 auring_bytelen(const audio_ring_t *ring)
353 {
354 return frametobyte(&ring->fmt, ring->capacity);
355 }
356
357 /*
358 * Take out n frames from head of ring.
359 * This function only manipurates counters. It doesn't manipurate any
360 * actual buffer data.
361 */
362 #define auring_take(ring, n) auring_take_(__func__, __LINE__, ring, n)
363 static __inline void
364 auring_take_(const char *func, int line, audio_ring_t *ring, int n)
365 {
366 DIAGNOSTIC_ring(ring);
367 KASSERTMSG(n >= 0, "called from %s:%d: n=%d", func, line, n);
368 KASSERTMSG(ring->used >= n, "called from %s:%d: ring->used=%d n=%d",
369 func, line, ring->used, n);
370
371 ring->head = auring_round(ring, ring->head + n);
372 ring->used -= n;
373 }
374
375 /*
376 * Append n frames into tail of ring.
377 * This function only manipurates counters. It doesn't manipurate any
378 * actual buffer data.
379 */
380 #define auring_push(ring, n) auring_push_(__func__, __LINE__, ring, n)
381 static __inline void
382 auring_push_(const char *func, int line, audio_ring_t *ring, int n)
383 {
384 DIAGNOSTIC_ring(ring);
385 KASSERT(n >= 0);
386 KASSERTMSG(ring->used + n <= ring->capacity,
387 "called from %s:%d: ring->used=%d n=%d ring->capacity=%d",
388 func, line, ring->used, n, ring->capacity);
389
390 ring->used += n;
391 }
392
393 /*
394 * Return the number of contiguous frames in used.
395 */
396 static __inline int
397 auring_get_contig_used(const audio_ring_t *ring)
398 {
399 DIAGNOSTIC_ring(ring);
400
401 if (ring->head + ring->used <= ring->capacity) {
402 return ring->used;
403 } else {
404 return ring->capacity - ring->head;
405 }
406 }
407
408 /*
409 * Return the number of contiguous free frames.
410 */
411 static __inline int
412 auring_get_contig_free(const audio_ring_t *ring)
413 {
414 DIAGNOSTIC_ring(ring);
415
416 if (ring->head + ring->used < ring->capacity) {
417 return ring->capacity - (ring->head + ring->used);
418 } else {
419 return ring->capacity - ring->used;
420 }
421 }
422
423 #endif /* !_SYS_DEV_AUDIO_AUDIODEF_H_ */
424