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