Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1390 lines
51 KiB

  1. // Voice.cpp
  2. // Copyright (c) 1996-2000 Microsoft Corporation. All Rights Reserved.
  3. //
  4. #include "common.h"
  5. #include <math.h>
  6. #include "muldiv32.h"
  7. #define STR_MODULENAME "DDKSynth.sys:Voice: "
  8. #ifdef _X86_
  9. #define MMX_ENABLED 1
  10. #endif
  11. #pragma code_seg()
  12. /*****************************************************************************
  13. * CVoiceLFO::CVoiceLFO()
  14. *****************************************************************************
  15. * Constructor for the low-frequency oscillator object.
  16. */
  17. CVoiceLFO::CVoiceLFO()
  18. {
  19. m_pModWheelIn = NULL;
  20. }
  21. /*****************************************************************************
  22. * snSineTable[]
  23. *****************************************************************************
  24. * Table of 16-bit integers, representing a sine wave.
  25. * value = sin((index*6.283185307)/256) * 100 where index = 0..255
  26. */
  27. const CHAR snSineTable[] = {
  28. // 0 1 2 3 4 5 6 7
  29. 0, 2, 4, 7, 9, 12, 14, 17,
  30. 19, 21, 24, 26, 29, 31, 33, 35,
  31. 38, 40, 42, 44, 47, 49, 51, 53,
  32. 55, 57, 59, 61, 63, 65, 67, 68,
  33. 70, 72, 74, 75, 77, 78, 80, 81,
  34. 83, 84, 85, 87, 88, 89, 90, 91,
  35. 92, 93, 94, 94, 95, 96, 97, 97,
  36. 98, 98, 98, 99, 99, 99, 99, 99,
  37. 100, 99, 99, 99, 99, 99, 98, 98,
  38. 98, 97, 97, 96, 95, 94, 94, 93,
  39. 92, 91, 90, 89, 88, 87, 85, 84,
  40. 83, 81, 80, 78, 77, 75, 74, 72,
  41. 70, 68, 67, 65, 63, 61, 59, 57,
  42. 55, 53, 51, 49, 47, 44, 42, 40,
  43. 38, 35, 33, 31, 29, 26, 24, 21,
  44. 19, 17, 14, 12, 9, 7, 4, 2,
  45. 0, -2, -4, -7, -9, -12, -14, -17,
  46. -19, -21, -24, -26, -29, -31, -33, -35,
  47. -38, -40, -42, -44, -47, -49, -51, -53,
  48. -55, -57, -59, -61, -63, -65, -67, -68,
  49. -70, -72, -74, -75, -77, -78, -80, -81,
  50. -83, -84, -85, -87, -88, -89, -90, -91,
  51. -92, -93, -94, -94, -95, -96, -97, -97,
  52. -98, -98, -98, -99, -99, -99, -99, -99,
  53. -100, -99, -99, -99, -99, -99, -98, -98,
  54. -98, -97, -97, -96, -95, -94, -94, -93,
  55. -92, -91, -90, -89, -88, -87, -85, -84,
  56. -83, -81, -80, -78, -77, -75, -74, -72,
  57. -70, -68, -67, -65, -63, -61, -59, -57,
  58. -55, -53, -51, -49, -47, -44, -42, -40,
  59. -38, -35, -33, -31, -29, -26, -24, -21,
  60. -19, -17, -14, -12, -9, -7, -4, -2
  61. };
  62. /*****************************************************************************
  63. * CVoiceLFO::StartVoice()
  64. *****************************************************************************
  65. * Start a voice with this LFO. Attach the given ModWheel receptor.
  66. */
  67. STIME CVoiceLFO::StartVoice(CSourceLFO *pSource,STIME stStartTime,
  68. CModWheelIn * pModWheelIn)
  69. {
  70. m_pModWheelIn = pModWheelIn;
  71. m_Source = *pSource;
  72. m_stStartTime = stStartTime;
  73. if ((m_Source.m_prMWPitchScale == 0) && (m_Source.m_vrMWVolumeScale == 0) &&
  74. (m_Source.m_prPitchScale == 0) && (m_Source.m_vrVolumeScale == 0))
  75. {
  76. m_stRepeatTime = 44100;
  77. }
  78. else
  79. {
  80. m_stRepeatTime = 2097152 / m_Source.m_pfFrequency; // (1/8 * 256 * 4096 * 16)
  81. }
  82. return (m_stRepeatTime);
  83. }
  84. /*****************************************************************************
  85. * CVoiceLFO::GetLevel()
  86. *****************************************************************************
  87. * Return the value of the LFO right now.
  88. */
  89. long CVoiceLFO::GetLevel(STIME stTime, STIME *pstNextTime)
  90. {
  91. stTime -= (m_stStartTime + m_Source.m_stDelay);
  92. if (stTime < 0)
  93. {
  94. *pstNextTime = -stTime;
  95. return (0);
  96. }
  97. *pstNextTime = m_stRepeatTime;
  98. stTime *= m_Source.m_pfFrequency;
  99. stTime = stTime >> (12 + 4); // We've added 4 extra bits of resolution...
  100. return (::snSineTable[stTime & 0xFF]);
  101. }
  102. /*****************************************************************************
  103. * CVoiceLFO::GetVolume()
  104. *****************************************************************************
  105. * Get the composite volume of the LFO.
  106. */
  107. VREL CVoiceLFO::GetVolume(STIME stTime, STIME *pstNextTime)
  108. {
  109. VREL vrVolume = m_pModWheelIn->GetModulation(stTime);
  110. vrVolume *= m_Source.m_vrMWVolumeScale;
  111. vrVolume /= 127;
  112. vrVolume += m_Source.m_vrVolumeScale;
  113. vrVolume *= GetLevel(stTime,pstNextTime);
  114. vrVolume /= 100;
  115. return (vrVolume);
  116. }
  117. /*****************************************************************************
  118. * CVoiceLFO::GetPitch()
  119. *****************************************************************************
  120. * Get the composite pitch of the LFO.
  121. */
  122. PREL CVoiceLFO::GetPitch(STIME stTime, STIME *pstNextTime)
  123. {
  124. PREL prPitch = m_pModWheelIn->GetModulation(stTime);
  125. prPitch *= m_Source.m_prMWPitchScale;
  126. prPitch /= 127;
  127. prPitch += m_Source.m_prPitchScale;
  128. prPitch *= GetLevel(stTime,pstNextTime);
  129. prPitch /= 100;
  130. return (prPitch);
  131. }
  132. /*****************************************************************************
  133. * CVoiceEG::CVoiceEG()
  134. *****************************************************************************
  135. * Constructor for the CVoiceEG object.
  136. */
  137. CVoiceEG::CVoiceEG()
  138. {
  139. m_stStopTime = 0;
  140. }
  141. /*****************************************************************************
  142. * snAttackTable[]
  143. *****************************************************************************
  144. * Table of 16-bit integers, representing a log attack curve.
  145. * value = (log10((index/200)^2)) * 10000 / 96 + 1000 where index = 0..199
  146. */
  147. const short snAttackTable[] = {
  148. // 0 1 2 3 4 5 6 7
  149. 0, 520, 583, 620, 646, 666, 682, 696,
  150. 708, 719, 728, 737, 745, 752, 759, 765,
  151. 771, 776, 782, 787, 791, 796, 800, 804,
  152. 808, 811, 815, 818, 822, 825, 828, 831,
  153. 834, 836, 839, 842, 844, 847, 849, 852,
  154. 854, 856, 858, 860, 863, 865, 867, 868,
  155. 870, 872, 874, 876, 878, 879, 881, 883,
  156. 884, 886, 887, 889, 891, 892, 894, 895,
  157. 896, 898, 899, 901, 902, 903, 905, 906,
  158. 907, 908, 910, 911, 912, 913, 914, 915,
  159. 917, 918, 919, 920, 921, 922, 923, 924,
  160. 925, 926, 927, 928, 929, 930, 931, 932,
  161. 933, 934, 935, 936, 937, 938, 939, 939,
  162. 940, 941, 942, 943, 944, 945, 945, 946,
  163. 947, 948, 949, 949, 950, 951, 952, 953,
  164. 953, 954, 955, 956, 956, 957, 958, 958,
  165. 959, 960, 961, 961, 962, 963, 963, 964,
  166. 965, 965, 966, 967, 967, 968, 969, 969,
  167. 970, 970, 971, 972, 972, 973, 973, 974,
  168. 975, 975, 976, 976, 977, 978, 978, 979,
  169. 979, 980, 980, 981, 982, 982, 983, 983,
  170. 984, 984, 985, 985, 986, 986, 987, 987,
  171. 988, 988, 989, 989, 990, 990, 991, 991,
  172. 992, 992, 993, 993, 994, 994, 995, 995,
  173. 996, 996, 997, 997, 998, 998, 999, 999,
  174. 1000
  175. };
  176. /*****************************************************************************
  177. * CVoiceEG::StopVoice()
  178. *****************************************************************************
  179. * Stop the envelope generator. Use a heuristic to hasten the cutoff,
  180. * depending on the current level.
  181. */
  182. void CVoiceEG::StopVoice(STIME stTime)
  183. {
  184. m_Source.m_stRelease *= GetLevel(stTime,&m_stStopTime,TRUE); // Adjust for current sustain level.
  185. m_Source.m_stRelease /= 1000;
  186. m_stStopTime = stTime;
  187. }
  188. /*****************************************************************************
  189. * CVoiceEG::QuickStopVoice()
  190. *****************************************************************************
  191. * Stop the envelope generator ASAP.
  192. */
  193. void CVoiceEG::QuickStopVoice(STIME stTime, DWORD dwSampleRate)
  194. {
  195. m_Source.m_stRelease *= GetLevel(stTime,&m_stStopTime,TRUE); // Adjust for current sustain level.
  196. m_Source.m_stRelease /= 1000;
  197. dwSampleRate /= 70;
  198. if (m_Source.m_stRelease > (long) dwSampleRate)
  199. {
  200. m_Source.m_stRelease = dwSampleRate;
  201. }
  202. m_stStopTime = stTime;
  203. }
  204. /*****************************************************************************
  205. * CVoiceEG::StartVoice()
  206. *****************************************************************************
  207. * Start the voice with the given envelope generator and parameters.
  208. */
  209. STIME CVoiceEG::StartVoice(CSourceEG *pSource, STIME stStartTime,
  210. WORD nKey, WORD nVelocity)
  211. {
  212. m_stStartTime = stStartTime;
  213. m_stStopTime = 0x7fffffffffffffff; // set to indefinite future
  214. m_Source = *pSource;
  215. // apply velocity to attack length scaling here
  216. m_Source.m_stAttack *= CDigitalAudio::PRELToPFRACT(nVelocity * m_Source.m_trVelAttackScale / 127);
  217. m_Source.m_stAttack /= 4096;
  218. m_Source.m_stDecay *= CDigitalAudio::PRELToPFRACT(nKey * m_Source.m_trKeyDecayScale / 127);
  219. m_Source.m_stDecay /= 4096;
  220. m_Source.m_stDecay *= (1000 - m_Source.m_pcSustain);
  221. m_Source.m_stDecay /= 1000;
  222. return ((STIME)m_Source.m_stAttack);
  223. }
  224. /*****************************************************************************
  225. * CVoiceEG::InAttack()
  226. *****************************************************************************
  227. * Are we in the attack phase still?
  228. */
  229. BOOL CVoiceEG::InAttack(STIME st)
  230. {
  231. // has note been released?
  232. if (st >= m_stStopTime)
  233. return FALSE;
  234. // past length of attack?
  235. if (st >= m_stStartTime + m_Source.m_stAttack)
  236. return FALSE;
  237. return TRUE;
  238. }
  239. /*****************************************************************************
  240. * CVoiceEG::InRelease()
  241. *****************************************************************************
  242. * Are we in the release phase yet?
  243. */
  244. BOOL CVoiceEG::InRelease(STIME st)
  245. {
  246. // has note been released?
  247. if (st > m_stStopTime)
  248. return TRUE;
  249. return FALSE;
  250. }
  251. /*****************************************************************************
  252. * CVoiceEG::GetLevel()
  253. *****************************************************************************
  254. * Get the envelope generator's current level, from 0 to 1000.
  255. */
  256. long CVoiceEG::GetLevel(STIME stEnd, STIME *pstNext, BOOL fVolume)
  257. {
  258. long lLevel = 0;
  259. if (stEnd <= m_stStopTime)
  260. {
  261. stEnd -= m_stStartTime;
  262. // note not released yet.
  263. if (stEnd < m_Source.m_stAttack)
  264. {
  265. // still in attack
  266. lLevel = 1000 * (long) stEnd;
  267. if (m_Source.m_stAttack)
  268. {
  269. lLevel /= (long) m_Source.m_stAttack;
  270. }
  271. else // This should never happen, but it does...
  272. {
  273. lLevel = 0;
  274. }
  275. *pstNext = m_Source.m_stAttack - stEnd;
  276. if (lLevel < 0) lLevel = 0;
  277. if (lLevel > 1000) lLevel = 1000;
  278. if (fVolume)
  279. {
  280. lLevel = ::snAttackTable[lLevel / 5];
  281. }
  282. }
  283. else
  284. {
  285. stEnd -= m_Source.m_stAttack;
  286. if (stEnd < m_Source.m_stDecay)
  287. {
  288. // still in decay
  289. lLevel = (1000 - m_Source.m_pcSustain) * (long) stEnd;
  290. lLevel /= (long) m_Source.m_stDecay;
  291. lLevel = 1000 - lLevel;
  292. // To improve the decay curve, set the next point to be 1/4, 1/2, or end of slope.
  293. // To avoid close duplicates, fudge an extra 100 samples.
  294. if (stEnd < ((m_Source.m_stDecay >> 2) - 100))
  295. {
  296. *pstNext = (m_Source.m_stDecay >> 2) - stEnd;
  297. }
  298. else if (stEnd < ((m_Source.m_stDecay >> 1) - 100))
  299. {
  300. *pstNext = (m_Source.m_stDecay >> 1) - stEnd;
  301. }
  302. else
  303. {
  304. *pstNext = m_Source.m_stDecay - stEnd; // Next is end of decay.
  305. }
  306. }
  307. else
  308. {
  309. // in sustain
  310. lLevel = m_Source.m_pcSustain;
  311. *pstNext = 44100;
  312. }
  313. }
  314. }
  315. else
  316. {
  317. STIME stBogus;
  318. // in release
  319. stEnd -= m_stStopTime;
  320. if (stEnd < m_Source.m_stRelease)
  321. {
  322. lLevel = GetLevel(m_stStopTime,&stBogus,fVolume) * (long) (m_Source.m_stRelease - stEnd);
  323. lLevel /= (long) m_Source.m_stRelease;
  324. if (stEnd < ((m_Source.m_stRelease >> 2) - 100))
  325. {
  326. *pstNext = (m_Source.m_stRelease >> 2) - stEnd;
  327. }
  328. else if (stEnd < ((m_Source.m_stRelease >> 1) - 100))
  329. {
  330. *pstNext = (m_Source.m_stRelease >> 1) - stEnd;
  331. }
  332. else
  333. {
  334. *pstNext = m_Source.m_stRelease - stEnd; // Next is end of decay.
  335. }
  336. }
  337. else
  338. {
  339. lLevel = 0; // !!! off
  340. *pstNext = 0x7FFFFFFFFFFFFFFF;
  341. }
  342. }
  343. return lLevel;
  344. }
  345. /*****************************************************************************
  346. * CVoiceEG::GetVolume()
  347. *****************************************************************************
  348. * Get the composite volume of the envelope generator in dB cents (1/100ths db).
  349. */
  350. VREL CVoiceEG::GetVolume(STIME stTime, STIME *pstNextTime)
  351. {
  352. VREL vrLevel = GetLevel(stTime, pstNextTime, TRUE) * 96;
  353. vrLevel /= 10;
  354. vrLevel = vrLevel - 9600;
  355. return vrLevel;
  356. }
  357. /*****************************************************************************
  358. * CVoiceEG::GetPitch()
  359. *****************************************************************************
  360. * Get the composite pitch of the envelope generator, in fractional scale units.
  361. */
  362. PREL CVoiceEG::GetPitch(STIME stTime, STIME *pstNextTime)
  363. {
  364. PREL prLevel;
  365. if (m_Source.m_sScale != 0)
  366. {
  367. prLevel = GetLevel(stTime, pstNextTime,FALSE);
  368. prLevel *= m_Source.m_sScale;
  369. prLevel /= 1000;
  370. }
  371. else
  372. {
  373. *pstNextTime = 44100;
  374. prLevel = 0;
  375. }
  376. return prLevel;
  377. }
  378. BOOL MultiMediaInstructionsSupported();
  379. /*****************************************************************************
  380. * CDigitalAudio::CDigitalAudio()
  381. *****************************************************************************
  382. * Initialize the digital audio object.
  383. * This object manages the sample looping and playback and
  384. * digitally controlled amplifier.
  385. */
  386. CDigitalAudio::CDigitalAudio()
  387. {
  388. m_pfBasePitch = 0;
  389. m_pfLastPitch = 0;
  390. m_pfLastSample = 0;
  391. m_pfLoopEnd = 0;
  392. m_pfLoopStart = 0;
  393. m_pfSampleLength = 0;
  394. m_prLastPitch = 0;
  395. m_vrLastLVolume = 0;
  396. m_vrLastRVolume = 0;
  397. m_vrBaseLVolume = 0;
  398. m_vrBaseRVolume = 0;
  399. m_vfLastLVolume = 0;
  400. m_vfLastRVolume = 0;
  401. m_ullLastSample = 0;
  402. m_ullLoopStart = 0;
  403. m_ullLoopEnd = 0;
  404. m_ullSampleLength = 0;
  405. m_fElGrande = FALSE;
  406. #ifdef MMX_ENABLED
  407. m_fMMXEnabled = MultiMediaInstructionsSupported();
  408. #endif // MMX_ENABLED
  409. };
  410. /*****************************************************************************
  411. * Other CDigitalAudio tables
  412. *****************************************************************************/
  413. // Pitch increment lookup.
  414. // value = ((index/1200)^2)*4096 where index = -100..100
  415. const /*PFRACT*/SHORT pfCents[] = {
  416. // 0 1 2 3 4 5 6 7
  417. 3866, 3868, 3870, 3872, 3875, 3877, 3879, 3881,
  418. 3884, 3886, 3888, 3890, 3893, 3895, 3897, 3899,
  419. 3902, 3904, 3906, 3908, 3911, 3913, 3915, 3917,
  420. 3920, 3922, 3924, 3926, 3929, 3931, 3933, 3935,
  421. 3938, 3940, 3942, 3945, 3947, 3949, 3951, 3954,
  422. 3956, 3958, 3961, 3963, 3965, 3967, 3970, 3972,
  423. 3974, 3977, 3979, 3981, 3983, 3986, 3988, 3990,
  424. 3993, 3995, 3997, 4000, 4002, 4004, 4007, 4009,
  425. 4011, 4014, 4016, 4018, 4020, 4023, 4025, 4027,
  426. 4030, 4032, 4034, 4037, 4039, 4041, 4044, 4046,
  427. 4048, 4051, 4053, 4055, 4058, 4060, 4063, 4065,
  428. 4067, 4070, 4072, 4074, 4077, 4079, 4081, 4084,
  429. 4086, 4088, 4091, 4093, 4096, 4098, 4100, 4103,
  430. 4105, 4107, 4110, 4112, 4114, 4117, 4119, 4122,
  431. 4124, 4126, 4129, 4131, 4134, 4136, 4138, 4141,
  432. 4143, 4145, 4148, 4150, 4153, 4155, 4157, 4160,
  433. 4162, 4165, 4167, 4170, 4172, 4174, 4177, 4179,
  434. 4182, 4184, 4186, 4189, 4191, 4194, 4196, 4199,
  435. 4201, 4203, 4206, 4208, 4211, 4213, 4216, 4218,
  436. 4220, 4223, 4225, 4228, 4230, 4233, 4235, 4237,
  437. 4240, 4242, 4245, 4247, 4250, 4252, 4255, 4257,
  438. 4260, 4262, 4265, 4267, 4269, 4272, 4274, 4277,
  439. 4279, 4282, 4284, 4287, 4289, 4292, 4294, 4297,
  440. 4299, 4302, 4304, 4307, 4309, 4312, 4314, 4317,
  441. 4319, 4322, 4324, 4327, 4329, 4332, 4334, 4337,
  442. 4339
  443. };
  444. // Four octaves up and down.
  445. // value = ((index/12)^2)*4096 where index = -48..48
  446. const PFRACT pfSemiTones[] = {
  447. // 0 1 2 3 4 5 6 7
  448. 256, 271, 287, 304, 322, 341, 362, 383,
  449. 406, 430, 456, 483, 512, 542, 574, 608,
  450. 645, 683, 724, 767, 812, 861, 912, 966,
  451. 1024, 1084, 1149, 1217, 1290, 1366, 1448, 1534,
  452. 1625, 1722, 1824, 1933, 2048, 2169, 2298, 2435,
  453. 2580, 2733, 2896, 3068, 3250, 3444, 3649, 3866,
  454. 4096, 4339, 4597, 4870, 5160, 5467, 5792, 6137,
  455. 6501, 6888, 7298, 7732, 8192, 8679, 9195, 9741,
  456. 10321, 10935, 11585, 12274, 13003, 13777, 14596, 15464,
  457. 16384, 17358, 18390, 19483, 20642, 21870, 23170, 24548,
  458. 26007, 27554, 29192, 30928, 32768, 34716, 36780, 38967,
  459. 41285, 43740, 46340, 49096, 52015, 55108, 58385, 61857,
  460. 65536
  461. };
  462. // dB conversion table.
  463. // value = (((index / 100)^10)^.5)*4095 where index = MINDB*10..MAXDB*10
  464. const /*VFRACT*/SHORT vfDbToVolume[] = {
  465. // 0 1 2 3 4 5 6 7
  466. 0, 0, 0, 0, 0, 0, 0, 0,
  467. 0, 0, 0, 0, 0, 0, 0, 0,
  468. 0, 0, 0, 0, 0, 0, 0, 0,
  469. 0, 0, 0, 0, 0, 0, 0, 0,
  470. 0, 0, 0, 0, 0, 0, 0, 0,
  471. 0, 0, 0, 0, 0, 0, 0, 0,
  472. 0, 0, 0, 0, 0, 0, 0, 0,
  473. 0, 0, 0, 0, 0, 0, 0, 0,
  474. 0, 0, 0, 0, 0, 0, 0, 0,
  475. 0, 0, 0, 0, 0, 0, 0, 0,
  476. 0, 0, 0, 0, 0, 0, 0, 0,
  477. 0, 0, 0, 0, 0, 0, 0, 0,
  478. 0, 0, 0, 0, 0, 0, 0, 0,
  479. 0, 0, 0, 0, 0, 0, 0, 0,
  480. 0, 0, 0, 0, 0, 0, 0, 0,
  481. 0, 0, 0, 0, 0, 0, 0, 0,
  482. 0, 0, 0, 0, 0, 0, 0, 0,
  483. 0, 0, 0, 0, 0, 0, 0, 0,
  484. 0, 0, 0, 0, 0, 0, 0, 0,
  485. 0, 0, 0, 0, 0, 0, 0, 0,
  486. 0, 0, 0, 0, 0, 0, 0, 0,
  487. 0, 0, 0, 0, 0, 0, 0, 0,
  488. 0, 0, 0, 0, 0, 0, 0, 0,
  489. 0, 0, 0, 0, 0, 0, 0, 0,
  490. 0, 0, 0, 0, 0, 0, 0, 0,
  491. 0, 0, 0, 0, 0, 0, 0, 0,
  492. 0, 0, 0, 0, 0, 0, 0, 0,
  493. 0, 0, 0, 0, 0, 0, 0, 0,
  494. 0, 0, 0, 0, 0, 0, 0, 0,
  495. 0, 0, 0, 0, 0, 0, 0, 0,
  496. 0, 0, 0, 0, 0, 0, 0, 0,
  497. 0, 0, 0, 0, 0, 0, 0, 0,
  498. 0, 0, 0, 0, 0, 0, 0, 0,
  499. 0, 0, 0, 0, 0, 0, 0, 0,
  500. 0, 0, 0, 0, 0, 0, 1, 1,
  501. 1, 1, 1, 1, 1, 1, 1, 1,
  502. 1, 1, 1, 1, 1, 1, 1, 1,
  503. 1, 1, 1, 1, 1, 1, 1, 1,
  504. 1, 1, 1, 1, 1, 1, 1, 1,
  505. 1, 1, 1, 1, 1, 1, 1, 1,
  506. 1, 1, 1, 1, 1, 1, 1, 1,
  507. 1, 1, 1, 1, 1, 1, 1, 1,
  508. 1, 1, 2, 2, 2, 2, 2, 2,
  509. 2, 2, 2, 2, 2, 2, 2, 2,
  510. 2, 2, 2, 2, 2, 2, 2, 2,
  511. 2, 2, 2, 2, 2, 2, 2, 2,
  512. 2, 2, 2, 2, 2, 3, 3, 3,
  513. 3, 3, 3, 3, 3, 3, 3, 3,
  514. 3, 3, 3, 3, 3, 3, 3, 3,
  515. 3, 3, 3, 3, 3, 3, 4, 4,
  516. 4, 4, 4, 4, 4, 4, 4, 4,
  517. 4, 4, 4, 4, 4, 4, 4, 4,
  518. 4, 4, 5, 5, 5, 5, 5, 5,
  519. 5, 5, 5, 5, 5, 5, 5, 5,
  520. 5, 5, 6, 6, 6, 6, 6, 6,
  521. 6, 6, 6, 6, 6, 6, 6, 7,
  522. 7, 7, 7, 7, 7, 7, 7, 7,
  523. 7, 7, 7, 8, 8, 8, 8, 8,
  524. 8, 8, 8, 8, 8, 9, 9, 9,
  525. 9, 9, 9, 9, 9, 9, 10, 10,
  526. 10, 10, 10, 10, 10, 10, 11, 11,
  527. 11, 11, 11, 11, 11, 11, 12, 12,
  528. 12, 12, 12, 12, 12, 13, 13, 13,
  529. 13, 13, 13, 14, 14, 14, 14, 14,
  530. 14, 15, 15, 15, 15, 15, 15, 16,
  531. 16, 16, 16, 16, 17, 17, 17, 17,
  532. 17, 18, 18, 18, 18, 18, 19, 19,
  533. 19, 19, 20, 20, 20, 20, 21, 21,
  534. 21, 21, 21, 22, 22, 22, 23, 23,
  535. 23, 23, 24, 24, 24, 24, 25, 25,
  536. 25, 26, 26, 26, 27, 27, 27, 28,
  537. 28, 28, 28, 29, 29, 30, 30, 30,
  538. 31, 31, 31, 32, 32, 32, 33, 33,
  539. 34, 34, 34, 35, 35, 36, 36, 36,
  540. 37, 37, 38, 38, 39, 39, 40, 40,
  541. 40, 41, 41, 42, 42, 43, 43, 44,
  542. 44, 45, 45, 46, 47, 47, 48, 48,
  543. 49, 49, 50, 50, 51, 52, 52, 53,
  544. 53, 54, 55, 55, 56, 57, 57, 58,
  545. 59, 59, 60, 61, 61, 62, 63, 64,
  546. 64, 65, 66, 67, 67, 68, 69, 70,
  547. 71, 71, 72, 73, 74, 75, 76, 77,
  548. 78, 78, 79, 80, 81, 82, 83, 84,
  549. 85, 86, 87, 88, 89, 90, 91, 92,
  550. 93, 94, 95, 97, 98, 99, 100, 101,
  551. 102, 104, 105, 106, 107, 108, 110, 111,
  552. 112, 114, 115, 116, 118, 119, 120, 122,
  553. 123, 125, 126, 128, 129, 130, 132, 134,
  554. 135, 137, 138, 140, 141, 143, 145, 146,
  555. 148, 150, 152, 153, 155, 157, 159, 161,
  556. 163, 164, 166, 168, 170, 172, 174, 176,
  557. 178, 180, 182, 185, 187, 189, 191, 193,
  558. 195, 198, 200, 202, 205, 207, 210, 212,
  559. 214, 217, 219, 222, 225, 227, 230, 232,
  560. 235, 238, 241, 243, 246, 249, 252, 255,
  561. 258, 261, 264, 267, 270, 273, 276, 280,
  562. 283, 286, 289, 293, 296, 300, 303, 307,
  563. 310, 314, 317, 321, 325, 329, 332, 336,
  564. 340, 344, 348, 352, 356, 360, 364, 369,
  565. 373, 377, 382, 386, 391, 395, 400, 404,
  566. 409, 414, 419, 423, 428, 433, 438, 443,
  567. 449, 454, 459, 464, 470, 475, 481, 486,
  568. 492, 498, 503, 509, 515, 521, 527, 533,
  569. 539, 546, 552, 558, 565, 571, 578, 585,
  570. 591, 598, 605, 612, 619, 626, 634, 641,
  571. 649, 656, 664, 671, 679, 687, 695, 703,
  572. 711, 719, 728, 736, 745, 753, 762, 771,
  573. 780, 789, 798, 807, 817, 826, 836, 845,
  574. 855, 865, 875, 885, 895, 906, 916, 927,
  575. 938, 948, 959, 971, 982, 993, 1005, 1016,
  576. 1028, 1040, 1052, 1064, 1077, 1089, 1102, 1114,
  577. 1127, 1140, 1154, 1167, 1181, 1194, 1208, 1222,
  578. 1236, 1250, 1265, 1280, 1294, 1309, 1325, 1340,
  579. 1355, 1371, 1387, 1403, 1419, 1436, 1452, 1469,
  580. 1486, 1504, 1521, 1539, 1556, 1574, 1593, 1611,
  581. 1630, 1649, 1668, 1687, 1707, 1726, 1746, 1767,
  582. 1787, 1808, 1829, 1850, 1871, 1893, 1915, 1937,
  583. 1959, 1982, 2005, 2028, 2052, 2076, 2100, 2124,
  584. 2149, 2173, 2199, 2224, 2250, 2276, 2302, 2329,
  585. 2356, 2383, 2411, 2439, 2467, 2496, 2524, 2554,
  586. 2583, 2613, 2643, 2674, 2705, 2736, 2768, 2800,
  587. 2833, 2865, 2899, 2932, 2966, 3000, 3035, 3070,
  588. 3106, 3142, 3178, 3215, 3252, 3290, 3328, 3367,
  589. 3406, 3445, 3485, 3525, 3566, 3607, 3649, 3691,
  590. 3734, 3777, 3821, 3865, 3910, 3955, 4001, 4048,
  591. 4095
  592. };
  593. /*****************************************************************************
  594. * CDigitalAudio::VRELToVFRACT()
  595. *****************************************************************************
  596. * Translate between VREL and VFRACT, clamping if necessary.
  597. */
  598. VFRACT CDigitalAudio::VRELToVFRACT(VREL vrVolume)
  599. {
  600. vrVolume /= 10;
  601. if (vrVolume < MINDB * 10) vrVolume = MINDB * 10;
  602. else if (vrVolume >= MAXDB * 10) vrVolume = MAXDB * 10;
  603. return (::vfDbToVolume[vrVolume - MINDB * 10]);
  604. }
  605. /*****************************************************************************
  606. * CDigitalAudio::PRELToPFRACT()
  607. *****************************************************************************
  608. * Translates from PREL to PFRACT, clamping if necessary.
  609. */
  610. PFRACT CDigitalAudio::PRELToPFRACT(PREL prPitch)
  611. {
  612. PFRACT pfPitch = 0;
  613. PREL prOctave;
  614. if (prPitch > 100)
  615. {
  616. if (prPitch > 4800)
  617. {
  618. prPitch = 4800;
  619. }
  620. prOctave = prPitch / 100;
  621. prPitch = prPitch % 100;
  622. pfPitch = ::pfCents[prPitch + 100];
  623. pfPitch <<= prOctave / 12;
  624. prOctave = prOctave % 12;
  625. pfPitch *= ::pfSemiTones[prOctave + 48];
  626. pfPitch >>= 12;
  627. }
  628. else if (prPitch < -100)
  629. {
  630. if (prPitch < -4800)
  631. {
  632. prPitch = -4800;
  633. }
  634. prOctave = prPitch / 100;
  635. prPitch = (-prPitch) % 100;
  636. pfPitch = ::pfCents[100 - prPitch];
  637. pfPitch >>= ((-prOctave) / 12);
  638. prOctave = (-prOctave) % 12;
  639. pfPitch *= ::pfSemiTones[48 - prOctave];
  640. pfPitch >>= 12;
  641. }
  642. else
  643. {
  644. pfPitch = ::pfCents[prPitch + 100];
  645. }
  646. return (pfPitch);
  647. }
  648. /*****************************************************************************
  649. * CDigitalAudio::ClearVoice()
  650. *****************************************************************************
  651. * Clear this voice in the Digital Audio Engine. The wave object can go away now.
  652. */
  653. void CDigitalAudio::ClearVoice()
  654. {
  655. if (m_Source.m_pWave != NULL)
  656. {
  657. m_Source.m_pWave->PlayOff();
  658. m_Source.m_pWave->Release(); // Releases wave structure.
  659. m_Source.m_pWave = NULL;
  660. }
  661. }
  662. /*****************************************************************************
  663. * CDigitalAudio::StartVoice()
  664. *****************************************************************************
  665. * Start a voice on the given synth and sample.
  666. */
  667. STIME CDigitalAudio::StartVoice(CSynth *pSynth, CSourceSample *pSample,
  668. VREL vrBaseLVolume, VREL vrBaseRVolume,
  669. PREL prBasePitch, long lKey)
  670. {
  671. m_vrBaseLVolume = vrBaseLVolume;
  672. m_vrBaseRVolume = vrBaseRVolume;
  673. m_vfLastLVolume = VRELToVFRACT(MIN_VOLUME);
  674. m_vfLastRVolume = VRELToVFRACT(MIN_VOLUME);
  675. m_vrLastLVolume = 0;
  676. m_vrLastRVolume = 0;
  677. m_prLastPitch = 0;
  678. m_Source = *pSample;
  679. m_pnWave = pSample->m_pWave->m_pnWave;
  680. m_pSynth = pSynth;
  681. pSample->m_pWave->AddRef(); // Keeps track of Wave usage.
  682. pSample->m_pWave->PlayOn();
  683. prBasePitch += pSample->m_prFineTune;
  684. prBasePitch += ((lKey - pSample->m_bMIDIRootKey) * 100);
  685. m_pfBasePitch = PRELToPFRACT(prBasePitch);
  686. m_pfBasePitch *= pSample->m_dwSampleRate;
  687. m_pfBasePitch /= pSynth->m_dwSampleRate;
  688. m_pfLastPitch = m_pfBasePitch;
  689. m_fElGrande = pSample->m_dwSampleLength >= 0x80000; // Greater than 512k.
  690. if ((pSample->m_dwLoopEnd - pSample->m_dwLoopStart) >= 0x80000)
  691. { // We can't handle loops greater than 1 meg!
  692. m_Source.m_bOneShot = TRUE;
  693. }
  694. m_ullLastSample = 0;
  695. m_ullLoopStart = pSample->m_dwLoopStart;
  696. m_ullLoopStart = m_ullLoopStart << 12;
  697. m_ullLoopEnd = pSample->m_dwLoopEnd;
  698. m_ullLoopEnd = m_ullLoopEnd << 12;
  699. m_ullSampleLength = pSample->m_dwSampleLength;
  700. m_ullSampleLength = m_ullSampleLength << 12;
  701. m_pfLastSample = 0;
  702. m_pfLoopStart = (long) m_ullLoopStart;
  703. m_pfLoopEnd = (long) m_ullLoopEnd;
  704. if (m_pfLoopEnd <= m_pfLoopStart) // Should never happen, but death if it does!
  705. {
  706. m_Source.m_bOneShot = TRUE;
  707. }
  708. if (m_fElGrande)
  709. {
  710. m_pfSampleLength = 0x7FFFFFFF;
  711. }
  712. else
  713. {
  714. m_pfSampleLength = (long) m_ullSampleLength;
  715. }
  716. return (0); // !!! what is this return value?
  717. }
  718. /* If the wave is bigger than one meg, the index can overflow.
  719. Solve this by assuming no mix session will ever be as great
  720. as one meg AND loops are never that long. We keep all our
  721. fractional indexes in two variables. In one case, m_pfLastSample,
  722. is the normal mode where the lower 12 bits are the fraction and
  723. the upper 20 bits are the index. And, m_ullLastSample
  724. is a LONGLONG with an extra 32 bits of index. The mix engine
  725. does not want the LONGLONGs, so we need to track the variables
  726. in the LONGLONGs and prepare them for the mixer as follows:
  727. Prior to mixing,
  728. if the sample is large (m_fElGrande is set), BeforeSampleMix()
  729. is called. This finds the starting point for the mix, which
  730. is either the current position or the start of the loop,
  731. whichever is earlier. It subtracts this starting point from
  732. the LONGLONG variables and stores an offset in m_dwAddressUpper.
  733. It also adjusts the pointer to the wave data appropriately.
  734. AfterSampleMix() does the inverse, reconstructing the the LONGLONG
  735. indeces and returning everthing back to normal.
  736. */
  737. /*****************************************************************************
  738. * CDigitalAudio::BeforeBigSampleMix()
  739. *****************************************************************************
  740. * Setup before doing a large sample mix/loop.
  741. */
  742. void CDigitalAudio::BeforeBigSampleMix()
  743. {
  744. if (m_fElGrande)
  745. {
  746. ULONGLONG ullBase = 0;
  747. DWORD dwBase;
  748. if (m_Source.m_bOneShot)
  749. {
  750. ullBase = m_ullLastSample;
  751. }
  752. else
  753. {
  754. if (m_ullLastSample < m_ullLoopStart)
  755. {
  756. ullBase = m_ullLastSample;
  757. }
  758. else
  759. {
  760. ullBase = m_ullLoopStart;
  761. }
  762. }
  763. ullBase >>= 12;
  764. dwBase = (DWORD) ullBase & 0xFFFFFFFE; // Clear bottom bit so 8 bit pointer aligns with short.
  765. ullBase = dwBase;
  766. ullBase <<= 12;
  767. m_dwAddressUpper = dwBase;
  768. m_pfLastSample = (long) (m_ullLastSample - ullBase);
  769. if ((m_ullLoopEnd - ullBase) < 0x7FFFFFFF)
  770. {
  771. m_pfLoopStart = (long) (m_ullLoopStart - ullBase);
  772. m_pfLoopEnd = (long) (m_ullLoopEnd - ullBase);
  773. }
  774. else
  775. {
  776. m_pfLoopStart = 0;
  777. m_pfLoopEnd = 0x7FFFFFFF;
  778. }
  779. ullBase = m_ullSampleLength - ullBase;
  780. if (ullBase > 0x7FFFFFFF)
  781. {
  782. m_pfSampleLength = 0x7FFFFFFF;
  783. }
  784. else
  785. {
  786. m_pfSampleLength = (long) ullBase;
  787. }
  788. if (m_Source.m_bSampleType & SFORMAT_8)
  789. {
  790. dwBase >>= 1;
  791. }
  792. m_pnWave = &m_Source.m_pWave->m_pnWave[dwBase];
  793. }
  794. }
  795. /*****************************************************************************
  796. * CDigitalAudio::AfterBigSampleMix()
  797. *****************************************************************************
  798. * Cleanup after doing a large sample mix/loop.
  799. */
  800. void CDigitalAudio::AfterBigSampleMix()
  801. {
  802. m_pnWave = m_Source.m_pWave->m_pnWave;
  803. if (m_fElGrande)
  804. {
  805. ULONGLONG ullBase = m_dwAddressUpper;
  806. m_ullLastSample = m_pfLastSample;
  807. m_ullLastSample += (ullBase << 12);
  808. m_dwAddressUpper = 0;
  809. }
  810. }
  811. /*****************************************************************************
  812. * CDigitalAudio::Mix()
  813. *****************************************************************************
  814. * Do a mix on this buffer. This handles loops and calling the different mix
  815. * functions (depending on format).
  816. */
  817. BOOL CDigitalAudio::Mix(short *pBuffer, DWORD dwLength, // length in SAMPLES
  818. VREL vrVolumeL,VREL vrVolumeR,
  819. PREL prPitch, DWORD dwStereo)
  820. {
  821. PFRACT pfDeltaPitch;
  822. PFRACT pfEnd;
  823. PFRACT pfLoopLen;
  824. PFRACT pfNewPitch;
  825. VFRACT vfNewLVolume;
  826. VFRACT vfNewRVolume;
  827. VFRACT vfDeltaLVolume;
  828. VFRACT vfDeltaRVolume;
  829. DWORD dwPeriod = 64;
  830. DWORD dwSoFar;
  831. DWORD dwStart; // position in WORDs
  832. DWORD dwMixChoice = dwStereo ? SPLAY_STEREO : 0;
  833. if (dwLength == 0) // Attack was instant.
  834. {
  835. m_pfLastPitch = (m_pfBasePitch * PRELToPFRACT(prPitch)) >> 12;
  836. m_vfLastLVolume = VRELToVFRACT(m_vrBaseLVolume + vrVolumeL);
  837. m_vfLastRVolume = VRELToVFRACT(m_vrBaseRVolume + vrVolumeR);
  838. m_prLastPitch = prPitch;
  839. m_vrLastLVolume = vrVolumeL;
  840. m_vrLastRVolume = vrVolumeR;
  841. return (TRUE);
  842. }
  843. if ((m_Source.m_pWave == NULL) || (m_Source.m_pWave->m_pnWave == NULL))
  844. {
  845. return FALSE;
  846. }
  847. DWORD dwMax = abs(vrVolumeL - m_vrLastLVolume);
  848. m_vrLastLVolume = vrVolumeL;
  849. dwMax = max((long)dwMax,abs(vrVolumeR - m_vrLastRVolume));
  850. m_vrLastRVolume = vrVolumeR;
  851. dwMax = max((long)dwMax,abs(prPitch - m_prLastPitch) << 1);
  852. dwMax >>= 1;
  853. m_prLastPitch = prPitch;
  854. if (dwMax > 0)
  855. {
  856. dwPeriod = (dwLength << 3) / dwMax;
  857. if (dwPeriod > 512)
  858. {
  859. dwPeriod = 512;
  860. }
  861. else if (dwPeriod < 1)
  862. {
  863. dwPeriod = 1;
  864. }
  865. }
  866. else
  867. {
  868. dwPeriod = 512; // Make it happen anyway.
  869. }
  870. // This makes MMX sound a little better (MMX bug will be fixed)
  871. dwPeriod += 3;
  872. dwPeriod &= 0xFFFFFFFC;
  873. pfNewPitch = m_pfBasePitch * PRELToPFRACT(prPitch);
  874. pfNewPitch >>= 12;
  875. pfDeltaPitch = MulDiv(pfNewPitch - m_pfLastPitch,dwPeriod << 8,dwLength);
  876. vfNewLVolume = VRELToVFRACT(m_vrBaseLVolume + vrVolumeL);
  877. vfNewRVolume = VRELToVFRACT(m_vrBaseRVolume + vrVolumeR);
  878. vfDeltaLVolume = MulDiv(vfNewLVolume - m_vfLastLVolume,dwPeriod << 8,dwLength);
  879. vfDeltaRVolume = MulDiv(vfNewRVolume - m_vfLastRVolume,dwPeriod << 8,dwLength);
  880. if (m_fMMXEnabled && (dwLength > 8))
  881. {
  882. dwMixChoice |= SPLAY_MMX;
  883. }
  884. dwMixChoice |= m_Source.m_bSampleType;
  885. dwStart = 0;
  886. for (;;)
  887. {
  888. if (dwLength <= 8)
  889. {
  890. dwMixChoice &= ~SPLAY_MMX;
  891. }
  892. if (m_fElGrande)
  893. {
  894. BeforeBigSampleMix();
  895. }
  896. if (m_Source.m_bOneShot)
  897. {
  898. pfEnd = m_pfSampleLength;
  899. pfLoopLen = 0;
  900. }
  901. else
  902. {
  903. pfEnd = m_pfLoopEnd;
  904. pfLoopLen = m_pfLoopEnd - m_pfLoopStart;
  905. if (pfLoopLen <= pfNewPitch)
  906. {
  907. return FALSE;
  908. }
  909. }
  910. switch (dwMixChoice)
  911. {
  912. case SFORMAT_8 | SPLAY_STEREO :
  913. dwSoFar = Mix8(&pBuffer[dwStart],dwLength,dwPeriod,
  914. vfDeltaLVolume, vfDeltaRVolume,
  915. pfDeltaPitch,
  916. pfEnd, pfLoopLen);
  917. break;
  918. case SFORMAT_8 :
  919. dwSoFar = MixMono8(&pBuffer[dwStart],dwLength,dwPeriod,
  920. vfDeltaLVolume,
  921. pfDeltaPitch,
  922. pfEnd, pfLoopLen);
  923. break;
  924. case SFORMAT_16 | SPLAY_STEREO :
  925. dwSoFar = Mix16(&pBuffer[dwStart],dwLength,dwPeriod,
  926. vfDeltaLVolume, vfDeltaRVolume,
  927. pfDeltaPitch,
  928. pfEnd, pfLoopLen);
  929. break;
  930. case SFORMAT_16 :
  931. dwSoFar = MixMono16(&pBuffer[dwStart],dwLength,dwPeriod,
  932. vfDeltaLVolume,
  933. pfDeltaPitch,
  934. pfEnd, pfLoopLen);
  935. break;
  936. #ifdef MMX_ENABLED
  937. case SFORMAT_8 | SPLAY_MMX | SPLAY_STEREO :
  938. dwSoFar = Mix8X(&pBuffer[dwStart],dwLength,dwPeriod,
  939. vfDeltaLVolume, vfDeltaRVolume ,
  940. pfDeltaPitch,
  941. pfEnd, pfLoopLen);
  942. break;
  943. case SFORMAT_16 | SPLAY_MMX | SPLAY_STEREO :
  944. dwSoFar = Mix16X(&pBuffer[dwStart],dwLength,dwPeriod,
  945. vfDeltaLVolume, vfDeltaRVolume,
  946. pfDeltaPitch,
  947. pfEnd, pfLoopLen);
  948. break;
  949. case SFORMAT_8 | SPLAY_MMX :
  950. dwSoFar = MixMono8X(&pBuffer[dwStart],dwLength,dwPeriod,
  951. vfDeltaLVolume,
  952. pfDeltaPitch,
  953. pfEnd, pfLoopLen);
  954. break;
  955. case SFORMAT_16 | SPLAY_MMX :
  956. dwSoFar = MixMono16X(&pBuffer[dwStart],dwLength,dwPeriod,
  957. vfDeltaLVolume,
  958. pfDeltaPitch,
  959. pfEnd, pfLoopLen);
  960. break;
  961. #endif
  962. default :
  963. return (FALSE);
  964. }
  965. if (m_fElGrande)
  966. {
  967. AfterBigSampleMix();
  968. }
  969. if (m_Source.m_bOneShot)
  970. {
  971. if (dwSoFar < dwLength)
  972. {
  973. return (FALSE);
  974. }
  975. break;
  976. }
  977. else
  978. {
  979. if (dwSoFar >= dwLength) break;
  980. // !!! even though we often handle loops in the mix function, sometimes
  981. // we don't, so we still need this code.
  982. // otherwise we must have reached the loop's end.
  983. dwStart += dwSoFar << dwStereo;
  984. dwLength -= dwSoFar;
  985. m_pfLastSample -= (m_pfLoopEnd - m_pfLoopStart);
  986. }
  987. }
  988. m_vfLastLVolume = vfNewLVolume;
  989. m_vfLastRVolume = vfNewRVolume;
  990. m_pfLastPitch = pfNewPitch;
  991. return (TRUE);
  992. }
  993. /*****************************************************************************
  994. * CVoice::CVoice()
  995. *****************************************************************************
  996. * Constructor for the CVoice object.
  997. */
  998. CVoice::CVoice()
  999. {
  1000. m_pControl = NULL;
  1001. m_pPitchBendIn = NULL;
  1002. m_pExpressionIn = NULL;
  1003. m_dwPriority = 0;
  1004. m_nPart = 0;
  1005. m_nKey = 0;
  1006. m_fInUse = FALSE;
  1007. m_fSustainOn = FALSE;
  1008. m_fNoteOn = FALSE;
  1009. m_fTag = FALSE;
  1010. m_stStartTime = 0;
  1011. m_stStopTime = 0x7fffffffffffffff;
  1012. m_vrVolume = 0;
  1013. m_fAllowOverlap = FALSE;
  1014. }
  1015. /*****************************************************************************
  1016. * svrPanToVREL[]
  1017. *****************************************************************************
  1018. * A table for 0-127 translation to log scale.
  1019. * value = log10(index/127) * 1000 where index = 0..128
  1020. */
  1021. const VREL svrPanToVREL[] = {
  1022. // 0 1 2 3 4 5 6 7
  1023. -2500, -2103, -1802, -1626, -1501, -1404, -1325, -1258,
  1024. -1200, -1149, -1103, -1062, -1024, -989, -957, -927,
  1025. -899, -873, -848, -825, -802, -781, -761, -742,
  1026. -723, -705, -688, -672, -656, -641, -626, -612,
  1027. -598, -585, -572, -559, -547, -535, -524, -512,
  1028. -501, -491, -480, -470, -460, -450, -441, -431,
  1029. -422, -413, -404, -396, -387, -379, -371, -363,
  1030. -355, -347, -340, -332, -325, -318, -311, -304,
  1031. -297, -290, -284, -277, -271, -264, -258, -252,
  1032. -246, -240, -234, -228, -222, -217, -211, -206,
  1033. -200, -195, -189, -184, -179, -174, -169, -164,
  1034. -159, -154, -149, -144, -140, -135, -130, -126,
  1035. -121, -117, -112, -108, -103, -99, -95, -90,
  1036. -86, -82, -78, -74, -70, -66, -62, -58,
  1037. -54, -50, -46, -43, -39, -35, -31, -28,
  1038. -24, -21, -17, -13, -10, -6, -3, 0
  1039. };
  1040. /*****************************************************************************
  1041. * CVoice::StopVoice()
  1042. *****************************************************************************
  1043. * Stop the voice if it is playing. Reset the envelope generators, sustain.
  1044. */
  1045. void CVoice::StopVoice(STIME stTime)
  1046. {
  1047. if (m_fNoteOn)
  1048. {
  1049. if (stTime <= m_stStartTime) stTime = m_stStartTime + 1;
  1050. m_PitchEG.StopVoice(stTime);
  1051. m_VolumeEG.StopVoice(stTime);
  1052. m_fNoteOn = FALSE;
  1053. m_fSustainOn = FALSE;
  1054. m_stStopTime = stTime;
  1055. }
  1056. }
  1057. /*****************************************************************************
  1058. * CVoice::QuickStopVoice()
  1059. *****************************************************************************
  1060. * Stop the voice ASAP. If the note is on or sustaining, turn everything off
  1061. * now, otherwise we just have to stop the volume EG (kill the decay curve).
  1062. */
  1063. void CVoice::QuickStopVoice(STIME stTime)
  1064. {
  1065. m_fTag = TRUE;
  1066. if (m_fNoteOn || m_fSustainOn)
  1067. {
  1068. if (stTime <= m_stStartTime) stTime = m_stStartTime + 1;
  1069. m_PitchEG.StopVoice(stTime);
  1070. m_VolumeEG.QuickStopVoice(stTime,m_pSynth->m_dwSampleRate);
  1071. m_fNoteOn = FALSE;
  1072. m_fSustainOn = FALSE;
  1073. m_stStopTime = stTime;
  1074. }
  1075. else
  1076. {
  1077. m_VolumeEG.QuickStopVoice(m_stStopTime,m_pSynth->m_dwSampleRate);
  1078. }
  1079. }
  1080. /*****************************************************************************
  1081. * CVoice::StartVoice()
  1082. *****************************************************************************
  1083. * Start the voice with all the given parameters for pitch, mod, pan, etc.
  1084. * The region must have an articulation and a wave. Mix it now if the
  1085. * start time dictates.
  1086. */
  1087. BOOL CVoice::StartVoice(CSynth *pSynth,
  1088. CSourceRegion *pRegion, STIME stStartTime,
  1089. CModWheelIn * pModWheelIn,
  1090. CPitchBendIn * pPitchBendIn,
  1091. CExpressionIn * pExpressionIn,
  1092. CVolumeIn * pVolumeIn,
  1093. CPanIn * pPanIn,
  1094. WORD nKey,WORD nVelocity,
  1095. VREL vrVolume,
  1096. PREL prPitch)
  1097. {
  1098. CSourceArticulation * pArticulation = pRegion->m_pArticulation;
  1099. if (pArticulation == NULL)
  1100. {
  1101. return FALSE;
  1102. }
  1103. // if we're going to handle volume later, don't read it now.
  1104. if (!pSynth->m_fAllowVolumeChangeWhilePlayingNote)
  1105. vrVolume += pVolumeIn->GetVolume(stStartTime);
  1106. prPitch += pRegion->m_prTuning;
  1107. m_dwGroup = pRegion->m_bGroup;
  1108. m_fAllowOverlap = pRegion->m_bAllowOverlap;
  1109. m_pSynth = pSynth;
  1110. vrVolume += CMIDIRecorder::VelocityToVolume(nVelocity);
  1111. // * (long) pArticulation->m_sVelToVolScale) / -9600);
  1112. vrVolume += pRegion->m_vrAttenuation;
  1113. m_lDefaultPan = pRegion->m_pArticulation->m_sDefaultPan;
  1114. // ignore pan here if allowing pan to vary after note starts
  1115. VREL vrLVolume;
  1116. VREL vrRVolume;
  1117. if (pSynth->m_dwStereo && !pSynth->m_fAllowPanWhilePlayingNote) {
  1118. long lPan = pPanIn->GetPan(stStartTime) + m_lDefaultPan;
  1119. if (lPan < 0) lPan = 0;
  1120. if (lPan > 127) lPan = 127;
  1121. vrLVolume = ::svrPanToVREL[127 - lPan] + vrVolume;
  1122. vrRVolume = ::svrPanToVREL[lPan] + vrVolume;
  1123. } else {
  1124. vrLVolume = vrVolume;
  1125. vrRVolume = vrVolume;
  1126. }
  1127. m_stMixTime = m_LFO.StartVoice(&pArticulation->m_LFO,
  1128. stStartTime, pModWheelIn);
  1129. STIME stMixTime = m_PitchEG.StartVoice(&pArticulation->m_PitchEG,
  1130. stStartTime, nKey, nVelocity);
  1131. if (stMixTime < m_stMixTime)
  1132. {
  1133. m_stMixTime = stMixTime;
  1134. }
  1135. stMixTime = m_VolumeEG.StartVoice(&pArticulation->m_VolumeEG,
  1136. stStartTime, nKey, nVelocity);
  1137. if (stMixTime < m_stMixTime)
  1138. {
  1139. m_stMixTime = stMixTime;
  1140. }
  1141. if (m_stMixTime > pSynth->m_stMaxSpan)
  1142. {
  1143. m_stMixTime = pSynth->m_stMaxSpan;
  1144. }
  1145. // Make sure we have a pointer to the wave ready:
  1146. if ((pRegion->m_Sample.m_pWave == NULL) || (pRegion->m_Sample.m_pWave->m_pnWave == NULL))
  1147. {
  1148. return (FALSE); // Do nothing if no sample.
  1149. }
  1150. m_DigitalAudio.StartVoice(pSynth,&pRegion->m_Sample,
  1151. vrLVolume, vrRVolume, prPitch, (long)nKey);
  1152. m_pPitchBendIn = pPitchBendIn;
  1153. m_pExpressionIn = pExpressionIn;
  1154. m_pPanIn = pPanIn;
  1155. m_pVolumeIn = pVolumeIn;
  1156. m_fNoteOn = TRUE;
  1157. m_fTag = FALSE;
  1158. m_stStartTime = stStartTime;
  1159. m_stLastMix = stStartTime - 1;
  1160. m_stStopTime = 0x7fffffffffffffff;
  1161. if (m_stMixTime == 0)
  1162. {
  1163. // zero length attack, be sure it isn't missed....
  1164. PREL prNewPitch = GetNewPitch(stStartTime);
  1165. VREL vrVolumeL, vrVolumeR;
  1166. GetNewVolume(stStartTime, vrVolumeL, vrVolumeR);
  1167. if (m_stMixTime > pSynth->m_stMaxSpan)
  1168. {
  1169. m_stMixTime = pSynth->m_stMaxSpan;
  1170. }
  1171. m_DigitalAudio.Mix(NULL, 0,
  1172. vrVolumeL, vrVolumeR, prNewPitch,
  1173. m_pSynth->m_dwStereo);
  1174. }
  1175. m_vrVolume = 0;
  1176. return (TRUE);
  1177. }
  1178. /*****************************************************************************
  1179. * CVoice::ClearVoice()
  1180. *****************************************************************************
  1181. * Clear the voice (just forward to the digital audio peer object).
  1182. */
  1183. void CVoice::ClearVoice()
  1184. {
  1185. m_fInUse = FALSE;
  1186. m_DigitalAudio.ClearVoice();
  1187. }
  1188. /*****************************************************************************
  1189. * CVoice::GetNewVolume()
  1190. *****************************************************************************
  1191. * Return the volume delta at time <stTime>.
  1192. * Volume is sum of volume envelope, LFO, expression, optionally the
  1193. * channel volume if we're allowing it to change, and optionally the current
  1194. * pan if we're allowing that to change.
  1195. * This will be added to the base volume calculated in CVoice::StartVoice().
  1196. */
  1197. void CVoice::GetNewVolume(STIME stTime, VREL& vrVolume, VREL &vrVolumeR)
  1198. {
  1199. STIME stMixTime;
  1200. vrVolume = m_VolumeEG.GetVolume(stTime,&stMixTime);
  1201. if (stMixTime < m_stMixTime) m_stMixTime = stMixTime;
  1202. // save pre-LFO volume for code that detects whether this note is off
  1203. m_vrVolume = vrVolume;
  1204. vrVolume += m_LFO.GetVolume(stTime,&stMixTime);
  1205. if (stMixTime < m_stMixTime) m_stMixTime = stMixTime;
  1206. vrVolume += m_pExpressionIn->GetVolume(stTime);
  1207. if (m_pSynth->m_fAllowVolumeChangeWhilePlayingNote)
  1208. vrVolume += m_pVolumeIn->GetVolume(stTime);
  1209. vrVolume += m_pSynth->m_vrGainAdjust;
  1210. vrVolumeR = vrVolume;
  1211. // handle pan here if allowing pan to vary after note starts
  1212. if (m_pSynth->m_dwStereo &&
  1213. m_pSynth->m_fAllowPanWhilePlayingNote)
  1214. {
  1215. // add current pan & instrument default pan
  1216. LONG lPan = m_pPanIn->GetPan(stTime) + m_lDefaultPan;
  1217. // don't go off either end....
  1218. if (lPan < 0) lPan = 0;
  1219. if (lPan > 127) lPan = 127;
  1220. vrVolume += ::svrPanToVREL[127 - lPan];
  1221. vrVolumeR += ::svrPanToVREL[lPan];
  1222. }
  1223. }
  1224. /*****************************************************************************
  1225. * CVoice::GetNewPitch()
  1226. *****************************************************************************
  1227. * Returns the current pitch for time <stTime>.
  1228. * Pitch is the sum of the pitch LFO, the pitch envelope, and the current
  1229. * pitch bend.
  1230. */
  1231. PREL CVoice::GetNewPitch(STIME stTime)
  1232. {
  1233. STIME stMixTime;
  1234. PREL prPitch = m_LFO.GetPitch(stTime,&stMixTime);
  1235. if (m_stMixTime > stMixTime) m_stMixTime = stMixTime;
  1236. prPitch += m_PitchEG.GetPitch(stTime,&stMixTime);
  1237. if (m_stMixTime > stMixTime) m_stMixTime = stMixTime;
  1238. prPitch += m_pPitchBendIn->GetPitch(stTime);
  1239. return prPitch;
  1240. }
  1241. /*****************************************************************************
  1242. * CVoice::Mix()
  1243. *****************************************************************************
  1244. * Mix this voice into the given buffer. Determine certain volume and pitch
  1245. * parameters, then call into the Digital Audio Engine.
  1246. */
  1247. DWORD CVoice::Mix( short *pBuffer, DWORD dwLength,
  1248. STIME stStart, STIME stEnd)
  1249. {
  1250. BOOL fInUse = TRUE;
  1251. BOOL fFullMix = TRUE;
  1252. STIME stEndMix = stStart;
  1253. STIME stStartMix = m_stStartTime;
  1254. if (stStartMix < stStart)
  1255. {
  1256. stStartMix = stStart;
  1257. }
  1258. if (m_stLastMix >= stEnd)
  1259. {
  1260. return (0);
  1261. }
  1262. if (m_stLastMix >= stStartMix)
  1263. {
  1264. stStartMix = m_stLastMix;
  1265. }
  1266. while (stStartMix < stEnd && fInUse)
  1267. {
  1268. stEndMix = stStartMix + m_stMixTime;
  1269. if (stEndMix > stEnd)
  1270. {
  1271. stEndMix = stEnd;
  1272. }
  1273. m_stMixTime = m_pSynth->m_stMaxSpan;
  1274. if ((m_stLastMix < m_stStopTime) && (m_stStopTime < stEnd))
  1275. {
  1276. if (m_stMixTime > (m_stStopTime - m_stLastMix))
  1277. {
  1278. m_stMixTime = m_stStopTime - m_stLastMix;
  1279. }
  1280. }
  1281. PREL prPitch = GetNewPitch(stEndMix);
  1282. VREL vrVolume, vrVolumeR;
  1283. GetNewVolume(stEndMix, vrVolume, vrVolumeR);
  1284. if (m_VolumeEG.InRelease(stEndMix))
  1285. {
  1286. if (m_vrVolume < PERCEIVED_MIN_VOLUME) // End of release slope
  1287. {
  1288. fInUse = FALSE;
  1289. }
  1290. }
  1291. fFullMix = m_DigitalAudio.Mix(&pBuffer[(stStartMix - stStart) <<
  1292. m_pSynth->m_dwStereo],
  1293. (DWORD) (stEndMix - stStartMix),
  1294. vrVolume, vrVolumeR, prPitch,
  1295. m_pSynth->m_dwStereo);
  1296. stStartMix = stEndMix;
  1297. }
  1298. m_fInUse = fInUse && fFullMix;
  1299. if (!m_fInUse)
  1300. {
  1301. ClearVoice();
  1302. m_stStopTime = stEndMix; // For measurement purposes.
  1303. }
  1304. m_stLastMix = stEndMix;
  1305. return (dwLength);
  1306. }