gus_volume.c 4.9 KB

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