emuopl.cpp 4.2 KB

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