Debian Codegraph @

src/engine/platform/dummy.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 #include "dummy.h"
21 #include "../engine.h"
22 #include <stdio.h>
23 #include <math.h>
24
25 #define CHIP_FREQBASE 2048
26
27 void DivPlatformDummy::acquire(short** buf, size_t len) {
28 int chanOut;
29 for (int i=0; i<chans; i++) {
30 oscBuf[i]->begin(len);
31 }
32 for (size_t i=0; i<len; i++) {
33 int out=0;
34 for (unsigned char j=0; j<chans; j++) {
35 if (chan[j].active) {
36 if (!isMuted[j]) {
37 chanOut=(((signed short)chan[j].pos)*chan[j].amp*chan[j].vol)>>12;
38 oscBuf[j]->putSample(i,chanOut<<1);
39 out+=chanOut;
40 } else {
41 oscBuf[j]->putSample(i,0);
42 }
43 chan[j].pos+=chan[j].freq;
44 } else {
45 oscBuf[j]->putSample(i,0);
46 }
47 }
48 if (out<-32768) out=-32768;
49 if (out>32767) out=32767;
50 buf[0][i]=out;
51 }
52 for (int i=0; i<chans; i++) {
53 oscBuf[i]->end(len);
54 }
55 }
56
57 void DivPlatformDummy::muteChannel(int ch, bool mute) {
58 isMuted[ch]=mute;
59 }
60
61 void DivPlatformDummy::tick(bool sysTick) {
62 for (unsigned char i=0; i<chans; i++) {
63 if (sysTick) {
64 chan[i].amp-=7;
65 if (chan[i].amp<15) chan[i].amp=15;
66 }
67
68 if (chan[i].freqChanged) {
69 chan[i].freqChanged=false;
70 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,0,false,false,0,0,chipClock,CHIP_FREQBASE);
71 }
72 }
73 }
74
75 void* DivPlatformDummy::getChanState(int ch) {
76 return &chan[ch];
77 }
78
79 DivDispatchOscBuffer* DivPlatformDummy::getOscBuffer(int ch) {
80 return oscBuf[ch];
81 }
82
83 int DivPlatformDummy::dispatch(DivCommand c) {
84 switch (c.cmd) {
85 case DIV_CMD_NOTE_ON:
86 if (c.value!=DIV_NOTE_NULL) {
87 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
88 chan[c.chan].freqChanged=true;
89 }
90 chan[c.chan].active=true;
91 chan[c.chan].amp=64;
92 break;
93 case DIV_CMD_NOTE_OFF:
94 chan[c.chan].active=false;
95 break;
96 case DIV_CMD_VOLUME:
97 chan[c.chan].vol=c.value;
98 if (chan[c.chan].vol>15) chan[c.chan].vol=15;
99 break;
100 case DIV_CMD_GET_VOLUME:
101 return chan[c.chan].vol;
102 break;
103 case DIV_CMD_PITCH:
104 chan[c.chan].pitch=c.value;
105 chan[c.chan].freqChanged=true;
106 break;
107 case DIV_CMD_NOTE_PORTA: {
108 int destFreq=NOTE_FREQUENCY(c.value2);
109 bool return2=false;
110 if (destFreq>chan[c.chan].baseFreq) {
111 chan[c.chan].baseFreq+=c.value;
112 if (chan[c.chan].baseFreq>=destFreq) {
113 chan[c.chan].baseFreq=destFreq;
114 return2=true;
115 }
116 } else {
117 chan[c.chan].baseFreq-=c.value;
118 if (chan[c.chan].baseFreq<=destFreq) {
119 chan[c.chan].baseFreq=destFreq;
120 return2=true;
121 }
122 }
123 chan[c.chan].freqChanged=true;
124 if (return2) return 2;
125 break;
126 }
127 case DIV_CMD_LEGATO:
128 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
129 chan[c.chan].freqChanged=true;
130 break;
131 case DIV_CMD_GET_VOLMAX:
132 return 15;
133 break;
134 default:
135 break;
136 }
137 return 1;
138 }
139
140 void DivPlatformDummy::notifyInsDeletion(void* ins) {
141 // nothing
142 }
143
144 void DivPlatformDummy::reset() {
145 for (int i=0; i<chans; i++) {
146 chan[i]=DivPlatformDummy::Channel();
147 chan[i].vol=0x0f;
148 }
149 }
150
151 int DivPlatformDummy::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
152 parent=p;
153 dumpWrites=false;
154 skipRegisterWrites=false;
155 for (int i=0; i<DIV_MAX_CHANS; i++) {
156 isMuted[i]=false;
157 if (i<channels) {
158 oscBuf[i]=new DivDispatchOscBuffer;
159 oscBuf[i]->setRate(65536);
160 }
161 }
162 rate=65536;
163 chipClock=65536;
164 chans=channels;
165 reset();
166 return channels;
167 }
168
169 void DivPlatformDummy::quit() {
170 for (int i=0; i<chans; i++) {
171 delete oscBuf[i];
172 }
173 }
174
175 DivPlatformDummy::~DivPlatformDummy() {
176 }