surroundopl.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Adplug - Replayer for many OPL2/OPL3 audio file formats.
  3. * Copyright (C) 1999 - 2010 Simon Peter, <dn.tlp@gmx.net>, et al.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * surroundopl.cpp - Wrapper class to provide a surround/harmonic effect
  20. * for another OPL emulator, by Adam Nielsen <malvineous@shikadi.net>
  21. *
  22. * Stereo harmonic algorithm by Adam Nielsen <malvineous@shikadi.net>
  23. * Please give credit if you use this algorithm elsewhere :-)
  24. */
  25. #include <math.h> // for pow()
  26. #include "surroundopl.h"
  27. //#include "debug.h"
  28. CSurroundopl::CSurroundopl(Copl *a, Copl *b, bool use16bit)
  29. : use16bit(use16bit),
  30. bufsize(4096),
  31. a(a), b(b)
  32. {
  33. currType = TYPE_OPL2;
  34. this->lbuf = new short[this->bufsize];
  35. this->rbuf = new short[this->bufsize];
  36. };
  37. CSurroundopl::~CSurroundopl()
  38. {
  39. delete[] this->rbuf;
  40. delete[] this->lbuf;
  41. delete a;
  42. delete b;
  43. }
  44. void CSurroundopl::update(short *buf, int samples)
  45. {
  46. if (samples * 2 > this->bufsize) {
  47. // Need to realloc the buffer
  48. delete[] this->rbuf;
  49. delete[] this->lbuf;
  50. this->bufsize = samples * 2;
  51. this->lbuf = new short[this->bufsize];
  52. this->rbuf = new short[this->bufsize];
  53. }
  54. a->update(this->lbuf, samples);
  55. b->update(this->rbuf, samples);
  56. // Copy the two mono OPL buffers into the stereo buffer
  57. for (int i = 0; i < samples; i++) {
  58. if (this->use16bit) {
  59. buf[i * 2] = this->lbuf[i];
  60. buf[i * 2 + 1] = this->rbuf[i];
  61. } else {
  62. ((char *)buf)[i * 2] = ((char *)this->lbuf)[i];
  63. ((char *)buf)[i * 2 + 1] = ((char *)this->rbuf)[i];
  64. }
  65. }
  66. }
  67. // template methods
  68. void CSurroundopl::write(int reg, int val)
  69. {
  70. a->write(reg, val);
  71. // Transpose the other channel to produce the harmonic effect
  72. int iChannel = -1;
  73. int iRegister = reg; // temp
  74. int iValue = val; // temp
  75. if ((iRegister >> 4 == 0xA) || (iRegister >> 4 == 0xB)) iChannel = iRegister & 0x0F;
  76. // Remember the FM state, so that the harmonic effect can access
  77. // previously assigned register values.
  78. /*if (((iRegister >> 4 == 0xB) && (iValue & 0x20) && !(this->iFMReg[iRegister] & 0x20)) ||
  79. (iRegister == 0xBD) && (
  80. ((iValue & 0x01) && !(this->iFMReg[0xBD] & 0x01))
  81. )) {
  82. this->iFMReg[iRegister] = iValue;
  83. }*/
  84. this->iFMReg[iRegister] = iValue;
  85. if ((iChannel >= 0)) {// && (i == 1)) {
  86. unsigned char iBlock = (this->iFMReg[0xB0 + iChannel] >> 2) & 0x07;
  87. unsigned short iFNum = ((this->iFMReg[0xB0 + iChannel] & 0x03) << 8) | this->iFMReg[0xA0 + iChannel];
  88. //double dbOriginalFreq = 50000.0 * (double)iFNum * pow(2, iBlock - 20);
  89. double dbOriginalFreq = 49716.0 * (double)iFNum * pow(2.0, iBlock - 20);
  90. unsigned char iNewBlock = iBlock;
  91. unsigned short iNewFNum;
  92. // Adjust the frequency and calculate the new FNum
  93. //double dbNewFNum = (dbOriginalFreq+(dbOriginalFreq/FREQ_OFFSET)) / (50000.0 * pow(2.0, iNewBlock - 20));
  94. //#define calcFNum() ((dbOriginalFreq+(dbOriginalFreq/FREQ_OFFSET)) / (50000.0 * pow(2.0, iNewBlock - 20)))
  95. #define calcFNum() ((dbOriginalFreq+(dbOriginalFreq/FREQ_OFFSET)) / (49716.0 * pow(2.0, iNewBlock - 20)))
  96. double dbNewFNum = calcFNum();
  97. // Make sure it's in range for the OPL chip
  98. if (dbNewFNum > 1023 - NEWBLOCK_LIMIT) {
  99. // It's too high, so move up one block (octave) and recalculate
  100. if (iNewBlock > 6) {
  101. // Uh oh, we're already at the highest octave!
  102. // AdPlug_LogWrite("OPL WARN: FNum %d/B#%d would need block 8+ after being transposed (new FNum is %d)\n",
  103. // iFNum, iBlock, (int)dbNewFNum);
  104. // The best we can do here is to just play the same note out of the second OPL, so at least it shouldn't
  105. // sound *too* bad (hopefully it will just miss out on the nice harmonic.)
  106. iNewBlock = iBlock;
  107. iNewFNum = iFNum;
  108. } else {
  109. iNewBlock++;
  110. iNewFNum = (unsigned short)calcFNum();
  111. }
  112. } else if (dbNewFNum < 0 + NEWBLOCK_LIMIT) {
  113. // It's too low, so move down one block (octave) and recalculate
  114. if (iNewBlock == 0) {
  115. // Uh oh, we're already at the lowest octave!
  116. // AdPlug_LogWrite("OPL WARN: FNum %d/B#%d would need block -1 after being transposed (new FNum is %d)!\n",
  117. // iFNum, iBlock, (int)dbNewFNum);
  118. // The best we can do here is to just play the same note out of the second OPL, so at least it shouldn't
  119. // sound *too* bad (hopefully it will just miss out on the nice harmonic.)
  120. iNewBlock = iBlock;
  121. iNewFNum = iFNum;
  122. } else {
  123. iNewBlock--;
  124. iNewFNum = (unsigned short)calcFNum();
  125. }
  126. } else {
  127. // Original calculation is within range, use that
  128. iNewFNum = (unsigned short)dbNewFNum;
  129. }
  130. // Sanity check
  131. if (iNewFNum > 1023) {
  132. // Uh oh, the new FNum is still out of range! (This shouldn't happen)
  133. // AdPlug_LogWrite("OPL ERR: Original note (FNum %d/B#%d is still out of range after change to FNum %d/B#%d!\n",
  134. // iFNum, iBlock, iNewFNum, iNewBlock);
  135. // The best we can do here is to just play the same note out of the second OPL, so at least it shouldn't
  136. // sound *too* bad (hopefully it will just miss out on the nice harmonic.)
  137. iNewBlock = iBlock;
  138. iNewFNum = iFNum;
  139. }
  140. if ((iRegister >= 0xB0) && (iRegister <= 0xB8)) {
  141. // Overwrite the supplied value with the new F-Number and Block.
  142. iValue = (iValue & ~0x1F) | (iNewBlock << 2) | ((iNewFNum >> 8) & 0x03);
  143. this->iCurrentTweakedBlock[iChannel] = iNewBlock; // save it so we don't have to update register 0xB0 later on
  144. this->iCurrentFNum[iChannel] = (unsigned char)iNewFNum;
  145. if (this->iTweakedFMReg[0xA0 + iChannel] != (iNewFNum & 0xFF)) {
  146. // Need to write out low bits
  147. unsigned char iAdditionalReg = 0xA0 + iChannel;
  148. unsigned char iAdditionalValue = iNewFNum & 0xFF;
  149. b->write(iAdditionalReg, iAdditionalValue);
  150. this->iTweakedFMReg[iAdditionalReg] = iAdditionalValue;
  151. }
  152. } else if ((iRegister >= 0xA0) && (iRegister <= 0xA8)) {
  153. // Overwrite the supplied value with the new F-Number.
  154. iValue = iNewFNum & 0xFF;
  155. // See if we need to update the block number, which is stored in a different register
  156. unsigned char iNewB0Value = (this->iFMReg[0xB0 + iChannel] & ~0x1F) | (iNewBlock << 2) | ((iNewFNum >> 8) & 0x03);
  157. if (
  158. (iNewB0Value & 0x20) && // but only update if there's a note currently playing (otherwise we can just wait
  159. (this->iTweakedFMReg[0xB0 + iChannel] != iNewB0Value) // until the next noteon and update it then)
  160. ) {
  161. // AdPlug_LogWrite("OPL INFO: CH%d - FNum %d/B#%d -> FNum %d/B#%d == keyon register update!\n",
  162. // iChannel, iFNum, iBlock, iNewFNum, iNewBlock);
  163. // The note is already playing, so we need to adjust the upper bits too
  164. unsigned char iAdditionalReg = 0xB0 + iChannel;
  165. b->write(iAdditionalReg, iNewB0Value);
  166. this->iTweakedFMReg[iAdditionalReg] = iNewB0Value;
  167. } // else the note is not playing, the upper bits will be set when the note is next played
  168. } // if (register 0xB0 or 0xA0)
  169. } // if (a register we're interested in)
  170. // Now write to the original register with a possibly modified value
  171. b->write(iRegister, iValue);
  172. this->iTweakedFMReg[iRegister] = iValue;
  173. };
  174. void CSurroundopl::init() {};