emuopl.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * AdPlug - Replayer for many OPL2/OPL3 audio file formats.
  3. * Copyright (C) 1999 - 2005 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. * emuopl.cpp - Emulated OPL, by Simon Peter <dn.tlp@gmx.net>
  20. */
  21. #include "emuopl.h"
  22. #ifndef NULL
  23. #define NULL 0
  24. #endif
  25. CEmuopl::CEmuopl(int rate, bool bit16, bool usestereo)
  26. : use16bit(bit16), stereo(usestereo), mixbufSamples(0), rate(rate) {
  27. //opl[0] = OPLCreate(OPL_TYPE_YM3812, 3579545, rate);
  28. //opl[1] = OPLCreate(OPL_TYPE_YM3812, 3579545, rate);
  29. opl[0] = opl[1] = NULL;
  30. currType = TYPE_OPL2;
  31. init();
  32. }
  33. CEmuopl::~CEmuopl() {
  34. OPLDestroy(opl[0]);
  35. OPLDestroy(opl[1]);
  36. if (mixbufSamples) {
  37. delete [] mixbuf0;
  38. delete [] mixbuf1;
  39. }
  40. }
  41. void CEmuopl::update(short *buf, int samples) {
  42. int i;
  43. //ensure that our mix buffers are adequately sized
  44. if (mixbufSamples < samples) {
  45. if (mixbufSamples) {
  46. delete[] mixbuf0;
  47. delete[] mixbuf1;
  48. }
  49. mixbufSamples = samples;
  50. //*2 = make room for stereo, if we need it
  51. mixbuf0 = new short[samples*2];
  52. mixbuf1 = new short[samples*2];
  53. }
  54. //data should be rendered to outbuf
  55. //tempbuf should be used as a temporary buffer
  56. //if we are supposed to generate 16bit output,
  57. //then outbuf may point directly to the actual waveform output "buf"
  58. //if we are supposed to generate 8bit output,
  59. //then outbuf cannot point to "buf" (because there will not be enough room)
  60. //and so it must point to a mixbuf instead--
  61. //it will be reduced to 8bit and put in "buf" later
  62. short *outbuf;
  63. short *tempbuf=mixbuf0;
  64. short *tempbuf2=mixbuf1;
  65. if (use16bit) outbuf = buf;
  66. else outbuf = mixbuf1;
  67. //...there is a potentially confusing situation where mixbuf1 can be aliased.
  68. //beware. it is a little loony.
  69. //all of the following rendering code produces 16bit output
  70. switch (currType) {
  71. case TYPE_OPL2:
  72. //for opl2 mode:
  73. //render chip0 to the output buffer
  74. YM3812UpdateOne(opl[0],outbuf,samples);
  75. //if we are supposed to output stereo,
  76. //then we need to dup the mono channel
  77. if (stereo)
  78. for (i=samples-1;i>=0;i--) {
  79. outbuf[i*2] = outbuf[i];
  80. outbuf[i*2+1] = outbuf[i];
  81. }
  82. break;
  83. case TYPE_OPL3: // unsupported
  84. break;
  85. case TYPE_DUAL_OPL2:
  86. //for dual opl2 mode:
  87. //render each chip to a different tempbuffer
  88. YM3812UpdateOne(opl[0],tempbuf2,samples);
  89. YM3812UpdateOne(opl[1],tempbuf,samples);
  90. //output stereo:
  91. //then we need to interleave the two buffers
  92. if (stereo) {
  93. //first, spread tempbuf's samples across left channel
  94. //left channel
  95. for (i=0;i<samples;i++)
  96. outbuf[i*2] = tempbuf2[i];
  97. //next, insert the samples from tempbuf2 into right channel
  98. for (i=0;i<samples;i++)
  99. outbuf[i*2+1] = tempbuf[i];
  100. } else
  101. //output mono:
  102. //then we need to mix the two buffers into buf
  103. for (i=0;i<samples;i++)
  104. outbuf[i] = (tempbuf[i]>>1) + (tempbuf2[i]>>1);
  105. break;
  106. }
  107. //now reduce to 8bit if we need to
  108. if (!use16bit)
  109. for (i=0;i<(stereo ? samples*2 : samples);i++)
  110. ((char *)buf)[i] = (outbuf[i] >> 8) ^ 0x80;
  111. }
  112. void CEmuopl::write(int reg, int val) {
  113. switch (currType) {
  114. case TYPE_OPL2:
  115. case TYPE_DUAL_OPL2:
  116. OPLWrite(opl[currChip], 0, reg);
  117. OPLWrite(opl[currChip], 1, val);
  118. break;
  119. case TYPE_OPL3: // unsupported
  120. break;
  121. }
  122. }
  123. void CEmuopl::init() {
  124. if (opl[0]) OPLDestroy(opl[0]);
  125. if (opl[1]) OPLDestroy(opl[1]);
  126. opl[0] = OPLCreate(OPL_TYPE_YM3812, 3579545, rate);
  127. opl[1] = OPLCreate(OPL_TYPE_YM3812, 3579545, rate);
  128. OPLResetChip(opl[0]);
  129. OPLResetChip(opl[1]);
  130. currChip = 0;
  131. }
  132. void CEmuopl::settype(ChipType type) {
  133. currType = type;
  134. }