2015-10-10 02:16:12 +00:00
|
|
|
auto DSP::brrDecode(Voice& v) -> void {
|
2010-08-09 13:28:56 +00:00
|
|
|
//state.t_brr_byte = ram[v.brr_addr + v.brr_offset] cached from previous clock cycle
|
2015-10-10 02:16:12 +00:00
|
|
|
signed nybbles = (state._brrByte << 8) + smp.apuram[(uint16)(v.brrAddress + v.brrOffset + 1)];
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2015-10-10 02:16:12 +00:00
|
|
|
const signed filter = (state._brrHeader >> 2) & 3;
|
|
|
|
const signed scale = (state._brrHeader >> 4);
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
//decode four samples
|
2015-10-10 02:16:12 +00:00
|
|
|
for(auto n : range(4)) {
|
2010-08-09 13:28:56 +00:00
|
|
|
//bits 12-15 = current nybble; sign extend, then shift right to 4-bit precision
|
|
|
|
//result: s = 4-bit sign-extended sample value
|
2015-10-10 02:16:12 +00:00
|
|
|
signed s = (int16)nybbles >> 12;
|
2010-08-09 13:28:56 +00:00
|
|
|
nybbles <<= 4; //slide nybble so that on next loop iteration, bits 12-15 = current nybble
|
|
|
|
|
|
|
|
if(scale <= 12) {
|
|
|
|
s <<= scale;
|
|
|
|
s >>= 1;
|
|
|
|
} else {
|
|
|
|
s &= ~0x7ff;
|
|
|
|
}
|
|
|
|
|
|
|
|
//apply IIR filter (2 is the most commonly used)
|
2015-10-10 02:16:12 +00:00
|
|
|
const signed p1 = v.buffer[v.bufferOffset - 1];
|
|
|
|
const signed p2 = v.buffer[v.bufferOffset - 2] >> 1;
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
switch(filter) {
|
2013-05-05 09:21:30 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
//s += p1 * 0.46875
|
|
|
|
s += p1 >> 1;
|
|
|
|
s += (-p1) >> 5;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
//s += p1 * 0.953125 - p2 * 0.46875
|
|
|
|
s += p1;
|
|
|
|
s -= p2;
|
|
|
|
s += p2 >> 4;
|
|
|
|
s += (p1 * -3) >> 6;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
//s += p1 * 0.8984375 - p2 * 0.40625
|
|
|
|
s += p1;
|
|
|
|
s -= p2;
|
|
|
|
s += (p1 * -13) >> 7;
|
|
|
|
s += (p2 * 3) >> 4;
|
|
|
|
break;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//adjust and write sample
|
|
|
|
s = sclamp<16>(s);
|
|
|
|
s = (int16)(s << 1);
|
2015-10-10 02:16:12 +00:00
|
|
|
v.buffer.write(v.bufferOffset++, s);
|
|
|
|
if(v.bufferOffset >= BrrBufferSize) v.bufferOffset = 0;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
}
|