| 1 |
// --------------------------------------------------------------------------- |
| 2 |
// This file is part of reSID, a MOS6581 SID emulator engine. |
| 3 |
// Copyright (C) 2004 Dag Lem <[email protected]> |
| 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 |
| 16 |
// along with this program; if not, write to the Free Software |
| 17 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
// --------------------------------------------------------------------------- |
| 19 |
|
| 20 |
#include "sid.h" |
| 21 |
#include <stdio.h> |
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
// ---------------------------------------------------------------------------- |
| 25 |
// Constructor. |
| 26 |
// ---------------------------------------------------------------------------- |
| 27 |
SID::SID() |
| 28 |
{ |
| 29 |
// Initialize pointers. |
| 30 |
sample = 0; |
| 31 |
fir = 0; |
| 32 |
|
| 33 |
voice[0].set_sync_source(&voice[2]); |
| 34 |
voice[1].set_sync_source(&voice[0]); |
| 35 |
voice[2].set_sync_source(&voice[1]); |
| 36 |
|
| 37 |
set_sampling_parameters(985248, SAMPLE_FAST, 44100); |
| 38 |
|
| 39 |
bus_value = 0; |
| 40 |
bus_value_ttl = 0; |
| 41 |
|
| 42 |
ext_in = 0; |
| 43 |
|
| 44 |
isMuted[0]=false; |
| 45 |
isMuted[1]=false; |
| 46 |
isMuted[2]=false; |
| 47 |
|
| 48 |
last_chan_out[0]=0; |
| 49 |
last_chan_out[1]=0; |
| 50 |
last_chan_out[2]=0; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
// ---------------------------------------------------------------------------- |
| 55 |
// Destructor. |
| 56 |
// ---------------------------------------------------------------------------- |
| 57 |
SID::~SID() |
| 58 |
{ |
| 59 |
delete[] sample; |
| 60 |
delete[] fir; |
| 61 |
} |
| 62 |
|
| 63 |
// ---------------------------------------------------------------------------- |
| 64 |
// Get DC offset of channel. |
| 65 |
// ---------------------------------------------------------------------------- |
| 66 |
sound_sample SID::get_dc(int ch) { |
| 67 |
return voice[ch].getDC(); |
| 68 |
} |
| 69 |
|
| 70 |
// ---------------------------------------------------------------------------- |
| 71 |
// Mute/unmute channel. |
| 72 |
// ---------------------------------------------------------------------------- |
| 73 |
void SID::set_is_muted(int ch, bool val) { |
| 74 |
if (ch<0 || ch>2) return; |
| 75 |
isMuted[ch]=val; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
// ---------------------------------------------------------------------------- |
| 80 |
// Set chip model. |
| 81 |
// ---------------------------------------------------------------------------- |
| 82 |
void SID::set_chip_model(chip_model model) |
| 83 |
{ |
| 84 |
for (int i = 0; i < 3; i++) { |
| 85 |
voice[i].set_chip_model(model); |
| 86 |
} |
| 87 |
|
| 88 |
filter.set_chip_model(model); |
| 89 |
extfilt.set_chip_model(model); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
// ---------------------------------------------------------------------------- |
| 94 |
// SID reset. |
| 95 |
// ---------------------------------------------------------------------------- |
| 96 |
void SID::reset() |
| 97 |
{ |
| 98 |
for (int i = 0; i < 3; i++) { |
| 99 |
voice[i].reset(); |
| 100 |
} |
| 101 |
filter.reset(); |
| 102 |
extfilt.reset(); |
| 103 |
|
| 104 |
bus_value = 0; |
| 105 |
bus_value_ttl = 0; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
// ---------------------------------------------------------------------------- |
| 110 |
// Write 16-bit sample to audio input. |
| 111 |
// NB! The caller is responsible for keeping the value within 16 bits. |
| 112 |
// Note that to mix in an external audio signal, the signal should be |
| 113 |
// resampled to 1MHz first to avoid sampling noise. |
| 114 |
// ---------------------------------------------------------------------------- |
| 115 |
void SID::input(int sample) |
| 116 |
{ |
| 117 |
// Voice outputs are 20 bits. Scale up to match three voices in order |
| 118 |
// to facilitate simulation of the MOS8580 "digi boost" hardware hack. |
| 119 |
ext_in = (sample << 4)*3; |
| 120 |
} |
| 121 |
|
| 122 |
// ---------------------------------------------------------------------------- |
| 123 |
// Read sample from audio output. |
| 124 |
// Both 16-bit and n-bit output is provided. |
| 125 |
// ---------------------------------------------------------------------------- |
| 126 |
int SID::output() |
| 127 |
{ |
| 128 |
const int range = 1 << 16; |
| 129 |
const int half = range >> 1; |
| 130 |
int sample = extfilt.output()/((4095*255 >> 7)*3*15*2/range); |
| 131 |
if (sample >= half) { |
| 132 |
return half - 1; |
| 133 |
} |
| 134 |
if (sample < -half) { |
| 135 |
return -half; |
| 136 |
} |
| 137 |
return sample; |
| 138 |
} |
| 139 |
|
| 140 |
int SID::output(int bits) |
| 141 |
{ |
| 142 |
const int range = 1 << bits; |
| 143 |
const int half = range >> 1; |
| 144 |
int sample = extfilt.output()/((4095*255 >> 7)*3*15*2/range); |
| 145 |
if (sample >= half) { |
| 146 |
return half - 1; |
| 147 |
} |
| 148 |
if (sample < -half) { |
| 149 |
return -half; |
| 150 |
} |
| 151 |
return sample; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
// ---------------------------------------------------------------------------- |
| 156 |
// Read registers. |
| 157 |
// |
| 158 |
// Reading a write only register returns the last byte written to any SID |
| 159 |
// register. The individual bits in this value start to fade down towards |
| 160 |
// zero after a few cycles. All bits reach zero within approximately |
| 161 |
// $2000 - $4000 cycles. |
| 162 |
// It has been claimed that this fading happens in an orderly fashion, however |
| 163 |
// sampling of write only registers reveals that this is not the case. |
| 164 |
// NB! This is not correctly modeled. |
| 165 |
// The actual use of write only registers has largely been made in the belief |
| 166 |
// that all SID registers are readable. To support this belief the read |
| 167 |
// would have to be done immediately after a write to the same register |
| 168 |
// (remember that an intermediate write to another register would yield that |
| 169 |
// value instead). With this in mind we return the last value written to |
| 170 |
// any SID register for $2000 cycles without modeling the bit fading. |
| 171 |
// ---------------------------------------------------------------------------- |
| 172 |
reg8 SID::read(reg8 offset) |
| 173 |
{ |
| 174 |
switch (offset) { |
| 175 |
case 0x19: |
| 176 |
return potx.readPOT(); |
| 177 |
case 0x1a: |
| 178 |
return poty.readPOT(); |
| 179 |
case 0x1b: |
| 180 |
return voice[2].wave.readOSC(); |
| 181 |
case 0x1c: |
| 182 |
return voice[2].envelope.readENV(); |
| 183 |
default: |
| 184 |
return bus_value; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
// ---------------------------------------------------------------------------- |
| 190 |
// Write registers. |
| 191 |
// ---------------------------------------------------------------------------- |
| 192 |
void SID::write(reg8 offset, reg8 value) |
| 193 |
{ |
| 194 |
bus_value = value; |
| 195 |
bus_value_ttl = 0x2000; |
| 196 |
|
| 197 |
switch (offset) { |
| 198 |
case 0x00: |
| 199 |
voice[0].wave.writeFREQ_LO(value); |
| 200 |
break; |
| 201 |
case 0x01: |
| 202 |
voice[0].wave.writeFREQ_HI(value); |
| 203 |
break; |
| 204 |
case 0x02: |
| 205 |
voice[0].wave.writePW_LO(value); |
| 206 |
break; |
| 207 |
case 0x03: |
| 208 |
voice[0].wave.writePW_HI(value); |
| 209 |
break; |
| 210 |
case 0x04: |
| 211 |
voice[0].writeCONTROL_REG(value); |
| 212 |
break; |
| 213 |
case 0x05: |
| 214 |
voice[0].envelope.writeATTACK_DECAY(value); |
| 215 |
break; |
| 216 |
case 0x06: |
| 217 |
voice[0].envelope.writeSUSTAIN_RELEASE(value); |
| 218 |
break; |
| 219 |
case 0x07: |
| 220 |
voice[1].wave.writeFREQ_LO(value); |
| 221 |
break; |
| 222 |
case 0x08: |
| 223 |
voice[1].wave.writeFREQ_HI(value); |
| 224 |
break; |
| 225 |
case 0x09: |
| 226 |
voice[1].wave.writePW_LO(value); |
| 227 |
break; |
| 228 |
case 0x0a: |
| 229 |
voice[1].wave.writePW_HI(value); |
| 230 |
break; |
| 231 |
case 0x0b: |
| 232 |
voice[1].writeCONTROL_REG(value); |
| 233 |
break; |
| 234 |
case 0x0c: |
| 235 |
voice[1].envelope.writeATTACK_DECAY(value); |
| 236 |
break; |
| 237 |
case 0x0d: |
| 238 |
voice[1].envelope.writeSUSTAIN_RELEASE(value); |
| 239 |
break; |
| 240 |
case 0x0e: |
| 241 |
voice[2].wave.writeFREQ_LO(value); |
| 242 |
break; |
| 243 |
case 0x0f: |
| 244 |
voice[2].wave.writeFREQ_HI(value); |
| 245 |
break; |
| 246 |
case 0x10: |
| 247 |
voice[2].wave.writePW_LO(value); |
| 248 |
break; |
| 249 |
case 0x11: |
| 250 |
voice[2].wave.writePW_HI(value); |
| 251 |
break; |
| 252 |
case 0x12: |
| 253 |
voice[2].writeCONTROL_REG(value); |
| 254 |
break; |
| 255 |
case 0x13: |
| 256 |
voice[2].envelope.writeATTACK_DECAY(value); |
| 257 |
break; |
| 258 |
case 0x14: |
| 259 |
voice[2].envelope.writeSUSTAIN_RELEASE(value); |
| 260 |
break; |
| 261 |
case 0x15: |
| 262 |
filter.writeFC_LO(value); |
| 263 |
break; |
| 264 |
case 0x16: |
| 265 |
filter.writeFC_HI(value); |
| 266 |
break; |
| 267 |
case 0x17: |
| 268 |
filter.writeRES_FILT(value); |
| 269 |
break; |
| 270 |
case 0x18: |
| 271 |
filter.writeMODE_VOL(value); |
| 272 |
break; |
| 273 |
default: |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
// ---------------------------------------------------------------------------- |
| 280 |
// Constructor. |
| 281 |
// ---------------------------------------------------------------------------- |
| 282 |
SID::State::State() |
| 283 |
{ |
| 284 |
int i; |
| 285 |
|
| 286 |
for (i = 0; i < 0x20; i++) { |
| 287 |
sid_register[i] = 0; |
| 288 |
} |
| 289 |
|
| 290 |
bus_value = 0; |
| 291 |
bus_value_ttl = 0; |
| 292 |
|
| 293 |
for (i = 0; i < 3; i++) { |
| 294 |
accumulator[i] = 0; |
| 295 |
shift_register[i] = 0x7ffff8; |
| 296 |
rate_counter[i] = 0; |
| 297 |
rate_counter_period[i] = 9; |
| 298 |
exponential_counter[i] = 0; |
| 299 |
exponential_counter_period[i] = 1; |
| 300 |
envelope_counter[i] = 0; |
| 301 |
envelope_state[i] = EnvelopeGenerator::RELEASE; |
| 302 |
hold_zero[i] = true; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
// ---------------------------------------------------------------------------- |
| 308 |
// Read state. |
| 309 |
// ---------------------------------------------------------------------------- |
| 310 |
SID::State SID::read_state() |
| 311 |
{ |
| 312 |
State state; |
| 313 |
int i, j; |
| 314 |
|
| 315 |
for (i = 0, j = 0; i < 3; i++, j += 7) { |
| 316 |
WaveformGenerator& wave = voice[i].wave; |
| 317 |
EnvelopeGenerator& envelope = voice[i].envelope; |
| 318 |
state.sid_register[j + 0] = wave.freq & 0xff; |
| 319 |
state.sid_register[j + 1] = wave.freq >> 8; |
| 320 |
state.sid_register[j + 2] = wave.pw & 0xff; |
| 321 |
state.sid_register[j + 3] = wave.pw >> 8; |
| 322 |
state.sid_register[j + 4] = |
| 323 |
(wave.waveform << 4) |
| 324 |
| (wave.test ? 0x08 : 0) |
| 325 |
| (wave.ring_mod ? 0x04 : 0) |
| 326 |
| (wave.sync ? 0x02 : 0) |
| 327 |
| (envelope.gate ? 0x01 : 0); |
| 328 |
state.sid_register[j + 5] = (envelope.attack << 4) | envelope.decay; |
| 329 |
state.sid_register[j + 6] = (envelope.sustain << 4) | envelope.release; |
| 330 |
} |
| 331 |
|
| 332 |
state.sid_register[j++] = filter.fc & 0x007; |
| 333 |
state.sid_register[j++] = filter.fc >> 3; |
| 334 |
state.sid_register[j++] = (filter.res << 4) | filter.filt; |
| 335 |
state.sid_register[j++] = |
| 336 |
(filter.voice3off ? 0x80 : 0) |
| 337 |
| (filter.hp_bp_lp << 4) |
| 338 |
| filter.vol; |
| 339 |
|
| 340 |
// These registers are superfluous, but included for completeness. |
| 341 |
for (; j < 0x1d; j++) { |
| 342 |
state.sid_register[j] = read(j); |
| 343 |
} |
| 344 |
for (; j < 0x20; j++) { |
| 345 |
state.sid_register[j] = 0; |
| 346 |
} |
| 347 |
|
| 348 |
state.bus_value = bus_value; |
| 349 |
state.bus_value_ttl = bus_value_ttl; |
| 350 |
|
| 351 |
for (i = 0; i < 3; i++) { |
| 352 |
state.accumulator[i] = voice[i].wave.accumulator; |
| 353 |
state.shift_register[i] = voice[i].wave.shift_register; |
| 354 |
state.rate_counter[i] = voice[i].envelope.rate_counter; |
| 355 |
state.rate_counter_period[i] = voice[i].envelope.rate_period; |
| 356 |
state.exponential_counter[i] = voice[i].envelope.exponential_counter; |
| 357 |
state.exponential_counter_period[i] = voice[i].envelope.exponential_counter_period; |
| 358 |
state.envelope_counter[i] = voice[i].envelope.envelope_counter; |
| 359 |
state.envelope_state[i] = voice[i].envelope.state; |
| 360 |
state.hold_zero[i] = voice[i].envelope.hold_zero; |
| 361 |
} |
| 362 |
|
| 363 |
return state; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
// ---------------------------------------------------------------------------- |
| 368 |
// Write state. |
| 369 |
// ---------------------------------------------------------------------------- |
| 370 |
void SID::write_state(const State& state) |
| 371 |
{ |
| 372 |
int i; |
| 373 |
|
| 374 |
for (i = 0; i <= 0x18; i++) { |
| 375 |
write(i, state.sid_register[i]); |
| 376 |
} |
| 377 |
|
| 378 |
bus_value = state.bus_value; |
| 379 |
bus_value_ttl = state.bus_value_ttl; |
| 380 |
|
| 381 |
for (i = 0; i < 3; i++) { |
| 382 |
voice[i].wave.accumulator = state.accumulator[i]; |
| 383 |
voice[i].wave.shift_register = state.shift_register[i]; |
| 384 |
voice[i].envelope.rate_counter = state.rate_counter[i]; |
| 385 |
voice[i].envelope.rate_period = state.rate_counter_period[i]; |
| 386 |
voice[i].envelope.exponential_counter = state.exponential_counter[i]; |
| 387 |
voice[i].envelope.exponential_counter_period = state.exponential_counter_period[i]; |
| 388 |
voice[i].envelope.envelope_counter = state.envelope_counter[i]; |
| 389 |
voice[i].envelope.state = state.envelope_state[i]; |
| 390 |
voice[i].envelope.hold_zero = state.hold_zero[i]; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
// ---------------------------------------------------------------------------- |
| 396 |
// Enable filter. |
| 397 |
// ---------------------------------------------------------------------------- |
| 398 |
void SID::enable_filter(bool enable) |
| 399 |
{ |
| 400 |
filter.enable_filter(enable); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
// ---------------------------------------------------------------------------- |
| 405 |
// Enable external filter. |
| 406 |
// ---------------------------------------------------------------------------- |
| 407 |
void SID::enable_external_filter(bool enable) |
| 408 |
{ |
| 409 |
extfilt.enable_filter(enable); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
// ---------------------------------------------------------------------------- |
| 414 |
// I0() computes the 0th order modified Bessel function of the first kind. |
| 415 |
// This function is originally from resample-1.5/filterkit.c by J. O. Smith. |
| 416 |
// ---------------------------------------------------------------------------- |
| 417 |
double SID::I0(double x) |
| 418 |
{ |
| 419 |
// Max error acceptable in I0. |
| 420 |
const double I0e = 1e-6; |
| 421 |
|
| 422 |
double sum, u, halfx, temp; |
| 423 |
int n; |
| 424 |
|
| 425 |
sum = u = n = 1; |
| 426 |
halfx = x/2.0; |
| 427 |
|
| 428 |
do { |
| 429 |
temp = halfx/n++; |
| 430 |
u *= temp*temp; |
| 431 |
sum += u; |
| 432 |
} while (u >= I0e*sum); |
| 433 |
|
| 434 |
return sum; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
// ---------------------------------------------------------------------------- |
| 439 |
// Setting of SID sampling parameters. |
| 440 |
// |
| 441 |
// Use a clock freqency of 985248Hz for PAL C64, 1022730Hz for NTSC C64. |
| 442 |
// The default end of passband frequency is pass_freq = 0.9*sample_freq/2 |
| 443 |
// for sample frequencies up to ~ 44.1kHz, and 20kHz for higher sample |
| 444 |
// frequencies. |
| 445 |
// |
| 446 |
// For resampling, the ratio between the clock frequency and the sample |
| 447 |
// frequency is limited as follows: |
| 448 |
// 125*clock_freq/sample_freq < 16384 |
| 449 |
// E.g. provided a clock frequency of ~ 1MHz, the sample frequency can not |
| 450 |
// be set lower than ~ 8kHz. A lower sample frequency would make the |
| 451 |
// resampling code overfill its 16k sample ring buffer. |
| 452 |
// |
| 453 |
// The end of passband frequency is also limited: |
| 454 |
// pass_freq <= 0.9*sample_freq/2 |
| 455 |
|
| 456 |
// E.g. for a 44.1kHz sampling rate the end of passband frequency is limited |
| 457 |
// to slightly below 20kHz. This constraint ensures that the FIR table is |
| 458 |
// not overfilled. |
| 459 |
// ---------------------------------------------------------------------------- |
| 460 |
bool SID::set_sampling_parameters(double clock_freq, sampling_method method, |
| 461 |
double sample_freq, double pass_freq, |
| 462 |
double filter_scale) |
| 463 |
{ |
| 464 |
// Check resampling constraints. |
| 465 |
if (method == SAMPLE_RESAMPLE_INTERPOLATE || method == SAMPLE_RESAMPLE_FAST) |
| 466 |
{ |
| 467 |
// Check whether the sample ring buffer would overfill. |
| 468 |
if (FIR_N*clock_freq/sample_freq >= RINGSIZE) { |
| 469 |
return false; |
| 470 |
} |
| 471 |
|
| 472 |
// The default passband limit is 0.9*sample_freq/2 for sample |
| 473 |
// frequencies below ~ 44.1kHz, and 20kHz for higher sample frequencies. |
| 474 |
if (pass_freq < 0) { |
| 475 |
pass_freq = 20000; |
| 476 |
if (2*pass_freq/sample_freq >= 0.9) { |
| 477 |
pass_freq = 0.9*sample_freq/2; |
| 478 |
} |
| 479 |
} |
| 480 |
// Check whether the FIR table would overfill. |
| 481 |
else if (pass_freq > 0.9*sample_freq/2) { |
| 482 |
return false; |
| 483 |
} |
| 484 |
|
| 485 |
// The filter scaling is only included to avoid clipping, so keep |
| 486 |
// it sane. |
| 487 |
if (filter_scale < 0.9 || filter_scale > 1.0) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
clock_frequency = clock_freq; |
| 493 |
sampling = method; |
| 494 |
|
| 495 |
cycles_per_sample = |
| 496 |
cycle_count(clock_freq/sample_freq*(1 << FIXP_SHIFT) + 0.5); |
| 497 |
|
| 498 |
sample_offset = 0; |
| 499 |
sample_prev = 0; |
| 500 |
|
| 501 |
// FIR initialization is only necessary for resampling. |
| 502 |
if (method != SAMPLE_RESAMPLE_INTERPOLATE && method != SAMPLE_RESAMPLE_FAST) |
| 503 |
{ |
| 504 |
delete[] sample; |
| 505 |
delete[] fir; |
| 506 |
sample = 0; |
| 507 |
fir = 0; |
| 508 |
return true; |
| 509 |
} |
| 510 |
|
| 511 |
const double pi = 3.1415926535897932385; |
| 512 |
|
| 513 |
// 16 bits -> -96dB stopband attenuation. |
| 514 |
const double A = -20*log10(1.0/(1 << 16)); |
| 515 |
// A fraction of the bandwidth is allocated to the transition band, |
| 516 |
double dw = (1 - 2*pass_freq/sample_freq)*pi; |
| 517 |
// The cutoff frequency is midway through the transition band. |
| 518 |
double wc = (2*pass_freq/sample_freq + 1)*pi/2; |
| 519 |
|
| 520 |
// For calculation of beta and N see the reference for the kaiserord |
| 521 |
// function in the MATLAB Signal Processing Toolbox: |
| 522 |
// http://www.mathworks.com/access/helpdesk/help/toolbox/signal/kaiserord.html |
| 523 |
const double beta = 0.1102*(A - 8.7); |
| 524 |
const double I0beta = I0(beta); |
| 525 |
|
| 526 |
// The filter order will maximally be 124 with the current constraints. |
| 527 |
// N >= (96.33 - 7.95)/(2.285*0.1*pi) -> N >= 123 |
| 528 |
// The filter order is equal to the number of zero crossings, i.e. |
| 529 |
// it should be an even number (sinc is symmetric about x = 0). |
| 530 |
int N = int((A - 7.95)/(2.285*dw) + 0.5); |
| 531 |
N += N & 1; |
| 532 |
|
| 533 |
double f_samples_per_cycle = sample_freq/clock_freq; |
| 534 |
double f_cycles_per_sample = clock_freq/sample_freq; |
| 535 |
|
| 536 |
// The filter length is equal to the filter order + 1. |
| 537 |
// The filter length must be an odd number (sinc is symmetric about x = 0). |
| 538 |
fir_N = int(N*f_cycles_per_sample) + 1; |
| 539 |
fir_N |= 1; |
| 540 |
|
| 541 |
// We clamp the filter table resolution to 2^n, making the fixpoint |
| 542 |
// sample_offset a whole multiple of the filter table resolution. |
| 543 |
int res = method == SAMPLE_RESAMPLE_INTERPOLATE ? |
| 544 |
FIR_RES_INTERPOLATE : FIR_RES_FAST; |
| 545 |
int n = (int)ceil(log(res/f_cycles_per_sample)/log(2)); |
| 546 |
fir_RES = 1 << n; |
| 547 |
|
| 548 |
// Allocate memory for FIR tables. |
| 549 |
delete[] fir; |
| 550 |
fir = new short[fir_N*fir_RES]; |
| 551 |
|
| 552 |
// Calculate fir_RES FIR tables for linear interpolation. |
| 553 |
for (int i = 0; i < fir_RES; i++) { |
| 554 |
int fir_offset = i*fir_N + fir_N/2; |
| 555 |
double j_offset = double(i)/fir_RES; |
| 556 |
// Calculate FIR table. This is the sinc function, weighted by the |
| 557 |
// Kaiser window. |
| 558 |
for (int j = -fir_N/2; j <= fir_N/2; j++) { |
| 559 |
double jx = j - j_offset; |
| 560 |
double wt = wc*jx/f_cycles_per_sample; |
| 561 |
double temp = jx/(fir_N/2); |
| 562 |
double Kaiser = |
| 563 |
fabs(temp) <= 1 ? I0(beta*sqrt(1 - temp*temp))/I0beta : 0; |
| 564 |
double sincwt = |
| 565 |
fabs(wt) >= 1e-6 ? sin(wt)/wt : 1; |
| 566 |
double val = |
| 567 |
(1 << FIR_SHIFT)*filter_scale*f_samples_per_cycle*wc/pi*sincwt*Kaiser; |
| 568 |
fir[fir_offset + j] = short(val + 0.5); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
// Allocate sample buffer. |
| 573 |
if (!sample) { |
| 574 |
sample = new short[RINGSIZE*2]; |
| 575 |
} |
| 576 |
// Clear sample buffer. |
| 577 |
for (int j = 0; j < RINGSIZE*2; j++) { |
| 578 |
sample[j] = 0; |
| 579 |
} |
| 580 |
sample_index = 0; |
| 581 |
|
| 582 |
return true; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
// ---------------------------------------------------------------------------- |
| 587 |
// Adjustment of SID sampling frequency. |
| 588 |
// |
| 589 |
// In some applications, e.g. a C64 emulator, it can be desirable to |
| 590 |
// synchronize sound with a timer source. This is supported by adjustment of |
| 591 |
// the SID sampling frequency. |
| 592 |
// |
| 593 |
// NB! Adjustment of the sampling frequency may lead to noticeable shifts in |
| 594 |
// frequency, and should only be used for interactive applications. Note also |
| 595 |
// that any adjustment of the sampling frequency will change the |
| 596 |
// characteristics of the resampling filter, since the filter is not rebuilt. |
| 597 |
// ---------------------------------------------------------------------------- |
| 598 |
void SID::adjust_sampling_frequency(double sample_freq) |
| 599 |
{ |
| 600 |
cycles_per_sample = |
| 601 |
cycle_count(clock_frequency/sample_freq*(1 << FIXP_SHIFT) + 0.5); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
// ---------------------------------------------------------------------------- |
| 606 |
// Return array of default spline interpolation points to map FC to |
| 607 |
// filter cutoff frequency. |
| 608 |
// ---------------------------------------------------------------------------- |
| 609 |
void SID::fc_default(const fc_point*& points, int& count) |
| 610 |
{ |
| 611 |
filter.fc_default(points, count); |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
// ---------------------------------------------------------------------------- |
| 616 |
// Return FC spline plotter object. |
| 617 |
// ---------------------------------------------------------------------------- |
| 618 |
PointPlotter<sound_sample> SID::fc_plotter() |
| 619 |
{ |
| 620 |
return filter.fc_plotter(); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
// ---------------------------------------------------------------------------- |
| 625 |
// SID clocking - 1 cycle. |
| 626 |
// ---------------------------------------------------------------------------- |
| 627 |
void SID::clock() |
| 628 |
{ |
| 629 |
int i; |
| 630 |
|
| 631 |
// Age bus value. |
| 632 |
if (--bus_value_ttl <= 0) { |
| 633 |
bus_value = 0; |
| 634 |
bus_value_ttl = 0; |
| 635 |
} |
| 636 |
|
| 637 |
// Clock amplitude modulators. |
| 638 |
for (i = 0; i < 3; i++) { |
| 639 |
voice[i].envelope.clock(); |
| 640 |
} |
| 641 |
|
| 642 |
// Clock oscillators. |
| 643 |
for (i = 0; i < 3; i++) { |
| 644 |
voice[i].wave.clock(); |
| 645 |
} |
| 646 |
|
| 647 |
// Synchronize oscillators. |
| 648 |
for (i = 0; i < 3; i++) { |
| 649 |
voice[i].wave.synchronize(); |
| 650 |
} |
| 651 |
|
| 652 |
// write voice output |
| 653 |
last_chan_out[0]=isMuted[0]?0:voice[0].output(); |
| 654 |
last_chan_out[1]=isMuted[1]?0:voice[1].output(); |
| 655 |
last_chan_out[2]=isMuted[2]?0:voice[2].output(); |
| 656 |
|
| 657 |
// Clock filter. |
| 658 |
filter.clock(last_chan_out[0], last_chan_out[1], last_chan_out[2], ext_in); |
| 659 |
|
| 660 |
// Clock external filter. |
| 661 |
extfilt.clock(filter.output()); |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
// ---------------------------------------------------------------------------- |
| 666 |
// SID clocking - delta_t cycles. |
| 667 |
// ---------------------------------------------------------------------------- |
| 668 |
void SID::clock(cycle_count delta_t) |
| 669 |
{ |
| 670 |
int i; |
| 671 |
|
| 672 |
if (delta_t <= 0) { |
| 673 |
return; |
| 674 |
} |
| 675 |
|
| 676 |
// Age bus value. |
| 677 |
bus_value_ttl -= delta_t; |
| 678 |
if (bus_value_ttl <= 0) { |
| 679 |
bus_value = 0; |
| 680 |
bus_value_ttl = 0; |
| 681 |
} |
| 682 |
|
| 683 |
// Clock amplitude modulators. |
| 684 |
for (i = 0; i < 3; i++) { |
| 685 |
voice[i].envelope.clock(delta_t); |
| 686 |
} |
| 687 |
|
| 688 |
// Clock and synchronize oscillators. |
| 689 |
// Loop until we reach the current cycle. |
| 690 |
cycle_count delta_t_osc = delta_t; |
| 691 |
while (delta_t_osc) { |
| 692 |
cycle_count delta_t_min = delta_t_osc; |
| 693 |
|
| 694 |
// Find minimum number of cycles to an oscillator accumulator MSB toggle. |
| 695 |
// We have to clock on each MSB on / MSB off for hard sync to operate |
| 696 |
// correctly. |
| 697 |
for (i = 0; i < 3; i++) { |
| 698 |
WaveformGenerator& wave = voice[i].wave; |
| 699 |
|
| 700 |
// It is only necessary to clock on the MSB of an oscillator that is |
| 701 |
// a sync source and has freq != 0. |
| 702 |
if (!(wave.sync_dest->sync && wave.freq)) { |
| 703 |
continue; |
| 704 |
} |
| 705 |
|
| 706 |
reg16 freq = wave.freq; |
| 707 |
reg24 accumulator = wave.accumulator; |
| 708 |
|
| 709 |
// Clock on MSB off if MSB is on, clock on MSB on if MSB is off. |
| 710 |
reg24 delta_accumulator = |
| 711 |
(accumulator & 0x800000 ? 0x1000000 : 0x800000) - accumulator; |
| 712 |
|
| 713 |
cycle_count delta_t_next = delta_accumulator/freq; |
| 714 |
if (delta_accumulator%freq) { |
| 715 |
++delta_t_next; |
| 716 |
} |
| 717 |
|
| 718 |
if (delta_t_next < delta_t_min) { |
| 719 |
delta_t_min = delta_t_next; |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
// Clock oscillators. |
| 724 |
for (i = 0; i < 3; i++) { |
| 725 |
voice[i].wave.clock(delta_t_min); |
| 726 |
} |
| 727 |
|
| 728 |
// Synchronize oscillators. |
| 729 |
for (i = 0; i < 3; i++) { |
| 730 |
voice[i].wave.synchronize(); |
| 731 |
} |
| 732 |
|
| 733 |
delta_t_osc -= delta_t_min; |
| 734 |
} |
| 735 |
|
| 736 |
// write voice output |
| 737 |
last_chan_out[0]=isMuted[0]?0:voice[0].output(); |
| 738 |
last_chan_out[1]=isMuted[1]?0:voice[1].output(); |
| 739 |
last_chan_out[2]=isMuted[2]?0:voice[2].output(); |
| 740 |
|
| 741 |
// Clock filter. |
| 742 |
filter.clock(delta_t, |
| 743 |
last_chan_out[0], last_chan_out[1], last_chan_out[2], ext_in); |
| 744 |
|
| 745 |
// Clock external filter. |
| 746 |
extfilt.clock(delta_t, filter.output()); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
// ---------------------------------------------------------------------------- |
| 751 |
// SID clocking with audio sampling. |
| 752 |
// Fixpoint arithmetics is used. |
| 753 |
// |
| 754 |
// The example below shows how to clock the SID a specified amount of cycles |
| 755 |
// while producing audio output: |
| 756 |
// |
| 757 |
// while (delta_t) { |
| 758 |
// bufindex += sid.clock(delta_t, buf + bufindex, buflength - bufindex); |
| 759 |
// write(dsp, buf, bufindex*2); |
| 760 |
// bufindex = 0; |
| 761 |
// } |
| 762 |
// |
| 763 |
// ---------------------------------------------------------------------------- |
| 764 |
int SID::clock(cycle_count& delta_t, short* buf, int n, int interleave) |
| 765 |
{ |
| 766 |
switch (sampling) { |
| 767 |
default: |
| 768 |
case SAMPLE_FAST: |
| 769 |
return clock_fast(delta_t, buf, n, interleave); |
| 770 |
case SAMPLE_INTERPOLATE: |
| 771 |
return clock_interpolate(delta_t, buf, n, interleave); |
| 772 |
case SAMPLE_RESAMPLE_INTERPOLATE: |
| 773 |
return clock_resample_interpolate(delta_t, buf, n, interleave); |
| 774 |
case SAMPLE_RESAMPLE_FAST: |
| 775 |
return clock_resample_fast(delta_t, buf, n, interleave); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
// ---------------------------------------------------------------------------- |
| 780 |
// SID clocking with audio sampling - delta clocking picking nearest sample. |
| 781 |
// ---------------------------------------------------------------------------- |
| 782 |
RESID_INLINE |
| 783 |
int SID::clock_fast(cycle_count& delta_t, short* buf, int n, |
| 784 |
int interleave) |
| 785 |
{ |
| 786 |
int s = 0; |
| 787 |
|
| 788 |
for (;;) { |
| 789 |
cycle_count next_sample_offset = sample_offset + cycles_per_sample + (1 << (FIXP_SHIFT - 1)); |
| 790 |
cycle_count delta_t_sample = next_sample_offset >> FIXP_SHIFT; |
| 791 |
if (delta_t_sample > delta_t) { |
| 792 |
break; |
| 793 |
} |
| 794 |
if (s >= n) { |
| 795 |
return s; |
| 796 |
} |
| 797 |
clock(delta_t_sample); |
| 798 |
delta_t -= delta_t_sample; |
| 799 |
sample_offset = (next_sample_offset & FIXP_MASK) - (1 << (FIXP_SHIFT - 1)); |
| 800 |
buf[s++*interleave] = output(); |
| 801 |
} |
| 802 |
|
| 803 |
clock(delta_t); |
| 804 |
sample_offset -= delta_t << FIXP_SHIFT; |
| 805 |
delta_t = 0; |
| 806 |
return s; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
// ---------------------------------------------------------------------------- |
| 811 |
// SID clocking with audio sampling - cycle based with linear sample |
| 812 |
// interpolation. |
| 813 |
// |
| 814 |
// Here the chip is clocked every cycle. This yields higher quality |
| 815 |
// sound since the samples are linearly interpolated, and since the |
| 816 |
// external filter attenuates frequencies above 16kHz, thus reducing |
| 817 |
// sampling noise. |
| 818 |
// ---------------------------------------------------------------------------- |
| 819 |
RESID_INLINE |
| 820 |
int SID::clock_interpolate(cycle_count& delta_t, short* buf, int n, |
| 821 |
int interleave) |
| 822 |
{ |
| 823 |
int s = 0; |
| 824 |
int i; |
| 825 |
|
| 826 |
for (;;) { |
| 827 |
cycle_count next_sample_offset = sample_offset + cycles_per_sample; |
| 828 |
cycle_count delta_t_sample = next_sample_offset >> FIXP_SHIFT; |
| 829 |
if (delta_t_sample > delta_t) { |
| 830 |
break; |
| 831 |
} |
| 832 |
if (s >= n) { |
| 833 |
return s; |
| 834 |
} |
| 835 |
for (i = 0; i < delta_t_sample - 1; i++) { |
| 836 |
clock(); |
| 837 |
} |
| 838 |
if (i < delta_t_sample) { |
| 839 |
sample_prev = output(); |
| 840 |
clock(); |
| 841 |
} |
| 842 |
|
| 843 |
delta_t -= delta_t_sample; |
| 844 |
sample_offset = next_sample_offset & FIXP_MASK; |
| 845 |
|
| 846 |
short sample_now = output(); |
| 847 |
buf[s++*interleave] = |
| 848 |
sample_prev + (sample_offset*(sample_now - sample_prev) >> FIXP_SHIFT); |
| 849 |
sample_prev = sample_now; |
| 850 |
} |
| 851 |
|
| 852 |
for (i = 0; i < delta_t - 1; i++) { |
| 853 |
clock(); |
| 854 |
} |
| 855 |
if (i < delta_t) { |
| 856 |
sample_prev = output(); |
| 857 |
clock(); |
| 858 |
} |
| 859 |
sample_offset -= delta_t << FIXP_SHIFT; |
| 860 |
delta_t = 0; |
| 861 |
return s; |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
// ---------------------------------------------------------------------------- |
| 866 |
// SID clocking with audio sampling - cycle based with audio resampling. |
| 867 |
// |
| 868 |
// This is the theoretically correct (and computationally intensive) audio |
| 869 |
// sample generation. The samples are generated by resampling to the specified |
| 870 |
// sampling frequency. The work rate is inversely proportional to the |
| 871 |
// percentage of the bandwidth allocated to the filter transition band. |
| 872 |
// |
| 873 |
// This implementation is based on the paper "A Flexible Sampling-Rate |
| 874 |
// Conversion Method", by J. O. Smith and P. Gosset, or rather on the |
| 875 |
// expanded tutorial on the "Digital Audio Resampling Home Page": |
| 876 |
// http://www-ccrma.stanford.edu/~jos/resample/ |
| 877 |
// |
| 878 |
// By building shifted FIR tables with samples according to the |
| 879 |
// sampling frequency, this implementation dramatically reduces the |
| 880 |
// computational effort in the filter convolutions, without any loss |
| 881 |
// of accuracy. The filter convolutions are also vectorizable on |
| 882 |
// current hardware. |
| 883 |
// |
| 884 |
// Further possible optimizations are: |
| 885 |
// * An equiripple filter design could yield a lower filter order, see |
| 886 |
// http://www.mwrf.com/Articles/ArticleID/7229/7229.html |
| 887 |
// * The Convolution Theorem could be used to bring the complexity of |
| 888 |
// convolution down from O(n*n) to O(n*log(n)) using the Fast Fourier |
| 889 |
// Transform, see http://en.wikipedia.org/wiki/Convolution_theorem |
| 890 |
// * Simply resampling in two steps can also yield computational |
| 891 |
// savings, since the transition band will be wider in the first step |
| 892 |
// and the required filter order is thus lower in this step. |
| 893 |
// Laurent Ganier has found the optimal intermediate sampling frequency |
| 894 |
// to be (via derivation of sum of two steps): |
| 895 |
// 2 * pass_freq + sqrt [ 2 * pass_freq * orig_sample_freq |
| 896 |
// * (dest_sample_freq - 2 * pass_freq) / dest_sample_freq ] |
| 897 |
// |
| 898 |
// NB! the result of right shifting negative numbers is really |
| 899 |
// implementation dependent in the C++ standard. |
| 900 |
// ---------------------------------------------------------------------------- |
| 901 |
RESID_INLINE |
| 902 |
int SID::clock_resample_interpolate(cycle_count& delta_t, short* buf, int n, |
| 903 |
int interleave) |
| 904 |
{ |
| 905 |
int s = 0; |
| 906 |
|
| 907 |
for (;;) { |
| 908 |
cycle_count next_sample_offset = sample_offset + cycles_per_sample; |
| 909 |
cycle_count delta_t_sample = next_sample_offset >> FIXP_SHIFT; |
| 910 |
if (delta_t_sample > delta_t) { |
| 911 |
break; |
| 912 |
} |
| 913 |
if (s >= n) { |
| 914 |
return s; |
| 915 |
} |
| 916 |
for (int i = 0; i < delta_t_sample; i++) { |
| 917 |
clock(); |
| 918 |
sample[sample_index] = sample[sample_index + RINGSIZE] = output(); |
| 919 |
++sample_index; |
| 920 |
sample_index &= 0x3fff; |
| 921 |
} |
| 922 |
delta_t -= delta_t_sample; |
| 923 |
sample_offset = next_sample_offset & FIXP_MASK; |
| 924 |
|
| 925 |
int fir_offset = sample_offset*fir_RES >> FIXP_SHIFT; |
| 926 |
int fir_offset_rmd = sample_offset*fir_RES & FIXP_MASK; |
| 927 |
short* fir_start = fir + fir_offset*fir_N; |
| 928 |
short* sample_start = sample + sample_index - fir_N + RINGSIZE; |
| 929 |
|
| 930 |
// Convolution with filter impulse response. |
| 931 |
int v1 = 0; |
| 932 |
for (int j = 0; j < fir_N; j++) { |
| 933 |
v1 += sample_start[j]*fir_start[j]; |
| 934 |
} |
| 935 |
|
| 936 |
// Use next FIR table, wrap around to first FIR table using |
| 937 |
// previous sample. |
| 938 |
if (++fir_offset == fir_RES) { |
| 939 |
fir_offset = 0; |
| 940 |
--sample_start; |
| 941 |
} |
| 942 |
fir_start = fir + fir_offset*fir_N; |
| 943 |
|
| 944 |
// Convolution with filter impulse response. |
| 945 |
int v2 = 0; |
| 946 |
for (int j = 0; j < fir_N; j++) { |
| 947 |
v2 += sample_start[j]*fir_start[j]; |
| 948 |
} |
| 949 |
|
| 950 |
// Linear interpolation. |
| 951 |
// fir_offset_rmd is equal for all samples, it can thus be factorized out: |
| 952 |
// sum(v1 + rmd*(v2 - v1)) = sum(v1) + rmd*(sum(v2) - sum(v1)) |
| 953 |
int v = v1 + (fir_offset_rmd*(v2 - v1) >> FIXP_SHIFT); |
| 954 |
|
| 955 |
v >>= FIR_SHIFT; |
| 956 |
|
| 957 |
// Saturated arithmetics to guard against 16 bit sample overflow. |
| 958 |
const int half = 1 << 15; |
| 959 |
if (v >= half) { |
| 960 |
v = half - 1; |
| 961 |
} |
| 962 |
else if (v < -half) { |
| 963 |
v = -half; |
| 964 |
} |
| 965 |
|
| 966 |
buf[s++*interleave] = v; |
| 967 |
} |
| 968 |
|
| 969 |
for (int i = 0; i < delta_t; i++) { |
| 970 |
clock(); |
| 971 |
sample[sample_index] = sample[sample_index + RINGSIZE] = output(); |
| 972 |
++sample_index; |
| 973 |
sample_index &= 0x3fff; |
| 974 |
} |
| 975 |
sample_offset -= delta_t << FIXP_SHIFT; |
| 976 |
delta_t = 0; |
| 977 |
return s; |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
// ---------------------------------------------------------------------------- |
| 982 |
// SID clocking with audio sampling - cycle based with audio resampling. |
| 983 |
// ---------------------------------------------------------------------------- |
| 984 |
RESID_INLINE |
| 985 |
int SID::clock_resample_fast(cycle_count& delta_t, short* buf, int n, |
| 986 |
int interleave) |
| 987 |
{ |
| 988 |
int s = 0; |
| 989 |
|
| 990 |
for (;;) { |
| 991 |
cycle_count next_sample_offset = sample_offset + cycles_per_sample; |
| 992 |
cycle_count delta_t_sample = next_sample_offset >> FIXP_SHIFT; |
| 993 |
if (delta_t_sample > delta_t) { |
| 994 |
break; |
| 995 |
} |
| 996 |
if (s >= n) { |
| 997 |
return s; |
| 998 |
} |
| 999 |
for (int i = 0; i < delta_t_sample; i++) { |
| 1000 |
clock(); |
| 1001 |
sample[sample_index] = sample[sample_index + RINGSIZE] = output(); |
| 1002 |
++sample_index; |
| 1003 |
sample_index &= 0x3fff; |
| 1004 |
} |
| 1005 |
delta_t -= delta_t_sample; |
| 1006 |
sample_offset = next_sample_offset & FIXP_MASK; |
| 1007 |
|
| 1008 |
int fir_offset = sample_offset*fir_RES >> FIXP_SHIFT; |
| 1009 |
short* fir_start = fir + fir_offset*fir_N; |
| 1010 |
short* sample_start = sample + sample_index - fir_N + RINGSIZE; |
| 1011 |
|
| 1012 |
// Convolution with filter impulse response. |
| 1013 |
int v = 0; |
| 1014 |
for (int j = 0; j < fir_N; j++) { |
| 1015 |
v += sample_start[j]*fir_start[j]; |
| 1016 |
} |
| 1017 |
|
| 1018 |
v >>= FIR_SHIFT; |
| 1019 |
|
| 1020 |
// Saturated arithmetics to guard against 16 bit sample overflow. |
| 1021 |
const int half = 1 << 15; |
| 1022 |
if (v >= half) { |
| 1023 |
v = half - 1; |
| 1024 |
} |
| 1025 |
else if (v < -half) { |
| 1026 |
v = -half; |
| 1027 |
} |
| 1028 |
|
| 1029 |
buf[s++*interleave] = v; |
| 1030 |
} |
| 1031 |
|
| 1032 |
for (int i = 0; i < delta_t; i++) { |
| 1033 |
clock(); |
| 1034 |
sample[sample_index] = sample[sample_index + RINGSIZE] = output(); |
| 1035 |
++sample_index; |
| 1036 |
sample_index &= 0x3fff; |
| 1037 |
} |
| 1038 |
sample_offset -= delta_t << FIXP_SHIFT; |
| 1039 |
delta_t = 0; |
| 1040 |
return s; |
| 1041 |
} |