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