temuopl.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * AdPlug - Replayer for many OPL2/OPL3 audio file formats.
  3. * Copyright (C) 1999 - 2004 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. * temuopl.cpp - Tatsuyuki Satoh's OPL2 emulator, by Simon Peter <dn.tlp@gmx.net>
  20. */
  21. #include "temuopl.h"
  22. CTemuopl::CTemuopl(int rate, bool bit16, bool usestereo)
  23. : use16bit(bit16), stereo(usestereo)
  24. {
  25. opl = OPLCreate(OPL_TYPE_YM3812, 3579545, rate);
  26. }
  27. CTemuopl::~CTemuopl()
  28. {
  29. OPLDestroy(opl);
  30. }
  31. void CTemuopl::update(short *buf, int samples)
  32. {
  33. int i;
  34. if(use16bit) {
  35. YM3812UpdateOne(opl,buf,samples);
  36. if(stereo)
  37. for(i=samples-1;i>=0;i--) {
  38. buf[i*2] = buf[i];
  39. buf[i*2+1] = buf[i];
  40. }
  41. } else {
  42. short *tempbuf = new short[stereo ? samples*2 : samples];
  43. int i;
  44. YM3812UpdateOne(opl,tempbuf,samples);
  45. if(stereo)
  46. for(i=samples-1;i>=0;i--) {
  47. tempbuf[i*2] = tempbuf[i];
  48. tempbuf[i*2+1] = tempbuf[i];
  49. }
  50. for(i=0;i<(stereo ? samples*2 : samples);i++)
  51. ((char *)buf)[i] = (tempbuf[i] >> 8) ^ 0x80;
  52. delete [] tempbuf;
  53. }
  54. }
  55. void CTemuopl::write(int reg, int val)
  56. {
  57. OPLWrite(opl,0,reg);
  58. OPLWrite(opl,1,val);
  59. }
  60. void CTemuopl::init()
  61. {
  62. OPLResetChip(opl);
  63. }