2 * Copyright (c) 2001 Cameron Grant <gandalf@vilnya.demon.co.uk>
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
26 * $FreeBSD: src/sys/dev/sound/pcm/vchan.c,v 1.5.2.4 2003/02/11 15:54:16 orion Exp $
27 * $DragonFly: src/sys/dev/sound/pcm/vchan.c,v 1.3 2006/09/05 00:55:43 dillon Exp $
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/pcm/vchan.h>
32 #include "feeder_if.h"
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/vchan.c,v 1.3 2006/09/05 00:55:43 dillon Exp $");
37 u_int32_t spd, fmt, blksz, bps, run;
38 struct pcm_channel *channel, *parent;
39 struct pcmchan_caps caps;
42 static u_int32_t vchan_fmt[] = {
43 AFMT_STEREO | AFMT_S16_LE,
48 vchan_mix_s16(int16_t *to, int16_t *tmp, unsigned int count)
51 * to is the output buffer, tmp is the input buffer
52 * count is the number of 16bit samples to mix
57 for(i = 0; i < count; i++) {
61 /* printf("%d + %d = %d (u)\n", to[i], tmp[i], x); */
65 /* printf("%d + %d = %d (o)\n", to[i], tmp[i], x); */
68 to[i] = x & 0x0000ffff;
74 feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32_t count, void *source)
76 /* we're going to abuse things a bit */
77 struct snd_dbuf *src = source;
78 struct pcmchan_children *cce;
79 struct pcm_channel *ch;
83 KASSERT(sndbuf_getsize(src) >= count, ("bad bufsize"));
88 * we are going to use our source as a temporary buffer since it's
89 * got no other purpose. we obtain our data by traversing the channel
90 * list of children and calling vchan_mix_* to mix count bytes from each
91 * into our destination buffer, b
94 tmp = (int16_t *)sndbuf_getbuf(src);
96 SLIST_FOREACH(cce, &c->children, link) {
98 if (ch->flags & CHN_F_TRIGGERED) {
99 if (ch->flags & CHN_F_MAPPED)
100 sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft));
101 cnt = FEEDER_FEED(ch->feeder, ch, (u_int8_t *)tmp, count, ch->bufsoft);
102 vchan_mix_s16(dst, tmp, cnt / 2);
109 static struct pcm_feederdesc feeder_vchan_s16_desc[] = {
110 {FEEDER_MIXER, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
113 static kobj_method_t feeder_vchan_s16_methods[] = {
114 KOBJMETHOD(feeder_feed, feed_vchan_s16),
117 FEEDER_DECLARE(feeder_vchan_s16, 2, NULL);
119 /************************************************************/
122 vchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
125 struct pcm_channel *parent = devinfo;
127 KASSERT(dir == PCMDIR_PLAY, ("vchan_init: bad direction"));
128 ch = kmalloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
132 ch->spd = DSP_DEFAULT_SPEED;
135 c->flags |= CHN_F_VIRTUAL;
141 vchan_free(kobj_t obj, void *data)
147 vchan_setformat(kobj_t obj, void *data, u_int32_t format)
149 struct vchinfo *ch = data;
150 struct pcm_channel *parent = ch->parent;
154 ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
155 ch->bps <<= (ch->fmt & AFMT_16BIT)? 1 : 0;
156 ch->bps <<= (ch->fmt & AFMT_32BIT)? 2 : 0;
157 chn_notify(parent, CHN_N_FORMAT);
162 vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
164 struct vchinfo *ch = data;
165 struct pcm_channel *parent = ch->parent;
168 chn_notify(parent, CHN_N_RATE);
173 vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
175 struct vchinfo *ch = data;
176 struct pcm_channel *parent = ch->parent;
179 ch->blksz = blocksize;
180 chn_notify(parent, CHN_N_BLOCKSIZE);
182 crate = ch->spd * ch->bps;
183 prate = sndbuf_getspd(parent->bufhard) * sndbuf_getbps(parent->bufhard);
184 blocksize = sndbuf_getblksz(parent->bufhard);
192 vchan_trigger(kobj_t obj, void *data, int go)
194 struct vchinfo *ch = data;
195 struct pcm_channel *parent = ch->parent;
197 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
200 ch->run = (go == PCMTRIG_START)? 1 : 0;
201 chn_notify(parent, CHN_N_TRIGGER);
206 static struct pcmchan_caps *
207 vchan_getcaps(kobj_t obj, void *data)
209 struct vchinfo *ch = data;
211 ch->caps.minspeed = sndbuf_getspd(ch->parent->bufhard);
212 ch->caps.maxspeed = ch->caps.minspeed;
213 ch->caps.fmtlist = vchan_fmt;
219 static kobj_method_t vchan_methods[] = {
220 KOBJMETHOD(channel_init, vchan_init),
221 KOBJMETHOD(channel_free, vchan_free),
222 KOBJMETHOD(channel_setformat, vchan_setformat),
223 KOBJMETHOD(channel_setspeed, vchan_setspeed),
224 KOBJMETHOD(channel_setblocksize, vchan_setblocksize),
225 KOBJMETHOD(channel_trigger, vchan_trigger),
226 KOBJMETHOD(channel_getcaps, vchan_getcaps),
229 CHANNEL_DECLARE(vchan);
231 /* virtual channel interface */
234 vchan_create(struct pcm_channel *parent)
236 struct snddev_info *d = parent->parentsnddev;
237 struct pcmchan_children *pce;
238 struct pcm_channel *child;
242 if (!(parent->flags & CHN_F_BUSY)) {
247 pce = kmalloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
253 /* create a new playback channel */
254 child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
256 kfree(pce, M_DEVBUF);
261 first = SLIST_EMPTY(&parent->children);
262 /* add us to our parent channel's children */
263 pce->channel = child;
264 SLIST_INSERT_HEAD(&parent->children, pce, link);
267 /* add us to our grandparent's channel list */
268 err = pcm_chn_add(d, child, !first);
270 pcm_chn_destroy(child);
271 kfree(pce, M_DEVBUF);
274 /* XXX gross ugly hack, kill murder death */
276 err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
278 printf("chn_reset: %d\n", err);
279 err = chn_setspeed(parent, 44100);
281 printf("chn_setspeed: %d\n", err);
288 vchan_destroy(struct pcm_channel *c)
290 struct pcm_channel *parent = c->parentchannel;
291 struct snddev_info *d = parent->parentsnddev;
292 struct pcmchan_children *pce;
296 if (!(parent->flags & CHN_F_BUSY)) {
300 if (SLIST_EMPTY(&parent->children)) {
305 /* remove us from our parent's children list */
306 SLIST_FOREACH(pce, &parent->children, link) {
307 if (pce->channel == c)
313 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
314 kfree(pce, M_DEVBUF);
316 last = SLIST_EMPTY(&parent->children);
318 parent->flags &= ~CHN_F_BUSY;
320 /* remove us from our grantparent's channel list */
321 err = pcm_chn_remove(d, c, !last);
326 /* destroy ourselves */
327 err = pcm_chn_destroy(c);
333 vchan_initsys(device_t dev)
336 struct snddev_info *d;
338 d = device_get_softc(dev);
339 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
340 OID_AUTO, "vchans", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
341 sysctl_hw_snd_vchans, "I", "")