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