Home | History | Annotate | Line # | Download | only in audio
audiodef.h revision 1.9
      1 /*	$NetBSD: audiodef.h,v 1.9 2020/02/22 06:58:39 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 #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 	SLIST_ENTRY(audio_file) entry;
    206 };
    207 
    208 struct audio_trackmixer {
    209 	struct audio_softc *sc;
    210 
    211 	int		mode;		/* AUMODE_PLAY or AUMODE_RECORD */
    212 	audio_format2_t	track_fmt;	/* track <-> trackmixer format */
    213 
    214 	int		frames_per_block; /* number of frames in a block */
    215 
    216 	/*
    217 	 * software master volume (0..256)
    218 	 * Must be protected by sc_intr_lock.
    219 	 */
    220 	u_int		volume;
    221 	/*
    222 	 * Volume recovery timer in auto gain control.
    223 	 * Must be protected by sc_intr_lock.
    224 	 */
    225 	int		voltimer;
    226 
    227 	audio_format2_t	mixfmt;
    228 	void		*mixsample;	/* mixing buf in double-sized int */
    229 
    230 	/*
    231 	 * true if trackmixer does LE<->BE conversion.
    232 	 * Generally an encoding conversion should be done by each hardware
    233 	 * driver but for most modern little endian drivers which support
    234 	 * only linear PCM it's troublesome issue to consider about big endian
    235 	 * arch.  Therefore, we do this conversion here only if the hardware
    236 	 * format is SLINEAR_OE:16.
    237 	 */
    238 	bool		swap_endian;
    239 
    240 	audio_filter_t	codec;		/* hardware codec */
    241 	audio_filter_arg_t codecarg;	/* and its argument */
    242 	audio_ring_t	codecbuf;	/* also used for wide->int conversion */
    243 
    244 	audio_ring_t	hwbuf;		/* HW I/O buf */
    245 
    246 	void		*sih;		/* softint cookie */
    247 
    248 	/* Must be protected by sc_lock. */
    249 	kcondvar_t	outcv;
    250 
    251 	uint64_t	mixseq;		/* seq# currently being mixed */
    252 	uint64_t	hwseq;		/* seq# HW output completed */
    253 
    254 	/* initial blktime n/d = AUDIO_BLK_MS / 1000 */
    255 	int		blktime_n;	/* blk time numerator */
    256 	int		blktime_d;	/* blk time denominator */
    257 
    258 	/* XXX */
    259 	uint64_t	hw_complete_counter;
    260 };
    261 
    262 /*
    263  * Audio Ring Buffer.
    264  */
    265 
    266 #ifdef DIAGNOSTIC
    267 #define DIAGNOSTIC_ring(ring)	audio_diagnostic_ring(__func__, (ring))
    268 extern void audio_diagnostic_ring(const char *, const audio_ring_t *);
    269 #else
    270 #define DIAGNOSTIC_ring(ring)
    271 #endif
    272 
    273 /*
    274  * Convert number of frames to number of bytes.
    275  */
    276 static __inline int
    277 frametobyte(const audio_format2_t *fmt, int frames)
    278 {
    279 	return frames * fmt->channels * fmt->stride / NBBY;
    280 }
    281 
    282 /*
    283  * Return the number of frames per block.
    284  */
    285 static __inline int
    286 frame_per_block(const audio_trackmixer_t *mixer, const audio_format2_t *fmt)
    287 {
    288 	return (fmt->sample_rate * mixer->blktime_n + mixer->blktime_d - 1) /
    289 	    mixer->blktime_d;
    290 }
    291 
    292 /*
    293  * Round idx.  idx must be non-negative and less than 2 * capacity.
    294  */
    295 static __inline int
    296 auring_round(const audio_ring_t *ring, int idx)
    297 {
    298 	DIAGNOSTIC_ring(ring);
    299 	KASSERTMSG(idx >= 0, "idx=%d", idx);
    300 	KASSERTMSG(idx < ring->capacity * 2,
    301 	    "idx=%d ring->capacity=%d", idx, ring->capacity);
    302 
    303 	if (idx < ring->capacity) {
    304 		return idx;
    305 	} else {
    306 		return idx - ring->capacity;
    307 	}
    308 }
    309 
    310 /*
    311  * Return ring's tail (= head + used) position.
    312  * This position indicates next frame of the last valid frames.
    313  */
    314 static __inline int
    315 auring_tail(const audio_ring_t *ring)
    316 {
    317 	return auring_round(ring, ring->head + ring->used);
    318 }
    319 
    320 /*
    321  * Return ring's head pointer.
    322  * This function can be used only if the stride of the 'ring' is equal to
    323  * the internal stride.  Don't use this for hw buffer.
    324  */
    325 static __inline aint_t *
    326 auring_headptr_aint(const audio_ring_t *ring)
    327 {
    328 	KASSERTMSG(ring->fmt.stride == sizeof(aint_t) * NBBY,
    329 	    "ring->fmt.stride=%d sizeof(aint_t)*NBBY=%zd",
    330 	    ring->fmt.stride, sizeof(aint_t) * NBBY);
    331 
    332 	return (aint_t *)ring->mem + ring->head * ring->fmt.channels;
    333 }
    334 
    335 /*
    336  * Return ring's tail (= head + used) pointer.
    337  * This function can be used only if the stride of the 'ring' is equal to
    338  * the internal stride.  Don't use this for hw buffer.
    339  */
    340 static __inline aint_t *
    341 auring_tailptr_aint(const audio_ring_t *ring)
    342 {
    343 	KASSERTMSG(ring->fmt.stride == sizeof(aint_t) * NBBY,
    344 	    "ring->fmt.stride=%d sizeof(aint_t)*NBBY=%zd",
    345 	    ring->fmt.stride, sizeof(aint_t) * NBBY);
    346 
    347 	return (aint_t *)ring->mem + auring_tail(ring) * ring->fmt.channels;
    348 }
    349 
    350 /*
    351  * Return ring's head pointer.
    352  * This function can be used even if the stride of the 'ring' is equal to
    353  * or not equal to the internal stride.
    354  */
    355 static __inline uint8_t *
    356 auring_headptr(const audio_ring_t *ring)
    357 {
    358 	return (uint8_t *)ring->mem +
    359 	    ring->head * ring->fmt.channels * ring->fmt.stride / NBBY;
    360 }
    361 
    362 /*
    363  * Return ring's tail pointer.
    364  * It points the next position of the last valid frames.
    365  * This function can be used even if the stride of the 'ring' is equal to
    366  * or not equal to the internal stride.
    367  */
    368 static __inline uint8_t *
    369 auring_tailptr(audio_ring_t *ring)
    370 {
    371 	return (uint8_t *)ring->mem +
    372 	    auring_tail(ring) * ring->fmt.channels * ring->fmt.stride / NBBY;
    373 }
    374 
    375 /*
    376  * Return ring's capacity in bytes.
    377  */
    378 static __inline int
    379 auring_bytelen(const audio_ring_t *ring)
    380 {
    381 	return frametobyte(&ring->fmt, ring->capacity);
    382 }
    383 
    384 /*
    385  * Take out n frames from head of ring.
    386  * This function only manipurates counters.  It doesn't manipurate any
    387  * actual buffer data.
    388  */
    389 #define auring_take(ring, n)	auring_take_(__func__, __LINE__, ring, n)
    390 static __inline void
    391 auring_take_(const char *func, int line, audio_ring_t *ring, int n)
    392 {
    393 	DIAGNOSTIC_ring(ring);
    394 	KASSERTMSG(n >= 0, "called from %s:%d: n=%d", func, line, n);
    395 	KASSERTMSG(ring->used >= n, "called from %s:%d: ring->used=%d n=%d",
    396 	    func, line, ring->used, n);
    397 
    398 	ring->head = auring_round(ring, ring->head + n);
    399 	ring->used -= n;
    400 }
    401 
    402 /*
    403  * Append n frames into tail of ring.
    404  * This function only manipurates counters.  It doesn't manipurate any
    405  * actual buffer data.
    406  */
    407 #define auring_push(ring, n)	auring_push_(__func__, __LINE__, ring, n)
    408 static __inline void
    409 auring_push_(const char *func, int line, audio_ring_t *ring, int n)
    410 {
    411 	DIAGNOSTIC_ring(ring);
    412 	KASSERT(n >= 0);
    413 	KASSERTMSG(ring->used + n <= ring->capacity,
    414 	    "called from %s:%d: ring->used=%d n=%d ring->capacity=%d",
    415 	    func, line, ring->used, n, ring->capacity);
    416 
    417 	ring->used += n;
    418 }
    419 
    420 /*
    421  * Return the number of contiguous frames in used.
    422  */
    423 static __inline int
    424 auring_get_contig_used(const audio_ring_t *ring)
    425 {
    426 	DIAGNOSTIC_ring(ring);
    427 
    428 	if (ring->head + ring->used <= ring->capacity) {
    429 		return ring->used;
    430 	} else {
    431 		return ring->capacity - ring->head;
    432 	}
    433 }
    434 
    435 /*
    436  * Return the number of contiguous free frames.
    437  */
    438 static __inline int
    439 auring_get_contig_free(const audio_ring_t *ring)
    440 {
    441 	DIAGNOSTIC_ring(ring);
    442 
    443 	if (ring->head + ring->used < ring->capacity) {
    444 		return ring->capacity - (ring->head + ring->used);
    445 	} else {
    446 		return ring->capacity - ring->used;
    447 	}
    448 }
    449 
    450 #endif /* !_SYS_DEV_AUDIO_AUDIODEF_H_ */
    451