Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
0004  */
0005 
0006 #include <linux/time.h>
0007 #include <linux/export.h>
0008 #include <sound/core.h>
0009 #include <sound/gus.h>
0010 #define __GUS_TABLES_ALLOC__
0011 #include "gus_tables.h"
0012 
0013 EXPORT_SYMBOL(snd_gf1_atten_table); /* for snd-gus-synth module */
0014 
0015 unsigned short snd_gf1_lvol_to_gvol_raw(unsigned int vol)
0016 {
0017     unsigned short e, m, tmp;
0018 
0019     if (vol > 65535)
0020         vol = 65535;
0021     tmp = vol;
0022     e = 7;
0023     if (tmp < 128) {
0024         while (e > 0 && tmp < (1 << e))
0025             e--;
0026     } else {
0027         while (tmp > 255) {
0028             tmp >>= 1;
0029             e++;
0030         }
0031     }
0032     m = vol - (1 << e);
0033     if (m > 0) {
0034         if (e > 8)
0035             m >>= e - 8;
0036         else if (e < 8)
0037             m <<= 8 - e;
0038         m &= 255;
0039     }
0040     return (e << 8) | m;
0041 }
0042 
0043 #if 0
0044 
0045 unsigned int snd_gf1_gvol_to_lvol_raw(unsigned short gf1_vol)
0046 {
0047     unsigned int rvol;
0048     unsigned short e, m;
0049 
0050     if (!gf1_vol)
0051         return 0;
0052     e = gf1_vol >> 8;
0053     m = (unsigned char) gf1_vol;
0054     rvol = 1 << e;
0055     if (e > 8)
0056         return rvol | (m << (e - 8));
0057     return rvol | (m >> (8 - e));
0058 }
0059 
0060 unsigned int snd_gf1_calc_ramp_rate(struct snd_gus_card * gus,
0061                     unsigned short start,
0062                     unsigned short end,
0063                     unsigned int us)
0064 {
0065     static const unsigned char vol_rates[19] =
0066     {
0067         23, 24, 26, 28, 29, 31, 32, 34,
0068         36, 37, 39, 40, 42, 44, 45, 47,
0069         49, 50, 52
0070     };
0071     unsigned short range, increment, value, i;
0072 
0073     start >>= 4;
0074     end >>= 4;
0075     if (start < end)
0076         us /= end - start;
0077     else
0078         us /= start - end;
0079     range = 4;
0080     value = gus->gf1.enh_mode ?
0081         vol_rates[0] :
0082         vol_rates[gus->gf1.active_voices - 14];
0083     for (i = 0; i < 3; i++) {
0084         if (us < value) {
0085             range = i;
0086             break;
0087         } else
0088             value <<= 3;
0089     }
0090     if (range == 4) {
0091         range = 3;
0092         increment = 1;
0093     } else
0094         increment = (value + (value >> 1)) / us;
0095     return (range << 6) | (increment & 0x3f);
0096 }
0097 
0098 #endif  /*  0  */
0099 
0100 unsigned short snd_gf1_translate_freq(struct snd_gus_card * gus, unsigned int freq16)
0101 {
0102     freq16 >>= 3;
0103     if (freq16 < 50)
0104         freq16 = 50;
0105     if (freq16 & 0xf8000000) {
0106         freq16 = ~0xf8000000;
0107         snd_printk(KERN_ERR "snd_gf1_translate_freq: overflow - freq = 0x%x\n", freq16);
0108     }
0109     return ((freq16 << 9) + (gus->gf1.playback_freq >> 1)) / gus->gf1.playback_freq;
0110 }
0111 
0112 #if 0
0113 
0114 short snd_gf1_compute_vibrato(short cents, unsigned short fc_register)
0115 {
0116     static const short vibrato_table[] =
0117     {
0118         0, 0, 32, 592, 61, 1175, 93, 1808,
0119         124, 2433, 152, 3007, 182, 3632, 213, 4290,
0120         241, 4834, 255, 5200
0121     };
0122 
0123     long depth;
0124     const short *vi1, *vi2;
0125     short pcents, v1;
0126 
0127     pcents = cents < 0 ? -cents : cents;
0128     for (vi1 = vibrato_table, vi2 = vi1 + 2; pcents > *vi2; vi1 = vi2, vi2 += 2);
0129     v1 = *(vi1 + 1);
0130     /* The FC table above is a list of pairs. The first number in the pair     */
0131     /* is the cents index from 0-255 cents, and the second number in the       */
0132     /* pair is the FC adjustment needed to change the pitch by the indexed     */
0133     /* number of cents. The table was created for an FC of 32768.              */
0134     /* The following expression does a linear interpolation against the        */
0135     /* approximated log curve in the table above, and then scales the number   */
0136     /* by the FC before the LFO. This calculation also adjusts the output      */
0137     /* value to produce the appropriate depth for the hardware. The depth      */
0138     /* is 2 * desired FC + 1.                                                  */
0139     depth = (((int) (*(vi2 + 1) - *vi1) * (pcents - *vi1) / (*vi2 - *vi1)) + v1) * fc_register >> 14;
0140     if (depth)
0141         depth++;
0142     if (depth > 255)
0143         depth = 255;
0144     return cents < 0 ? -(short) depth : (short) depth;
0145 }
0146 
0147 unsigned short snd_gf1_compute_pitchbend(unsigned short pitchbend, unsigned short sens)
0148 {
0149     static const long log_table[] = {1024, 1085, 1149, 1218, 1290, 1367, 1448, 1534, 1625, 1722, 1825, 1933};
0150     int wheel, sensitivity;
0151     unsigned int mantissa, f1, f2;
0152     unsigned short semitones, f1_index, f2_index, f1_power, f2_power;
0153     char bend_down = 0;
0154     int bend;
0155 
0156     if (!sens)
0157         return 1024;
0158     wheel = (int) pitchbend - 8192;
0159     sensitivity = ((int) sens * wheel) / 128;
0160     if (sensitivity < 0) {
0161         bend_down = 1;
0162         sensitivity = -sensitivity;
0163     }
0164     semitones = (unsigned int) (sensitivity >> 13);
0165     mantissa = sensitivity % 8192;
0166     f1_index = semitones % 12;
0167     f2_index = (semitones + 1) % 12;
0168     f1_power = semitones / 12;
0169     f2_power = (semitones + 1) / 12;
0170     f1 = log_table[f1_index] << f1_power;
0171     f2 = log_table[f2_index] << f2_power;
0172     bend = (int) ((((f2 - f1) * mantissa) >> 13) + f1);
0173     if (bend_down)
0174         bend = 1048576L / bend;
0175     return bend;
0176 }
0177 
0178 unsigned short snd_gf1_compute_freq(unsigned int freq,
0179                     unsigned int rate,
0180                     unsigned short mix_rate)
0181 {
0182     unsigned int fc;
0183     int scale = 0;
0184 
0185     while (freq >= 4194304L) {
0186         scale++;
0187         freq >>= 1;
0188     }
0189     fc = (freq << 10) / rate;
0190     if (fc > 97391L) {
0191         fc = 97391;
0192         snd_printk(KERN_ERR "patch: (1) fc frequency overflow - %u\n", fc);
0193     }
0194     fc = (fc * 44100UL) / mix_rate;
0195     while (scale--)
0196         fc <<= 1;
0197     if (fc > 65535L) {
0198         fc = 65535;
0199         snd_printk(KERN_ERR "patch: (2) fc frequency overflow - %u\n", fc);
0200     }
0201     return (unsigned short) fc;
0202 }
0203 
0204 #endif  /*  0  */