1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 #include <linux/time.h>
7 #include <linux/export.h>
8 #include <sound/core.h>
10 #define __GUS_TABLES_ALLOC__
11 #include "gus_tables.h"
13 EXPORT_SYMBOL(snd_gf1_atten_table); /* for snd-gus-synth module */
15 unsigned short snd_gf1_lvol_to_gvol_raw(unsigned int vol)
17 unsigned short e, m, tmp;
24 while (e > 0 && tmp < (1 << e))
45 unsigned int snd_gf1_gvol_to_lvol_raw(unsigned short gf1_vol)
53 m = (unsigned char) gf1_vol;
56 return rvol | (m << (e - 8));
57 return rvol | (m >> (8 - e));
60 unsigned int snd_gf1_calc_ramp_rate(struct snd_gus_card * gus,
65 static const unsigned char vol_rates[19] =
67 23, 24, 26, 28, 29, 31, 32, 34,
68 36, 37, 39, 40, 42, 44, 45, 47,
71 unsigned short range, increment, value, i;
80 value = gus->gf1.enh_mode ?
82 vol_rates[gus->gf1.active_voices - 14];
83 for (i = 0; i < 3; i++) {
94 increment = (value + (value >> 1)) / us;
95 return (range << 6) | (increment & 0x3f);
100 unsigned short snd_gf1_translate_freq(struct snd_gus_card * gus, unsigned int freq16)
105 if (freq16 & 0xf8000000) {
106 freq16 = ~0xf8000000;
107 snd_printk(KERN_ERR "snd_gf1_translate_freq: overflow - freq = 0x%x\n", freq16);
109 return ((freq16 << 9) + (gus->gf1.playback_freq >> 1)) / gus->gf1.playback_freq;
114 short snd_gf1_compute_vibrato(short cents, unsigned short fc_register)
116 static const short vibrato_table[] =
118 0, 0, 32, 592, 61, 1175, 93, 1808,
119 124, 2433, 152, 3007, 182, 3632, 213, 4290,
124 const short *vi1, *vi2;
127 pcents = cents < 0 ? -cents : cents;
128 for (vi1 = vibrato_table, vi2 = vi1 + 2; pcents > *vi2; vi1 = vi2, vi2 += 2);
130 /* The FC table above is a list of pairs. The first number in the pair */
131 /* is the cents index from 0-255 cents, and the second number in the */
132 /* pair is the FC adjustment needed to change the pitch by the indexed */
133 /* number of cents. The table was created for an FC of 32768. */
134 /* The following expression does a linear interpolation against the */
135 /* approximated log curve in the table above, and then scales the number */
136 /* by the FC before the LFO. This calculation also adjusts the output */
137 /* value to produce the appropriate depth for the hardware. The depth */
138 /* is 2 * desired FC + 1. */
139 depth = (((int) (*(vi2 + 1) - *vi1) * (pcents - *vi1) / (*vi2 - *vi1)) + v1) * fc_register >> 14;
144 return cents < 0 ? -(short) depth : (short) depth;
147 unsigned short snd_gf1_compute_pitchbend(unsigned short pitchbend, unsigned short sens)
149 static const long log_table[] = {1024, 1085, 1149, 1218, 1290, 1367, 1448, 1534, 1625, 1722, 1825, 1933};
150 int wheel, sensitivity;
151 unsigned int mantissa, f1, f2;
152 unsigned short semitones, f1_index, f2_index, f1_power, f2_power;
158 wheel = (int) pitchbend - 8192;
159 sensitivity = ((int) sens * wheel) / 128;
160 if (sensitivity < 0) {
162 sensitivity = -sensitivity;
164 semitones = (unsigned int) (sensitivity >> 13);
165 mantissa = sensitivity % 8192;
166 f1_index = semitones % 12;
167 f2_index = (semitones + 1) % 12;
168 f1_power = semitones / 12;
169 f2_power = (semitones + 1) / 12;
170 f1 = log_table[f1_index] << f1_power;
171 f2 = log_table[f2_index] << f2_power;
172 bend = (int) ((((f2 - f1) * mantissa) >> 13) + f1);
174 bend = 1048576L / bend;
178 unsigned short snd_gf1_compute_freq(unsigned int freq,
180 unsigned short mix_rate)
185 while (freq >= 4194304L) {
189 fc = (freq << 10) / rate;
192 snd_printk(KERN_ERR "patch: (1) fc frequency overflow - %u\n", fc);
194 fc = (fc * 44100UL) / mix_rate;
199 snd_printk(KERN_ERR "patch: (2) fc frequency overflow - %u\n", fc);
201 return (unsigned short) fc;