Import 2.3.18pre1
[davej-history.git] / drivers / sound / gus_vol.c
blob3e5752d8f8183172aae2528619881adeb4e5f38e
2 /*
3 * gus_vol.c - Compute volume for GUS.
4 */
5 /*
6 * Copyright (C) by Hannu Savolainen 1993-1997
8 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
9 * Version 2 (June 1991). See the "COPYING" file distributed with this software
10 * for more info.
12 #include <linux/config.h>
14 #include "sound_config.h"
15 #ifdef CONFIG_GUS
16 #include "gus_linearvol.h"
18 #define GUS_VOLUME gus_wave_volume
21 extern int gus_wave_volume;
24 * Calculate gus volume from note velocity, main volume, expression, and
25 * intrinsic patch volume given in patch library. Expression is multiplied
26 * in, so it emphasizes differences in note velocity, while main volume is
27 * added in -- I don't know whether this is right, but it seems reasonable to
28 * me. (In the previous stage, main volume controller messages were changed
29 * to expression controller messages, if they were found to be used for
30 * dynamic volume adjustments, so here, main volume can be assumed to be
31 * constant throughout a song.)
33 * Intrinsic patch volume is added in, but if over 64 is also multiplied in, so
34 * we can give a big boost to very weak voices like nylon guitar and the
35 * basses. The normal value is 64. Strings are assigned lower values.
38 unsigned short gus_adagio_vol(int vel, int mainv, int xpn, int voicev)
40 int i, m, n, x;
44 * A voice volume of 64 is considered neutral, so adjust the main volume if
45 * something other than this neutral value was assigned in the patch
46 * library.
48 x = 256 + 6 * (voicev - 64);
51 * Boost expression by voice volume above neutral.
54 if (voicev > 65)
55 xpn += voicev - 64;
56 xpn += (voicev - 64) / 2;
59 * Combine multiplicative and level components.
61 x = vel * xpn * 6 + (voicev / 4) * x;
63 #ifdef GUS_VOLUME
65 * Further adjustment by installation-specific master volume control
66 * (default 60).
68 x = (x * GUS_VOLUME * GUS_VOLUME) / 10000;
69 #endif
71 #ifdef GUS_USE_CHN_MAIN_VOLUME
73 * Experimental support for the channel main volume
76 mainv = (mainv / 2) + 64; /* Scale to 64 to 127 */
77 x = (x * mainv * mainv) / 16384;
78 #endif
80 if (x < 2)
81 return (0);
82 else if (x >= 65535)
83 return ((15 << 8) | 255);
86 * Convert to GUS's logarithmic form with 4 bit exponent i and 8 bit
87 * mantissa m.
90 n = x;
91 i = 7;
92 if (n < 128)
94 while (i > 0 && n < (1 << i))
95 i--;
97 else
99 while (n > 255)
101 n >>= 1;
102 i++;
106 * Mantissa is part of linear volume not expressed in exponent. (This is
107 * not quite like real logs -- I wonder if it's right.)
109 m = x - (1 << i);
112 * Adjust mantissa to 8 bits.
114 if (m > 0)
116 if (i > 8)
117 m >>= i - 8;
118 else if (i < 8)
119 m <<= 8 - i;
121 return ((i << 8) + m);
125 * Volume-values are interpreted as linear values. Volume is based on the
126 * value supplied with SEQ_START_NOTE(), channel main volume (if compiled in)
127 * and the volume set by the mixer-device (default 60%).
130 unsigned short gus_linear_vol(int vol, int mainvol)
132 int mixer_mainvol;
134 if (vol <= 0)
135 vol = 0;
136 else if (vol >= 127)
137 vol = 127;
139 #ifdef GUS_VOLUME
140 mixer_mainvol = GUS_VOLUME;
141 #else
142 mixer_mainvol = 100;
143 #endif
145 #ifdef GUS_USE_CHN_MAIN_VOLUME
146 if (mainvol <= 0)
147 mainvol = 0;
148 else if (mainvol >= 127)
149 mainvol = 127;
150 #else
151 mainvol = 127;
152 #endif
153 return gus_linearvol[(((vol * mainvol) / 127) * mixer_mainvol) / 100];
156 #endif