Debian Codegraph @

src/gui/fmPreview.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _USE_MATH_DEFINES
21 #include "gui.h"
22 #include "../../extern/opn/ym3438.h"
23 #include "../../extern/opm/opm.h"
24 #include "../../extern/opl/opl3.h"
25 #include "../../extern/ESFMu/esfm.h"
26 extern "C" {
27 #include "../../extern/Nuked-OPLL/opll.h"
28 }
29 #include "../engine/platform/sound/ymfm/ymfm_opz.h"
30 #include "../engine/bsr.h"
31
32 #define OPN_WRITE(addr,val) \
33 OPN2_Write((ym3438_t*)fmPreviewOPN,0,(addr)); \
34 do { \
35 OPN2_Clock((ym3438_t*)fmPreviewOPN,out); \
36 } while (((ym3438_t*)fmPreviewOPN)->write_busy); \
37 OPN2_Write((ym3438_t*)fmPreviewOPN,1,(val)); \
38 do { \
39 OPN2_Clock((ym3438_t*)fmPreviewOPN,out); \
40 } while (((ym3438_t*)fmPreviewOPN)->write_busy);
41
42 const unsigned char dtTableFMP[8]={
43 7,6,5,0,1,2,3,4
44 };
45
46 void FurnaceGUI::renderFMPreviewOPN(const DivInstrumentFM& params, int pos) {
47 if (fmPreviewOPN==NULL) {
48 fmPreviewOPN=new ym3438_t;
49 pos=0;
50 }
51 short out[2];
52 int aOut=0;
53 bool mult0=false;
54
55 if (pos==0) {
56 OPN2_Reset((ym3438_t*)fmPreviewOPN);
57 OPN2_SetChipType((ym3438_t*)fmPreviewOPN,ym3438_mode_opn);
58
59 // set params
60 for (int i=0; i<4; i++) {
61 if ((params.op[i].mult&15)==0) {
62 mult0=true;
63 break;
64 }
65 }
66 for (int i=0; i<4; i++) {
67 const DivInstrumentFM::Operator& op=params.op[i];
68 unsigned short baseAddr=i*4;
69 OPN_WRITE(baseAddr+0x40,op.tl);
70 OPN_WRITE(baseAddr+0x30,(op.mult&15)|(dtTableFMP[op.dt&7]<<4));
71 OPN_WRITE(baseAddr+0x50,(op.ar&31)|(op.rs<<6));
72 OPN_WRITE(baseAddr+0x60,(op.dr&31)|(op.am<<7));
73 OPN_WRITE(baseAddr+0x70,op.d2r&31);
74 OPN_WRITE(baseAddr+0x80,(op.rr&15)|(op.sl<<4));
75 OPN_WRITE(baseAddr+0x90,op.ssgEnv&15);
76 }
77 OPN_WRITE(0xb0,(params.alg&7)|((params.fb&7)<<3));
78 OPN_WRITE(0xb4,0xc0|(params.fms&7)|((params.ams&3)<<4));
79 OPN_WRITE(0xa4,mult0?0x1c:0x14); // frequency
80 OPN_WRITE(0xa0,0);
81 OPN_WRITE(
82 0x28,
83 (params.op[0].enable?0x10:0)|
84 (params.op[2].enable?0x20:0)|
85 (params.op[1].enable?0x40:0)|
86 (params.op[3].enable?0x80:0)
87 ); // key on
88 }
89
90 // render
91 for (int i=0; i<FM_PREVIEW_SIZE; i++) {
92 aOut=0;
93 for (int j=0; j<24; j++) {
94 OPN2_Clock((ym3438_t*)fmPreviewOPN,out);
95 }
96 aOut+=((ym3438_t*)fmPreviewOPN)->ch_out[0];
97 if (aOut<-32768) aOut=-32768;
98 if (aOut>32767) aOut=32767;
99 fmPreview[i]=aOut;
100 }
101 }
102
103 #define OPM_WRITE(addr,val) \
104 OPM_Write((opm_t*)fmPreviewOPM,0,(addr)); \
105 do { \
106 OPM_Clock((opm_t*)fmPreviewOPM,out,NULL,NULL,NULL); \
107 OPM_Clock((opm_t*)fmPreviewOPM,out,NULL,NULL,NULL); \
108 } while (((opm_t*)fmPreviewOPM)->write_busy); \
109 OPM_Write((opm_t*)fmPreviewOPM,1,(val)); \
110 do { \
111 OPM_Clock((opm_t*)fmPreviewOPM,out,NULL,NULL,NULL); \
112 OPM_Clock((opm_t*)fmPreviewOPM,out,NULL,NULL,NULL); \
113 } while (((opm_t*)fmPreviewOPM)->write_busy);
114
115 void FurnaceGUI::renderFMPreviewOPM(const DivInstrumentFM& params, int pos) {
116 if (fmPreviewOPM==NULL) {
117 fmPreviewOPM=new opm_t;
118 pos=0;
119 }
120 int out[2];
121 int aOut=0;
122 bool mult0=false;
123
124 if (pos==0) {
125 OPM_Reset((opm_t*)fmPreviewOPM);
126
127 // set params
128 for (int i=0; i<4; i++) {
129 if ((params.op[i].mult&15)==0) {
130 mult0=true;
131 break;
132 }
133 }
134 for (int i=0; i<4; i++) {
135 const DivInstrumentFM::Operator& op=params.op[i];
136 unsigned short baseAddr=i*8;
137 OPM_WRITE(baseAddr+0x40,(op.mult&15)|(dtTableFMP[op.dt&7]<<4));
138 OPM_WRITE(baseAddr+0x60,op.tl);
139 OPM_WRITE(baseAddr+0x80,(op.ar&31)|(op.rs<<6));
140 OPM_WRITE(baseAddr+0xa0,(op.dr&31)|(op.am<<7));
141 OPM_WRITE(baseAddr+0xc0,(op.d2r&31)|(op.dt2<<6));
142 OPM_WRITE(baseAddr+0xe0,(op.rr&15)|(op.sl<<4));
143 }
144 OPM_WRITE(0x20,(params.alg&7)|((params.fb&7)<<3)|0xc0);
145 OPM_WRITE(0x38,((params.fms&7)<<4)|(params.ams&3));
146 OPM_WRITE(0x28,mult0?0x39:0x29); // frequency
147 OPM_WRITE(0x30,0xe6);
148 OPM_WRITE(
149 0x08,
150 (params.op[0].enable?0x08:0)|
151 (params.op[2].enable?0x10:0)|
152 (params.op[1].enable?0x20:0)|
153 (params.op[3].enable?0x40:0)
154 ); // key on
155 }
156
157 // render
158 for (int i=0; i<FM_PREVIEW_SIZE; i++) {
159 aOut=0;
160 for (int j=0; j<32; j++) {
161 OPM_Clock((opm_t*)fmPreviewOPM,out,NULL,NULL,NULL);
162 }
163 aOut+=out[0];
164 if (aOut<-32768) aOut=-32768;
165 if (aOut>32767) aOut=32767;
166 fmPreview[i]=aOut;
167 }
168 }
169
170 #define OPLL_WRITE(addr,val) \
171 OPLL_Write((opll_t*)fmPreviewOPLL,0,(addr)); \
172 for (int _i=0; _i<3; _i++) { \
173 OPLL_Clock((opll_t*)fmPreviewOPLL,out); \
174 } \
175 OPLL_Write((opll_t*)fmPreviewOPLL,1,(val)); \
176 for (int _i=0; _i<21; _i++) { \
177 OPLL_Clock((opll_t*)fmPreviewOPLL,out); \
178 }
179
180 void FurnaceGUI::renderFMPreviewOPLL(const DivInstrumentFM& params, int pos) {
181 if (fmPreviewOPLL==NULL) {
182 fmPreviewOPLL=new opm_t;
183 pos=0;
184 }
185 int out[2];
186 int aOut=0;
187 bool mult0=false;
188
189 if (pos==0) {
190 OPLL_Reset((opll_t*)fmPreviewOPLL,opll_type_ym2413);
191
192 // set params
193 const DivInstrumentFM::Operator& mod=params.op[0];
194 const DivInstrumentFM::Operator& car=params.op[1];
195 if (params.opllPreset==0) {
196 for (int i=0; i<2; i++) {
197 if ((params.op[i].mult&15)==0) {
198 mult0=true;
199 break;
200 }
201 }
202 OPLL_WRITE(0x00,(mod.am<<7)|(mod.vib<<6)|((mod.ssgEnv&8)<<2)|(mod.ksr<<4)|(mod.mult));
203 OPLL_WRITE(0x01,(car.am<<7)|(car.vib<<6)|((car.ssgEnv&8)<<2)|(car.ksr<<4)|(car.mult));
204 OPLL_WRITE(0x02,(mod.ksl<<6)|(mod.tl&63));
205 OPLL_WRITE(0x03,(car.ksl<<6)|((params.fms&1)<<4)|((params.ams&1)<<3)|(params.fb&7));
206 OPLL_WRITE(0x04,(mod.ar<<4)|(mod.dr));
207 OPLL_WRITE(0x05,(car.ar<<4)|(car.dr));
208 OPLL_WRITE(0x06,(mod.sl<<4)|(mod.rr));
209 OPLL_WRITE(0x07,(car.sl<<4)|(car.rr));
210 }
211 OPLL_WRITE(0x10,0);
212 OPLL_WRITE(0x30,(params.opllPreset<<4)|(car.tl&15));
213 OPLL_WRITE(0x20,(params.alg?0x20:0)|(mult0?0x15:0x13));
214 }
215
216 // render
217 for (int i=0; i<FM_PREVIEW_SIZE; i++) {
218 aOut=0;
219 for (int j=0; j<36; j++) {
220 OPLL_Clock((opll_t*)fmPreviewOPLL,out);
221 aOut+=out[0]<<4;
222 }
223 if (aOut<-32768) aOut=-32768;
224 if (aOut>32767) aOut=32767;
225 fmPreview[i]=aOut;
226 }
227 }
228
229 #define OPL_WRITE(addr,val) \
230 OPL3_WriteReg((opl3_chip*)fmPreviewOPL,(addr),(val)); \
231 OPL3_Generate4Ch((opl3_chip*)fmPreviewOPL,out);
232
233 const unsigned char lPreviewSlots[4]={
234 0, 3, 8, 11
235 };
236
237 const unsigned char lOpMap[4]={
238 0, 2, 1, 3
239 };
240
241 void FurnaceGUI::renderFMPreviewOPL(const DivInstrumentFM& params, int pos) {
242 if (fmPreviewOPL==NULL) {
243 fmPreviewOPL=new opl3_chip;
244 pos=0;
245 }
246 short out[4];
247 bool mult0=false;
248
249 if (pos==0) {
250 OPL3_Reset((opl3_chip*)fmPreviewOPL,49716);
251
252 // set params
253 int ops=(params.ops==4)?4:2;
254 for (int i=0; i<ops; i++) {
255 if ((params.op[i].mult&15)==0) {
256 mult0=true;
257 break;
258 }
259 }
260
261 OPL_WRITE(0x01,0x20);
262 OPL_WRITE(0x105,1);
263 if (ops==4) {
264 OPL_WRITE(0x104,1);
265 }
266 for (int i=0; i<ops; i++) {
267 const DivInstrumentFM::Operator& op=params.op[(ops==4)?lOpMap[i]:i];
268 unsigned short baseAddr=lPreviewSlots[i];
269
270 OPL_WRITE(baseAddr+0x40,op.tl|(op.ksl<<6));
271 OPL_WRITE(baseAddr+0x20,(op.am<<7)|(op.vib<<6)|(op.sus<<5)|(op.ksr<<4)|op.mult);
272 OPL_WRITE(baseAddr+0x60,(op.ar<<4)|op.dr);
273 OPL_WRITE(baseAddr+0x80,(op.sl<<4)|op.rr);
274 OPL_WRITE(baseAddr+0xe0,op.ws&7);
275 }
276
277 OPL_WRITE(0xc0,(params.alg&1)|(params.fb<<1)|0x10);
278 if (ops==4) {
279 OPL_WRITE(0xc3,((params.alg>>1)&1)|(params.fb<<1)|0x10);
280 }
281 OPL_WRITE(0xa0,0);
282 if (ops==4) {
283 OPL_WRITE(0xa3,0);
284 }
285 OPL_WRITE(0xb0,mult0?0x2a:0x26);
286 if (ops==4) {
287 OPL_WRITE(0xb3,mult0?0x2a:0x26);
288 }
289 }
290
291 // render
292 for (int i=0; i<FM_PREVIEW_SIZE; i++) {
293 OPL3_Generate4Ch((opl3_chip*)fmPreviewOPL,out);
294 OPL3_Generate4Ch((opl3_chip*)fmPreviewOPL,out);
295 fmPreview[i]=CLAMP(out[0]*2,-32768,32767);
296 }
297 }
298
299 #define OPZ_WRITE(addr,val) \
300 ((ymfm::ym2414*)fmPreviewOPZ)->write(0,(addr)); \
301 ((ymfm::ym2414*)fmPreviewOPZ)->write(1,(val)); \
302 ((ymfm::ym2414*)fmPreviewOPZ)->generate(&out,1);
303
304 void FurnaceGUI::renderFMPreviewOPZ(const DivInstrumentFM& params, int pos) {
305 if (fmPreviewOPZ==NULL) {
306 fmPreviewOPZInterface=new ymfm::ymfm_interface();
307 fmPreviewOPZ=new ymfm::ym2414(*(ymfm::ymfm_interface*)fmPreviewOPZInterface);
308 pos=0;
309 }
310 ymfm::ymfm_output<2> out;
311 int aOut=0;
312 bool mult0=false;
313
314 if (pos==0) {
315 ((ymfm::ym2414*)fmPreviewOPZ)->reset();
316
317 // set params
318 for (int i=0; i<4; i++) {
319 if ((params.op[i].mult&15)==0) {
320 mult0=true;
321 break;
322 }
323 }
324 for (int i=0; i<4; i++) {
325 const DivInstrumentFM::Operator& op=params.op[i];
326 unsigned short baseAddr=i*8;
327 OPZ_WRITE(baseAddr+0x40,(op.mult&15)|((op.egt?(op.dt&7):dtTableFMP[op.dt&7])<<4));
328 OPZ_WRITE(baseAddr+0x40,(op.dvb&15)|((op.ws&7)<<4)|0x80);
329 OPZ_WRITE(baseAddr+0x60,op.tl);
330 OPZ_WRITE(baseAddr+0x80,(op.ar&31)|(op.egt<<5)|(op.rs<<6));
331 OPZ_WRITE(baseAddr+0xa0,(op.dr&31)|(op.am<<7));
332 OPZ_WRITE(baseAddr+0xc0,(op.d2r&31)|(op.dt2<<6));
333 OPZ_WRITE(baseAddr+0xc0,(op.dam&7)|(op.ksl<<6)|0x20);
334 OPZ_WRITE(baseAddr+0xe0,(op.rr&15)|(op.sl<<4));
335 }
336 OPZ_WRITE(0x38,((params.fms&7)<<4)|(params.ams&3));
337 OPZ_WRITE(0x38,((params.fms2&7)<<4)|(params.ams2&3)|0x84);
338 OPZ_WRITE(0x28,mult0?0x39:0x29); // frequency
339 OPZ_WRITE(0x30,0xe7);
340 OPZ_WRITE(0x20,(params.alg&7)|((params.fb&7)<<3)|0x40); // key on
341 }
342
343 // render
344 for (int i=0; i<FM_PREVIEW_SIZE; i++) {
345 aOut=0;
346 ((ymfm::ym2414*)fmPreviewOPZ)->generate(&out,1);
347 aOut+=out.data[0];
348 if (aOut<-32768) aOut=-32768;
349 if (aOut>32767) aOut=32767;
350 fmPreview[i]=aOut;
351 }
352 }
353
354 #define ESFM_WRITE(addr,val) \
355 ESFM_write_reg_buffered_fast((esfm_chip*)fmPreviewESFM,(addr),(val))
356
357 void FurnaceGUI::renderFMPreviewESFM(const DivInstrumentFM& params, const DivInstrumentESFM& esfmParams, int pos) {
358 if (fmPreviewESFM==NULL) {
359 fmPreviewESFM=new esfm_chip;
360 pos=0;
361 }
362 short out[4];
363 bool mult0=false;
364
365 if (pos==0) {
366 ESFM_init((esfm_chip*)fmPreviewESFM,0);
367 // set native mode
368 ESFM_WRITE(0x105, 0x80);
369
370 // set params
371 for (int i=0; i<4; i++) {
372 if ((params.op[i].mult&15)==0) {
373 mult0=true;
374 break;
375 }
376 }
377
378 for (int i=0; i<4; i++) {
379 const DivInstrumentFM::Operator& op=params.op[i];
380 const DivInstrumentESFM::Operator& opE=esfmParams.op[i];
381 unsigned short baseAddr=i*8;
382 unsigned char freqL, freqH;
383 if (opE.fixed) {
384 freqL=opE.dt;
385 freqH=opE.ct&0x1f;
386 } else {
387 // perform detune calculation
388 int offset=(opE.ct<<7)+opE.dt;
389 double fbase=(mult0?2048.0:1024.0)*pow(2.0,(float)offset/(128.0*12.0));
390 int bf=round(fbase);
391 int block=0;
392 if (bf>0x3ff) {
393 block=bsr32(bf)-10;
394 bf>>=block;
395 }
396 freqL=bf&0xff;
397 freqH=((block&7)<<2)|((bf>>8)&3);
398 }
399
400 ESFM_WRITE(baseAddr+0,(op.am<<7)|((op.vib&1)<<6)|((op.sus&1)<<5)|((op.ksr&1)<<4)|(op.mult&0x0f));
401 ESFM_WRITE(baseAddr+1,(op.ksl<<6)|(op.tl&0x3f));
402 ESFM_WRITE(baseAddr+2,(op.ar<<4)|(op.dr&0x0f));
403 ESFM_WRITE(baseAddr+3,(op.sl<<4)|(op.rr&0x0f));
404
405 ESFM_WRITE(baseAddr+4,freqL);
406 ESFM_WRITE(baseAddr+5,(opE.delay<<5)|freqH);
407
408 ESFM_WRITE(baseAddr+6,(op.dam<<7)|((op.dvb&1)<<6)|((opE.right&1)<<5)|((opE.left&1)<<4)|((opE.modIn&7)<<1));
409 ESFM_WRITE(baseAddr+7,(opE.outLvl<<5)|((i==3?esfmParams.noise:0)<<3)|(op.ws&7));
410 }
411 }
412
413 // note on
414 ESFM_WRITE(0x240, 1);
415
416 // render
417 for (int i=0; i<FM_PREVIEW_SIZE; i++) {
418 ESFM_generate((esfm_chip*)fmPreviewESFM,out);
419 ESFM_generate((esfm_chip*)fmPreviewESFM,out);
420 fmPreview[i]=CLAMP(out[0]+out[1],-32768,32767);
421 }
422 }
423
424 void FurnaceGUI::renderFMPreview(const DivInstrument* ins, int pos) {
425 switch (ins->type) {
426 case DIV_INS_FM:
427 renderFMPreviewOPN(ins->fm,pos);
428 break;
429 case DIV_INS_OPM:
430 renderFMPreviewOPM(ins->fm,pos);
431 break;
432 case DIV_INS_OPLL:
433 renderFMPreviewOPLL(ins->fm,pos);
434 break;
435 case DIV_INS_OPL:
436 renderFMPreviewOPL(ins->fm,pos);
437 break;
438 case DIV_INS_OPZ:
439 renderFMPreviewOPZ(ins->fm,pos);
440 break;
441 case DIV_INS_ESFM:
442 renderFMPreviewESFM(ins->fm,ins->esfm,pos);
443 default:
444 break;
445 }
446 }