| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2022 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 "Mikey.hpp" |
| 21 |
#include <array> |
| 22 |
#include <cstdint> |
| 23 |
#include <vector> |
| 24 |
#include <functional> |
| 25 |
#include <cassert> |
| 26 |
#include <algorithm> |
| 27 |
#include <limits> |
| 28 |
|
| 29 |
#if defined( _MSC_VER ) |
| 30 |
|
| 31 |
#include <intrin.h> |
| 32 |
|
| 33 |
static uint32_t popcnt_generic( uint32_t x ) |
| 34 |
{ |
| 35 |
int v = 0; |
| 36 |
while ( x != 0 ) |
| 37 |
{ |
| 38 |
x &= x - 1; |
| 39 |
v++; |
| 40 |
} |
| 41 |
return v; |
| 42 |
} |
| 43 |
|
| 44 |
#if defined( _M_IX86 ) || defined( _M_X64 ) |
| 45 |
|
| 46 |
static uint32_t popcnt_intrinsic( uint32_t x ) |
| 47 |
{ |
| 48 |
return __popcnt( x ); |
| 49 |
} |
| 50 |
|
| 51 |
static uint32_t( *popcnt )( uint32_t ); |
| 52 |
|
| 53 |
//detecting popcnt availability on msvc intel |
| 54 |
static void selectPOPCNT() |
| 55 |
{ |
| 56 |
int info[4]; |
| 57 |
__cpuid( info, 1 ); |
| 58 |
if ( ( info[2] & ( (int)1 << 23 ) ) != 0 ) |
| 59 |
{ |
| 60 |
popcnt = &popcnt_intrinsic; |
| 61 |
} |
| 62 |
else |
| 63 |
{ |
| 64 |
popcnt = &popcnt_generic; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#else //defined( _M_IX86 ) || defined( _M_X64 ) |
| 69 |
|
| 70 |
//MSVC non INTEL should use generic implementation |
| 71 |
inline void selectPOPCNT() |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
#define popcnt popcnt_generic |
| 76 |
|
| 77 |
#endif |
| 78 |
|
| 79 |
#else //defined( _MSC_VER ) |
| 80 |
|
| 81 |
//non MVSC should use builtin implementation |
| 82 |
|
| 83 |
inline void selectPOPCNT() |
| 84 |
{ |
| 85 |
} |
| 86 |
|
| 87 |
#define popcnt __builtin_popcount |
| 88 |
|
| 89 |
#endif |
| 90 |
|
| 91 |
namespace Lynx |
| 92 |
{ |
| 93 |
|
| 94 |
namespace |
| 95 |
{ |
| 96 |
|
| 97 |
static constexpr int64_t CNT_MAX = std::numeric_limits<int64_t>::max() & ~15; |
| 98 |
|
| 99 |
int32_t clamp( int32_t v, int32_t lo, int32_t hi ) |
| 100 |
{ |
| 101 |
return v < lo ? lo : ( v > hi ? hi : v ); |
| 102 |
} |
| 103 |
|
| 104 |
class Timer |
| 105 |
{ |
| 106 |
public: |
| 107 |
Timer() : mValueUpdateTick{}, mAudShift {}, mEnableReload{}, mEnableCount{}, mTimerDone{}, mBackup{ 0 }, mValue{ 0 } |
| 108 |
{ |
| 109 |
} |
| 110 |
|
| 111 |
int64_t setBackup( int64_t tick, uint8_t backup ) |
| 112 |
{ |
| 113 |
mBackup = backup; |
| 114 |
return computeAction( tick ); |
| 115 |
} |
| 116 |
|
| 117 |
int64_t setControlA( int64_t tick, uint8_t controlA ) |
| 118 |
{ |
| 119 |
mTimerDone ^= ( controlA & CONTROLA::RESET_DONE ) != 0; |
| 120 |
mEnableReload = ( controlA & CONTROLA::ENABLE_RELOAD ) != 0; |
| 121 |
mEnableCount = ( controlA & CONTROLA::ENABLE_COUNT ) != 0; |
| 122 |
mAudShift = controlA & CONTROLA::AUD_CLOCK_MASK; |
| 123 |
|
| 124 |
return computeAction( tick ); |
| 125 |
} |
| 126 |
|
| 127 |
int64_t setCount( int64_t tick, uint8_t value ) |
| 128 |
{ |
| 129 |
return computeTriggerTime( tick ); |
| 130 |
} |
| 131 |
|
| 132 |
void setControlB( uint8_t controlB ) |
| 133 |
{ |
| 134 |
mTimerDone = ( controlB & CONTROLB::TIMER_DONE ) != 0; |
| 135 |
} |
| 136 |
|
| 137 |
int64_t fireAction( int64_t tick ) |
| 138 |
{ |
| 139 |
mTimerDone = true; |
| 140 |
|
| 141 |
return computeAction( tick ); |
| 142 |
} |
| 143 |
|
| 144 |
uint8_t getBackup() const |
| 145 |
{ |
| 146 |
return mBackup; |
| 147 |
} |
| 148 |
|
| 149 |
uint8_t getCount( int64_t tick ) |
| 150 |
{ |
| 151 |
updateValue( tick ); |
| 152 |
return mValue; |
| 153 |
} |
| 154 |
|
| 155 |
private: |
| 156 |
|
| 157 |
int64_t scaleDiff( int64_t older, int64_t newer ) const |
| 158 |
{ |
| 159 |
int64_t const mask = (int64_t)( ~0ull << ( mAudShift + 4 ) ); |
| 160 |
return ( ( newer & mask ) - ( older & mask ) ) >> ( mAudShift + 4 ); |
| 161 |
} |
| 162 |
|
| 163 |
void updateValue( int64_t tick ) |
| 164 |
{ |
| 165 |
if ( mEnableCount ) |
| 166 |
mValue = (uint8_t)std::max( (int64_t)0, mValue - scaleDiff( mValueUpdateTick, tick ) ); |
| 167 |
mValueUpdateTick = tick; |
| 168 |
} |
| 169 |
|
| 170 |
int64_t computeTriggerTime( int64_t tick ) |
| 171 |
{ |
| 172 |
if ( mEnableCount && mValue != 0 ) |
| 173 |
{ |
| 174 |
//tick value is increased by multipy of 16 (1 MHz resolution) lower bits are unchanged |
| 175 |
return tick + ( 1ull + mValue ) * ( 1ull << ( mAudShift + 4 ) ); |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
return CNT_MAX; //infinite |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
int64_t computeAction( int64_t tick ) |
| 184 |
{ |
| 185 |
updateValue( tick ); |
| 186 |
if ( mValue == 0 && mEnableReload ) |
| 187 |
{ |
| 188 |
mValue = mBackup; |
| 189 |
} |
| 190 |
|
| 191 |
return computeTriggerTime( tick ); |
| 192 |
} |
| 193 |
|
| 194 |
private: |
| 195 |
struct CONTROLA |
| 196 |
{ |
| 197 |
static constexpr uint8_t RESET_DONE = 0b01000000; |
| 198 |
static constexpr uint8_t ENABLE_RELOAD = 0b00010000; |
| 199 |
static constexpr uint8_t ENABLE_COUNT = 0b00001000; |
| 200 |
static constexpr uint8_t AUD_CLOCK_MASK = 0b00000111; |
| 201 |
}; |
| 202 |
struct CONTROLB |
| 203 |
{ |
| 204 |
static constexpr uint8_t TIMER_DONE = 0b00001000; |
| 205 |
}; |
| 206 |
|
| 207 |
private: |
| 208 |
int64_t mValueUpdateTick; |
| 209 |
int mAudShift; |
| 210 |
bool mEnableReload; |
| 211 |
bool mEnableCount; |
| 212 |
bool mTimerDone; |
| 213 |
uint8_t mBackup; |
| 214 |
uint8_t mValue; |
| 215 |
}; |
| 216 |
|
| 217 |
class AudioChannel |
| 218 |
{ |
| 219 |
public: |
| 220 |
AudioChannel( uint32_t number ) : mTimer{}, mNumber{ number }, mShiftRegister{}, mTapSelector{}, mEnableIntegrate{}, mVolume{}, mOutput{}, mCtrlA{} |
| 221 |
{ |
| 222 |
} |
| 223 |
|
| 224 |
int64_t fireAction( int64_t tick ) |
| 225 |
{ |
| 226 |
trigger(); |
| 227 |
return adjust( mTimer.fireAction( tick ) ); |
| 228 |
} |
| 229 |
|
| 230 |
void setVolume( int8_t value ) |
| 231 |
{ |
| 232 |
mVolume = value; |
| 233 |
} |
| 234 |
|
| 235 |
void setFeedback( uint8_t value ) |
| 236 |
{ |
| 237 |
mTapSelector = ( mTapSelector & 0b0011'1100'0000 ) | ( value & 0b0011'1111 ) | ( ( (int)value & 0b1100'0000 ) << 4 ); |
| 238 |
} |
| 239 |
|
| 240 |
void setOutput( uint8_t value ) |
| 241 |
{ |
| 242 |
mOutput = value; |
| 243 |
} |
| 244 |
|
| 245 |
void setShift( uint8_t value ) |
| 246 |
{ |
| 247 |
mShiftRegister = ( mShiftRegister & 0xff00 ) | value; |
| 248 |
} |
| 249 |
|
| 250 |
int64_t setBackup( int64_t tick, uint8_t value ) |
| 251 |
{ |
| 252 |
return adjust( mTimer.setBackup( tick, value ) ); |
| 253 |
} |
| 254 |
|
| 255 |
int64_t setControl( int64_t tick, uint8_t value ) |
| 256 |
{ |
| 257 |
if ( mCtrlA == value ) |
| 258 |
return 0; |
| 259 |
mCtrlA = value; |
| 260 |
|
| 261 |
mTapSelector = ( mTapSelector & 0b1111'0111'1111 ) | ( value & FEEDBACK_7 ); |
| 262 |
mEnableIntegrate = ( value & ENABLE_INTEGRATE ) != 0; |
| 263 |
return adjust( mTimer.setControlA( tick, value & ~( FEEDBACK_7 | ENABLE_INTEGRATE ) ) ); |
| 264 |
} |
| 265 |
|
| 266 |
int64_t setCounter( int64_t tick, uint8_t value ) |
| 267 |
{ |
| 268 |
return adjust( mTimer.setCount( tick, value ) ); |
| 269 |
} |
| 270 |
|
| 271 |
void setOther( uint8_t value ) |
| 272 |
{ |
| 273 |
mShiftRegister = ( mShiftRegister & 0b0000'1111'1111 ) | ( ( (int)value & 0b1111'0000 ) << 4 ); |
| 274 |
mTimer.setControlB( value & 0b0000'1111 ); |
| 275 |
} |
| 276 |
|
| 277 |
int8_t getOutput() const |
| 278 |
{ |
| 279 |
return mOutput; |
| 280 |
} |
| 281 |
|
| 282 |
void fillRegisterPool( int64_t tick, uint8_t* regs ) |
| 283 |
{ |
| 284 |
regs[0] = mVolume; |
| 285 |
regs[1] = mTapSelector & 0xff; |
| 286 |
regs[2] = mOutput; |
| 287 |
regs[3] = mShiftRegister & 0xff; |
| 288 |
regs[4] = mTimer.getBackup(); |
| 289 |
regs[5] = mCtrlA; |
| 290 |
regs[6] = mTimer.getCount( tick ); |
| 291 |
regs[7] = ( ( mShiftRegister >> 4 ) & 0xf0 ); |
| 292 |
} |
| 293 |
|
| 294 |
private: |
| 295 |
|
| 296 |
int64_t adjust( int64_t tick ) const |
| 297 |
{ |
| 298 |
//ticks are advancing in 1 MHz resolution, so lower 4 bits are unused. |
| 299 |
//timer number is encoded on lowest 2 bits. |
| 300 |
return tick | mNumber; |
| 301 |
} |
| 302 |
|
| 303 |
void trigger() |
| 304 |
{ |
| 305 |
uint32_t xorGate = mTapSelector & mShiftRegister; |
| 306 |
uint32_t parity = popcnt( xorGate ) & 1; |
| 307 |
uint32_t newShift = ( mShiftRegister << 1 ) | ( parity ^ 1 ); |
| 308 |
mShiftRegister = newShift; |
| 309 |
|
| 310 |
if ( mEnableIntegrate ) |
| 311 |
{ |
| 312 |
int32_t temp = mOutput + ( ( newShift & 1 ) ? mVolume : -mVolume ); |
| 313 |
mOutput = (int8_t)clamp( temp, (int32_t)std::numeric_limits<int8_t>::min(), (int32_t)std::numeric_limits<int8_t>::max() ); |
| 314 |
} |
| 315 |
else |
| 316 |
{ |
| 317 |
mOutput = ( newShift & 1 ) ? mVolume : -mVolume; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
private: |
| 322 |
static constexpr uint8_t FEEDBACK_7 = 0b10000000; |
| 323 |
static constexpr uint8_t ENABLE_INTEGRATE = 0b00100000; |
| 324 |
|
| 325 |
private: |
| 326 |
Timer mTimer; |
| 327 |
uint32_t mNumber; |
| 328 |
|
| 329 |
uint32_t mShiftRegister; |
| 330 |
uint32_t mTapSelector; |
| 331 |
bool mEnableIntegrate; |
| 332 |
int8_t mVolume; |
| 333 |
int8_t mOutput; |
| 334 |
uint8_t mCtrlA; |
| 335 |
}; |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
/* |
| 341 |
"Queue" holding event timepoints. |
| 342 |
- 4 channel timer fire points |
| 343 |
- 1 sample point |
| 344 |
Time is in 16 MHz units but only with 1 MHz resolution. |
| 345 |
Four LSBs are used to encode event kind 0-3 are channels, 4 is sampling. |
| 346 |
*/ |
| 347 |
class ActionQueue |
| 348 |
{ |
| 349 |
public: |
| 350 |
|
| 351 |
|
| 352 |
ActionQueue() : mTab{ CNT_MAX | 0, CNT_MAX | 1, CNT_MAX | 2, CNT_MAX | 3, CNT_MAX | 4 } |
| 353 |
{ |
| 354 |
} |
| 355 |
|
| 356 |
void push( int64_t value ) |
| 357 |
{ |
| 358 |
size_t idx = value & 15; |
| 359 |
if ( idx < mTab.size() ) |
| 360 |
{ |
| 361 |
if ( value & ~15 ) |
| 362 |
{ |
| 363 |
//writing only non-zero values |
| 364 |
mTab[idx] = value; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
int64_t pop() |
| 370 |
{ |
| 371 |
int64_t min1 = std::min( mTab[0], mTab[1] ); |
| 372 |
int64_t min2 = std::min( mTab[2], mTab[3] ); |
| 373 |
int64_t min3 = std::min( min1, mTab[4] ); |
| 374 |
int64_t min4 = std::min( min2, min3 ); |
| 375 |
|
| 376 |
assert( ( min4 & 15 ) < (int64_t)mTab.size() ); |
| 377 |
mTab[min4 & 15] = CNT_MAX | ( min4 & 15 ); |
| 378 |
|
| 379 |
return min4; |
| 380 |
} |
| 381 |
|
| 382 |
private: |
| 383 |
std::array<int64_t, 5> mTab; |
| 384 |
}; |
| 385 |
|
| 386 |
|
| 387 |
class MikeyPimpl |
| 388 |
{ |
| 389 |
public: |
| 390 |
|
| 391 |
struct AudioSample |
| 392 |
{ |
| 393 |
int16_t left; |
| 394 |
int16_t right; |
| 395 |
}; |
| 396 |
|
| 397 |
static constexpr uint16_t VOLCNTRL = 0x0; |
| 398 |
static constexpr uint16_t FEEDBACK = 0x1; |
| 399 |
static constexpr uint16_t OUTPUT = 0x2; |
| 400 |
static constexpr uint16_t SHIFT = 0x3; |
| 401 |
static constexpr uint16_t BACKUP = 0x4; |
| 402 |
static constexpr uint16_t CONTROL = 0x5; |
| 403 |
static constexpr uint16_t COUNTER = 0x6; |
| 404 |
static constexpr uint16_t OTHER = 0x7; |
| 405 |
|
| 406 |
static constexpr uint16_t ATTENREG0 = 0x40; |
| 407 |
static constexpr uint16_t ATTENREG1 = 0x41; |
| 408 |
static constexpr uint16_t ATTENREG2 = 0x42; |
| 409 |
static constexpr uint16_t ATTENREG3 = 0x43; |
| 410 |
static constexpr uint16_t MPAN = 0x44; |
| 411 |
static constexpr uint16_t MSTEREO = 0x50; |
| 412 |
|
| 413 |
MikeyPimpl() : mAudioChannels{ AudioChannel{0}, AudioChannel{1}, AudioChannel{2}, AudioChannel{3} }, |
| 414 |
mAttenuationLeft{ 0x3c, 0x3c, 0x3c, 0x3c }, |
| 415 |
mAttenuationRight{ 0x3c, 0x3c, 0x3c, 0x3c }, |
| 416 |
mRegisterPool{}, mPan{ 0xff }, mStereo{} |
| 417 |
{ |
| 418 |
std::fill_n( mRegisterPool.data(), mRegisterPool.size(), (uint8_t)0xff ); |
| 419 |
} |
| 420 |
|
| 421 |
~MikeyPimpl() {} |
| 422 |
|
| 423 |
int64_t write( int64_t tick, uint8_t address, uint8_t value ) |
| 424 |
{ |
| 425 |
assert( address >= 0x20 ); |
| 426 |
|
| 427 |
if ( address < 0x40 ) |
| 428 |
{ |
| 429 |
size_t idx = ( address >> 3 ) & 3; |
| 430 |
switch ( address & 0x7 ) |
| 431 |
{ |
| 432 |
case VOLCNTRL: |
| 433 |
mAudioChannels[idx].setVolume( (int8_t)value ); |
| 434 |
break; |
| 435 |
case FEEDBACK: |
| 436 |
mAudioChannels[idx].setFeedback( value ); |
| 437 |
break; |
| 438 |
case OUTPUT: |
| 439 |
mAudioChannels[idx].setOutput( value ); |
| 440 |
break; |
| 441 |
case SHIFT: |
| 442 |
mAudioChannels[idx].setShift( value ); |
| 443 |
break; |
| 444 |
case BACKUP: |
| 445 |
return mAudioChannels[idx].setBackup( tick, value ); |
| 446 |
case CONTROL: |
| 447 |
return mAudioChannels[idx].setControl( tick, value ); |
| 448 |
case COUNTER: |
| 449 |
return mAudioChannels[idx].setCounter( tick, value ); |
| 450 |
case OTHER: |
| 451 |
mAudioChannels[idx].setOther( value ); |
| 452 |
break; |
| 453 |
} |
| 454 |
} |
| 455 |
else |
| 456 |
{ |
| 457 |
int idx = address & 3; |
| 458 |
switch ( address ) |
| 459 |
{ |
| 460 |
case ATTENREG0: |
| 461 |
case ATTENREG1: |
| 462 |
case ATTENREG2: |
| 463 |
case ATTENREG3: |
| 464 |
mRegisterPool[8*4+idx] = value; |
| 465 |
mAttenuationRight[idx] = ( value & 0x0f ) << 2; |
| 466 |
mAttenuationLeft[idx] = ( value & 0xf0 ) >> 2; |
| 467 |
break; |
| 468 |
case MPAN: |
| 469 |
mPan = value; |
| 470 |
break; |
| 471 |
case MSTEREO: |
| 472 |
mStereo = value; |
| 473 |
break; |
| 474 |
default: |
| 475 |
break; |
| 476 |
} |
| 477 |
} |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
int64_t fireTimer( int64_t tick ) |
| 482 |
{ |
| 483 |
size_t timer = tick & 0x0f; |
| 484 |
assert( timer < 4 ); |
| 485 |
return mAudioChannels[timer].fireAction( tick ); |
| 486 |
} |
| 487 |
|
| 488 |
AudioSample sampleAudio( int* oscb ) const |
| 489 |
{ |
| 490 |
int left{}; |
| 491 |
int right{}; |
| 492 |
|
| 493 |
for ( size_t i = 0; i < 4; ++i ) |
| 494 |
{ |
| 495 |
int oscbWrite = 0; |
| 496 |
|
| 497 |
if ( ( mStereo & ( (uint8_t)0x01 << i ) ) == 0 ) |
| 498 |
{ |
| 499 |
const int attenuation = ( mPan & ( (uint8_t)0x01 << i ) ) != 0 ? mAttenuationLeft[i] : 0x3c; |
| 500 |
left += mAudioChannels[i].getOutput() * attenuation; |
| 501 |
oscbWrite += mAudioChannels[i].getOutput() * attenuation; |
| 502 |
} |
| 503 |
|
| 504 |
if ( ( mStereo & ( (uint8_t)0x10 << i ) ) == 0 ) |
| 505 |
{ |
| 506 |
const int attenuation = ( mPan & ( (uint8_t)0x01 << i ) ) != 0 ? mAttenuationRight[i] : 0x3c; |
| 507 |
right += mAudioChannels[i].getOutput() * attenuation; |
| 508 |
oscbWrite += mAudioChannels[i].getOutput() * attenuation; |
| 509 |
} |
| 510 |
|
| 511 |
if (oscb!=NULL) { |
| 512 |
oscb[i]=oscbWrite; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
return { (int16_t)left, (int16_t)right }; |
| 517 |
} |
| 518 |
|
| 519 |
uint8_t const* getRegisterPool( int64_t tick ) |
| 520 |
{ |
| 521 |
for ( size_t i = 0; i < mAudioChannels.size(); ++i ) |
| 522 |
{ |
| 523 |
mAudioChannels[i].fillRegisterPool( tick, mRegisterPool.data() + 8 * i ); |
| 524 |
} |
| 525 |
|
| 526 |
return mRegisterPool.data(); |
| 527 |
} |
| 528 |
|
| 529 |
private: |
| 530 |
|
| 531 |
std::array<AudioChannel, 4> mAudioChannels; |
| 532 |
std::array<int, 4> mAttenuationLeft; |
| 533 |
std::array<int, 4> mAttenuationRight; |
| 534 |
std::array<uint8_t, 4 * 8 + 4> mRegisterPool; |
| 535 |
|
| 536 |
uint8_t mPan; |
| 537 |
uint8_t mStereo; |
| 538 |
}; |
| 539 |
|
| 540 |
|
| 541 |
Mikey::Mikey( uint32_t sampleRate ) : mMikey{ std::make_unique<MikeyPimpl>() }, mQueue{ std::make_unique<ActionQueue>() }, mTick{}, mNextTick{}, mSampleRate{ sampleRate }, mSamplesRemainder{}, mTicksPerSample{ 16000000 / mSampleRate, 16000000 % mSampleRate } |
| 542 |
{ |
| 543 |
selectPOPCNT(); |
| 544 |
enqueueSampling(); |
| 545 |
} |
| 546 |
|
| 547 |
Mikey::~Mikey() |
| 548 |
{ |
| 549 |
} |
| 550 |
|
| 551 |
void Mikey::write( uint8_t address, uint8_t value ) |
| 552 |
{ |
| 553 |
if ( auto action = mMikey->write( mTick, address, value ) ) |
| 554 |
{ |
| 555 |
mQueue->push( action ); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
void Mikey::enqueueSampling() |
| 560 |
{ |
| 561 |
mTick = mNextTick & ~15; |
| 562 |
mNextTick = mNextTick + mTicksPerSample.first; |
| 563 |
mSamplesRemainder += mTicksPerSample.second; |
| 564 |
if ( mSamplesRemainder > mSampleRate ) |
| 565 |
{ |
| 566 |
mSamplesRemainder %= mSampleRate; |
| 567 |
mNextTick += 1; |
| 568 |
} |
| 569 |
|
| 570 |
mQueue->push( ( mNextTick & ~15 ) | 4 ); |
| 571 |
} |
| 572 |
|
| 573 |
void Mikey::sampleAudio( int16_t* bufL, int16_t* bufR, size_t size, int* oscb ) |
| 574 |
{ |
| 575 |
size_t i = 0; |
| 576 |
while ( i < size ) |
| 577 |
{ |
| 578 |
int64_t value = mQueue->pop(); |
| 579 |
if ( ( value & 4 ) == 0 ) |
| 580 |
{ |
| 581 |
if ( auto newAction = mMikey->fireTimer( value ) ) |
| 582 |
{ |
| 583 |
mQueue->push( newAction ); |
| 584 |
} |
| 585 |
} |
| 586 |
else |
| 587 |
{ |
| 588 |
auto sample = mMikey->sampleAudio( oscb ); |
| 589 |
bufL[i] = sample.left; |
| 590 |
bufR[i] = sample.right; |
| 591 |
i += 1; |
| 592 |
enqueueSampling(); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
uint8_t const* Mikey::getRegisterPool() |
| 598 |
{ |
| 599 |
return mMikey->getRegisterPool( mTick ); |
| 600 |
} |
| 601 |
|
| 602 |
} |