PITCH.C 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /**********************************************************************
  16. module: PITCH.C
  17. author: James R. Dose
  18. date: June 14, 1993
  19. Routines for pitch scaling.
  20. (c) Copyright 1993 James R. Dose. All Rights Reserved.
  21. **********************************************************************/
  22. #include <stdlib.h>
  23. //#include <math.h>
  24. #include "dpmi.h"
  25. #include "standard.h"
  26. #include "pitch.h"
  27. #define MAXDETUNE 25
  28. static unsigned long PitchTable[ 12 ][ MAXDETUNE ] =
  29. {
  30. { 0x10000, 0x10097, 0x1012f, 0x101c7, 0x10260, 0x102f9, 0x10392, 0x1042c,
  31. 0x104c6, 0x10561, 0x105fb, 0x10696, 0x10732, 0x107ce, 0x1086a, 0x10907,
  32. 0x109a4, 0x10a41, 0x10adf, 0x10b7d, 0x10c1b, 0x10cba, 0x10d59, 0x10df8,
  33. 0x10e98 },
  34. { 0x10f38, 0x10fd9, 0x1107a, 0x1111b, 0x111bd, 0x1125f, 0x11302, 0x113a5,
  35. 0x11448, 0x114eb, 0x1158f, 0x11634, 0x116d8, 0x1177e, 0x11823, 0x118c9,
  36. 0x1196f, 0x11a16, 0x11abd, 0x11b64, 0x11c0c, 0x11cb4, 0x11d5d, 0x11e06,
  37. 0x11eaf },
  38. { 0x11f59, 0x12003, 0x120ae, 0x12159, 0x12204, 0x122b0, 0x1235c, 0x12409,
  39. 0x124b6, 0x12563, 0x12611, 0x126bf, 0x1276d, 0x1281c, 0x128cc, 0x1297b,
  40. 0x12a2b, 0x12adc, 0x12b8d, 0x12c3e, 0x12cf0, 0x12da2, 0x12e55, 0x12f08,
  41. 0x12fbc },
  42. { 0x1306f, 0x13124, 0x131d8, 0x1328d, 0x13343, 0x133f9, 0x134af, 0x13566,
  43. 0x1361d, 0x136d5, 0x1378d, 0x13846, 0x138fe, 0x139b8, 0x13a72, 0x13b2c,
  44. 0x13be6, 0x13ca1, 0x13d5d, 0x13e19, 0x13ed5, 0x13f92, 0x1404f, 0x1410d,
  45. 0x141cb },
  46. { 0x1428a, 0x14349, 0x14408, 0x144c8, 0x14588, 0x14649, 0x1470a, 0x147cc,
  47. 0x1488e, 0x14951, 0x14a14, 0x14ad7, 0x14b9b, 0x14c5f, 0x14d24, 0x14dea,
  48. 0x14eaf, 0x14f75, 0x1503c, 0x15103, 0x151cb, 0x15293, 0x1535b, 0x15424,
  49. 0x154ee },
  50. { 0x155b8, 0x15682, 0x1574d, 0x15818, 0x158e4, 0x159b0, 0x15a7d, 0x15b4a,
  51. 0x15c18, 0x15ce6, 0x15db4, 0x15e83, 0x15f53, 0x16023, 0x160f4, 0x161c5,
  52. 0x16296, 0x16368, 0x1643a, 0x1650d, 0x165e1, 0x166b5, 0x16789, 0x1685e,
  53. 0x16934 },
  54. { 0x16a09, 0x16ae0, 0x16bb7, 0x16c8e, 0x16d66, 0x16e3e, 0x16f17, 0x16ff1,
  55. 0x170ca, 0x171a5, 0x17280, 0x1735b, 0x17437, 0x17513, 0x175f0, 0x176ce,
  56. 0x177ac, 0x1788a, 0x17969, 0x17a49, 0x17b29, 0x17c09, 0x17cea, 0x17dcc,
  57. 0x17eae },
  58. { 0x17f91, 0x18074, 0x18157, 0x1823c, 0x18320, 0x18406, 0x184eb, 0x185d2,
  59. 0x186b8, 0x187a0, 0x18888, 0x18970, 0x18a59, 0x18b43, 0x18c2d, 0x18d17,
  60. 0x18e02, 0x18eee, 0x18fda, 0x190c7, 0x191b5, 0x192a2, 0x19391, 0x19480,
  61. 0x1956f },
  62. { 0x1965f, 0x19750, 0x19841, 0x19933, 0x19a25, 0x19b18, 0x19c0c, 0x19d00,
  63. 0x19df4, 0x19ee9, 0x19fdf, 0x1a0d5, 0x1a1cc, 0x1a2c4, 0x1a3bc, 0x1a4b4,
  64. 0x1a5ad, 0x1a6a7, 0x1a7a1, 0x1a89c, 0x1a998, 0x1aa94, 0x1ab90, 0x1ac8d,
  65. 0x1ad8b },
  66. { 0x1ae89, 0x1af88, 0x1b088, 0x1b188, 0x1b289, 0x1b38a, 0x1b48c, 0x1b58f,
  67. 0x1b692, 0x1b795, 0x1b89a, 0x1b99f, 0x1baa4, 0x1bbaa, 0x1bcb1, 0x1bdb8,
  68. 0x1bec0, 0x1bfc9, 0x1c0d2, 0x1c1dc, 0x1c2e6, 0x1c3f1, 0x1c4fd, 0x1c609,
  69. 0x1c716 },
  70. { 0x1c823, 0x1c931, 0x1ca40, 0x1cb50, 0x1cc60, 0x1cd70, 0x1ce81, 0x1cf93,
  71. 0x1d0a6, 0x1d1b9, 0x1d2cd, 0x1d3e1, 0x1d4f6, 0x1d60c, 0x1d722, 0x1d839,
  72. 0x1d951, 0x1da69, 0x1db82, 0x1dc9c, 0x1ddb6, 0x1ded1, 0x1dfec, 0x1e109,
  73. 0x1e225 },
  74. { 0x1e343, 0x1e461, 0x1e580, 0x1e6a0, 0x1e7c0, 0x1e8e0, 0x1ea02, 0x1eb24,
  75. 0x1ec47, 0x1ed6b, 0x1ee8f, 0x1efb4, 0x1f0d9, 0x1f1ff, 0x1f326, 0x1f44e,
  76. 0x1f576, 0x1f69f, 0x1f7c9, 0x1f8f3, 0x1fa1e, 0x1fb4a, 0x1fc76, 0x1fda3,
  77. 0x1fed1 }
  78. };
  79. //static int PITCH_Installed = FALSE;
  80. /*---------------------------------------------------------------------
  81. Function: PITCH_Init
  82. Initializes pitch table.
  83. ---------------------------------------------------------------------*/
  84. /*
  85. void PITCH_Init
  86. (
  87. void
  88. )
  89. {
  90. int note;
  91. int detune;
  92. if ( !PITCH_Installed )
  93. {
  94. for( note = 0; note < 12; note++ )
  95. {
  96. for( detune = 0; detune < MAXDETUNE; detune++ )
  97. {
  98. PitchTable[ note ][ detune ] = 0x10000 *
  99. pow( 2, ( note * MAXDETUNE + detune ) / ( 12.0 * MAXDETUNE ) );
  100. }
  101. }
  102. PITCH_Installed = TRUE;
  103. }
  104. }
  105. */
  106. /**********************************************************************
  107. Memory locked functions:
  108. **********************************************************************/
  109. #define PITCH_LockStart PITCH_GetScale
  110. /*---------------------------------------------------------------------
  111. Function: PITCH_GetScale
  112. Returns a fixed-point value to scale number the specified amount.
  113. ---------------------------------------------------------------------*/
  114. unsigned long PITCH_GetScale
  115. (
  116. int pitchoffset
  117. )
  118. {
  119. unsigned long scale;
  120. int octaveshift;
  121. int noteshift;
  122. int note;
  123. int detune;
  124. // if ( !PITCH_Installed )
  125. // {
  126. // PITCH_Init();
  127. // }
  128. if ( pitchoffset == 0 )
  129. {
  130. return( PitchTable[ 0 ][ 0 ] );
  131. }
  132. noteshift = pitchoffset % 1200;
  133. if ( noteshift < 0 )
  134. {
  135. noteshift += 1200;
  136. }
  137. note = noteshift / 100;
  138. detune = ( noteshift % 100 ) / ( 100 / MAXDETUNE );
  139. octaveshift = ( pitchoffset - noteshift ) / 1200;
  140. if ( detune < 0 )
  141. {
  142. detune += ( 100 / MAXDETUNE );
  143. note--;
  144. if ( note < 0 )
  145. {
  146. note += 12;
  147. octaveshift--;
  148. }
  149. }
  150. scale = PitchTable[ note ][ detune ];
  151. if ( octaveshift < 0 )
  152. {
  153. scale >>= -octaveshift;
  154. }
  155. else
  156. {
  157. scale <<= octaveshift;
  158. }
  159. return( scale );
  160. }
  161. /*---------------------------------------------------------------------
  162. Function: PITCH_LockEnd
  163. Used for determining the length of the functions to lock in memory.
  164. ---------------------------------------------------------------------*/
  165. static void PITCH_LockEnd
  166. (
  167. void
  168. )
  169. {
  170. }
  171. /*---------------------------------------------------------------------
  172. Function: PITCH_UnlockMemory
  173. Unlocks all neccessary data.
  174. ---------------------------------------------------------------------*/
  175. void PITCH_UnlockMemory
  176. (
  177. void
  178. )
  179. {
  180. DPMI_UnlockMemoryRegion( PITCH_LockStart, PITCH_LockEnd );
  181. DPMI_Unlock( PitchTable );
  182. // DPMI_Unlock( PITCH_Installed );
  183. }
  184. /*---------------------------------------------------------------------
  185. Function: PITCH_LockMemory
  186. Unlocks all neccessary data.
  187. ---------------------------------------------------------------------*/
  188. int PITCH_LockMemory
  189. (
  190. void
  191. )
  192. {
  193. int status;
  194. status = DPMI_LockMemoryRegion( PITCH_LockStart, PITCH_LockEnd );
  195. status |= DPMI_Lock( PitchTable );
  196. // status |= DPMI_Lock( PITCH_Installed );
  197. if ( status != DPMI_Ok )
  198. {
  199. PITCH_UnlockMemory();
  200. return( PITCH_Error );
  201. }
  202. return( PITCH_Ok );
  203. }