audiodef.h revision 1.5 1 /* $NetBSD: audiodef.h,v 1.5 2019/06/25 13:07:48 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 /* conversion stage */
67 typedef struct {
68 audio_ring_t srcbuf;
69 audio_ring_t *dst;
70 audio_filter_t filter;
71 audio_filter_arg_t arg;
72 } audio_stage_t;
73
74 typedef enum {
75 AUDIO_STATE_CLEAR, /* no data, no need to drain */
76 AUDIO_STATE_RUNNING, /* need to drain */
77 AUDIO_STATE_DRAINING, /* now draining */
78 } audio_state_t;
79
80 typedef struct audio_track {
81 /*
82 * AUMODE_PLAY for playback track, or
83 * AUMODE_RECORD for recoding track.
84 * Note that AUMODE_PLAY_ALL is maintained by file->mode, not here.
85 */
86 int mode;
87
88 audio_ring_t usrbuf; /* user i/o buffer */
89 u_int usrbuf_blksize; /* usrbuf block size in bytes */
90 struct uvm_object *uobj;
91 bool mmapped; /* device is mmap()-ed */
92 u_int usrbuf_stamp; /* transferred bytes from/to stage */
93 u_int usrbuf_stamp_last; /* last stamp */
94 u_int usrbuf_usedhigh;/* high water mark in bytes */
95 u_int usrbuf_usedlow; /* low water mark in bytes */
96
97 /*
98 * Track input format. It means usrbuf.fmt for playback, or
99 * mixer->trackfmt for recording.
100 */
101 audio_format2_t inputfmt;
102
103 /*
104 * Pointer to track (conversion stage's) input buffer.
105 * Must be protected by track lock (only for recording track).
106 */
107 audio_ring_t *input;
108 /*
109 * Track (conversion stage's) output buffer.
110 * Must be protected by track lock (only for playback track).
111 */
112 audio_ring_t outbuf;
113
114 audio_stage_t codec; /* encoding conversion stage */
115 audio_stage_t chvol; /* channel volume stage */
116 audio_stage_t chmix; /* channel mix stage */
117 audio_stage_t freq; /* frequency conversion stage */
118
119 /* Work area for frequency conversion. */
120 u_int freq_step; /* src/dst ratio */
121 u_int freq_current; /* counter */
122 u_int freq_leap; /* correction counter per block */
123 aint_t freq_prev[AUDIO_MAX_CHANNELS]; /* previous values */
124 aint_t freq_curr[AUDIO_MAX_CHANNELS]; /* current values */
125
126 /* Per-channel volumes (0..256) */
127 uint16_t ch_volume[AUDIO_MAX_CHANNELS];
128 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
129 /* Track volume (0..256) */
130 u_int volume;
131 #endif
132
133 audio_trackmixer_t *mixer; /* connected track mixer */
134
135 /* Sequence number picked up by track mixer. */
136 uint64_t seq;
137
138 audio_state_t pstate; /* playback state */
139 bool is_pause;
140
141 /* Statistic counters. */
142 uint64_t inputcounter; /* # of frames input to track */
143 uint64_t outputcounter; /* # of frames output from track */
144 uint64_t useriobytes; /* # of bytes xfer to/from userland */
145 uint64_t dropframes; /* # of frames dropped */
146 int eofcounter; /* count of zero-sized write */
147
148 /*
149 * Non-zero if the track is in use.
150 * Must access atomically.
151 */
152 volatile uint lock;
153
154 int id; /* track id for debug */
155 } audio_track_t;
156
157 struct audio_file {
158 struct audio_softc *sc;
159 dev_t dev;
160
161 /*
162 * Playback and recording track, or NULL if the track is unavailable.
163 */
164 audio_track_t *ptrack;
165 audio_track_t *rtrack;
166
167 /*
168 * Indicates the operation mode of this file.
169 * AUMODE_PLAY means playback is requested.
170 * AUMODE_RECORD means recording is requested.
171 * AUMODE_PLAY_ALL affects nothing but can be get/set for backward
172 * compatibility.
173 */
174 int mode;
175
176 /* process who wants audio SIGIO. */
177 pid_t async_audio;
178
179 SLIST_ENTRY(audio_file) entry;
180 };
181
182 struct audio_trackmixer {
183 struct audio_softc *sc;
184
185 int mode; /* AUMODE_PLAY or AUMODE_RECORD */
186 audio_format2_t track_fmt; /* track <-> trackmixer format */
187
188 int frames_per_block; /* number of frames in a block */
189
190 /*
191 * software master volume (0..256)
192 * Must be protected by sc_intr_lock.
193 */
194 u_int volume;
195
196 audio_format2_t mixfmt;
197 void *mixsample; /* mixing buf in double-sized int */
198
199 /*
200 * true if trackmixer does LE<->BE conversion.
201 * Generally an encoding conversion should be done by each hardware
202 * driver but for most modern little endian drivers which support
203 * only linear PCM it's troublesome issue to consider about big endian
204 * arch. Therefore, we do this conversion here only if the hardware
205 * format is SLINEAR_OE:16.
206 */
207 bool swap_endian;
208
209 audio_filter_t codec; /* hardware codec */
210 audio_filter_arg_t codecarg; /* and its argument */
211 audio_ring_t codecbuf; /* also used for wide->int conversion */
212
213 audio_ring_t hwbuf; /* HW I/O buf */
214
215 void *sih; /* softint cookie */
216
217 /* Must be protected by sc_lock. */
218 kcondvar_t outcv;
219
220 uint64_t mixseq; /* seq# currently being mixed */
221 uint64_t hwseq; /* seq# HW output completed */
222
223 /* initial blktime n/d = AUDIO_BLK_MS / 1000 */
224 int blktime_n; /* blk time numerator */
225 int blktime_d; /* blk time denominator */
226
227 /* XXX */
228 uint64_t hw_complete_counter;
229 };
230
231 /*
232 * Audio Ring Buffer.
233 */
234
235 #ifdef DIAGNOSTIC
236 #define DIAGNOSTIC_ring(ring) audio_diagnostic_ring(__func__, (ring))
237 extern void audio_diagnostic_ring(const char *, const audio_ring_t *);
238 #else
239 #define DIAGNOSTIC_ring(ring)
240 #endif
241
242 /*
243 * Convert number of frames to number of bytes.
244 */
245 static __inline int
246 frametobyte(const audio_format2_t *fmt, int frames)
247 {
248 return frames * fmt->channels * fmt->stride / NBBY;
249 }
250
251 /*
252 * Return the number of frames per block.
253 */
254 static __inline int
255 frame_per_block(const audio_trackmixer_t *mixer, const audio_format2_t *fmt)
256 {
257 return (fmt->sample_rate * mixer->blktime_n + mixer->blktime_d - 1) /
258 mixer->blktime_d;
259 }
260
261 /*
262 * Round idx. idx must be non-negative and less than 2 * capacity.
263 */
264 static __inline int
265 auring_round(const audio_ring_t *ring, int idx)
266 {
267 DIAGNOSTIC_ring(ring);
268 KASSERT(idx >= 0);
269 KASSERT(idx < ring->capacity * 2);
270
271 if (idx < ring->capacity) {
272 return idx;
273 } else {
274 return idx - ring->capacity;
275 }
276 }
277
278 /*
279 * Return ring's tail (= head + used) position.
280 * This position indicates next frame of the last valid frames.
281 */
282 static __inline int
283 auring_tail(const audio_ring_t *ring)
284 {
285 return auring_round(ring, ring->head + ring->used);
286 }
287
288 /*
289 * Return ring's head pointer.
290 * This function can be used only if the stride of the 'ring' is equal to
291 * the internal stride. Don't use this for hw buffer.
292 */
293 static __inline aint_t *
294 auring_headptr_aint(const audio_ring_t *ring)
295 {
296 KASSERT(ring->fmt.stride == sizeof(aint_t) * NBBY);
297
298 return (aint_t *)ring->mem + ring->head * ring->fmt.channels;
299 }
300
301 /*
302 * Return ring's tail (= head + used) pointer.
303 * This function can be used only if the stride of the 'ring' is equal to
304 * the internal stride. Don't use this for hw buffer.
305 */
306 static __inline aint_t *
307 auring_tailptr_aint(const audio_ring_t *ring)
308 {
309 KASSERT(ring->fmt.stride == sizeof(aint_t) * NBBY);
310
311 return (aint_t *)ring->mem + auring_tail(ring) * ring->fmt.channels;
312 }
313
314 /*
315 * Return ring's head pointer.
316 * This function can be used even if the stride of the 'ring' is equal to
317 * or not equal to the internal stride.
318 */
319 static __inline uint8_t *
320 auring_headptr(const audio_ring_t *ring)
321 {
322 return (uint8_t *)ring->mem +
323 ring->head * ring->fmt.channels * ring->fmt.stride / NBBY;
324 }
325
326 /*
327 * Return ring's tail pointer.
328 * It points the next position of the last valid frames.
329 * This function can be used even if the stride of the 'ring' is equal to
330 * or not equal to the internal stride.
331 */
332 static __inline uint8_t *
333 auring_tailptr(audio_ring_t *ring)
334 {
335 return (uint8_t *)ring->mem +
336 auring_tail(ring) * ring->fmt.channels * ring->fmt.stride / NBBY;
337 }
338
339 /*
340 * Return ring's capacity in bytes.
341 */
342 static __inline int
343 auring_bytelen(const audio_ring_t *ring)
344 {
345 return frametobyte(&ring->fmt, ring->capacity);
346 }
347
348 /*
349 * Take out n frames from head of ring.
350 * This function only manipurates counters. It doesn't manipurate any
351 * actual buffer data.
352 */
353 #define auring_take(ring, n) auring_take_(__func__, __LINE__, ring, n)
354 static __inline void
355 auring_take_(const char *func, int line, audio_ring_t *ring, int n)
356 {
357 DIAGNOSTIC_ring(ring);
358 KASSERTMSG(n >= 0, "called from %s:%d: n=%d", func, line, n);
359 KASSERTMSG(ring->used >= n, "called from %s:%d: ring->used=%d n=%d",
360 func, line, ring->used, n);
361
362 ring->head = auring_round(ring, ring->head + n);
363 ring->used -= n;
364 }
365
366 /*
367 * Append n frames into tail of ring.
368 * This function only manipurates counters. It doesn't manipurate any
369 * actual buffer data.
370 */
371 #define auring_push(ring, n) auring_push_(__func__, __LINE__, ring, n)
372 static __inline void
373 auring_push_(const char *func, int line, audio_ring_t *ring, int n)
374 {
375 DIAGNOSTIC_ring(ring);
376 KASSERT(n >= 0);
377 KASSERTMSG(ring->used + n <= ring->capacity,
378 "called from %s:%d: ring->used=%d n=%d ring->capacity=%d",
379 func, line, ring->used, n, ring->capacity);
380
381 ring->used += n;
382 }
383
384 /*
385 * Return the number of contiguous frames in used.
386 */
387 static __inline int
388 auring_get_contig_used(const audio_ring_t *ring)
389 {
390 DIAGNOSTIC_ring(ring);
391
392 if (ring->head + ring->used <= ring->capacity) {
393 return ring->used;
394 } else {
395 return ring->capacity - ring->head;
396 }
397 }
398
399 /*
400 * Return the number of contiguous free frames.
401 */
402 static __inline int
403 auring_get_contig_free(const audio_ring_t *ring)
404 {
405 DIAGNOSTIC_ring(ring);
406
407 if (ring->head + ring->used < ring->capacity) {
408 return ring->capacity - (ring->head + ring->used);
409 } else {
410 return ring->capacity - ring->used;
411 }
412 }
413
414 #endif /* !_SYS_DEV_AUDIO_AUDIODEF_H_ */
415