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