Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / sound / isa / gus / gus_volume.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  */
20
21 #include <sound/driver.h>
22 #include <linux/time.h>
23 #include <sound/core.h>
24 #include <sound/gus.h>
25 #define __GUS_TABLES_ALLOC__
26 #include "gus_tables.h"
27
28 EXPORT_SYMBOL(snd_gf1_atten_table); /* for snd-gus-synth module */
29
30 unsigned short snd_gf1_lvol_to_gvol_raw(unsigned int vol)
31 {
32         unsigned short e, m, tmp;
33
34         if (vol > 65535)
35                 vol = 65535;
36         tmp = vol;
37         e = 7;
38         if (tmp < 128) {
39                 while (e > 0 && tmp < (1 << e))
40                         e--;
41         } else {
42                 while (tmp > 255) {
43                         tmp >>= 1;
44                         e++;
45                 }
46         }
47         m = vol - (1 << e);
48         if (m > 0) {
49                 if (e > 8)
50                         m >>= e - 8;
51                 else if (e < 8)
52                         m <<= 8 - e;
53                 m &= 255;
54         }
55         return (e << 8) | m;
56 }
57
58 unsigned int snd_gf1_gvol_to_lvol_raw(unsigned short gf1_vol)
59 {
60         unsigned int rvol;
61         unsigned short e, m;
62
63         if (!gf1_vol)
64                 return 0;
65         e = gf1_vol >> 8;
66         m = (unsigned char) gf1_vol;
67         rvol = 1 << e;
68         if (e > 8)
69                 return rvol | (m << (e - 8));
70         return rvol | (m >> (8 - e));
71 }
72
73 unsigned int snd_gf1_calc_ramp_rate(snd_gus_card_t * gus,
74                                     unsigned short start,
75                                     unsigned short end,
76                                     unsigned int us)
77 {
78         static unsigned char vol_rates[19] =
79         {
80                 23, 24, 26, 28, 29, 31, 32, 34,
81                 36, 37, 39, 40, 42, 44, 45, 47,
82                 49, 50, 52
83         };
84         unsigned short range, increment, value, i;
85
86         start >>= 4;
87         end >>= 4;
88         if (start < end)
89                 us /= end - start;
90         else
91                 us /= start - end;
92         range = 4;
93         value = gus->gf1.enh_mode ?
94             vol_rates[0] :
95             vol_rates[gus->gf1.active_voices - 14];
96         for (i = 0; i < 3; i++) {
97                 if (us < value) {
98                         range = i;
99                         break;
100                 } else
101                         value <<= 3;
102         }
103         if (range == 4) {
104                 range = 3;
105                 increment = 1;
106         } else
107                 increment = (value + (value >> 1)) / us;
108         return (range << 6) | (increment & 0x3f);
109 }
110
111 unsigned short snd_gf1_translate_freq(snd_gus_card_t * gus, unsigned int freq16)
112 {
113         freq16 >>= 3;
114         if (freq16 < 50)
115                 freq16 = 50;
116         if (freq16 & 0xf8000000) {
117                 freq16 = ~0xf8000000;
118                 snd_printk("snd_gf1_translate_freq: overflow - freq = 0x%x\n", freq16);
119         }
120         return ((freq16 << 9) + (gus->gf1.playback_freq >> 1)) / gus->gf1.playback_freq;
121 }
122
123 short snd_gf1_compute_vibrato(short cents, unsigned short fc_register)
124 {
125         static short vibrato_table[] =
126         {
127                 0, 0, 32, 592, 61, 1175, 93, 1808,
128                 124, 2433, 152, 3007, 182, 3632, 213, 4290,
129                 241, 4834, 255, 5200
130         };
131
132         long depth;
133         short *vi1, *vi2, pcents, v1;
134
135         pcents = cents < 0 ? -cents : cents;
136         for (vi1 = vibrato_table, vi2 = vi1 + 2; pcents > *vi2; vi1 = vi2, vi2 += 2);
137         v1 = *(vi1 + 1);
138         /* The FC table above is a list of pairs. The first number in the pair     */
139         /* is the cents index from 0-255 cents, and the second number in the       */
140         /* pair is the FC adjustment needed to change the pitch by the indexed     */
141         /* number of cents. The table was created for an FC of 32768.              */
142         /* The following expression does a linear interpolation against the        */
143         /* approximated log curve in the table above, and then scales the number   */
144         /* by the FC before the LFO. This calculation also adjusts the output      */
145         /* value to produce the appropriate depth for the hardware. The depth      */
146         /* is 2 * desired FC + 1.                                                  */
147         depth = (((int) (*(vi2 + 1) - *vi1) * (pcents - *vi1) / (*vi2 - *vi1)) + v1) * fc_register >> 14;
148         if (depth)
149                 depth++;
150         if (depth > 255)
151                 depth = 255;
152         return cents < 0 ? -(short) depth : (short) depth;
153 }
154
155 unsigned short snd_gf1_compute_pitchbend(unsigned short pitchbend, unsigned short sens)
156 {
157         static long log_table[] = {1024, 1085, 1149, 1218, 1290, 1367, 1448, 1534, 1625, 1722, 1825, 1933};
158         int wheel, sensitivity;
159         unsigned int mantissa, f1, f2;
160         unsigned short semitones, f1_index, f2_index, f1_power, f2_power;
161         char bend_down = 0;
162         int bend;
163
164         if (!sens)
165                 return 1024;
166         wheel = (int) pitchbend - 8192;
167         sensitivity = ((int) sens * wheel) / 128;
168         if (sensitivity < 0) {
169                 bend_down = 1;
170                 sensitivity = -sensitivity;
171         }
172         semitones = (unsigned int) (sensitivity >> 13);
173         mantissa = sensitivity % 8192;
174         f1_index = semitones % 12;
175         f2_index = (semitones + 1) % 12;
176         f1_power = semitones / 12;
177         f2_power = (semitones + 1) / 12;
178         f1 = log_table[f1_index] << f1_power;
179         f2 = log_table[f2_index] << f2_power;
180         bend = (int) ((((f2 - f1) * mantissa) >> 13) + f1);
181         if (bend_down)
182                 bend = 1048576L / bend;
183         return bend;
184 }
185
186 unsigned short snd_gf1_compute_freq(unsigned int freq,
187                                     unsigned int rate,
188                                     unsigned short mix_rate)
189 {
190         unsigned int fc;
191         int scale = 0;
192
193         while (freq >= 4194304L) {
194                 scale++;
195                 freq >>= 1;
196         }
197         fc = (freq << 10) / rate;
198         if (fc > 97391L) {
199                 fc = 97391;
200                 snd_printk("patch: (1) fc frequency overflow - %u\n", fc);
201         }
202         fc = (fc * 44100UL) / mix_rate;
203         while (scale--)
204                 fc <<= 1;
205         if (fc > 65535L) {
206                 fc = 65535;
207                 snd_printk("patch: (2) fc frequency overflow - %u\n", fc);
208         }
209         return (unsigned short) fc;
210 }