11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 3c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds #include <linux/time.h> 7d81a6d71SPaul Gortmaker #include <linux/export.h> 81da177e4SLinus Torvalds #include <sound/core.h> 91da177e4SLinus Torvalds #include <sound/gus.h> 101da177e4SLinus Torvalds #define __GUS_TABLES_ALLOC__ 111da177e4SLinus Torvalds #include "gus_tables.h" 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds EXPORT_SYMBOL(snd_gf1_atten_table); /* for snd-gus-synth module */ 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds unsigned short snd_gf1_lvol_to_gvol_raw(unsigned int vol) 161da177e4SLinus Torvalds { 171da177e4SLinus Torvalds unsigned short e, m, tmp; 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds if (vol > 65535) 201da177e4SLinus Torvalds vol = 65535; 211da177e4SLinus Torvalds tmp = vol; 221da177e4SLinus Torvalds e = 7; 231da177e4SLinus Torvalds if (tmp < 128) { 241da177e4SLinus Torvalds while (e > 0 && tmp < (1 << e)) 251da177e4SLinus Torvalds e--; 261da177e4SLinus Torvalds } else { 271da177e4SLinus Torvalds while (tmp > 255) { 281da177e4SLinus Torvalds tmp >>= 1; 291da177e4SLinus Torvalds e++; 301da177e4SLinus Torvalds } 311da177e4SLinus Torvalds } 321da177e4SLinus Torvalds m = vol - (1 << e); 331da177e4SLinus Torvalds if (m > 0) { 341da177e4SLinus Torvalds if (e > 8) 351da177e4SLinus Torvalds m >>= e - 8; 361da177e4SLinus Torvalds else if (e < 8) 371da177e4SLinus Torvalds m <<= 8 - e; 381da177e4SLinus Torvalds m &= 255; 391da177e4SLinus Torvalds } 401da177e4SLinus Torvalds return (e << 8) | m; 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds 43209ac85dSAdrian Bunk #if 0 44209ac85dSAdrian Bunk 451da177e4SLinus Torvalds unsigned int snd_gf1_gvol_to_lvol_raw(unsigned short gf1_vol) 461da177e4SLinus Torvalds { 471da177e4SLinus Torvalds unsigned int rvol; 481da177e4SLinus Torvalds unsigned short e, m; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds if (!gf1_vol) 511da177e4SLinus Torvalds return 0; 521da177e4SLinus Torvalds e = gf1_vol >> 8; 531da177e4SLinus Torvalds m = (unsigned char) gf1_vol; 541da177e4SLinus Torvalds rvol = 1 << e; 551da177e4SLinus Torvalds if (e > 8) 561da177e4SLinus Torvalds return rvol | (m << (e - 8)); 571da177e4SLinus Torvalds return rvol | (m >> (8 - e)); 581da177e4SLinus Torvalds } 591da177e4SLinus Torvalds 605e2da206STakashi Iwai unsigned int snd_gf1_calc_ramp_rate(struct snd_gus_card * gus, 611da177e4SLinus Torvalds unsigned short start, 621da177e4SLinus Torvalds unsigned short end, 631da177e4SLinus Torvalds unsigned int us) 641da177e4SLinus Torvalds { 6515a1af95STakashi Iwai static const unsigned char vol_rates[19] = 661da177e4SLinus Torvalds { 671da177e4SLinus Torvalds 23, 24, 26, 28, 29, 31, 32, 34, 681da177e4SLinus Torvalds 36, 37, 39, 40, 42, 44, 45, 47, 691da177e4SLinus Torvalds 49, 50, 52 701da177e4SLinus Torvalds }; 711da177e4SLinus Torvalds unsigned short range, increment, value, i; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds start >>= 4; 741da177e4SLinus Torvalds end >>= 4; 751da177e4SLinus Torvalds if (start < end) 761da177e4SLinus Torvalds us /= end - start; 771da177e4SLinus Torvalds else 781da177e4SLinus Torvalds us /= start - end; 791da177e4SLinus Torvalds range = 4; 801da177e4SLinus Torvalds value = gus->gf1.enh_mode ? 811da177e4SLinus Torvalds vol_rates[0] : 821da177e4SLinus Torvalds vol_rates[gus->gf1.active_voices - 14]; 831da177e4SLinus Torvalds for (i = 0; i < 3; i++) { 841da177e4SLinus Torvalds if (us < value) { 851da177e4SLinus Torvalds range = i; 861da177e4SLinus Torvalds break; 871da177e4SLinus Torvalds } else 881da177e4SLinus Torvalds value <<= 3; 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds if (range == 4) { 911da177e4SLinus Torvalds range = 3; 921da177e4SLinus Torvalds increment = 1; 931da177e4SLinus Torvalds } else 941da177e4SLinus Torvalds increment = (value + (value >> 1)) / us; 951da177e4SLinus Torvalds return (range << 6) | (increment & 0x3f); 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds 98209ac85dSAdrian Bunk #endif /* 0 */ 99209ac85dSAdrian Bunk 1005e2da206STakashi Iwai unsigned short snd_gf1_translate_freq(struct snd_gus_card * gus, unsigned int freq16) 1011da177e4SLinus Torvalds { 1021da177e4SLinus Torvalds freq16 >>= 3; 1031da177e4SLinus Torvalds if (freq16 < 50) 1041da177e4SLinus Torvalds freq16 = 50; 1051da177e4SLinus Torvalds if (freq16 & 0xf8000000) { 1061da177e4SLinus Torvalds freq16 = ~0xf8000000; 107*a6676811STakashi Iwai dev_err(gus->card->dev, "%s: overflow - freq = 0x%x\n", 108*a6676811STakashi Iwai __func__, freq16); 1091da177e4SLinus Torvalds } 1101da177e4SLinus Torvalds return ((freq16 << 9) + (gus->gf1.playback_freq >> 1)) / gus->gf1.playback_freq; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 113209ac85dSAdrian Bunk #if 0 114209ac85dSAdrian Bunk 1151da177e4SLinus Torvalds short snd_gf1_compute_vibrato(short cents, unsigned short fc_register) 1161da177e4SLinus Torvalds { 11715a1af95STakashi Iwai static const short vibrato_table[] = 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds 0, 0, 32, 592, 61, 1175, 93, 1808, 1201da177e4SLinus Torvalds 124, 2433, 152, 3007, 182, 3632, 213, 4290, 1211da177e4SLinus Torvalds 241, 4834, 255, 5200 1221da177e4SLinus Torvalds }; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds long depth; 12515a1af95STakashi Iwai const short *vi1, *vi2; 12615a1af95STakashi Iwai short pcents, v1; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds pcents = cents < 0 ? -cents : cents; 1291da177e4SLinus Torvalds for (vi1 = vibrato_table, vi2 = vi1 + 2; pcents > *vi2; vi1 = vi2, vi2 += 2); 1301da177e4SLinus Torvalds v1 = *(vi1 + 1); 1311da177e4SLinus Torvalds /* The FC table above is a list of pairs. The first number in the pair */ 1321da177e4SLinus Torvalds /* is the cents index from 0-255 cents, and the second number in the */ 1331da177e4SLinus Torvalds /* pair is the FC adjustment needed to change the pitch by the indexed */ 1341da177e4SLinus Torvalds /* number of cents. The table was created for an FC of 32768. */ 1351da177e4SLinus Torvalds /* The following expression does a linear interpolation against the */ 1361da177e4SLinus Torvalds /* approximated log curve in the table above, and then scales the number */ 1371da177e4SLinus Torvalds /* by the FC before the LFO. This calculation also adjusts the output */ 1381da177e4SLinus Torvalds /* value to produce the appropriate depth for the hardware. The depth */ 1391da177e4SLinus Torvalds /* is 2 * desired FC + 1. */ 1401da177e4SLinus Torvalds depth = (((int) (*(vi2 + 1) - *vi1) * (pcents - *vi1) / (*vi2 - *vi1)) + v1) * fc_register >> 14; 1411da177e4SLinus Torvalds if (depth) 1421da177e4SLinus Torvalds depth++; 1431da177e4SLinus Torvalds if (depth > 255) 1441da177e4SLinus Torvalds depth = 255; 1451da177e4SLinus Torvalds return cents < 0 ? -(short) depth : (short) depth; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds unsigned short snd_gf1_compute_pitchbend(unsigned short pitchbend, unsigned short sens) 1491da177e4SLinus Torvalds { 15015a1af95STakashi Iwai static const long log_table[] = {1024, 1085, 1149, 1218, 1290, 1367, 1448, 1534, 1625, 1722, 1825, 1933}; 1511da177e4SLinus Torvalds int wheel, sensitivity; 1521da177e4SLinus Torvalds unsigned int mantissa, f1, f2; 1531da177e4SLinus Torvalds unsigned short semitones, f1_index, f2_index, f1_power, f2_power; 1541da177e4SLinus Torvalds char bend_down = 0; 1551da177e4SLinus Torvalds int bend; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds if (!sens) 1581da177e4SLinus Torvalds return 1024; 1591da177e4SLinus Torvalds wheel = (int) pitchbend - 8192; 1601da177e4SLinus Torvalds sensitivity = ((int) sens * wheel) / 128; 1611da177e4SLinus Torvalds if (sensitivity < 0) { 1621da177e4SLinus Torvalds bend_down = 1; 1631da177e4SLinus Torvalds sensitivity = -sensitivity; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds semitones = (unsigned int) (sensitivity >> 13); 1661da177e4SLinus Torvalds mantissa = sensitivity % 8192; 1671da177e4SLinus Torvalds f1_index = semitones % 12; 1681da177e4SLinus Torvalds f2_index = (semitones + 1) % 12; 1691da177e4SLinus Torvalds f1_power = semitones / 12; 1701da177e4SLinus Torvalds f2_power = (semitones + 1) / 12; 1711da177e4SLinus Torvalds f1 = log_table[f1_index] << f1_power; 1721da177e4SLinus Torvalds f2 = log_table[f2_index] << f2_power; 1731da177e4SLinus Torvalds bend = (int) ((((f2 - f1) * mantissa) >> 13) + f1); 1741da177e4SLinus Torvalds if (bend_down) 1751da177e4SLinus Torvalds bend = 1048576L / bend; 1761da177e4SLinus Torvalds return bend; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds unsigned short snd_gf1_compute_freq(unsigned int freq, 1801da177e4SLinus Torvalds unsigned int rate, 1811da177e4SLinus Torvalds unsigned short mix_rate) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds unsigned int fc; 1841da177e4SLinus Torvalds int scale = 0; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds while (freq >= 4194304L) { 1871da177e4SLinus Torvalds scale++; 1881da177e4SLinus Torvalds freq >>= 1; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds fc = (freq << 10) / rate; 1911da177e4SLinus Torvalds if (fc > 97391L) { 1921da177e4SLinus Torvalds fc = 97391; 193*a6676811STakashi Iwai pr_err("patch: (1) fc frequency overflow - %u\n", fc); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds fc = (fc * 44100UL) / mix_rate; 1961da177e4SLinus Torvalds while (scale--) 1971da177e4SLinus Torvalds fc <<= 1; 1981da177e4SLinus Torvalds if (fc > 65535L) { 1991da177e4SLinus Torvalds fc = 65535; 200*a6676811STakashi Iwai pr_err("patch: (2) fc frequency overflow - %u\n", fc); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds return (unsigned short) fc; 2031da177e4SLinus Torvalds } 204209ac85dSAdrian Bunk 205209ac85dSAdrian Bunk #endif /* 0 */ 206