demuopl.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // demuopl.cpp
  3. // SDLPal
  4. //
  5. // Created by palxex on 14-7-20.
  6. // Copyright (c) 2014 Wei Mingzhi. All rights reserved.
  7. //
  8. #include "demuopl.h"
  9. #include <string.h>
  10. CDemuopl::CDemuopl(int rate, bool bit16, bool usestereo)
  11. : use16bit(bit16), stereo(usestereo), rate(rate), chip(adlib_init(rate))
  12. {
  13. currType = TYPE_OPL2;
  14. }
  15. CDemuopl::~CDemuopl()
  16. {
  17. adlib_release(chip);
  18. }
  19. void CDemuopl::init()
  20. {
  21. adlib_release(chip);
  22. chip = adlib_init(rate);
  23. }
  24. void CDemuopl::update(short *buf, int samples)
  25. {
  26. if (!chip) return;
  27. short *mixbuf1 = NULL;
  28. short *outbuf;
  29. if (use16bit)
  30. outbuf = buf;
  31. else
  32. outbuf = mixbuf1 = new short[samples * 2];
  33. adlib_getsample(chip, outbuf, samples);
  34. if (stereo) {
  35. for (int i = samples - 1; i >= 0; i--) {
  36. outbuf[i * 2] = outbuf[i];
  37. outbuf[i * 2 + 1] = outbuf[i];
  38. }
  39. }
  40. //now reduce to 8bit if we need to
  41. if (!use16bit) {
  42. for (int i = 0; i < (stereo ? samples * 2 : samples); i++)
  43. ((char *)buf)[i] = (outbuf[i] >> 8) ^ 0x80;
  44. delete[] mixbuf1;
  45. }
  46. }
  47. // template methods
  48. void CDemuopl::write(int reg, int val)
  49. {
  50. if (chip) adlib_write(chip, reg, val);
  51. }