2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <dev/sound/pcm/sound.h>
29 #include "feeder_if.h"
31 SND_DECLARE_FILE("$FreeBSD$");
34 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel)
38 b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO);
39 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc);
47 sndbuf_destroy(struct snd_dbuf *b)
50 free(b->tmpbuf, M_DEVBUF);
52 free(b->shadbuf, M_DEVBUF);
53 if (!(b->flags & SNDBUF_F_MANAGED) && b->buf)
54 free(b->buf, M_DEVBUF);
59 sndbuf_getbufaddr(struct snd_dbuf *buf)
61 return (buf->buf_addr);
65 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error)
67 struct snd_dbuf *b = (struct snd_dbuf *)arg;
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);
75 b->buf_addr = segs[0].ds_addr;
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.
86 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size)
92 b->bufsize = b->maxsize;
94 b->flags |= SNDBUF_F_MANAGED;
95 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT,
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);
105 ret = sndbuf_resize(b, 2, b->maxsize / 2);
112 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size)
115 b->flags |= SNDBUF_F_MANAGED;
118 b->bufsize = b->maxsize;
119 return sndbuf_resize(b, 2, b->maxsize / 2);
123 sndbuf_free(struct snd_dbuf *b)
126 free(b->tmpbuf, M_DEVBUF);
130 free(b->shadbuf, M_DEVBUF);
134 if (!(b->flags & SNDBUF_F_MANAGED) && b->buf)
135 free(b->buf, M_DEVBUF);
138 bus_dmamap_unload(b->dmatag, b->dmamap);
140 if (b->dmamap && b->buf)
141 bus_dmamem_free(b->dmatag, b->buf, b->dmamap);
147 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
149 u_int8_t *tmpbuf, *f2;
151 chn_lock(b->channel);
158 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize)) {
159 chn_unlock(b->channel);
162 if (blkcnt == b->blkcnt && blksz == b->blksz)
165 chn_unlock(b->channel);
166 tmpbuf = malloc(blkcnt * blksz, M_DEVBUF, M_NOWAIT);
169 chn_lock(b->channel);
172 b->bufsize = blkcnt * blksz;
176 chn_unlock(b->channel);
180 chn_unlock(b->channel);
185 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
187 u_int8_t *buf, *tmpbuf, *f1, *f2;
188 u_int8_t *shadbuf, *f3;
189 unsigned int bufsize;
192 if (blkcnt < 2 || blksz < 16)
195 bufsize = blksz * blkcnt;
197 chn_unlock(b->channel);
198 buf = malloc(bufsize, M_DEVBUF, M_WAITOK);
199 tmpbuf = malloc(bufsize, M_DEVBUF, M_WAITOK);
200 shadbuf = malloc(bufsize, M_DEVBUF, M_WAITOK);
202 chn_lock(b->channel);
206 b->bufsize = bufsize;
207 b->maxsize = bufsize;
213 b->shadbuf = shadbuf;
218 chn_unlock(b->channel);
228 chn_lock(b->channel);
233 * @brief Zero out space in buffer free area
235 * This function clears a chunk of @c length bytes in the buffer free area
236 * (i.e., where the next write will be placed).
238 * @param b buffer context
239 * @param length number of bytes to blank
242 sndbuf_clear(struct snd_dbuf *b, unsigned int length)
249 if (length > b->bufsize)
252 data = sndbuf_zerodata(b->fmt);
254 i = sndbuf_getfreeptr(b);
255 p = sndbuf_getbuf(b);
266 * @brief Zap buffer contents, resetting "ready area" fields
268 * @param b buffer context
271 sndbuf_fillsilence(struct snd_dbuf *b)
274 memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize);
280 * @brief Reset buffer w/o flushing statistics
282 * This function just zeroes out buffer contents and sets the "ready length"
283 * to zero. This was originally to facilitate minimal playback interruption
284 * (i.e., dropped samples) in SNDCTL_DSP_SILENCE/SKIP ioctls.
286 * @param b buffer context
289 sndbuf_softreset(struct snd_dbuf *b)
292 if (b->buf && b->bufsize > 0)
293 sndbuf_clear(b, b->bufsize);
297 sndbuf_reset(struct snd_dbuf *b)
306 if (b->buf && b->bufsize > 0)
307 sndbuf_clear(b, b->bufsize);
308 sndbuf_clearshadow(b);
312 sndbuf_getfmt(struct snd_dbuf *b)
318 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
322 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
323 if (b->fmt & AFMT_16BIT)
325 else if (b->fmt & AFMT_24BIT)
327 else if (b->fmt & AFMT_32BIT)
333 sndbuf_getspd(struct snd_dbuf *b)
339 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
345 sndbuf_getalign(struct snd_dbuf *b)
347 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
349 return align[b->bps - 1];
353 sndbuf_getblkcnt(struct snd_dbuf *b)
359 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt)
365 sndbuf_getblksz(struct snd_dbuf *b)
371 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz)
377 sndbuf_getbps(struct snd_dbuf *b)
383 sndbuf_getbuf(struct snd_dbuf *b)
389 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
391 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs));
397 sndbuf_getsize(struct snd_dbuf *b)
403 sndbuf_getmaxsize(struct snd_dbuf *b)
409 sndbuf_runsz(struct snd_dbuf *b)
415 sndbuf_setrun(struct snd_dbuf *b, int go)
417 b->dl = go? b->blksz : 0;
421 sndbuf_getsel(struct snd_dbuf *b)
426 /************************************************************/
428 sndbuf_getxrun(struct snd_dbuf *b)
430 SNDBUF_LOCKASSERT(b);
436 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun)
438 SNDBUF_LOCKASSERT(b);
444 sndbuf_gethwptr(struct snd_dbuf *b)
446 SNDBUF_LOCKASSERT(b);
452 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr)
454 SNDBUF_LOCKASSERT(b);
460 sndbuf_getready(struct snd_dbuf *b)
462 SNDBUF_LOCKASSERT(b);
463 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
469 sndbuf_getreadyptr(struct snd_dbuf *b)
471 SNDBUF_LOCKASSERT(b);
472 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
478 sndbuf_getfree(struct snd_dbuf *b)
480 SNDBUF_LOCKASSERT(b);
481 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
483 return b->bufsize - b->rl;
487 sndbuf_getfreeptr(struct snd_dbuf *b)
489 SNDBUF_LOCKASSERT(b);
490 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp));
491 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
493 return (b->rp + b->rl) % b->bufsize;
497 sndbuf_getblocks(struct snd_dbuf *b)
499 SNDBUF_LOCKASSERT(b);
501 return b->total / b->blksz;
505 sndbuf_getprevblocks(struct snd_dbuf *b)
507 SNDBUF_LOCKASSERT(b);
509 return b->prev_total / b->blksz;
513 sndbuf_gettotal(struct snd_dbuf *b)
515 SNDBUF_LOCKASSERT(b);
521 sndbuf_updateprevtotal(struct snd_dbuf *b)
523 SNDBUF_LOCKASSERT(b);
525 b->prev_total = b->total;
529 snd_xbytes(unsigned int v, unsigned int from, unsigned int to)
531 unsigned int w, x, y;
536 if (from == 0 || to == 0 || v == 0)
549 return (unsigned int)(((u_int64_t)v * to) / from);
553 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
555 if (from == NULL || to == NULL || v == 0)
558 return snd_xbytes(v, sndbuf_getbps(from) * sndbuf_getspd(from),
559 sndbuf_getbps(to) * sndbuf_getspd(to));
563 sndbuf_zerodata(u_int32_t fmt)
565 if (fmt & AFMT_SIGNED)
567 else if (fmt & AFMT_MU_LAW)
569 else if (fmt & AFMT_A_LAW)
574 /************************************************************/
577 * @brief Acquire buffer space to extend ready area
579 * This function extends the ready area length by @c count bytes, and may
580 * optionally copy samples from another location stored in @c from. The
581 * counter @c snd_dbuf::total is also incremented by @c count bytes.
583 * @param b audio buffer
584 * @param from sample source (optional)
585 * @param count number of bytes to acquire
587 * @retval 0 Unconditional
590 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
594 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b)));
595 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
599 l = min(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b));
600 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l);
607 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
613 * @brief Dispose samples from channel buffer, increasing size of ready area
615 * This function discards samples from the supplied buffer by advancing the
616 * ready area start pointer and decrementing the ready area length. If
617 * @c to is not NULL, then the discard samples will be copied to the location
620 * @param b PCM channel sound buffer
621 * @param to destination buffer (optional)
622 * @param count number of bytes to discard
624 * @returns 0 unconditionally
627 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
631 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b)));
632 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl));
635 l = min(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b));
636 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l);
639 b->rp = (b->rp + l) % b->bufsize;
644 b->rp = (b->rp + count) % b->bufsize;
646 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count));
651 /* count is number of bytes we want added to destination buffer */
653 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
657 KASSERT(count > 0, ("can't feed 0 bytes"));
659 if (sndbuf_getfree(to) < count)
663 cnt = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
665 sndbuf_acquire(to, to->tmpbuf, cnt);
666 /* the root feeder has called sndbuf_dispose(from, , bytes fetched) */
668 } while (count && cnt);
673 /************************************************************/
676 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
680 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize);
682 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
684 printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
686 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
688 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags);
692 /************************************************************/
694 sndbuf_getflags(struct snd_dbuf *b)
700 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on)
708 * @brief Clear the shadow buffer by filling with samples equal to zero.
710 * @param b buffer to clear
713 sndbuf_clearshadow(struct snd_dbuf *b)
715 KASSERT(b != NULL, ("b is a null pointer"));
716 KASSERT(b->sl >= 0, ("illegal shadow length"));
718 if ((b->shadbuf != NULL) && (b->sl > 0))
719 memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl);
722 #ifdef OSSV4_EXPERIMENT
724 * @brief Return peak value from samples in buffer ready area.
726 * Peak ranges from 0-32767. If channel is monaural, most significant 16
727 * bits will be zero. For now, only expects to work with 1-2 channel
730 * @note Currently only operates with linear PCM formats.
732 * @param b buffer to analyze
733 * @param lpeak pointer to store left peak value
734 * @param rpeak pointer to store right peak value
737 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp)
739 u_int32_t lpeak, rpeak;
745 * @todo fill this in later