yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dsp.cc
Go to the documentation of this file.
1#include "app/emu/audio/dsp.h"
2
3#include <cstring>
4
5namespace yaze {
6namespace emu {
7
8static const int rateValues[32] = {0, 2048, 1536, 1280, 1024, 768, 640, 512,
9 384, 320, 256, 192, 160, 128, 96, 80,
10 64, 48, 40, 32, 24, 20, 16, 12,
11 10, 8, 6, 5, 4, 3, 2, 1};
12
13static const int rateOffsets[32] = {0, 0, 1040, 536, 0, 1040, 536, 0, 1040,
14 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
15 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
16 536, 0, 1040, 536, 0};
17
18static const int gaussValues[512] = {
19 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
20 0x000, 0x000, 0x000, 0x000, 0x000, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
21 0x001, 0x001, 0x001, 0x001, 0x001, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002,
22 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004,
23 0x005, 0x005, 0x005, 0x005, 0x006, 0x006, 0x006, 0x006, 0x007, 0x007, 0x007,
24 0x008, 0x008, 0x008, 0x009, 0x009, 0x009, 0x00a, 0x00a, 0x00a, 0x00b, 0x00b,
25 0x00b, 0x00c, 0x00c, 0x00d, 0x00d, 0x00e, 0x00e, 0x00f, 0x00f, 0x00f, 0x010,
26 0x010, 0x011, 0x011, 0x012, 0x013, 0x013, 0x014, 0x014, 0x015, 0x015, 0x016,
27 0x017, 0x017, 0x018, 0x018, 0x019, 0x01a, 0x01b, 0x01b, 0x01c, 0x01d, 0x01d,
28 0x01e, 0x01f, 0x020, 0x020, 0x021, 0x022, 0x023, 0x024, 0x024, 0x025, 0x026,
29 0x027, 0x028, 0x029, 0x02a, 0x02b, 0x02c, 0x02d, 0x02e, 0x02f, 0x030, 0x031,
30 0x032, 0x033, 0x034, 0x035, 0x036, 0x037, 0x038, 0x03a, 0x03b, 0x03c, 0x03d,
31 0x03e, 0x040, 0x041, 0x042, 0x043, 0x045, 0x046, 0x047, 0x049, 0x04a, 0x04c,
32 0x04d, 0x04e, 0x050, 0x051, 0x053, 0x054, 0x056, 0x057, 0x059, 0x05a, 0x05c,
33 0x05e, 0x05f, 0x061, 0x063, 0x064, 0x066, 0x068, 0x06a, 0x06b, 0x06d, 0x06f,
34 0x071, 0x073, 0x075, 0x076, 0x078, 0x07a, 0x07c, 0x07e, 0x080, 0x082, 0x084,
35 0x086, 0x089, 0x08b, 0x08d, 0x08f, 0x091, 0x093, 0x096, 0x098, 0x09a, 0x09c,
36 0x09f, 0x0a1, 0x0a3, 0x0a6, 0x0a8, 0x0ab, 0x0ad, 0x0af, 0x0b2, 0x0b4, 0x0b7,
37 0x0ba, 0x0bc, 0x0bf, 0x0c1, 0x0c4, 0x0c7, 0x0c9, 0x0cc, 0x0cf, 0x0d2, 0x0d4,
38 0x0d7, 0x0da, 0x0dd, 0x0e0, 0x0e3, 0x0e6, 0x0e9, 0x0ec, 0x0ef, 0x0f2, 0x0f5,
39 0x0f8, 0x0fb, 0x0fe, 0x101, 0x104, 0x107, 0x10b, 0x10e, 0x111, 0x114, 0x118,
40 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c, 0x130, 0x133, 0x137, 0x13a, 0x13e,
41 0x141, 0x145, 0x148, 0x14c, 0x150, 0x153, 0x157, 0x15b, 0x15f, 0x162, 0x166,
42 0x16a, 0x16e, 0x172, 0x176, 0x17a, 0x17d, 0x181, 0x185, 0x189, 0x18d, 0x191,
43 0x195, 0x19a, 0x19e, 0x1a2, 0x1a6, 0x1aa, 0x1ae, 0x1b2, 0x1b7, 0x1bb, 0x1bf,
44 0x1c3, 0x1c8, 0x1cc, 0x1d0, 0x1d5, 0x1d9, 0x1dd, 0x1e2, 0x1e6, 0x1eb, 0x1ef,
45 0x1f3, 0x1f8, 0x1fc, 0x201, 0x205, 0x20a, 0x20f, 0x213, 0x218, 0x21c, 0x221,
46 0x226, 0x22a, 0x22f, 0x233, 0x238, 0x23d, 0x241, 0x246, 0x24b, 0x250, 0x254,
47 0x259, 0x25e, 0x263, 0x267, 0x26c, 0x271, 0x276, 0x27b, 0x280, 0x284, 0x289,
48 0x28e, 0x293, 0x298, 0x29d, 0x2a2, 0x2a6, 0x2ab, 0x2b0, 0x2b5, 0x2ba, 0x2bf,
49 0x2c4, 0x2c9, 0x2ce, 0x2d3, 0x2d8, 0x2dc, 0x2e1, 0x2e6, 0x2eb, 0x2f0, 0x2f5,
50 0x2fa, 0x2ff, 0x304, 0x309, 0x30e, 0x313, 0x318, 0x31d, 0x322, 0x326, 0x32b,
51 0x330, 0x335, 0x33a, 0x33f, 0x344, 0x349, 0x34e, 0x353, 0x357, 0x35c, 0x361,
52 0x366, 0x36b, 0x370, 0x374, 0x379, 0x37e, 0x383, 0x388, 0x38c, 0x391, 0x396,
53 0x39b, 0x39f, 0x3a4, 0x3a9, 0x3ad, 0x3b2, 0x3b7, 0x3bb, 0x3c0, 0x3c5, 0x3c9,
54 0x3ce, 0x3d2, 0x3d7, 0x3dc, 0x3e0, 0x3e5, 0x3e9, 0x3ed, 0x3f2, 0x3f6, 0x3fb,
55 0x3ff, 0x403, 0x408, 0x40c, 0x410, 0x415, 0x419, 0x41d, 0x421, 0x425, 0x42a,
56 0x42e, 0x432, 0x436, 0x43a, 0x43e, 0x442, 0x446, 0x44a, 0x44e, 0x452, 0x455,
57 0x459, 0x45d, 0x461, 0x465, 0x468, 0x46c, 0x470, 0x473, 0x477, 0x47a, 0x47e,
58 0x481, 0x485, 0x488, 0x48c, 0x48f, 0x492, 0x496, 0x499, 0x49c, 0x49f, 0x4a2,
59 0x4a6, 0x4a9, 0x4ac, 0x4af, 0x4b2, 0x4b5, 0x4b7, 0x4ba, 0x4bd, 0x4c0, 0x4c3,
60 0x4c5, 0x4c8, 0x4cb, 0x4cd, 0x4d0, 0x4d2, 0x4d5, 0x4d7, 0x4d9, 0x4dc, 0x4de,
61 0x4e0, 0x4e3, 0x4e5, 0x4e7, 0x4e9, 0x4eb, 0x4ed, 0x4ef, 0x4f1, 0x4f3, 0x4f5,
62 0x4f6, 0x4f8, 0x4fa, 0x4fb, 0x4fd, 0x4ff, 0x500, 0x502, 0x503, 0x504, 0x506,
63 0x507, 0x508, 0x50a, 0x50b, 0x50c, 0x50d, 0x50e, 0x50f, 0x510, 0x511, 0x511,
64 0x512, 0x513, 0x514, 0x514, 0x515, 0x516, 0x516, 0x517, 0x517, 0x517, 0x518,
65 0x518, 0x518, 0x518, 0x518, 0x519, 0x519};
66
67void Dsp::Reset() {
68 memset(ram, 0, sizeof(ram));
69 ram[0x7c] = 0xff; // set ENDx
70 for (int i = 0; i < 8; i++) {
71 channel[i].pitch = 0;
72 channel[i].pitchCounter = 0;
73 channel[i].pitchModulation = false;
74 memset(channel[i].decodeBuffer, 0, sizeof(channel[i].decodeBuffer));
75 channel[i].bufferOffset = 0;
76 channel[i].srcn = 0;
77 channel[i].decodeOffset = 0;
78 channel[i].blockOffset = 0;
79 channel[i].brrHeader = 0;
80 channel[i].useNoise = false;
81 channel[i].startDelay = 0;
82 memset(channel[i].adsrRates, 0, sizeof(channel[i].adsrRates));
83 channel[i].adsrState = 0;
84 channel[i].sustainLevel = 0;
85 channel[i].gainSustainLevel = 0;
86 channel[i].useGain = false;
87 channel[i].gainMode = 0;
88 channel[i].directGain = false;
89 channel[i].gainValue = 0;
90 channel[i].preclampGain = 0;
91 channel[i].gain = 0;
92 channel[i].keyOn = false;
93 channel[i].keyOff = false;
94 channel[i].sampleOut = 0;
95 channel[i].volumeL = 0;
96 channel[i].volumeR = 0;
97 channel[i].echoEnable = false;
98 }
99 counter = 0;
100 dirPage = 0;
101 evenCycle = true;
102 mute = true;
103 reset = true;
104 masterVolumeL = 0;
105 masterVolumeR = 0;
106 sampleOutL = 0;
107 sampleOutR = 0;
108 echoOutL = 0;
109 echoOutR = 0;
110 noiseSample = 0x4000;
111 noiseRate = 0;
112 echoWrites = false;
113 echoVolumeL = 0;
114 echoVolumeR = 0;
115 feedbackVolume = 0;
116 echoBufferAdr = 0;
117 echoDelay = 0;
118 echoLength = 0;
119 echoBufferIndex = 0;
120 firBufferIndex = 0;
121 memset(firValues, 0, sizeof(firValues));
122 memset(firBufferL, 0, sizeof(firBufferL));
123 memset(firBufferR, 0, sizeof(firBufferR));
124 memset(sampleBuffer, 0, sizeof(sampleBuffer));
125 sampleOffset = 0;
126}
127
131
133 sampleOutL = 0;
134 sampleOutR = 0;
135 echoOutL = 0;
136 echoOutR = 0;
137 for (int i = 0; i < 8; i++) {
138 CycleChannel(i);
139 }
140 HandleEcho(); // also applies master volume
141 counter = counter == 0 ? 30720 : counter - 1;
142 HandleNoise();
144 // handle mute flag
145 if (mute) {
146 sampleOutL = 0;
147 sampleOutR = 0;
148 }
149 // put final sample in the samplebuffer
150 sampleBuffer[(sampleOffset & 0x3ff) * 2] = sampleOutL;
151 sampleBuffer[(sampleOffset++ & 0x3ff) * 2 + 1] = sampleOutR;
152}
153
154static int clamp16(int val) {
155 return val < -0x8000 ? -0x8000 : (val > 0x7fff ? 0x7fff : val);
156}
157
158static int clip16(int val) { return (int16_t)(val & 0xffff); }
159
160bool Dsp::CheckCounter(int rate) {
161 if (rate == 0) return false;
162 return ((counter + rateOffsets[rate]) % rateValues[rate]) == 0;
163}
164
166 // increment fir buffer index
168 firBufferIndex &= 0x7;
169 // get value out of ram
170 uint16_t adr = echoBufferAdr + echoBufferIndex;
171 int16_t ramSample = aram_[adr] | (aram_[(adr + 1) & 0xffff] << 8);
172 firBufferL[firBufferIndex] = ramSample >> 1;
173 ramSample = aram_[(adr + 2) & 0xffff] | (aram_[(adr + 3) & 0xffff] << 8);
174 firBufferR[firBufferIndex] = ramSample >> 1;
175 // calculate FIR-sum
176 int sumL = 0, sumR = 0;
177 for (int i = 0; i < 8; i++) {
178 sumL += (firBufferL[(firBufferIndex + i + 1) & 0x7] * firValues[i]) >> 6;
179 sumR += (firBufferR[(firBufferIndex + i + 1) & 0x7] * firValues[i]) >> 6;
180 if (i == 6) {
181 // clip to 16-bit before last addition
182 sumL = clip16(sumL);
183 sumR = clip16(sumR);
184 }
185 }
186 sumL = clamp16(sumL) & ~1;
187 sumR = clamp16(sumR) & ~1;
188 // apply master volume and modify output with sum
189 sampleOutL = clamp16(((sampleOutL * masterVolumeL) >> 7) +
190 ((sumL * echoVolumeL) >> 7));
191 sampleOutR = clamp16(((sampleOutR * masterVolumeR) >> 7) +
192 ((sumR * echoVolumeR) >> 7));
193 // get echo value
194 int echoL = clamp16(echoOutL + clip16((sumL * feedbackVolume) >> 7)) & ~1;
195 int echoR = clamp16(echoOutR + clip16((sumR * feedbackVolume) >> 7)) & ~1;
196 // write it to ram
197 if (echoWrites) {
198 aram_[adr] = echoL & 0xff;
199 aram_[(adr + 1) & 0xffff] = echoL >> 8;
200 aram_[(adr + 2) & 0xffff] = echoR & 0xff;
201 aram_[(adr + 3) & 0xffff] = echoR >> 8;
202 }
203 // handle indexes
204 if (echoBufferIndex == 0) {
205 echoLength = echoDelay * 4;
206 }
207 echoBufferIndex += 4;
209 echoBufferIndex = 0;
210 }
211}
212
213void Dsp::CycleChannel(int ch) {
214 // handle pitch counter
215 int pitch = channel[ch].pitch;
216 if (ch > 0 && channel[ch].pitchModulation) {
217 pitch += ((channel[ch - 1].sampleOut >> 5) * pitch) >> 10;
218 }
219 // get current brr header and get sample address
220 channel[ch].brrHeader = aram_[channel[ch].decodeOffset];
221 uint16_t samplePointer = dirPage + 4 * channel[ch].srcn;
222 if (channel[ch].startDelay == 0) samplePointer += 2;
223 uint16_t sampleAdr =
224 aram_[samplePointer] | (aram_[(samplePointer + 1) & 0xffff] << 8);
225 // handle starting of sample
226 if (channel[ch].startDelay > 0) {
227 if (channel[ch].startDelay == 5) {
228 // first keyed on
229 channel[ch].decodeOffset = sampleAdr;
230 channel[ch].blockOffset = 1;
231 channel[ch].bufferOffset = 0;
232 channel[ch].brrHeader = 0;
233 ram[0x7c] &= ~(1 << ch); // clear ENDx
234 }
235 channel[ch].gain = 0;
236 channel[ch].startDelay--;
237 channel[ch].pitchCounter = 0;
238 if (channel[ch].startDelay > 0 && channel[ch].startDelay < 4) {
239 channel[ch].pitchCounter = 0x4000;
240 }
241 pitch = 0;
242 }
243 // get sample
244 int sample = 0;
245 if (channel[ch].useNoise) {
246 sample = clip16(noiseSample * 2);
247 } else {
248 sample = GetSample(ch);
249 }
250 sample = ((sample * channel[ch].gain) >> 11) & ~1;
251 // handle reset and release
252 if (reset || (channel[ch].brrHeader & 0x03) == 1) {
253 channel[ch].adsrState = 3; // go to release
254 channel[ch].gain = 0;
255 }
256 // handle keyon/keyoff
257 if (evenCycle) {
258 if (channel[ch].keyOff) {
259 channel[ch].adsrState = 3; // go to release
260 }
261 if (channel[ch].keyOn) {
262 channel[ch].startDelay = 5;
263 channel[ch].adsrState = 0; // go to attack
264 channel[ch].keyOn = false;
265 }
266 }
267 // handle envelope
268 if (channel[ch].startDelay == 0) {
269 HandleGain(ch);
270 }
271 // decode new brr samples if needed and update offsets
272 if (channel[ch].pitchCounter >= 0x4000) {
273 DecodeBrr(ch);
274 if (channel[ch].blockOffset >= 7) {
275 if (channel[ch].brrHeader & 0x1) {
276 channel[ch].decodeOffset = sampleAdr;
277 ram[0x7c] |= 1 << ch; // set ENDx
278 } else {
279 channel[ch].decodeOffset += 9;
280 }
281 channel[ch].blockOffset = 1;
282 } else {
283 channel[ch].blockOffset += 2;
284 }
285 }
286 // update pitch counter
287 channel[ch].pitchCounter &= 0x3fff;
288 channel[ch].pitchCounter += pitch;
289 if (channel[ch].pitchCounter > 0x7fff) channel[ch].pitchCounter = 0x7fff;
290 // set outputs
291 ram[(ch << 4) | 8] = channel[ch].gain >> 4;
292 ram[(ch << 4) | 9] = sample >> 8;
293 channel[ch].sampleOut = sample;
294 sampleOutL = clamp16(sampleOutL + ((sample * channel[ch].volumeL) >> 7));
295 sampleOutR = clamp16(sampleOutR + ((sample * channel[ch].volumeR) >> 7));
296 if (channel[ch].echoEnable) {
297 echoOutL = clamp16(echoOutL + ((sample * channel[ch].volumeL) >> 7));
298 echoOutR = clamp16(echoOutR + ((sample * channel[ch].volumeR) >> 7));
299 }
300}
301
302void Dsp::HandleGain(int ch) {
303 int newGain = channel[ch].gain;
304 int rate = 0;
305 // handle gain mode
306 if (channel[ch].adsrState == 3) { // release
307 rate = 31;
308 newGain -= 8;
309 } else {
310 if (!channel[ch].useGain) {
311 rate = channel[ch].adsrRates[channel[ch].adsrState];
312 switch (channel[ch].adsrState) {
313 case 0:
314 newGain += rate == 31 ? 1024 : 32;
315 break; // attack
316 case 1:
317 newGain -= ((newGain - 1) >> 8) + 1;
318 break; // decay
319 case 2:
320 newGain -= ((newGain - 1) >> 8) + 1;
321 break; // sustain
322 }
323 } else {
324 if (!channel[ch].directGain) {
325 rate = channel[ch].adsrRates[3];
326 switch (channel[ch].gainMode) {
327 case 0:
328 newGain -= 32;
329 break; // linear decrease
330 case 1:
331 newGain -= ((newGain - 1) >> 8) + 1;
332 break; // exponential decrease
333 case 2:
334 newGain += 32;
335 break; // linear increase
336 case 3:
337 newGain += (channel[ch].preclampGain < 0x600) ? 32 : 8;
338 break; // bent increase
339 }
340 } else { // direct gain
341 rate = 31;
342 newGain = channel[ch].gainValue;
343 }
344 }
345 }
346 // use sustain level according to mode
347 int sustainLevel = channel[ch].useGain ? channel[ch].gainSustainLevel
348 : channel[ch].sustainLevel;
349 if (channel[ch].adsrState == 1 && (newGain >> 8) == sustainLevel) {
350 channel[ch].adsrState = 2; // go to sustain
351 }
352 // store pre-clamped gain (for bent increase)
353 channel[ch].preclampGain = newGain & 0xffff;
354 // clamp gain
355 if (newGain < 0 || newGain > 0x7ff) {
356 newGain = newGain < 0 ? 0 : 0x7ff;
357 if (channel[ch].adsrState == 0) {
358 channel[ch].adsrState = 1; // go to decay
359 }
360 }
361 // store new value
362 if (CheckCounter(rate)) channel[ch].gain = newGain;
363}
364
365int16_t Dsp::GetSample(int ch) {
366 int pos = (channel[ch].pitchCounter >> 12) + channel[ch].bufferOffset;
367 int offset = (channel[ch].pitchCounter >> 4) & 0xff;
368 int16_t news = channel[ch].decodeBuffer[(pos + 3) % 12];
369 int16_t olds = channel[ch].decodeBuffer[(pos + 2) % 12];
370 int16_t olders = channel[ch].decodeBuffer[(pos + 1) % 12];
371 int16_t oldests = channel[ch].decodeBuffer[pos % 12];
372 int out = (gaussValues[0xff - offset] * oldests) >> 11;
373 out += (gaussValues[0x1ff - offset] * olders) >> 11;
374 out += (gaussValues[0x100 + offset] * olds) >> 11;
375 out = clip16(out) + ((gaussValues[offset] * news) >> 11);
376 return clamp16(out) & ~1;
377}
378
379void Dsp::DecodeBrr(int ch) {
380 int shift = channel[ch].brrHeader >> 4;
381 int filter = (channel[ch].brrHeader & 0xc) >> 2;
382 int bOff = channel[ch].bufferOffset;
383 int old = channel[ch].decodeBuffer[bOff == 0 ? 11 : bOff - 1] >> 1;
384 int older = channel[ch].decodeBuffer[bOff == 0 ? 10 : bOff - 2] >> 1;
385 uint8_t curByte = 0;
386 for (int i = 0; i < 4; i++) {
387 int s = 0;
388 if (i & 1) {
389 s = curByte & 0xf;
390 } else {
391 curByte = aram_[(channel[ch].decodeOffset + channel[ch].blockOffset +
392 (i >> 1)) &
393 0xffff];
394 s = curByte >> 4;
395 }
396 if (s > 7) s -= 16;
397 if (shift <= 0xc) {
398 s = (s << shift) >> 1;
399 } else {
400 s = (s >> 3) << 12;
401 }
402 switch (filter) {
403 case 1:
404 s += old + (-old >> 4);
405 break;
406 case 2:
407 s += 2 * old + ((3 * -old) >> 5) - older + (older >> 4);
408 break;
409 case 3:
410 s += 2 * old + ((13 * -old) >> 6) - older + ((3 * older) >> 4);
411 break;
412 }
413 channel[ch].decodeBuffer[bOff + i] = clamp16(s) * 2; // cuts off bit 15
414 older = old;
415 old = channel[ch].decodeBuffer[bOff + i] >> 1;
416 }
417 channel[ch].bufferOffset += 4;
418 if (channel[ch].bufferOffset >= 12) channel[ch].bufferOffset = 0;
419}
420
422 if (CheckCounter(noiseRate)) {
423 int bit = (noiseSample & 1) ^ ((noiseSample >> 1) & 1);
424 noiseSample = ((noiseSample >> 1) & 0x3fff) | (bit << 14);
425 }
426}
427
428uint8_t Dsp::Read(uint8_t adr) { return ram[adr]; }
429
430void Dsp::Write(uint8_t adr, uint8_t val) {
431 int ch = adr >> 4;
432 switch (adr) {
433 case 0x00:
434 case 0x10:
435 case 0x20:
436 case 0x30:
437 case 0x40:
438 case 0x50:
439 case 0x60:
440 case 0x70: {
441 channel[ch].volumeL = val;
442 break;
443 }
444 case 0x01:
445 case 0x11:
446 case 0x21:
447 case 0x31:
448 case 0x41:
449 case 0x51:
450 case 0x61:
451 case 0x71: {
452 channel[ch].volumeR = val;
453 break;
454 }
455 case 0x02:
456 case 0x12:
457 case 0x22:
458 case 0x32:
459 case 0x42:
460 case 0x52:
461 case 0x62:
462 case 0x72: {
463 channel[ch].pitch = (channel[ch].pitch & 0x3f00) | val;
464 break;
465 }
466 case 0x03:
467 case 0x13:
468 case 0x23:
469 case 0x33:
470 case 0x43:
471 case 0x53:
472 case 0x63:
473 case 0x73: {
474 channel[ch].pitch = ((channel[ch].pitch & 0x00ff) | (val << 8)) & 0x3fff;
475 break;
476 }
477 case 0x04:
478 case 0x14:
479 case 0x24:
480 case 0x34:
481 case 0x44:
482 case 0x54:
483 case 0x64:
484 case 0x74: {
485 channel[ch].srcn = val;
486 break;
487 }
488 case 0x05:
489 case 0x15:
490 case 0x25:
491 case 0x35:
492 case 0x45:
493 case 0x55:
494 case 0x65:
495 case 0x75: {
496 channel[ch].adsrRates[0] = (val & 0xf) * 2 + 1;
497 channel[ch].adsrRates[1] = ((val & 0x70) >> 4) * 2 + 16;
498 channel[ch].useGain = (val & 0x80) == 0;
499 break;
500 }
501 case 0x06:
502 case 0x16:
503 case 0x26:
504 case 0x36:
505 case 0x46:
506 case 0x56:
507 case 0x66:
508 case 0x76: {
509 channel[ch].adsrRates[2] = val & 0x1f;
510 channel[ch].sustainLevel = (val & 0xe0) >> 5;
511 break;
512 }
513 case 0x07:
514 case 0x17:
515 case 0x27:
516 case 0x37:
517 case 0x47:
518 case 0x57:
519 case 0x67:
520 case 0x77: {
521 channel[ch].directGain = (val & 0x80) == 0;
522 channel[ch].gainMode = (val & 0x60) >> 5;
523 channel[ch].adsrRates[3] = val & 0x1f;
524 channel[ch].gainValue = (val & 0x7f) * 16;
525 channel[ch].gainSustainLevel = (val & 0xe0) >> 5;
526 break;
527 }
528 case 0x0c: {
529 masterVolumeL = val;
530 break;
531 }
532 case 0x1c: {
533 masterVolumeR = val;
534 break;
535 }
536 case 0x2c: {
537 echoVolumeL = val;
538 break;
539 }
540 case 0x3c: {
541 echoVolumeR = val;
542 break;
543 }
544 case 0x4c: {
545 for (int i = 0; i < 8; i++) {
546 channel[i].keyOn = val & (1 << i);
547 }
548 break;
549 }
550 case 0x5c: {
551 for (int i = 0; i < 8; i++) {
552 channel[i].keyOff = val & (1 << i);
553 }
554 break;
555 }
556 case 0x6c: {
557 reset = val & 0x80;
558 mute = val & 0x40;
559 echoWrites = (val & 0x20) == 0;
560 noiseRate = val & 0x1f;
561 break;
562 }
563 case 0x7c: {
564 val = 0; // any write clears ENDx
565 break;
566 }
567 case 0x0d: {
568 feedbackVolume = val;
569 break;
570 }
571 case 0x2d: {
572 for (int i = 0; i < 8; i++) {
573 channel[i].pitchModulation = val & (1 << i);
574 }
575 break;
576 }
577 case 0x3d: {
578 for (int i = 0; i < 8; i++) {
579 channel[i].useNoise = val & (1 << i);
580 }
581 break;
582 }
583 case 0x4d: {
584 for (int i = 0; i < 8; i++) {
585 channel[i].echoEnable = val & (1 << i);
586 }
587 break;
588 }
589 case 0x5d: {
590 dirPage = val << 8;
591 break;
592 }
593 case 0x6d: {
594 echoBufferAdr = val << 8;
595 break;
596 }
597 case 0x7d: {
598 echoDelay =
599 (val & 0xf) * 512; // 2048-byte steps, stereo sample is 4 bytes
600 break;
601 }
602 case 0x0f:
603 case 0x1f:
604 case 0x2f:
605 case 0x3f:
606 case 0x4f:
607 case 0x5f:
608 case 0x6f:
609 case 0x7f: {
610 firValues[ch] = val;
611 break;
612 }
613 }
614 ram[adr] = val;
615}
616
617void Dsp::GetSamples(int16_t* sample_data, int samples_per_frame,
618 bool pal_timing) {
619 // resample from 534 / 641 samples per frame to wanted value
620 float wantedSamples = (pal_timing ? 641.0 : 534.0);
621 double adder = wantedSamples / samples_per_frame;
622 double location = lastFrameBoundary - wantedSamples;
623 for (int i = 0; i < samples_per_frame; i++) {
624 sample_data[i * 2] = sample_buffer_[(((int)location) & 0x3ff) * 2];
625 sample_data[i * 2 + 1] = sample_buffer_[(((int)location) & 0x3ff) * 2 + 1];
626 location += adder;
627 }
628}
629
630} // namespace emu
631} // namespace yaze
uint16_t echoBufferIndex
Definition dsp.h:141
uint32_t lastFrameBoundary
Definition dsp.h:149
void CycleChannel(int ch)
Definition dsp.cc:213
void HandleGain(int ch)
Definition dsp.cc:302
void GetSamples(int16_t *sample_data, int samples_per_frame, bool pal_timing)
Definition dsp.cc:617
int16_t sample_buffer_[0x400 *2]
Definition dsp.h:108
int16_t sampleOutR
Definition dsp.h:127
void DecodeBrr(int ch)
Definition dsp.cc:379
int16_t echoOutR
Definition dsp.h:129
int8_t feedbackVolume
Definition dsp.h:137
int16_t sampleBuffer[0x400 *2]
Definition dsp.h:147
uint8_t Read(uint8_t adr)
Definition dsp.cc:428
std::vector< uint8_t > & aram_
Definition dsp.h:111
uint8_t noiseRate
Definition dsp.h:132
bool CheckCounter(int rate)
Definition dsp.cc:160
uint16_t counter
Definition dsp.h:118
uint16_t sampleOffset
Definition dsp.h:148
uint16_t echoBufferAdr
Definition dsp.h:138
void Cycle()
Definition dsp.cc:132
int8_t masterVolumeL
Definition dsp.h:123
uint16_t echoDelay
Definition dsp.h:139
void HandleEcho()
Definition dsp.cc:165
int8_t firValues[8]
Definition dsp.h:143
int16_t sampleOutL
Definition dsp.h:126
void Reset()
Definition dsp.cc:67
void Write(uint8_t adr, uint8_t val)
Definition dsp.cc:430
int8_t masterVolumeR
Definition dsp.h:124
bool evenCycle
Definition dsp.h:120
uint8_t firBufferIndex
Definition dsp.h:142
void NewFrame()
Definition dsp.cc:128
int16_t firBufferR[8]
Definition dsp.h:145
int16_t noiseSample
Definition dsp.h:131
uint16_t dirPage
Definition dsp.h:119
int16_t echoOutL
Definition dsp.h:128
int16_t firBufferL[8]
Definition dsp.h:144
uint16_t echoLength
Definition dsp.h:140
int8_t echoVolumeR
Definition dsp.h:136
bool echoWrites
Definition dsp.h:134
int8_t echoVolumeL
Definition dsp.h:135
bool mute
Definition dsp.h:121
uint8_t ram[0x80]
Definition dsp.h:114
void HandleNoise()
Definition dsp.cc:421
int16_t GetSample(int ch)
Definition dsp.cc:365
DspChannel channel[8]
Definition dsp.h:116
bool reset
Definition dsp.h:122
SNES Emulation and debugging tools.
Definition apu.cc:13
Main namespace for the application.
Definition controller.cc:18