feeder_volume.c revision 152427
11638Srgrimes/*-
250476Speter * Copyright (c) 2005 Ariff Abdullah <ariff@FreeBSD.org>
31638Srgrimes * All rights reserved.
4156813Sru *
5156813Sru * Redistribution and use in source and binary forms, with or without
6124747Sru * modification, are permitted provided that the following conditions
7124747Sru * are met:
8124747Sru * 1. Redistributions of source code must retain the above copyright
9124747Sru *    notice, this list of conditions and the following disclaimer.
10124747Sru * 2. Redistributions in binary form must reproduce the above copyright
11183442Sed *    notice, this list of conditions and the following disclaimer in the
12124747Sru *    documentation and/or other materials provided with the distribution.
13124747Sru *
14153366Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15124747Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16124747Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17133673Sstefanf * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18124747Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19124747Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20123987Smtm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21124747Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22124747Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23124747Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24124747Sru * SUCH DAMAGE.
25124747Sru *
26124747Sru * feeder_volume, a long 'Lost Technology' rather than a new feature.
27124747Sru */
28124747Sru
29124747Sru#include <dev/sound/pcm/sound.h>
30124747Sru#include "feeder_if.h"
31124747Sru
32124747SruSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder_volume.c 152427 2005-11-14 18:37:59Z ariff $");
33124747Sru
34124747Srustatic int
35124747Srufeed_volume_s16(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
36124747Sru		uint32_t count, void *source)
37124747Sru{
38124747Sru	int i, j, k, vol[2];
39143658Sdas	int16_t *buf;
40183442Sed
41183442Sed	k = FEEDER_FEED(f->source, c, b, count & ~1, source);
42124747Sru	if (k < 2) {
43124747Sru#if 0
44124747Sru		device_printf(c->dev, "%s: Not enough data (Got: %d bytes)\n",
45124747Sru				__func__, k);
46124747Sru#endif
47130062Spjd		return 0;
48124747Sru	}
49124747Sru#if 0
50124747Sru	if (k & 1)
51124747Sru		device_printf(c->dev, "%s: Bytes not 16bit aligned.\n", __func__);
52124747Sru#endif
53124747Sru	k &= ~1;
54124747Sru	i = k >> 1;
55124747Sru	buf = (int16_t *)b;
56124747Sru	vol[0] = c->volume & 0x7f;
57124747Sru	vol[1] = (c->volume >> 8) & 0x7f;
58124747Sru	while (i > 0) {
59124747Sru		i--;
60130062Spjd		j = (vol[i & 1] * buf[i]) / 100;
61124747Sru		if (j > 32767)
62124747Sru			j = 32767;
63124747Sru		if (j < -32768)
64124747Sru			j = -32768;
65124747Sru		buf[i] = j;
66124747Sru	}
67124747Sru	return k;
68192926Sed}
69124747Sru
70124747Srustatic struct pcm_feederdesc feeder_volume_s16_desc[] = {
71124747Sru	{FEEDER_VOLUME, AFMT_S16_LE|AFMT_STEREO, AFMT_S16_LE|AFMT_STEREO, 0},
72124747Sru	{0, 0, 0, 0},
73124747Sru};
74124747Srustatic kobj_method_t feeder_volume_s16_methods[] = {
75130062Spjd    	KOBJMETHOD(feeder_feed, feed_volume_s16),
76124747Sru	{0, 0}
77124747Sru};
78124747SruFEEDER_DECLARE(feeder_volume_s16, 2, NULL);
79124747Sru