xref: /freebsd/sys/dev/sound/pcm/buffer.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
1 /*-
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <dev/sound/pcm/sound.h>
28 
29 #include "feeder_if.h"
30 
31 SND_DECLARE_FILE("$FreeBSD$");
32 
33 struct snd_dbuf *
34 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
35 {
36 	struct snd_dbuf *b;
37 
38 	b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
39 	snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
40 	b->dev = dev;
41 	b->channel = channel;
42 
43 	return b;
44 }
45 
46 void
47 sndbuf_destroy(struct snd_dbuf *b)
48 {
49 	if (b->tmpbuf)
50 		free(b->tmpbuf, M_DEVBUF);
51 	if (b->shadbuf)
52 		free(b->shadbuf, M_DEVBUF);
53 	if (!(b->flags & SNDBUF_F_MANAGED) && b->buf)
54 		free(b->buf, M_DEVBUF);
55 	free(b, M_DEVBUF);
56 }
57 
58 bus_addr_t
59 sndbuf_getbufaddr(struct snd_dbuf *buf)
60 {
61 	return (buf->buf_addr);
62 }
63 
64 static void
65 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
66 {
67 	struct snd_dbuf *b = (struct snd_dbuf *)arg;
68 
69 	if (bootverbose) {
70 		device_printf(b->dev, "sndbuf_setmap %lx, %lx; ",
71 		    (u_long)segs[0].ds_addr, (u_long)segs[0].ds_len);
72 		printf("%p -> %lx\n", b->buf, (u_long)segs[0].ds_addr);
73 	}
74 	if (error == 0)
75 		b->buf_addr = segs[0].ds_addr;
76 	else
77 		b->buf_addr = 0;
78 }
79 
80 /*
81  * Allocate memory for DMA buffer. If the device does not use DMA transfers,
82  * the driver can call malloc(9) and sndbuf_setup() itself.
83  */
84 
85 int
86 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
87 {
88 	int ret;
89 
90 	b->dmatag = dmatag;
91 	b->maxsize = size;
92 	b->bufsize = b->maxsize;
93 	b->buf_addr = 0;
94 	b->flags |= SNDBUF_F_MANAGED;
95 	if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT,
96 	    &b->dmamap))
97 		return (ENOMEM);
98 	if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize,
99 	    sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) {
100 		bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
101 		b->dmamap = NULL;
102 		return (ENOMEM);
103 	}
104 
105 	ret = sndbuf_resize(b, 2, b->maxsize / 2);
106 	if (ret != 0)
107 		sndbuf_free(b);
108 	return (ret);
109 }
110 
111 int
112 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
113 {
114 	if (buf)
115 		b->flags |= SNDBUF_F_MANAGED;
116 	b->buf = buf;
117 	b->maxsize = size;
118 	b->bufsize = b->maxsize;
119 	return sndbuf_resize(b, 2, b->maxsize / 2);
120 }
121 
122 void
123 sndbuf_free(struct snd_dbuf *b)
124 {
125 	if (b->tmpbuf)
126 		free(b->tmpbuf, M_DEVBUF);
127 	b->tmpbuf = NULL;
128 
129 	if (b->shadbuf)
130 		free(b->shadbuf, M_DEVBUF);
131 	b->shadbuf = NULL;
132 	b->sl = 0;
133 
134 	if (!(b->flags & SNDBUF_F_MANAGED) && b->buf)
135 		free(b->buf, M_DEVBUF);
136 
137 	if (b->dmamap)
138 		bus_dmamap_unload(b->dmatag, b->dmamap);
139 
140 	if (b->dmamap && b->buf)
141 		bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
142 	b->dmamap = NULL;
143 	b->buf = NULL;
144 }
145 
146 int
147 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
148 {
149 	u_int8_t *tmpbuf, *f2;
150 
151 	chn_lock(b->channel);
152 	if (b->maxsize == 0)
153 		goto out;
154 	if (blkcnt == 0)
155 		blkcnt = b->blkcnt;
156 	if (blksz == 0)
157 		blksz = b->blksz;
158 	if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize)) {
159 		chn_unlock(b->channel);
160 		return EINVAL;
161 	}
162 	if (blkcnt == b->blkcnt && blksz == b->blksz)
163 		goto out;
164 
165 	chn_unlock(b->channel);
166 	tmpbuf = malloc(blkcnt * blksz, M_DEVBUF, M_NOWAIT);
167 	if (tmpbuf == NULL)
168 		return ENOMEM;
169 	chn_lock(b->channel);
170 	b->blkcnt = blkcnt;
171 	b->blksz = blksz;
172 	b->bufsize = blkcnt * blksz;
173 	f2 =  b->tmpbuf;
174 	b->tmpbuf = tmpbuf;
175 	sndbuf_reset(b);
176 	chn_unlock(b->channel);
177 	free(f2, M_DEVBUF);
178 	return 0;
179 out:
180 	chn_unlock(b->channel);
181 	return 0;
182 }
183 
184 int
185 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
186 {
187         u_int8_t *buf, *tmpbuf, *f1, *f2;
188 	u_int8_t *shadbuf, *f3;
189         unsigned int bufsize;
190 	int ret;
191 
192 	if (blkcnt < 2 || blksz < 16)
193 		return EINVAL;
194 
195 	bufsize = blksz * blkcnt;
196 
197 	chn_unlock(b->channel);
198 	buf = malloc(bufsize, M_DEVBUF, M_WAITOK);
199 	if (buf == NULL) {
200 		ret = ENOMEM;
201 		goto out;
202 	}
203 
204 	tmpbuf = malloc(bufsize, M_DEVBUF, M_WAITOK);
205    	if (tmpbuf == NULL) {
206 		free(buf, M_DEVBUF);
207 		ret = ENOMEM;
208 		goto out;
209 	}
210 
211 	shadbuf = malloc(bufsize, M_DEVBUF, M_WAITOK);
212 	if (shadbuf == NULL) {
213 		free(buf, M_DEVBUF);
214 		free(tmpbuf, M_DEVBUF);
215 		ret = ENOMEM;
216 		goto out;
217 	}
218 
219 	chn_lock(b->channel);
220 
221 	b->blkcnt = blkcnt;
222 	b->blksz = blksz;
223 	b->bufsize = bufsize;
224 	b->maxsize = bufsize;
225 	f1 = b->buf;
226 	f2 = b->tmpbuf;
227 	b->buf = buf;
228 	b->tmpbuf = tmpbuf;
229 	f3 = b->shadbuf;
230 	b->shadbuf = shadbuf;
231 	b->sl = bufsize;
232 
233 	sndbuf_reset(b);
234 
235 	chn_unlock(b->channel);
236       	if (f1)
237 		free(f1, M_DEVBUF);
238       	if (f2)
239 		free(f2, M_DEVBUF);
240 	if (f3)
241 		free(f3, M_DEVBUF);
242 
243 	ret = 0;
244 out:
245 	chn_lock(b->channel);
246 	return ret;
247 }
248 
249 /**
250  * @brief Zero out space in buffer free area
251  *
252  * This function clears a chunk of @c length bytes in the buffer free area
253  * (i.e., where the next write will be placed).
254  *
255  * @param b		buffer context
256  * @param length	number of bytes to blank
257  */
258 void
259 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
260 {
261 	int i;
262 	u_char data, *p;
263 
264 	if (length == 0)
265 		return;
266 	if (length > b->bufsize)
267 		length = b->bufsize;
268 
269 	data = sndbuf_zerodata(b->fmt);
270 
271 	i = sndbuf_getfreeptr(b);
272 	p = sndbuf_getbuf(b);
273 	while (length > 0) {
274 		p[i] = data;
275 		length--;
276 		i++;
277 		if (i >= b->bufsize)
278 			i = 0;
279 	}
280 }
281 
282 /**
283  * @brief Zap buffer contents, resetting "ready area" fields
284  *
285  * @param b	buffer context
286  */
287 void
288 sndbuf_fillsilence(struct snd_dbuf *b)
289 {
290 	if (b->bufsize > 0)
291 		memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
292 	b->rp = 0;
293 	b->rl = b->bufsize;
294 }
295 
296 /**
297  * @brief Reset buffer w/o flushing statistics
298  *
299  * This function just zeroes out buffer contents and sets the "ready length"
300  * to zero.  This was originally to facilitate minimal playback interruption
301  * (i.e., dropped samples) in SNDCTL_DSP_SILENCE/SKIP ioctls.
302  *
303  * @param b	buffer context
304  */
305 void
306 sndbuf_softreset(struct snd_dbuf *b)
307 {
308 	b->rl = 0;
309 	if (b->buf && b->bufsize > 0)
310 		sndbuf_clear(b, b->bufsize);
311 }
312 
313 void
314 sndbuf_reset(struct snd_dbuf *b)
315 {
316 	b->hp = 0;
317 	b->rp = 0;
318 	b->rl = 0;
319 	b->dl = 0;
320 	b->prev_total = 0;
321 	b->total = 0;
322 	b->xrun = 0;
323 	if (b->buf && b->bufsize > 0)
324 		sndbuf_clear(b, b->bufsize);
325 	sndbuf_clearshadow(b);
326 }
327 
328 u_int32_t
329 sndbuf_getfmt(struct snd_dbuf *b)
330 {
331 	return b->fmt;
332 }
333 
334 int
335 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
336 {
337 	b->fmt = fmt;
338 	b->bps = 1;
339 	b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
340 	if (b->fmt & AFMT_16BIT)
341 		b->bps <<= 1;
342 	else if (b->fmt & AFMT_24BIT)
343 		b->bps *= 3;
344 	else if (b->fmt & AFMT_32BIT)
345 		b->bps <<= 2;
346 	return 0;
347 }
348 
349 unsigned int
350 sndbuf_getspd(struct snd_dbuf *b)
351 {
352 	return b->spd;
353 }
354 
355 void
356 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
357 {
358 	b->spd = spd;
359 }
360 
361 unsigned int
362 sndbuf_getalign(struct snd_dbuf *b)
363 {
364 	static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
365 
366 	return align[b->bps - 1];
367 }
368 
369 unsigned int
370 sndbuf_getblkcnt(struct snd_dbuf *b)
371 {
372 	return b->blkcnt;
373 }
374 
375 void
376 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
377 {
378 	b->blkcnt = blkcnt;
379 }
380 
381 unsigned int
382 sndbuf_getblksz(struct snd_dbuf *b)
383 {
384 	return b->blksz;
385 }
386 
387 void
388 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
389 {
390 	b->blksz = blksz;
391 }
392 
393 unsigned int
394 sndbuf_getbps(struct snd_dbuf *b)
395 {
396 	return b->bps;
397 }
398 
399 void *
400 sndbuf_getbuf(struct snd_dbuf *b)
401 {
402 	return b->buf;
403 }
404 
405 void *
406 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
407 {
408 	KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
409 
410 	return b->buf + ofs;
411 }
412 
413 unsigned int
414 sndbuf_getsize(struct snd_dbuf *b)
415 {
416 	return b->bufsize;
417 }
418 
419 unsigned int
420 sndbuf_getmaxsize(struct snd_dbuf *b)
421 {
422 	return b->maxsize;
423 }
424 
425 unsigned int
426 sndbuf_runsz(struct snd_dbuf *b)
427 {
428 	return b->dl;
429 }
430 
431 void
432 sndbuf_setrun(struct snd_dbuf *b, int go)
433 {
434 	b->dl = go? b->blksz : 0;
435 }
436 
437 struct selinfo *
438 sndbuf_getsel(struct snd_dbuf *b)
439 {
440 	return &b->sel;
441 }
442 
443 /************************************************************/
444 unsigned int
445 sndbuf_getxrun(struct snd_dbuf *b)
446 {
447 	SNDBUF_LOCKASSERT(b);
448 
449 	return b->xrun;
450 }
451 
452 void
453 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun)
454 {
455 	SNDBUF_LOCKASSERT(b);
456 
457 	b->xrun = xrun;
458 }
459 
460 unsigned int
461 sndbuf_gethwptr(struct snd_dbuf *b)
462 {
463 	SNDBUF_LOCKASSERT(b);
464 
465 	return b->hp;
466 }
467 
468 void
469 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
470 {
471 	SNDBUF_LOCKASSERT(b);
472 
473 	b->hp = ptr;
474 }
475 
476 unsigned int
477 sndbuf_getready(struct snd_dbuf *b)
478 {
479 	SNDBUF_LOCKASSERT(b);
480 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
481 
482 	return b->rl;
483 }
484 
485 unsigned int
486 sndbuf_getreadyptr(struct snd_dbuf *b)
487 {
488 	SNDBUF_LOCKASSERT(b);
489 	KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
490 
491 	return b->rp;
492 }
493 
494 unsigned int
495 sndbuf_getfree(struct snd_dbuf *b)
496 {
497 	SNDBUF_LOCKASSERT(b);
498 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
499 
500 	return b->bufsize - b->rl;
501 }
502 
503 unsigned int
504 sndbuf_getfreeptr(struct snd_dbuf *b)
505 {
506 	SNDBUF_LOCKASSERT(b);
507 	KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
508 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
509 
510 	return (b->rp + b->rl) % b->bufsize;
511 }
512 
513 unsigned int
514 sndbuf_getblocks(struct snd_dbuf *b)
515 {
516 	SNDBUF_LOCKASSERT(b);
517 
518 	return b->total / b->blksz;
519 }
520 
521 unsigned int
522 sndbuf_getprevblocks(struct snd_dbuf *b)
523 {
524 	SNDBUF_LOCKASSERT(b);
525 
526 	return b->prev_total / b->blksz;
527 }
528 
529 unsigned int
530 sndbuf_gettotal(struct snd_dbuf *b)
531 {
532 	SNDBUF_LOCKASSERT(b);
533 
534 	return b->total;
535 }
536 
537 void
538 sndbuf_updateprevtotal(struct snd_dbuf *b)
539 {
540 	SNDBUF_LOCKASSERT(b);
541 
542 	b->prev_total = b->total;
543 }
544 
545 unsigned int
546 snd_xbytes(unsigned int v, unsigned int from, unsigned int to)
547 {
548 	unsigned int w, x, y;
549 
550 	if (from == to)
551 		return v;
552 
553 	if (from == 0 || to == 0 || v == 0)
554 		return 0;
555 
556 	x = from;
557 	y = to;
558 	while (y != 0) {
559 		w = x % y;
560 		x = y;
561 		y = w;
562 	}
563 	from /= x;
564 	to /= x;
565 
566 	return (unsigned int)(((u_int64_t)v * to) / from);
567 }
568 
569 unsigned int
570 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
571 {
572 	if (from == NULL || to == NULL || v == 0)
573 		return 0;
574 
575 	return snd_xbytes(v, sndbuf_getbps(from) * sndbuf_getspd(from),
576 	    sndbuf_getbps(to) * sndbuf_getspd(to));
577 }
578 
579 u_int8_t
580 sndbuf_zerodata(u_int32_t fmt)
581 {
582 	if (fmt & AFMT_SIGNED)
583 		return (0x00);
584 	else if (fmt & AFMT_MU_LAW)
585 		return (0x7f);
586 	else if (fmt & AFMT_A_LAW)
587 		return (0x55);
588 	return (0x80);
589 }
590 
591 /************************************************************/
592 
593 /**
594  * @brief Acquire buffer space to extend ready area
595  *
596  * This function extends the ready area length by @c count bytes, and may
597  * optionally copy samples from another location stored in @c from.  The
598  * counter @c snd_dbuf::total is also incremented by @c count bytes.
599  *
600  * @param b	audio buffer
601  * @param from	sample source (optional)
602  * @param count	number of bytes to acquire
603  *
604  * @retval 0	Unconditional
605  */
606 int
607 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
608 {
609 	int l;
610 
611 	KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
612 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
613 	b->total += count;
614 	if (from != NULL) {
615 		while (count > 0) {
616 			l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
617 			bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
618 			from += l;
619 			b->rl += l;
620 			count -= l;
621 		}
622 	} else
623 		b->rl += count;
624 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
625 
626 	return 0;
627 }
628 
629 /**
630  * @brief Dispose samples from channel buffer, increasing size of ready area
631  *
632  * This function discards samples from the supplied buffer by advancing the
633  * ready area start pointer and decrementing the ready area length.  If
634  * @c to is not NULL, then the discard samples will be copied to the location
635  * it points to.
636  *
637  * @param b	PCM channel sound buffer
638  * @param to	destination buffer (optional)
639  * @param count	number of bytes to discard
640  *
641  * @returns 0 unconditionally
642  */
643 int
644 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
645 {
646 	int l;
647 
648 	KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
649 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
650 	if (to != NULL) {
651 		while (count > 0) {
652 			l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
653 			bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
654 			to += l;
655 			b->rl -= l;
656 			b->rp = (b->rp + l) % b->bufsize;
657 			count -= l;
658 		}
659 	} else {
660 		b->rl -= count;
661 		b->rp = (b->rp + count) % b->bufsize;
662 	}
663 	KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
664 
665 	return 0;
666 }
667 
668 /* count is number of bytes we want added to destination buffer */
669 int
670 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
671 {
672 	unsigned int cnt;
673 
674 	KASSERT(count > 0, ("can't feed 0 bytes"));
675 
676 	if (sndbuf_getfree(to) < count)
677 		return EINVAL;
678 
679 	do {
680 		cnt = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
681 		if (cnt)
682 			sndbuf_acquire(to, to->tmpbuf, cnt);
683 		/* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
684 		count -= cnt;
685 	} while (count && cnt);
686 
687 	return 0;
688 }
689 
690 /************************************************************/
691 
692 void
693 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
694 {
695 	printf("%s: [", s);
696 	if (what & 0x01)
697 		printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
698 	if (what & 0x02)
699 		printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
700 	if (what & 0x04)
701 		printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
702    	if (what & 0x08)
703 		printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
704 	if (what & 0x10)
705 		printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
706 	printf(" ]\n");
707 }
708 
709 /************************************************************/
710 u_int32_t
711 sndbuf_getflags(struct snd_dbuf *b)
712 {
713 	return b->flags;
714 }
715 
716 void
717 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
718 {
719 	b->flags &= ~flags;
720 	if (on)
721 		b->flags |= flags;
722 }
723 
724 /**
725  * @brief Clear the shadow buffer by filling with samples equal to zero.
726  *
727  * @param b buffer to clear
728  */
729 void
730 sndbuf_clearshadow(struct snd_dbuf *b)
731 {
732 	KASSERT(b != NULL, ("b is a null pointer"));
733 	KASSERT(b->sl >= 0, ("illegal shadow length"));
734 
735 	if ((b->shadbuf != NULL) && (b->sl > 0))
736 		memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl);
737 }
738 
739 #ifdef OSSV4_EXPERIMENT
740 /**
741  * @brief Return peak value from samples in buffer ready area.
742  *
743  * Peak ranges from 0-32767.  If channel is monaural, most significant 16
744  * bits will be zero.  For now, only expects to work with 1-2 channel
745  * buffers.
746  *
747  * @note  Currently only operates with linear PCM formats.
748  *
749  * @param b buffer to analyze
750  * @param lpeak pointer to store left peak value
751  * @param rpeak pointer to store right peak value
752  */
753 void
754 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp)
755 {
756 	u_int32_t lpeak, rpeak;
757 
758 	lpeak = 0;
759 	rpeak = 0;
760 
761 	/**
762 	 * @todo fill this in later
763 	 */
764 }
765 #endif
766