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