gus_volume.c 5.5 KB

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