Home | History | Annotate | Line # | Download | only in audio
audiodef.h revision 1.7.4.2
      1 /*	$NetBSD: audiodef.h,v 1.7.4.2 2020/02/29 20:19:07 ad 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 #ifdef _KERNEL_OPT
     33 #include "opt_audio.h"
     34 #endif
     35 
     36 /* Number of HW buffer's blocks. */
     37 #define NBLKHW (3)
     38 
     39 /* Number of track output buffer's blocks.  Must be > NBLKHW */
     40 #define NBLKOUT	(4)
     41 
     42 /* Minimum number of usrbuf's blocks. */
     43 #define AUMINNOBLK	(3)
     44 
     45 /*
     46  * Hardware blocksize in msec.
     47  * We use 40 msec as default.  (1 / 40ms) = 25 = 5^2.
     48  * In this case, the number of frames in a block can be an integer
     49  * even if the frequency is a multiple of 100 (44100, 48000, etc),
     50  * or even if 15625Hz (vs(4)).
     51  */
     52 #if !defined(AUDIO_BLK_MS)
     53 #define AUDIO_BLK_MS 40
     54 #endif
     55 
     56 /*
     57  * Whether the playback mixer use single buffer mode.
     58  * It reduces the latency one block but needs machine power.
     59  * In case of the double buffer (as default), it increases the latency
     60  * but can be expected to stabilize even on slower machines.
     61  */
     62 /* #define AUDIO_HW_SINGLE_BUFFER */
     63 
     64 /*
     65  * Whether supports per-track volume.
     66  * For now, there are no user interfaces to get/set it.
     67  */
     68 /* #define AUDIO_SUPPORT_TRACK_VOLUME */
     69 
     70 /*
     71  * AUDIO_SCALEDOWN()
     72  * This macro should be used for audio wave data only.
     73  *
     74  * The arithmetic shift right (ASR) (in other words, floor()) is good for
     75  * this purpose, and will be faster than division on the most platform.
     76  * The division (in other words, truncate()) is not so bad alternate for
     77  * this purpose, and will be fast enough.
     78  * (Using ASR is 1.9 times faster than division on my amd64, and 1.3 times
     79  * faster on my m68k.  -- isaki 201801.)
     80  *
     81  * However, the right shift operator ('>>') for negative integer is
     82  * "implementation defined" behavior in C (note that it's not "undefined"
     83  * behavior).  So only if implementation defines '>>' as ASR, we use it.
     84  */
     85 #if defined(__GNUC__)
     86 /* gcc defines '>>' as ASR. */
     87 #define AUDIO_SCALEDOWN(value, bits)	((value) >> (bits))
     88 #else
     89 #define AUDIO_SCALEDOWN(value, bits)	((value) / (1 << (bits)))
     90 #endif
     91 
     92 /* conversion stage */
     93 typedef struct {
     94 	audio_ring_t srcbuf;
     95 	audio_ring_t *dst;
     96 	audio_filter_t filter;
     97 	audio_filter_arg_t arg;
     98 } audio_stage_t;
     99 
    100 typedef enum {
    101 	AUDIO_STATE_CLEAR,	/* no data, no need to drain */
    102 	AUDIO_STATE_RUNNING,	/* need to drain */
    103 	AUDIO_STATE_DRAINING,	/* now draining */
    104 } audio_state_t;
    105 
    106 typedef struct audio_track {
    107 	/*
    108 	 * AUMODE_PLAY for playback track, or
    109 	 * AUMODE_RECORD for recoding track.
    110 	 * Note that AUMODE_PLAY_ALL is maintained by file->mode, not here.
    111 	 */
    112 	int mode;
    113 
    114 	audio_ring_t	usrbuf;		/* user i/o buffer */
    115 	u_int		usrbuf_blksize;	/* usrbuf block size in bytes */
    116 	struct uvm_object *uobj;
    117 	bool		mmapped;	/* device is mmap()-ed */
    118 	u_int		usrbuf_stamp;	/* transferred bytes from/to stage */
    119 	u_int		usrbuf_stamp_last; /* last stamp */
    120 	u_int		usrbuf_usedhigh;/* high water mark in bytes */
    121 	u_int		usrbuf_usedlow;	/* low water mark in bytes */
    122 
    123 	/*
    124 	 * Track input format.  It means usrbuf.fmt for playback, or
    125 	 * mixer->trackfmt for recording.
    126 	 */
    127 	audio_format2_t	inputfmt;
    128 
    129 	/*
    130 	 * Pointer to track (conversion stage's) input buffer.
    131 	 * Must be protected by track lock (only for recording track).
    132 	 */
    133 	audio_ring_t	*input;
    134 	/*
    135 	 * Track (conversion stage's) output buffer.
    136 	 * Must be protected by track lock (only for playback track).
    137 	 */
    138 	audio_ring_t	outbuf;
    139 
    140 	audio_stage_t	codec;		/* encoding conversion stage */
    141 	audio_stage_t	chvol;		/* channel volume stage */
    142 	audio_stage_t	chmix;		/* channel mix stage */
    143 	audio_stage_t	freq;		/* frequency conversion stage */
    144 
    145 	/* Work area for frequency conversion.  */
    146 	u_int		freq_step;	/* src/dst ratio */
    147 	u_int		freq_current;	/* counter */
    148 	u_int		freq_leap;	/* correction counter per block */
    149 	aint_t		freq_prev[AUDIO_MAX_CHANNELS];	/* previous values */
    150 	aint_t		freq_curr[AUDIO_MAX_CHANNELS];	/* current values */
    151 
    152 	/* Per-channel volumes (0..256) */
    153 	uint16_t ch_volume[AUDIO_MAX_CHANNELS];
    154 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
    155 	/* Track volume (0..256) */
    156 	u_int		volume;
    157 #endif
    158 
    159 	audio_trackmixer_t *mixer;	/* connected track mixer */
    160 
    161 	/* Sequence number picked up by track mixer. */
    162 	uint64_t	seq;
    163 
    164 	audio_state_t	pstate;		/* playback state */
    165 	bool		is_pause;
    166 
    167 	/* Statistic counters. */
    168 	uint64_t	inputcounter;	/* # of frames input to track */
    169 	uint64_t	outputcounter;	/* # of frames output from track */
    170 	uint64_t	useriobytes;	/* # of bytes xfer to/from userland */
    171 	uint64_t	dropframes;	/* # of frames dropped */
    172 	int		eofcounter;	/* count of zero-sized write */
    173 
    174 	/*
    175 	 * Non-zero if the track is in use.
    176 	 * Must access atomically.
    177 	 */
    178 	volatile uint	lock;
    179 
    180 	int		id;		/* track id for debug */
    181 } audio_track_t;
    182 
    183 struct audio_file {
    184 	struct audio_softc *sc;
    185 	dev_t		dev;
    186 
    187 	/*
    188 	 * Playback and recording track, or NULL if the track is unavailable.
    189 	 */
    190 	audio_track_t	*ptrack;
    191 	audio_track_t	*rtrack;
    192 
    193 	/*
    194 	 * Indicates the operation mode of this file.
    195 	 * AUMODE_PLAY means playback is requested.
    196 	 * AUMODE_RECORD means recording is requested.
    197 	 * AUMODE_PLAY_ALL affects nothing but can be get/set for backward
    198 	 * compatibility.
    199 	 */
    200 	int		mode;
    201 
    202 	/* process who wants audio SIGIO. */
    203 	pid_t		async_audio;
    204 
    205 	/* true when closing */
    206 	bool		dying;
    207 
    208 	SLIST_ENTRY(audio_file) entry;
    209 };
    210 
    211 struct audio_trackmixer {
    212 	struct audio_softc *sc;
    213 
    214 	int		mode;		/* AUMODE_PLAY or AUMODE_RECORD */
    215 	audio_format2_t	track_fmt;	/* track <-> trackmixer format */
    216 
    217 	int		frames_per_block; /* number of frames in a block */
    218 
    219 	/*
    220 	 * software master volume (0..256)
    221 	 * Must be protected by sc_intr_lock.
    222 	 */
    223 	u_int		volume;
    224 	/*
    225 	 * Volume recovery timer in auto gain control.
    226 	 * Must be protected by sc_intr_lock.
    227 	 */
    228 	int		voltimer;
    229 
    230 	audio_format2_t	mixfmt;
    231 	void		*mixsample;	/* mixing buf in double-sized int */
    232 
    233 	/*
    234 	 * true if trackmixer does LE<->BE conversion.
    235 	 * Generally an encoding conversion should be done by each hardware
    236 	 * driver but for most modern little endian drivers which support
    237 	 * only linear PCM it's troublesome issue to consider about big endian
    238 	 * arch.  Therefore, we do this conversion here only if the hardware
    239 	 * format is SLINEAR_OE:16.
    240 	 */
    241 	bool		swap_endian;
    242 
    243 	audio_filter_t	codec;		/* hardware codec */
    244 	audio_filter_arg_t codecarg;	/* and its argument */
    245 	audio_ring_t	codecbuf;	/* also used for wide->int conversion */
    246 
    247 	audio_ring_t	hwbuf;		/* HW I/O buf */
    248 
    249 	void		*sih;		/* softint cookie */
    250 
    251 	/* Must be protected by sc_lock. */
    252 	kcondvar_t	outcv;
    253 
    254 	uint64_t	mixseq;		/* seq# currently being mixed */
    255 	uint64_t	hwseq;		/* seq# HW output completed */
    256 
    257 	/* initial blktime n/d = AUDIO_BLK_MS / 1000 */
    258 	int		blktime_n;	/* blk time numerator */
    259 	int		blktime_d;	/* blk time denominator */
    260 
    261 	/* XXX */
    262 	uint64_t	hw_complete_counter;
    263 };
    264 
    265 /*
    266  * Audio Ring Buffer.
    267  */
    268 
    269 #ifdef DIAGNOSTIC
    270 #define DIAGNOSTIC_ring(ring)	audio_diagnostic_ring(__func__, (ring))
    271 extern void audio_diagnostic_ring(const char *, const audio_ring_t *);
    272 #else
    273 #define DIAGNOSTIC_ring(ring)
    274 #endif
    275 
    276 /*
    277  * Convert number of frames to number of bytes.
    278  */
    279 static __inline int
    280 frametobyte(const audio_format2_t *fmt, int frames)
    281 {
    282 	return frames * fmt->channels * fmt->stride / NBBY;
    283 }
    284 
    285 /*
    286  * Return the number of frames per block.
    287  */
    288 static __inline int
    289 frame_per_block(const audio_trackmixer_t *mixer, const audio_format2_t *fmt)
    290 {
    291 	return (fmt->sample_rate * mixer->blktime_n + mixer->blktime_d - 1) /
    292 	    mixer->blktime_d;
    293 }
    294 
    295 /*
    296  * Round idx.  idx must be non-negative and less than 2 * capacity.
    297  */
    298 static __inline int
    299 auring_round(const audio_ring_t *ring, int idx)
    300 {
    301 	DIAGNOSTIC_ring(ring);
    302 	KASSERTMSG(idx >= 0, "idx=%d", idx);
    303 	KASSERTMSG(idx < ring->capacity * 2,
    304 	    "idx=%d ring->capacity=%d", idx, ring->capacity);
    305 
    306 	if (idx < ring->capacity) {
    307 		return idx;
    308 	} else {
    309 		return idx - ring->capacity;
    310 	}
    311 }
    312 
    313 /*
    314  * Return ring's tail (= head + used) position.
    315  * This position indicates next frame of the last valid frames.
    316  */
    317 static __inline int
    318 auring_tail(const audio_ring_t *ring)
    319 {
    320 	return auring_round(ring, ring->head + ring->used);
    321 }
    322 
    323 /*
    324  * Return ring's head pointer.
    325  * This function can be used only if the stride of the 'ring' is equal to
    326  * the internal stride.  Don't use this for hw buffer.
    327  */
    328 static __inline aint_t *
    329 auring_headptr_aint(const audio_ring_t *ring)
    330 {
    331 	KASSERTMSG(ring->fmt.stride == sizeof(aint_t) * NBBY,
    332 	    "ring->fmt.stride=%d sizeof(aint_t)*NBBY=%zd",
    333 	    ring->fmt.stride, sizeof(aint_t) * NBBY);
    334 
    335 	return (aint_t *)ring->mem + ring->head * ring->fmt.channels;
    336 }
    337 
    338 /*
    339  * Return ring's tail (= head + used) pointer.
    340  * This function can be used only if the stride of the 'ring' is equal to
    341  * the internal stride.  Don't use this for hw buffer.
    342  */
    343 static __inline aint_t *
    344 auring_tailptr_aint(const audio_ring_t *ring)
    345 {
    346 	KASSERTMSG(ring->fmt.stride == sizeof(aint_t) * NBBY,
    347 	    "ring->fmt.stride=%d sizeof(aint_t)*NBBY=%zd",
    348 	    ring->fmt.stride, sizeof(aint_t) * NBBY);
    349 
    350 	return (aint_t *)ring->mem + auring_tail(ring) * ring->fmt.channels;
    351 }
    352 
    353 /*
    354  * Return ring's head pointer.
    355  * This function can be used even if the stride of the 'ring' is equal to
    356  * or not equal to the internal stride.
    357  */
    358 static __inline uint8_t *
    359 auring_headptr(const audio_ring_t *ring)
    360 {
    361 	return (uint8_t *)ring->mem +
    362 	    ring->head * ring->fmt.channels * ring->fmt.stride / NBBY;
    363 }
    364 
    365 /*
    366  * Return ring's tail pointer.
    367  * It points the next position of the last valid frames.
    368  * This function can be used even if the stride of the 'ring' is equal to
    369  * or not equal to the internal stride.
    370  */
    371 static __inline uint8_t *
    372 auring_tailptr(audio_ring_t *ring)
    373 {
    374 	return (uint8_t *)ring->mem +
    375 	    auring_tail(ring) * ring->fmt.channels * ring->fmt.stride / NBBY;
    376 }
    377 
    378 /*
    379  * Return ring's capacity in bytes.
    380  */
    381 static __inline int
    382 auring_bytelen(const audio_ring_t *ring)
    383 {
    384 	return frametobyte(&ring->fmt, ring->capacity);
    385 }
    386 
    387 /*
    388  * Take out n frames from head of ring.
    389  * This function only manipurates counters.  It doesn't manipurate any
    390  * actual buffer data.
    391  */
    392 #define auring_take(ring, n)	auring_take_(__func__, __LINE__, ring, n)
    393 static __inline void
    394 auring_take_(const char *func, int line, audio_ring_t *ring, int n)
    395 {
    396 	DIAGNOSTIC_ring(ring);
    397 	KASSERTMSG(n >= 0, "called from %s:%d: n=%d", func, line, n);
    398 	KASSERTMSG(ring->used >= n, "called from %s:%d: ring->used=%d n=%d",
    399 	    func, line, ring->used, n);
    400 
    401 	ring->head = auring_round(ring, ring->head + n);
    402 	ring->used -= n;
    403 }
    404 
    405 /*
    406  * Append n frames into tail of ring.
    407  * This function only manipurates counters.  It doesn't manipurate any
    408  * actual buffer data.
    409  */
    410 #define auring_push(ring, n)	auring_push_(__func__, __LINE__, ring, n)
    411 static __inline void
    412 auring_push_(const char *func, int line, audio_ring_t *ring, int n)
    413 {
    414 	DIAGNOSTIC_ring(ring);
    415 	KASSERT(n >= 0);
    416 	KASSERTMSG(ring->used + n <= ring->capacity,
    417 	    "called from %s:%d: ring->used=%d n=%d ring->capacity=%d",
    418 	    func, line, ring->used, n, ring->capacity);
    419 
    420 	ring->used += n;
    421 }
    422 
    423 /*
    424  * Return the number of contiguous frames in used.
    425  */
    426 static __inline int
    427 auring_get_contig_used(const audio_ring_t *ring)
    428 {
    429 	DIAGNOSTIC_ring(ring);
    430 
    431 	if (ring->head + ring->used <= ring->capacity) {
    432 		return ring->used;
    433 	} else {
    434 		return ring->capacity - ring->head;
    435 	}
    436 }
    437 
    438 /*
    439  * Return the number of contiguous free frames.
    440  */
    441 static __inline int
    442 auring_get_contig_free(const audio_ring_t *ring)
    443 {
    444 	DIAGNOSTIC_ring(ring);
    445 
    446 	if (ring->head + ring->used < ring->capacity) {
    447 		return ring->capacity - (ring->head + ring->used);
    448 	} else {
    449 		return ring->capacity - ring->used;
    450 	}
    451 }
    452 
    453 #endif /* !_SYS_DEV_AUDIO_AUDIODEF_H_ */
    454