xref: /linux/sound/isa/gus/gus_volume.c (revision c1017a4cdb68ae5368fbc9ee42c77f1f5dca8916)
11da177e4SLinus Torvalds /*
2*c1017a4cSJaroslav Kysela  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *   This program is free software; you can redistribute it and/or modify
61da177e4SLinus Torvalds  *   it under the terms of the GNU General Public License as published by
71da177e4SLinus Torvalds  *   the Free Software Foundation; either version 2 of the License, or
81da177e4SLinus Torvalds  *   (at your option) any later version.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *   This program is distributed in the hope that it will be useful,
111da177e4SLinus Torvalds  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
121da177e4SLinus Torvalds  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131da177e4SLinus Torvalds  *   GNU General Public License for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *   You should have received a copy of the GNU General Public License
161da177e4SLinus Torvalds  *   along with this program; if not, write to the Free Software
171da177e4SLinus Torvalds  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <sound/driver.h>
221da177e4SLinus Torvalds #include <linux/time.h>
231da177e4SLinus Torvalds #include <sound/core.h>
241da177e4SLinus Torvalds #include <sound/gus.h>
251da177e4SLinus Torvalds #define __GUS_TABLES_ALLOC__
261da177e4SLinus Torvalds #include "gus_tables.h"
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds EXPORT_SYMBOL(snd_gf1_atten_table); /* for snd-gus-synth module */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds unsigned short snd_gf1_lvol_to_gvol_raw(unsigned int vol)
311da177e4SLinus Torvalds {
321da177e4SLinus Torvalds 	unsigned short e, m, tmp;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 	if (vol > 65535)
351da177e4SLinus Torvalds 		vol = 65535;
361da177e4SLinus Torvalds 	tmp = vol;
371da177e4SLinus Torvalds 	e = 7;
381da177e4SLinus Torvalds 	if (tmp < 128) {
391da177e4SLinus Torvalds 		while (e > 0 && tmp < (1 << e))
401da177e4SLinus Torvalds 			e--;
411da177e4SLinus Torvalds 	} else {
421da177e4SLinus Torvalds 		while (tmp > 255) {
431da177e4SLinus Torvalds 			tmp >>= 1;
441da177e4SLinus Torvalds 			e++;
451da177e4SLinus Torvalds 		}
461da177e4SLinus Torvalds 	}
471da177e4SLinus Torvalds 	m = vol - (1 << e);
481da177e4SLinus Torvalds 	if (m > 0) {
491da177e4SLinus Torvalds 		if (e > 8)
501da177e4SLinus Torvalds 			m >>= e - 8;
511da177e4SLinus Torvalds 		else if (e < 8)
521da177e4SLinus Torvalds 			m <<= 8 - e;
531da177e4SLinus Torvalds 		m &= 255;
541da177e4SLinus Torvalds 	}
551da177e4SLinus Torvalds 	return (e << 8) | m;
561da177e4SLinus Torvalds }
571da177e4SLinus Torvalds 
58209ac85dSAdrian Bunk #if 0
59209ac85dSAdrian Bunk 
601da177e4SLinus Torvalds unsigned int snd_gf1_gvol_to_lvol_raw(unsigned short gf1_vol)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	unsigned int rvol;
631da177e4SLinus Torvalds 	unsigned short e, m;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	if (!gf1_vol)
661da177e4SLinus Torvalds 		return 0;
671da177e4SLinus Torvalds 	e = gf1_vol >> 8;
681da177e4SLinus Torvalds 	m = (unsigned char) gf1_vol;
691da177e4SLinus Torvalds 	rvol = 1 << e;
701da177e4SLinus Torvalds 	if (e > 8)
711da177e4SLinus Torvalds 		return rvol | (m << (e - 8));
721da177e4SLinus Torvalds 	return rvol | (m >> (8 - e));
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds 
755e2da206STakashi Iwai unsigned int snd_gf1_calc_ramp_rate(struct snd_gus_card * gus,
761da177e4SLinus Torvalds 				    unsigned short start,
771da177e4SLinus Torvalds 				    unsigned short end,
781da177e4SLinus Torvalds 				    unsigned int us)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	static unsigned char vol_rates[19] =
811da177e4SLinus Torvalds 	{
821da177e4SLinus Torvalds 		23, 24, 26, 28, 29, 31, 32, 34,
831da177e4SLinus Torvalds 		36, 37, 39, 40, 42, 44, 45, 47,
841da177e4SLinus Torvalds 		49, 50, 52
851da177e4SLinus Torvalds 	};
861da177e4SLinus Torvalds 	unsigned short range, increment, value, i;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	start >>= 4;
891da177e4SLinus Torvalds 	end >>= 4;
901da177e4SLinus Torvalds 	if (start < end)
911da177e4SLinus Torvalds 		us /= end - start;
921da177e4SLinus Torvalds 	else
931da177e4SLinus Torvalds 		us /= start - end;
941da177e4SLinus Torvalds 	range = 4;
951da177e4SLinus Torvalds 	value = gus->gf1.enh_mode ?
961da177e4SLinus Torvalds 	    vol_rates[0] :
971da177e4SLinus Torvalds 	    vol_rates[gus->gf1.active_voices - 14];
981da177e4SLinus Torvalds 	for (i = 0; i < 3; i++) {
991da177e4SLinus Torvalds 		if (us < value) {
1001da177e4SLinus Torvalds 			range = i;
1011da177e4SLinus Torvalds 			break;
1021da177e4SLinus Torvalds 		} else
1031da177e4SLinus Torvalds 			value <<= 3;
1041da177e4SLinus Torvalds 	}
1051da177e4SLinus Torvalds 	if (range == 4) {
1061da177e4SLinus Torvalds 		range = 3;
1071da177e4SLinus Torvalds 		increment = 1;
1081da177e4SLinus Torvalds 	} else
1091da177e4SLinus Torvalds 		increment = (value + (value >> 1)) / us;
1101da177e4SLinus Torvalds 	return (range << 6) | (increment & 0x3f);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
113209ac85dSAdrian Bunk #endif  /*  0  */
114209ac85dSAdrian Bunk 
1155e2da206STakashi Iwai unsigned short snd_gf1_translate_freq(struct snd_gus_card * gus, unsigned int freq16)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	freq16 >>= 3;
1181da177e4SLinus Torvalds 	if (freq16 < 50)
1191da177e4SLinus Torvalds 		freq16 = 50;
1201da177e4SLinus Torvalds 	if (freq16 & 0xf8000000) {
1211da177e4SLinus Torvalds 		freq16 = ~0xf8000000;
12299b359baSTakashi Iwai 		snd_printk(KERN_ERR "snd_gf1_translate_freq: overflow - freq = 0x%x\n", freq16);
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds 	return ((freq16 << 9) + (gus->gf1.playback_freq >> 1)) / gus->gf1.playback_freq;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
127209ac85dSAdrian Bunk #if 0
128209ac85dSAdrian Bunk 
1291da177e4SLinus Torvalds short snd_gf1_compute_vibrato(short cents, unsigned short fc_register)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	static short vibrato_table[] =
1321da177e4SLinus Torvalds 	{
1331da177e4SLinus Torvalds 		0, 0, 32, 592, 61, 1175, 93, 1808,
1341da177e4SLinus Torvalds 		124, 2433, 152, 3007, 182, 3632, 213, 4290,
1351da177e4SLinus Torvalds 		241, 4834, 255, 5200
1361da177e4SLinus Torvalds 	};
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	long depth;
1391da177e4SLinus Torvalds 	short *vi1, *vi2, pcents, v1;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	pcents = cents < 0 ? -cents : cents;
1421da177e4SLinus Torvalds 	for (vi1 = vibrato_table, vi2 = vi1 + 2; pcents > *vi2; vi1 = vi2, vi2 += 2);
1431da177e4SLinus Torvalds 	v1 = *(vi1 + 1);
1441da177e4SLinus Torvalds 	/* The FC table above is a list of pairs. The first number in the pair     */
1451da177e4SLinus Torvalds 	/* is the cents index from 0-255 cents, and the second number in the       */
1461da177e4SLinus Torvalds 	/* pair is the FC adjustment needed to change the pitch by the indexed     */
1471da177e4SLinus Torvalds 	/* number of cents. The table was created for an FC of 32768.              */
1481da177e4SLinus Torvalds 	/* The following expression does a linear interpolation against the        */
1491da177e4SLinus Torvalds 	/* approximated log curve in the table above, and then scales the number   */
1501da177e4SLinus Torvalds 	/* by the FC before the LFO. This calculation also adjusts the output      */
1511da177e4SLinus Torvalds 	/* value to produce the appropriate depth for the hardware. The depth      */
1521da177e4SLinus Torvalds 	/* is 2 * desired FC + 1.                                                  */
1531da177e4SLinus Torvalds 	depth = (((int) (*(vi2 + 1) - *vi1) * (pcents - *vi1) / (*vi2 - *vi1)) + v1) * fc_register >> 14;
1541da177e4SLinus Torvalds 	if (depth)
1551da177e4SLinus Torvalds 		depth++;
1561da177e4SLinus Torvalds 	if (depth > 255)
1571da177e4SLinus Torvalds 		depth = 255;
1581da177e4SLinus Torvalds 	return cents < 0 ? -(short) depth : (short) depth;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds unsigned short snd_gf1_compute_pitchbend(unsigned short pitchbend, unsigned short sens)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	static long log_table[] = {1024, 1085, 1149, 1218, 1290, 1367, 1448, 1534, 1625, 1722, 1825, 1933};
1641da177e4SLinus Torvalds 	int wheel, sensitivity;
1651da177e4SLinus Torvalds 	unsigned int mantissa, f1, f2;
1661da177e4SLinus Torvalds 	unsigned short semitones, f1_index, f2_index, f1_power, f2_power;
1671da177e4SLinus Torvalds 	char bend_down = 0;
1681da177e4SLinus Torvalds 	int bend;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	if (!sens)
1711da177e4SLinus Torvalds 		return 1024;
1721da177e4SLinus Torvalds 	wheel = (int) pitchbend - 8192;
1731da177e4SLinus Torvalds 	sensitivity = ((int) sens * wheel) / 128;
1741da177e4SLinus Torvalds 	if (sensitivity < 0) {
1751da177e4SLinus Torvalds 		bend_down = 1;
1761da177e4SLinus Torvalds 		sensitivity = -sensitivity;
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 	semitones = (unsigned int) (sensitivity >> 13);
1791da177e4SLinus Torvalds 	mantissa = sensitivity % 8192;
1801da177e4SLinus Torvalds 	f1_index = semitones % 12;
1811da177e4SLinus Torvalds 	f2_index = (semitones + 1) % 12;
1821da177e4SLinus Torvalds 	f1_power = semitones / 12;
1831da177e4SLinus Torvalds 	f2_power = (semitones + 1) / 12;
1841da177e4SLinus Torvalds 	f1 = log_table[f1_index] << f1_power;
1851da177e4SLinus Torvalds 	f2 = log_table[f2_index] << f2_power;
1861da177e4SLinus Torvalds 	bend = (int) ((((f2 - f1) * mantissa) >> 13) + f1);
1871da177e4SLinus Torvalds 	if (bend_down)
1881da177e4SLinus Torvalds 		bend = 1048576L / bend;
1891da177e4SLinus Torvalds 	return bend;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds unsigned short snd_gf1_compute_freq(unsigned int freq,
1931da177e4SLinus Torvalds 				    unsigned int rate,
1941da177e4SLinus Torvalds 				    unsigned short mix_rate)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds 	unsigned int fc;
1971da177e4SLinus Torvalds 	int scale = 0;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	while (freq >= 4194304L) {
2001da177e4SLinus Torvalds 		scale++;
2011da177e4SLinus Torvalds 		freq >>= 1;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 	fc = (freq << 10) / rate;
2041da177e4SLinus Torvalds 	if (fc > 97391L) {
2051da177e4SLinus Torvalds 		fc = 97391;
20699b359baSTakashi Iwai 		snd_printk(KERN_ERR "patch: (1) fc frequency overflow - %u\n", fc);
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 	fc = (fc * 44100UL) / mix_rate;
2091da177e4SLinus Torvalds 	while (scale--)
2101da177e4SLinus Torvalds 		fc <<= 1;
2111da177e4SLinus Torvalds 	if (fc > 65535L) {
2121da177e4SLinus Torvalds 		fc = 65535;
21399b359baSTakashi Iwai 		snd_printk(KERN_ERR "patch: (2) fc frequency overflow - %u\n", fc);
2141da177e4SLinus Torvalds 	}
2151da177e4SLinus Torvalds 	return (unsigned short) fc;
2161da177e4SLinus Torvalds }
217209ac85dSAdrian Bunk 
218209ac85dSAdrian Bunk #endif  /*  0  */
219