1 /* 2 * gus_vol.c - Compute volume for GUS. 3 */ 4 /* 5 * Copyright by Hannu Savolainen 1993-1996 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are 9 * met: 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 2. 11 * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 #include <linux/config.h> 28 29 #include "sound_config.h" 30 #ifdef CONFIG_GUS 31 #include "gus_linearvol.h" 32 33 #define GUS_VOLUME gus_wave_volume 34 35 36 extern int gus_wave_volume; 37 38 /* 39 * Calculate gus volume from note velocity, main volume, expression, and 40 * intrinsic patch volume given in patch library. Expression is multiplied 41 * in, so it emphasizes differences in note velocity, while main volume is 42 * added in -- I don't know whether this is right, but it seems reasonable to 43 * me. (In the previous stage, main volume controller messages were changed 44 * to expression controller messages, if they were found to be used for 45 * dynamic volume adjustments, so here, main volume can be assumed to be 46 * constant throughout a song.) 47 * 48 * Intrinsic patch volume is added in, but if over 64 is also multiplied in, so 49 * we can give a big boost to very weak voices like nylon guitar and the 50 * basses. The normal value is 64. Strings are assigned lower values. 51 */ 52 unsigned short 53 gus_adagio_vol (int vel, int mainv, int xpn, int voicev) /* */ 54 { 55 int i, m, n, x; 56 57 58 /* 59 * A voice volume of 64 is considered neutral, so adjust the main volume if 60 * something other than this neutral value was assigned in the patch 61 * library. 62 */ 63 x = 256 + 6 * (voicev - 64); 64 65 /* 66 * Boost expression by voice volume above neutral. 67 */ 68 if (voicev > 65) 69 xpn += voicev - 64; 70 xpn += (voicev - 64) / 2; 71 72 /* 73 * Combine multiplicative and level components. 74 */ 75 x = vel * xpn * 6 + (voicev / 4) * x; 76 77 #ifdef GUS_VOLUME 78 /* 79 * Further adjustment by installation-specific master volume control 80 * (default 60). 81 */ 82 x = (x * GUS_VOLUME * GUS_VOLUME) / 10000; 83 #endif 84 85 #ifdef GUS_USE_CHN_MAIN_VOLUME 86 /* 87 * Experimental support for the channel main volume 88 */ 89 90 mainv = (mainv / 2) + 64; /* Scale to 64 to 127 */ 91 x = (x * mainv * mainv) / 16384; 92 #endif 93 94 if (x < 2) 95 return (0); 96 else if (x >= 65535) 97 return ((15 << 8) | 255); 98 99 /* 100 * Convert to gus's logarithmic form with 4 bit exponent i and 8 bit 101 * mantissa m. 102 */ 103 n = x; 104 i = 7; 105 if (n < 128) 106 { 107 while (i > 0 && n < (1 << i)) 108 i--; 109 } 110 else 111 while (n > 255) 112 { 113 n >>= 1; 114 i++; 115 } 116 /* 117 * Mantissa is part of linear volume not expressed in exponent. (This is 118 * not quite like real logs -- I wonder if it's right.) 119 */ 120 m = x - (1 << i); 121 122 /* 123 * Adjust mantissa to 8 bits. 124 */ 125 if (m > 0) 126 { 127 if (i > 8) 128 m >>= i - 8; 129 else if (i < 8) 130 m <<= 8 - i; 131 } 132 133 return ((i << 8) + m); 134 } 135 136 /* 137 * Volume-values are interpreted as linear values. Volume is based on the 138 * value supplied with SEQ_START_NOTE(), channel main volume (if compiled in) 139 * and the volume set by the mixer-device (default 60%). 140 */ 141 142 unsigned short 143 gus_linear_vol (int vol, int mainvol) /* */ 144 { 145 int mixer_mainvol; 146 147 if (vol <= 0) 148 vol = 0; 149 else if (vol >= 127) 150 vol = 127; 151 152 #ifdef GUS_VOLUME 153 mixer_mainvol = GUS_VOLUME; 154 #else 155 mixer_mainvol = 100; 156 #endif 157 158 #ifdef GUS_USE_CHN_MAIN_VOLUME 159 if (mainvol <= 0) 160 mainvol = 0; 161 else if (mainvol >= 127) 162 mainvol = 127; 163 #else 164 mainvol = 127; 165 #endif 166 167 return gus_linearvol[(((vol * mainvol) / 127) * mixer_mainvol) / 100]; 168 } 169 170 #endif