123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // demuopl.cpp
- // SDLPal
- //
- // Created by palxex on 14-7-20.
- // Copyright (c) 2014年 Wei Mingzhi. All rights reserved.
- //
- #include "demuopl.h"
- #include <math.h>
- #include <stdlib.h> // rand()
- #include <string.h>
- int CDemuopl::channels = 0;
- struct OPLHandler {
- virtual void init(int rate) = 0;
- virtual void getsample(short *buf,int samples) = 0;
- virtual void write(int reg,int val) = 0;
- };
- namespace OPLCore {
- #include "dosbox_opl.cpp"
- struct DOSBoxOPLHandler : OPLHandler {
- void init(int rate) {
- adlib_init(rate);
- }
- void getsample(short *buf, int samples) {
- adlib_getsample(buf, samples);
- }
- void write(int reg, int val) {
- adlib_write(reg, val);
- }
- };
- }
- namespace OPLCore2 {
- #include "dosbox_opl.cpp"
- struct DOSBoxOPLHandler : OPLHandler {
- void init(int rate) {
- adlib_init(rate);
- }
- void getsample(short *buf, int samples) {
- adlib_getsample(buf, samples);
- }
- void write(int reg, int val) {
- adlib_write(reg, val);
- }
- };
- }
- CDemuopl::CDemuopl(int rate, bool bit16, bool usestereo)
- :use16bit(bit16), stereo(usestereo)
- {
- if (channels++ == 0)
- pHandler = new OPLCore::DOSBoxOPLHandler;
- else
- pHandler = new OPLCore2::DOSBoxOPLHandler;
- pHandler->init(rate);
- currType = TYPE_OPL2;
- }
- void CDemuopl::update(short *buf, int samples)
- {
- short *mixbuf1 = NULL;
- short *outbuf;
- if (use16bit) outbuf = buf;
- else{
- mixbuf1 = new short[samples * 2];
- outbuf = mixbuf1;
- }
- pHandler->getsample(outbuf, samples);
- if (stereo)
- for (int i = samples - 1; i >= 0; i--) {
- outbuf[i * 2] = outbuf[i];
- outbuf[i * 2 + 1] = outbuf[i];
- }
- //now reduce to 8bit if we need to
- if (!use16bit) {
- for (int i = 0; i < (stereo ? samples * 2 : samples); i++)
- ((char *)buf)[i] = (outbuf[i] >> 8) ^ 0x80;
- delete[] mixbuf1;
- }
- }
- // template methods
- void CDemuopl::write(int reg, int val)
- {
- pHandler->write(reg, val);
- };
|