tuxanci2-engine
Game engine for tuxanci2
Log | Files | Refs | README | LICENSE

ft2.c (109717B)


      1 /*
      2 Derived from ft2-clone v2.28
      3 
      4 BSD 3-Clause License
      5 
      6 Copyright (c) 2016-2026, Olav Sørensen
      7 All rights reserved.
      8 
      9 Redistribution and use in source and binary forms, with or without
     10 modification, are permitted provided that the following conditions are met:
     11 
     12 1. Redistributions of source code must retain the above copyright notice, this
     13    list of conditions and the following disclaimer.
     14 
     15 2. Redistributions in binary form must reproduce the above copyright notice,
     16    this list of conditions and the following disclaimer in the documentation
     17    and/or other materials provided with the distribution.
     18 
     19 3. Neither the name of the copyright holder nor the names of its
     20    contributors may be used to endorse or promote products derived from
     21    this software without specific prior written permission.
     22 
     23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     26 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
     27 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     29 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     31 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33 */
     34 
     35 #include <stdint.h>
     36 #include <stdbool.h>
     37 #include <stddef.h>
     38 #include <stdlib.h>
     39 #include "audio.h"
     40 #include "ft2.h"
     41 
     42 #define MIN_BPM 32
     43 #define MAX_BPM 255
     44 #define MAX_SPEED 31
     45 #define MAX_CHANNELS 32
     46 #define NOTE_C4 (4*12)
     47 #define NOTE_OFF 97
     48 #define MAX_NOTES ((10*12*16)+16)
     49 #define MAX_PATTERNS 256
     50 #define MAX_PATT_LEN 256
     51 #define MAX_INST 128
     52 #define MAX_SMP_PER_INST 16
     53 #define MAX_ORDERS 256
     54 #define MAX_SAMPLE_LEN 0x3FFFFFFF
     55 #define INSTR_HEADER_SIZE 263
     56 #define FREQUENCY_MUL_FACTOR 3181584u
     57 #define FREQUENCY_DIV_FACTOR 21276843u
     58 #define SAMPLES_PER_TICK_NUMERATOR 110250u
     59 #define QUICK_VOLRAMP_SAMPLES 220u
     60 #define MAX_VOICE_DELTA 0x0FFFFFFFu
     61 #define MIX_CHUNK 256
     62 #define OUTPUT_SHIFT 10
     63 
     64 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
     65 
     66 enum {
     67 	CS_UPDATE_VOL = 1,
     68 	CF_UPDATE_PERIOD = 2,
     69 	CS_TRIGGER_VOICE = 4,
     70 	CS_UPDATE_PAN = 8,
     71 	CS_USE_QUICK_VOLRAMP = 16,
     72 	LOOP_DISABLED = 0,
     73 	LOOP_FORWARD = 1,
     74 	LOOP_PINGPONG = 2,
     75 	SAMPLE_16BIT = 16,
     76 	SAMPLE_STEREO = 32,
     77 	SAMPLE_ADPCM = 64,
     78 	ENV_ENABLED = 1,
     79 	ENV_SUSTAIN = 2,
     80 	ENV_LOOP = 4
     81 };
     82 
     83 #define GET_LOOPTYPE(smp_flags) ((smp_flags) & (LOOP_FORWARD | LOOP_PINGPONG))
     84 #define DISABLE_LOOP(smp_flags) ((smp_flags) &= ~(LOOP_FORWARD | LOOP_PINGPONG))
     85 
     86 typedef struct note_t {
     87 	uint8_t note;
     88     uint8_t instr;
     89     uint8_t vol;
     90     uint8_t efx;
     91     uint8_t efx_data;
     92 } note_t;
     93 
     94 typedef struct sample_t {
     95 	int8_t *data_ptr;
     96 	int8_t finetune;
     97     int8_t relative_note;
     98 	uint8_t volume;
     99     uint8_t flags;
    100     uint8_t panning;
    101 	int32_t length;
    102     int32_t loop_start;
    103     int32_t loop_length;
    104 } sample_t;
    105 
    106 typedef struct instr_t {
    107 	bool mute;
    108 	uint8_t note2_sample_lut[96];
    109 	uint8_t vol_env_length;
    110 	uint8_t pan_env_length;
    111 	uint8_t vol_env_sustain;
    112 	uint8_t vol_env_loop_start;
    113 	uint8_t vol_env_loop_end;
    114 	uint8_t pan_env_sustain;
    115 	uint8_t pan_env_loop_start;
    116 	uint8_t pan_env_loop_end;
    117 	uint8_t vol_env_flags;
    118 	uint8_t pan_env_flags;
    119 	uint8_t auto_vib_type;
    120 	uint8_t auto_vib_sweep;
    121 	uint8_t auto_vib_depth;
    122 	uint8_t auto_vib_rate;
    123 	uint16_t fadeout;
    124 	int16_t vol_env_points[12][2];
    125 	int16_t pan_env_points[12][2];
    126 	int16_t num_samples;
    127 	sample_t smp[MAX_SMP_PER_INST];
    128 } instr_t;
    129 
    130 typedef struct channel_t {
    131 	bool key_off;
    132 	bool channel_off;
    133 	bool mute;
    134 	bool semitone_porta_mode;
    135 	uint8_t status;
    136 	int8_t relative_note;
    137 	int8_t finetune;
    138 	uint8_t smp_num;
    139 	uint8_t instr_num;
    140 	uint8_t efx_data;
    141 	uint8_t efx;
    142 	uint8_t sample_offset;
    143 	uint8_t tremor_param;
    144 	uint8_t tremor_pos;
    145 	uint8_t glob_vol_slide_speed;
    146 	uint8_t panning_slide_speed;
    147 	uint8_t vib_trem_ctrl;
    148 	uint8_t portamento_direction;
    149 	uint8_t vibrato_pos;
    150 	uint8_t tremolo_pos;
    151 	uint8_t vibrato_speed;
    152 	uint8_t vibrato_depth;
    153 	uint8_t tremolo_speed;
    154 	uint8_t tremolo_depth;
    155 	uint8_t pattern_loop_start_row;
    156 	uint8_t pattern_loop_counter;
    157 	uint8_t vol_slide_speed;
    158 	uint8_t f_vol_slide_up_speed;
    159 	uint8_t f_vol_slide_down_speed;
    160 	uint8_t f_pitch_slide_up_speed;
    161 	uint8_t f_pitch_slide_down_speed;
    162 	uint8_t ef_pitch_slide_up_speed;
    163 	uint8_t ef_pitch_slide_down_speed;
    164 	uint8_t pitch_slide_up_speed;
    165 	uint8_t pitch_slide_down_speed;
    166 	uint8_t note_retrig_speed;
    167 	uint8_t note_retrig_counter;
    168 	uint8_t note_retrig_vol;
    169 	uint8_t vol_column_vol;
    170 	uint8_t note_num;
    171 	uint8_t pan_env_pos;
    172 	uint8_t auto_vib_pos;
    173 	uint8_t vol_env_pos;
    174 	uint8_t real_vol;
    175 	uint8_t old_vol;
    176 	uint8_t out_vol;
    177 	uint8_t old_pan;
    178 	uint8_t out_pan;
    179 	uint8_t final_pan;
    180 	int16_t vol_env_delta;
    181 	int16_t vol_env_value;
    182 	int16_t pan_env_delta;
    183 	int16_t pan_env_value;
    184 	uint16_t out_period;
    185 	uint16_t real_period;
    186 	uint16_t final_period;
    187 	uint16_t copy_of_instr_and_note;
    188 	uint16_t portamento_target_period;
    189 	uint16_t portamento_speed;
    190 	uint16_t vol_env_tick;
    191 	uint16_t pan_env_tick;
    192 	uint16_t auto_vib_amp;
    193 	uint16_t auto_vib_sweep;
    194 	uint16_t fadeout_vol;
    195 	uint16_t fadeout_speed;
    196 	uint16_t final_vol;
    197 	int32_t smp_start_pos;
    198 	sample_t *smp_ptr;
    199 	instr_t *instr_ptr;
    200 } channel_t;
    201 
    202 typedef struct song_t {
    203 	bool p_break_flag;
    204 	bool pos_jump_flag;
    205 	uint8_t patt_del_time;
    206 	uint8_t patt_del_time2;
    207 	uint8_t p_break_pos;
    208 	uint8_t orders[MAX_ORDERS];
    209 	int16_t song_pos;
    210 	int16_t patt_num;
    211 	int16_t row;
    212 	int16_t curr_num_rows;
    213 	uint16_t song_length;
    214 	uint16_t song_loop_start;
    215 	uint16_t bpm;
    216 	uint16_t speed;
    217 	uint16_t initial_speed;
    218 	uint16_t initial_bpm;
    219 	uint16_t global_volume;
    220 	uint16_t tick;
    221 	int32_t num_channels;
    222 } song_t;
    223 
    224 typedef void (*efx_routine)(channel_t *ch, uint8_t param);
    225 typedef void (*vol_column_efx_routine)(channel_t *ch);
    226 typedef void (*vol_column_efx_routine2)(channel_t *ch, uint8_t *vol_column_data);
    227 
    228 typedef struct voice_t {
    229 	const int8_t *data;
    230 	bool active;
    231 	bool is_16bit;
    232 	bool sampling_backwards;
    233 	bool is_fade_out_voice;
    234 	uint8_t loop_type;
    235 	uint8_t panning;
    236 	uint16_t volume;
    237 	uint32_t delta;
    238 	uint32_t position;
    239 	uint32_t position_frac;
    240 	uint32_t sample_end;
    241 	uint32_t loop_start;
    242 	uint32_t loop_length;
    243 	int32_t curr_volume_l;
    244 	int32_t curr_volume_r;
    245 	int32_t target_volume_l;
    246 	int32_t target_volume_r;
    247 	int32_t volume_delta_l;
    248 	int32_t volume_delta_r;
    249 	uint32_t volume_ramp_length;
    250 } voice_t;
    251 
    252 static song_t song;
    253 static channel_t channel[MAX_CHANNELS];
    254 static instr_t *instr[MAX_INST + 1];
    255 static note_t *pattern[MAX_PATTERNS];
    256 static int16_t pattern_num_rows[MAX_PATTERNS];
    257 static const uint16_t *note2_period_lut;
    258 static bool linear_periods_flag;
    259 static bool bxx_overflow;
    260 static bool song_loaded;
    261 static bool song_playing;
    262 static const note_t nil_pattern_line[MAX_CHANNELS];
    263 
    264 static voice_t voice[MAX_CHANNELS * 2];
    265 static uint32_t panning_table[256 + 1];
    266 static bool panning_table_ready;
    267 static uint32_t samples_per_tick;
    268 static uint32_t mixer_bpm = 125;
    269 static uint32_t tick_remainder;
    270 static uint32_t tick_samples_left;
    271 static int audio_left = -1;
    272 static int audio_right = -1;
    273 static uint32_t dither_state = 0x12345000u;
    274 static int32_t mix_l[MIX_CHUNK], mix_r[MIX_CHUNK];
    275 static int8_t out_l[MIX_CHUNK], out_r[MIX_CHUNK];
    276 
    277 static void set_mixer_bpm(int32_t bpm) {
    278 	if (bpm < MIN_BPM || bpm > MAX_BPM) {
    279 		return;
    280 	}
    281 
    282 	mixer_bpm = (uint32_t)bpm;
    283 }
    284 
    285 static const uint8_t arpeggio_tab[32] = {
    286 	0,
    287 	1,
    288 	2,
    289 	0,
    290 	1,
    291 	2,
    292 	0,
    293 	1,
    294 	2,
    295 	0,
    296 	1,
    297 	2,
    298 	0,
    299 	1,
    300 	2,
    301 	0,
    302 	0x00,
    303 	0x18,
    304 	0x31,
    305 	0x4A,
    306 	0x61,
    307 	0x78,
    308 	0x8D,
    309 	0xA1,
    310 	0xB4,
    311 	0xC5,
    312 	0xD4,
    313 	0xE0,
    314 	0xEB,
    315 	0xF4,
    316 	0xFA,
    317 	0xFD
    318 };
    319 
    320 static const int8_t auto_vib_sine_tab[256] = {
    321 	0,
    322 	-2,
    323 	-3,
    324 	-5,
    325 	-6,
    326 	-8,
    327 	-9,
    328 	-11,
    329 	-12,
    330 	-14,
    331 	-16,
    332 	-17,
    333 	-19,
    334 	-20,
    335 	-22,
    336 	-23,
    337 	-24,
    338 	-26,
    339 	-27,
    340 	-29,
    341 	-30,
    342 	-32,
    343 	-33,
    344 	-34,
    345 	-36,
    346 	-37,
    347 	-38,
    348 	-39,
    349 	-41,
    350 	-42,
    351 	-43,
    352 	-44,
    353 	-45,
    354 	-46,
    355 	-47,
    356 	-48,
    357 	-49,
    358 	-50,
    359 	-51,
    360 	-52,
    361 	-53,
    362 	-54,
    363 	-55,
    364 	-56,
    365 	-56,
    366 	-57,
    367 	-58,
    368 	-59,
    369 	-59,
    370 	-60,
    371 	-60,
    372 	-61,
    373 	-61,
    374 	-62,
    375 	-62,
    376 	-62,
    377 	-63,
    378 	-63,
    379 	-63,
    380 	-64,
    381 	-64,
    382 	-64,
    383 	-64,
    384 	-64,
    385 	-64,
    386 	-64,
    387 	-64,
    388 	-64,
    389 	-64,
    390 	-64,
    391 	-63,
    392 	-63,
    393 	-63,
    394 	-62,
    395 	-62,
    396 	-62,
    397 	-61,
    398 	-61,
    399 	-60,
    400 	-60,
    401 	-59,
    402 	-59,
    403 	-58,
    404 	-57,
    405 	-56,
    406 	-56,
    407 	-55,
    408 	-54,
    409 	-53,
    410 	-52,
    411 	-51,
    412 	-50,
    413 	-49,
    414 	-48,
    415 	-47,
    416 	-46,
    417 	-45,
    418 	-44,
    419 	-43,
    420 	-42,
    421 	-41,
    422 	-39,
    423 	-38,
    424 	-37,
    425 	-36,
    426 	-34,
    427 	-33,
    428 	-32,
    429 	-30,
    430 	-29,
    431 	-27,
    432 	-26,
    433 	-24,
    434 	-23,
    435 	-22,
    436 	-20,
    437 	-19,
    438 	-17,
    439 	-16,
    440 	-14,
    441 	-12,
    442 	-11,
    443 	-9,
    444 	-8,
    445 	-6,
    446 	-5,
    447 	-3,
    448 	-2,
    449 	0,
    450 	2,
    451 	3,
    452 	5,
    453 	6,
    454 	8,
    455 	9,
    456 	11,
    457 	12,
    458 	14,
    459 	16,
    460 	17,
    461 	19,
    462 	20,
    463 	22,
    464 	23,
    465 	24,
    466 	26,
    467 	27,
    468 	29,
    469 	30,
    470 	32,
    471 	33,
    472 	34,
    473 	36,
    474 	37,
    475 	38,
    476 	39,
    477 	41,
    478 	42,
    479 	43,
    480 	44,
    481 	45,
    482 	46,
    483 	47,
    484 	48,
    485 	49,
    486 	50,
    487 	51,
    488 	52,
    489 	53,
    490 	54,
    491 	55,
    492 	56,
    493 	56,
    494 	57,
    495 	58,
    496 	59,
    497 	59,
    498 	60,
    499 	60,
    500 	61,
    501 	61,
    502 	62,
    503 	62,
    504 	62,
    505 	63,
    506 	63,
    507 	63,
    508 	64,
    509 	64,
    510 	64,
    511 	64,
    512 	64,
    513 	64,
    514 	64,
    515 	64,
    516 	64,
    517 	64,
    518 	64,
    519 	63,
    520 	63,
    521 	63,
    522 	62,
    523 	62,
    524 	62,
    525 	61,
    526 	61,
    527 	60,
    528 	60,
    529 	59,
    530 	59,
    531 	58,
    532 	57,
    533 	56,
    534 	56,
    535 	55,
    536 	54,
    537 	53,
    538 	52,
    539 	51,
    540 	50,
    541 	49,
    542 	48,
    543 	47,
    544 	46,
    545 	45,
    546 	44,
    547 	43,
    548 	42,
    549 	41,
    550 	39,
    551 	38,
    552 	37,
    553 	36,
    554 	34,
    555 	33,
    556 	32,
    557 	30,
    558 	29,
    559 	27,
    560 	26,
    561 	24,
    562 	23,
    563 	22,
    564 	20,
    565 	19,
    566 	17,
    567 	16,
    568 	14,
    569 	12,
    570 	11,
    571 	9,
    572 	8,
    573 	6,
    574 	5,
    575 	3,
    576 	2
    577 };
    578 
    579 static const uint8_t vibrato_tab[32] = {
    580 	0,
    581 	24,
    582 	49,
    583 	74,
    584 	97,
    585 	120,
    586 	141,
    587 	161,
    588 	180,
    589 	197,
    590 	212,
    591 	224,
    592 	235,
    593 	244,
    594 	250,
    595 	253,
    596 	255,
    597 	253,
    598 	250,
    599 	244,
    600 	235,
    601 	224,
    602 	212,
    603 	197,
    604 	180,
    605 	161,
    606 	141,
    607 	120,
    608 	97,
    609 	74,
    610 	49,
    611 	24
    612 };
    613 
    614 static const uint16_t linear_period_lut[1936] = {
    615 	7744,
    616 	7740,
    617 	7736,
    618 	7732,
    619 	7728,
    620 	7724,
    621 	7720,
    622 	7716,
    623 	7712,
    624 	7708,
    625 	7704,
    626 	7700,
    627 	7696,
    628 	7692,
    629 	7688,
    630 	7684,
    631 	7680,
    632 	7676,
    633 	7672,
    634 	7668,
    635 	7664,
    636 	7660,
    637 	7656,
    638 	7652,
    639 	7648,
    640 	7644,
    641 	7640,
    642 	7636,
    643 	7632,
    644 	7628,
    645 	7624,
    646 	7620,
    647 	7616,
    648 	7612,
    649 	7608,
    650 	7604,
    651 	7600,
    652 	7596,
    653 	7592,
    654 	7588,
    655 	7584,
    656 	7580,
    657 	7576,
    658 	7572,
    659 	7568,
    660 	7564,
    661 	7560,
    662 	7556,
    663 	7552,
    664 	7548,
    665 	7544,
    666 	7540,
    667 	7536,
    668 	7532,
    669 	7528,
    670 	7524,
    671 	7520,
    672 	7516,
    673 	7512,
    674 	7508,
    675 	7504,
    676 	7500,
    677 	7496,
    678 	7492,
    679 	7488,
    680 	7484,
    681 	7480,
    682 	7476,
    683 	7472,
    684 	7468,
    685 	7464,
    686 	7460,
    687 	7456,
    688 	7452,
    689 	7448,
    690 	7444,
    691 	7440,
    692 	7436,
    693 	7432,
    694 	7428,
    695 	7424,
    696 	7420,
    697 	7416,
    698 	7412,
    699 	7408,
    700 	7404,
    701 	7400,
    702 	7396,
    703 	7392,
    704 	7388,
    705 	7384,
    706 	7380,
    707 	7376,
    708 	7372,
    709 	7368,
    710 	7364,
    711 	7360,
    712 	7356,
    713 	7352,
    714 	7348,
    715 	7344,
    716 	7340,
    717 	7336,
    718 	7332,
    719 	7328,
    720 	7324,
    721 	7320,
    722 	7316,
    723 	7312,
    724 	7308,
    725 	7304,
    726 	7300,
    727 	7296,
    728 	7292,
    729 	7288,
    730 	7284,
    731 	7280,
    732 	7276,
    733 	7272,
    734 	7268,
    735 	7264,
    736 	7260,
    737 	7256,
    738 	7252,
    739 	7248,
    740 	7244,
    741 	7240,
    742 	7236,
    743 	7232,
    744 	7228,
    745 	7224,
    746 	7220,
    747 	7216,
    748 	7212,
    749 	7208,
    750 	7204,
    751 	7200,
    752 	7196,
    753 	7192,
    754 	7188,
    755 	7184,
    756 	7180,
    757 	7176,
    758 	7172,
    759 	7168,
    760 	7164,
    761 	7160,
    762 	7156,
    763 	7152,
    764 	7148,
    765 	7144,
    766 	7140,
    767 	7136,
    768 	7132,
    769 	7128,
    770 	7124,
    771 	7120,
    772 	7116,
    773 	7112,
    774 	7108,
    775 	7104,
    776 	7100,
    777 	7096,
    778 	7092,
    779 	7088,
    780 	7084,
    781 	7080,
    782 	7076,
    783 	7072,
    784 	7068,
    785 	7064,
    786 	7060,
    787 	7056,
    788 	7052,
    789 	7048,
    790 	7044,
    791 	7040,
    792 	7036,
    793 	7032,
    794 	7028,
    795 	7024,
    796 	7020,
    797 	7016,
    798 	7012,
    799 	7008,
    800 	7004,
    801 	7000,
    802 	6996,
    803 	6992,
    804 	6988,
    805 	6984,
    806 	6980,
    807 	6976,
    808 	6972,
    809 	6968,
    810 	6964,
    811 	6960,
    812 	6956,
    813 	6952,
    814 	6948,
    815 	6944,
    816 	6940,
    817 	6936,
    818 	6932,
    819 	6928,
    820 	6924,
    821 	6920,
    822 	6916,
    823 	6912,
    824 	6908,
    825 	6904,
    826 	6900,
    827 	6896,
    828 	6892,
    829 	6888,
    830 	6884,
    831 	6880,
    832 	6876,
    833 	6872,
    834 	6868,
    835 	6864,
    836 	6860,
    837 	6856,
    838 	6852,
    839 	6848,
    840 	6844,
    841 	6840,
    842 	6836,
    843 	6832,
    844 	6828,
    845 	6824,
    846 	6820,
    847 	6816,
    848 	6812,
    849 	6808,
    850 	6804,
    851 	6800,
    852 	6796,
    853 	6792,
    854 	6788,
    855 	6784,
    856 	6780,
    857 	6776,
    858 	6772,
    859 	6768,
    860 	6764,
    861 	6760,
    862 	6756,
    863 	6752,
    864 	6748,
    865 	6744,
    866 	6740,
    867 	6736,
    868 	6732,
    869 	6728,
    870 	6724,
    871 	6720,
    872 	6716,
    873 	6712,
    874 	6708,
    875 	6704,
    876 	6700,
    877 	6696,
    878 	6692,
    879 	6688,
    880 	6684,
    881 	6680,
    882 	6676,
    883 	6672,
    884 	6668,
    885 	6664,
    886 	6660,
    887 	6656,
    888 	6652,
    889 	6648,
    890 	6644,
    891 	6640,
    892 	6636,
    893 	6632,
    894 	6628,
    895 	6624,
    896 	6620,
    897 	6616,
    898 	6612,
    899 	6608,
    900 	6604,
    901 	6600,
    902 	6596,
    903 	6592,
    904 	6588,
    905 	6584,
    906 	6580,
    907 	6576,
    908 	6572,
    909 	6568,
    910 	6564,
    911 	6560,
    912 	6556,
    913 	6552,
    914 	6548,
    915 	6544,
    916 	6540,
    917 	6536,
    918 	6532,
    919 	6528,
    920 	6524,
    921 	6520,
    922 	6516,
    923 	6512,
    924 	6508,
    925 	6504,
    926 	6500,
    927 	6496,
    928 	6492,
    929 	6488,
    930 	6484,
    931 	6480,
    932 	6476,
    933 	6472,
    934 	6468,
    935 	6464,
    936 	6460,
    937 	6456,
    938 	6452,
    939 	6448,
    940 	6444,
    941 	6440,
    942 	6436,
    943 	6432,
    944 	6428,
    945 	6424,
    946 	6420,
    947 	6416,
    948 	6412,
    949 	6408,
    950 	6404,
    951 	6400,
    952 	6396,
    953 	6392,
    954 	6388,
    955 	6384,
    956 	6380,
    957 	6376,
    958 	6372,
    959 	6368,
    960 	6364,
    961 	6360,
    962 	6356,
    963 	6352,
    964 	6348,
    965 	6344,
    966 	6340,
    967 	6336,
    968 	6332,
    969 	6328,
    970 	6324,
    971 	6320,
    972 	6316,
    973 	6312,
    974 	6308,
    975 	6304,
    976 	6300,
    977 	6296,
    978 	6292,
    979 	6288,
    980 	6284,
    981 	6280,
    982 	6276,
    983 	6272,
    984 	6268,
    985 	6264,
    986 	6260,
    987 	6256,
    988 	6252,
    989 	6248,
    990 	6244,
    991 	6240,
    992 	6236,
    993 	6232,
    994 	6228,
    995 	6224,
    996 	6220,
    997 	6216,
    998 	6212,
    999 	6208,
   1000 	6204,
   1001 	6200,
   1002 	6196,
   1003 	6192,
   1004 	6188,
   1005 	6184,
   1006 	6180,
   1007 	6176,
   1008 	6172,
   1009 	6168,
   1010 	6164,
   1011 	6160,
   1012 	6156,
   1013 	6152,
   1014 	6148,
   1015 	6144,
   1016 	6140,
   1017 	6136,
   1018 	6132,
   1019 	6128,
   1020 	6124,
   1021 	6120,
   1022 	6116,
   1023 	6112,
   1024 	6108,
   1025 	6104,
   1026 	6100,
   1027 	6096,
   1028 	6092,
   1029 	6088,
   1030 	6084,
   1031 	6080,
   1032 	6076,
   1033 	6072,
   1034 	6068,
   1035 	6064,
   1036 	6060,
   1037 	6056,
   1038 	6052,
   1039 	6048,
   1040 	6044,
   1041 	6040,
   1042 	6036,
   1043 	6032,
   1044 	6028,
   1045 	6024,
   1046 	6020,
   1047 	6016,
   1048 	6012,
   1049 	6008,
   1050 	6004,
   1051 	6000,
   1052 	5996,
   1053 	5992,
   1054 	5988,
   1055 	5984,
   1056 	5980,
   1057 	5976,
   1058 	5972,
   1059 	5968,
   1060 	5964,
   1061 	5960,
   1062 	5956,
   1063 	5952,
   1064 	5948,
   1065 	5944,
   1066 	5940,
   1067 	5936,
   1068 	5932,
   1069 	5928,
   1070 	5924,
   1071 	5920,
   1072 	5916,
   1073 	5912,
   1074 	5908,
   1075 	5904,
   1076 	5900,
   1077 	5896,
   1078 	5892,
   1079 	5888,
   1080 	5884,
   1081 	5880,
   1082 	5876,
   1083 	5872,
   1084 	5868,
   1085 	5864,
   1086 	5860,
   1087 	5856,
   1088 	5852,
   1089 	5848,
   1090 	5844,
   1091 	5840,
   1092 	5836,
   1093 	5832,
   1094 	5828,
   1095 	5824,
   1096 	5820,
   1097 	5816,
   1098 	5812,
   1099 	5808,
   1100 	5804,
   1101 	5800,
   1102 	5796,
   1103 	5792,
   1104 	5788,
   1105 	5784,
   1106 	5780,
   1107 	5776,
   1108 	5772,
   1109 	5768,
   1110 	5764,
   1111 	5760,
   1112 	5756,
   1113 	5752,
   1114 	5748,
   1115 	5744,
   1116 	5740,
   1117 	5736,
   1118 	5732,
   1119 	5728,
   1120 	5724,
   1121 	5720,
   1122 	5716,
   1123 	5712,
   1124 	5708,
   1125 	5704,
   1126 	5700,
   1127 	5696,
   1128 	5692,
   1129 	5688,
   1130 	5684,
   1131 	5680,
   1132 	5676,
   1133 	5672,
   1134 	5668,
   1135 	5664,
   1136 	5660,
   1137 	5656,
   1138 	5652,
   1139 	5648,
   1140 	5644,
   1141 	5640,
   1142 	5636,
   1143 	5632,
   1144 	5628,
   1145 	5624,
   1146 	5620,
   1147 	5616,
   1148 	5612,
   1149 	5608,
   1150 	5604,
   1151 	5600,
   1152 	5596,
   1153 	5592,
   1154 	5588,
   1155 	5584,
   1156 	5580,
   1157 	5576,
   1158 	5572,
   1159 	5568,
   1160 	5564,
   1161 	5560,
   1162 	5556,
   1163 	5552,
   1164 	5548,
   1165 	5544,
   1166 	5540,
   1167 	5536,
   1168 	5532,
   1169 	5528,
   1170 	5524,
   1171 	5520,
   1172 	5516,
   1173 	5512,
   1174 	5508,
   1175 	5504,
   1176 	5500,
   1177 	5496,
   1178 	5492,
   1179 	5488,
   1180 	5484,
   1181 	5480,
   1182 	5476,
   1183 	5472,
   1184 	5468,
   1185 	5464,
   1186 	5460,
   1187 	5456,
   1188 	5452,
   1189 	5448,
   1190 	5444,
   1191 	5440,
   1192 	5436,
   1193 	5432,
   1194 	5428,
   1195 	5424,
   1196 	5420,
   1197 	5416,
   1198 	5412,
   1199 	5408,
   1200 	5404,
   1201 	5400,
   1202 	5396,
   1203 	5392,
   1204 	5388,
   1205 	5384,
   1206 	5380,
   1207 	5376,
   1208 	5372,
   1209 	5368,
   1210 	5364,
   1211 	5360,
   1212 	5356,
   1213 	5352,
   1214 	5348,
   1215 	5344,
   1216 	5340,
   1217 	5336,
   1218 	5332,
   1219 	5328,
   1220 	5324,
   1221 	5320,
   1222 	5316,
   1223 	5312,
   1224 	5308,
   1225 	5304,
   1226 	5300,
   1227 	5296,
   1228 	5292,
   1229 	5288,
   1230 	5284,
   1231 	5280,
   1232 	5276,
   1233 	5272,
   1234 	5268,
   1235 	5264,
   1236 	5260,
   1237 	5256,
   1238 	5252,
   1239 	5248,
   1240 	5244,
   1241 	5240,
   1242 	5236,
   1243 	5232,
   1244 	5228,
   1245 	5224,
   1246 	5220,
   1247 	5216,
   1248 	5212,
   1249 	5208,
   1250 	5204,
   1251 	5200,
   1252 	5196,
   1253 	5192,
   1254 	5188,
   1255 	5184,
   1256 	5180,
   1257 	5176,
   1258 	5172,
   1259 	5168,
   1260 	5164,
   1261 	5160,
   1262 	5156,
   1263 	5152,
   1264 	5148,
   1265 	5144,
   1266 	5140,
   1267 	5136,
   1268 	5132,
   1269 	5128,
   1270 	5124,
   1271 	5120,
   1272 	5116,
   1273 	5112,
   1274 	5108,
   1275 	5104,
   1276 	5100,
   1277 	5096,
   1278 	5092,
   1279 	5088,
   1280 	5084,
   1281 	5080,
   1282 	5076,
   1283 	5072,
   1284 	5068,
   1285 	5064,
   1286 	5060,
   1287 	5056,
   1288 	5052,
   1289 	5048,
   1290 	5044,
   1291 	5040,
   1292 	5036,
   1293 	5032,
   1294 	5028,
   1295 	5024,
   1296 	5020,
   1297 	5016,
   1298 	5012,
   1299 	5008,
   1300 	5004,
   1301 	5000,
   1302 	4996,
   1303 	4992,
   1304 	4988,
   1305 	4984,
   1306 	4980,
   1307 	4976,
   1308 	4972,
   1309 	4968,
   1310 	4964,
   1311 	4960,
   1312 	4956,
   1313 	4952,
   1314 	4948,
   1315 	4944,
   1316 	4940,
   1317 	4936,
   1318 	4932,
   1319 	4928,
   1320 	4924,
   1321 	4920,
   1322 	4916,
   1323 	4912,
   1324 	4908,
   1325 	4904,
   1326 	4900,
   1327 	4896,
   1328 	4892,
   1329 	4888,
   1330 	4884,
   1331 	4880,
   1332 	4876,
   1333 	4872,
   1334 	4868,
   1335 	4864,
   1336 	4860,
   1337 	4856,
   1338 	4852,
   1339 	4848,
   1340 	4844,
   1341 	4840,
   1342 	4836,
   1343 	4832,
   1344 	4828,
   1345 	4824,
   1346 	4820,
   1347 	4816,
   1348 	4812,
   1349 	4808,
   1350 	4804,
   1351 	4800,
   1352 	4796,
   1353 	4792,
   1354 	4788,
   1355 	4784,
   1356 	4780,
   1357 	4776,
   1358 	4772,
   1359 	4768,
   1360 	4764,
   1361 	4760,
   1362 	4756,
   1363 	4752,
   1364 	4748,
   1365 	4744,
   1366 	4740,
   1367 	4736,
   1368 	4732,
   1369 	4728,
   1370 	4724,
   1371 	4720,
   1372 	4716,
   1373 	4712,
   1374 	4708,
   1375 	4704,
   1376 	4700,
   1377 	4696,
   1378 	4692,
   1379 	4688,
   1380 	4684,
   1381 	4680,
   1382 	4676,
   1383 	4672,
   1384 	4668,
   1385 	4664,
   1386 	4660,
   1387 	4656,
   1388 	4652,
   1389 	4648,
   1390 	4644,
   1391 	4640,
   1392 	4636,
   1393 	4632,
   1394 	4628,
   1395 	4624,
   1396 	4620,
   1397 	4616,
   1398 	4612,
   1399 	4608,
   1400 	4604,
   1401 	4600,
   1402 	4596,
   1403 	4592,
   1404 	4588,
   1405 	4584,
   1406 	4580,
   1407 	4576,
   1408 	4572,
   1409 	4568,
   1410 	4564,
   1411 	4560,
   1412 	4556,
   1413 	4552,
   1414 	4548,
   1415 	4544,
   1416 	4540,
   1417 	4536,
   1418 	4532,
   1419 	4528,
   1420 	4524,
   1421 	4520,
   1422 	4516,
   1423 	4512,
   1424 	4508,
   1425 	4504,
   1426 	4500,
   1427 	4496,
   1428 	4492,
   1429 	4488,
   1430 	4484,
   1431 	4480,
   1432 	4476,
   1433 	4472,
   1434 	4468,
   1435 	4464,
   1436 	4460,
   1437 	4456,
   1438 	4452,
   1439 	4448,
   1440 	4444,
   1441 	4440,
   1442 	4436,
   1443 	4432,
   1444 	4428,
   1445 	4424,
   1446 	4420,
   1447 	4416,
   1448 	4412,
   1449 	4408,
   1450 	4404,
   1451 	4400,
   1452 	4396,
   1453 	4392,
   1454 	4388,
   1455 	4384,
   1456 	4380,
   1457 	4376,
   1458 	4372,
   1459 	4368,
   1460 	4364,
   1461 	4360,
   1462 	4356,
   1463 	4352,
   1464 	4348,
   1465 	4344,
   1466 	4340,
   1467 	4336,
   1468 	4332,
   1469 	4328,
   1470 	4324,
   1471 	4320,
   1472 	4316,
   1473 	4312,
   1474 	4308,
   1475 	4304,
   1476 	4300,
   1477 	4296,
   1478 	4292,
   1479 	4288,
   1480 	4284,
   1481 	4280,
   1482 	4276,
   1483 	4272,
   1484 	4268,
   1485 	4264,
   1486 	4260,
   1487 	4256,
   1488 	4252,
   1489 	4248,
   1490 	4244,
   1491 	4240,
   1492 	4236,
   1493 	4232,
   1494 	4228,
   1495 	4224,
   1496 	4220,
   1497 	4216,
   1498 	4212,
   1499 	4208,
   1500 	4204,
   1501 	4200,
   1502 	4196,
   1503 	4192,
   1504 	4188,
   1505 	4184,
   1506 	4180,
   1507 	4176,
   1508 	4172,
   1509 	4168,
   1510 	4164,
   1511 	4160,
   1512 	4156,
   1513 	4152,
   1514 	4148,
   1515 	4144,
   1516 	4140,
   1517 	4136,
   1518 	4132,
   1519 	4128,
   1520 	4124,
   1521 	4120,
   1522 	4116,
   1523 	4112,
   1524 	4108,
   1525 	4104,
   1526 	4100,
   1527 	4096,
   1528 	4092,
   1529 	4088,
   1530 	4084,
   1531 	4080,
   1532 	4076,
   1533 	4072,
   1534 	4068,
   1535 	4064,
   1536 	4060,
   1537 	4056,
   1538 	4052,
   1539 	4048,
   1540 	4044,
   1541 	4040,
   1542 	4036,
   1543 	4032,
   1544 	4028,
   1545 	4024,
   1546 	4020,
   1547 	4016,
   1548 	4012,
   1549 	4008,
   1550 	4004,
   1551 	4000,
   1552 	3996,
   1553 	3992,
   1554 	3988,
   1555 	3984,
   1556 	3980,
   1557 	3976,
   1558 	3972,
   1559 	3968,
   1560 	3964,
   1561 	3960,
   1562 	3956,
   1563 	3952,
   1564 	3948,
   1565 	3944,
   1566 	3940,
   1567 	3936,
   1568 	3932,
   1569 	3928,
   1570 	3924,
   1571 	3920,
   1572 	3916,
   1573 	3912,
   1574 	3908,
   1575 	3904,
   1576 	3900,
   1577 	3896,
   1578 	3892,
   1579 	3888,
   1580 	3884,
   1581 	3880,
   1582 	3876,
   1583 	3872,
   1584 	3868,
   1585 	3864,
   1586 	3860,
   1587 	3856,
   1588 	3852,
   1589 	3848,
   1590 	3844,
   1591 	3840,
   1592 	3836,
   1593 	3832,
   1594 	3828,
   1595 	3824,
   1596 	3820,
   1597 	3816,
   1598 	3812,
   1599 	3808,
   1600 	3804,
   1601 	3800,
   1602 	3796,
   1603 	3792,
   1604 	3788,
   1605 	3784,
   1606 	3780,
   1607 	3776,
   1608 	3772,
   1609 	3768,
   1610 	3764,
   1611 	3760,
   1612 	3756,
   1613 	3752,
   1614 	3748,
   1615 	3744,
   1616 	3740,
   1617 	3736,
   1618 	3732,
   1619 	3728,
   1620 	3724,
   1621 	3720,
   1622 	3716,
   1623 	3712,
   1624 	3708,
   1625 	3704,
   1626 	3700,
   1627 	3696,
   1628 	3692,
   1629 	3688,
   1630 	3684,
   1631 	3680,
   1632 	3676,
   1633 	3672,
   1634 	3668,
   1635 	3664,
   1636 	3660,
   1637 	3656,
   1638 	3652,
   1639 	3648,
   1640 	3644,
   1641 	3640,
   1642 	3636,
   1643 	3632,
   1644 	3628,
   1645 	3624,
   1646 	3620,
   1647 	3616,
   1648 	3612,
   1649 	3608,
   1650 	3604,
   1651 	3600,
   1652 	3596,
   1653 	3592,
   1654 	3588,
   1655 	3584,
   1656 	3580,
   1657 	3576,
   1658 	3572,
   1659 	3568,
   1660 	3564,
   1661 	3560,
   1662 	3556,
   1663 	3552,
   1664 	3548,
   1665 	3544,
   1666 	3540,
   1667 	3536,
   1668 	3532,
   1669 	3528,
   1670 	3524,
   1671 	3520,
   1672 	3516,
   1673 	3512,
   1674 	3508,
   1675 	3504,
   1676 	3500,
   1677 	3496,
   1678 	3492,
   1679 	3488,
   1680 	3484,
   1681 	3480,
   1682 	3476,
   1683 	3472,
   1684 	3468,
   1685 	3464,
   1686 	3460,
   1687 	3456,
   1688 	3452,
   1689 	3448,
   1690 	3444,
   1691 	3440,
   1692 	3436,
   1693 	3432,
   1694 	3428,
   1695 	3424,
   1696 	3420,
   1697 	3416,
   1698 	3412,
   1699 	3408,
   1700 	3404,
   1701 	3400,
   1702 	3396,
   1703 	3392,
   1704 	3388,
   1705 	3384,
   1706 	3380,
   1707 	3376,
   1708 	3372,
   1709 	3368,
   1710 	3364,
   1711 	3360,
   1712 	3356,
   1713 	3352,
   1714 	3348,
   1715 	3344,
   1716 	3340,
   1717 	3336,
   1718 	3332,
   1719 	3328,
   1720 	3324,
   1721 	3320,
   1722 	3316,
   1723 	3312,
   1724 	3308,
   1725 	3304,
   1726 	3300,
   1727 	3296,
   1728 	3292,
   1729 	3288,
   1730 	3284,
   1731 	3280,
   1732 	3276,
   1733 	3272,
   1734 	3268,
   1735 	3264,
   1736 	3260,
   1737 	3256,
   1738 	3252,
   1739 	3248,
   1740 	3244,
   1741 	3240,
   1742 	3236,
   1743 	3232,
   1744 	3228,
   1745 	3224,
   1746 	3220,
   1747 	3216,
   1748 	3212,
   1749 	3208,
   1750 	3204,
   1751 	3200,
   1752 	3196,
   1753 	3192,
   1754 	3188,
   1755 	3184,
   1756 	3180,
   1757 	3176,
   1758 	3172,
   1759 	3168,
   1760 	3164,
   1761 	3160,
   1762 	3156,
   1763 	3152,
   1764 	3148,
   1765 	3144,
   1766 	3140,
   1767 	3136,
   1768 	3132,
   1769 	3128,
   1770 	3124,
   1771 	3120,
   1772 	3116,
   1773 	3112,
   1774 	3108,
   1775 	3104,
   1776 	3100,
   1777 	3096,
   1778 	3092,
   1779 	3088,
   1780 	3084,
   1781 	3080,
   1782 	3076,
   1783 	3072,
   1784 	3068,
   1785 	3064,
   1786 	3060,
   1787 	3056,
   1788 	3052,
   1789 	3048,
   1790 	3044,
   1791 	3040,
   1792 	3036,
   1793 	3032,
   1794 	3028,
   1795 	3024,
   1796 	3020,
   1797 	3016,
   1798 	3012,
   1799 	3008,
   1800 	3004,
   1801 	3000,
   1802 	2996,
   1803 	2992,
   1804 	2988,
   1805 	2984,
   1806 	2980,
   1807 	2976,
   1808 	2972,
   1809 	2968,
   1810 	2964,
   1811 	2960,
   1812 	2956,
   1813 	2952,
   1814 	2948,
   1815 	2944,
   1816 	2940,
   1817 	2936,
   1818 	2932,
   1819 	2928,
   1820 	2924,
   1821 	2920,
   1822 	2916,
   1823 	2912,
   1824 	2908,
   1825 	2904,
   1826 	2900,
   1827 	2896,
   1828 	2892,
   1829 	2888,
   1830 	2884,
   1831 	2880,
   1832 	2876,
   1833 	2872,
   1834 	2868,
   1835 	2864,
   1836 	2860,
   1837 	2856,
   1838 	2852,
   1839 	2848,
   1840 	2844,
   1841 	2840,
   1842 	2836,
   1843 	2832,
   1844 	2828,
   1845 	2824,
   1846 	2820,
   1847 	2816,
   1848 	2812,
   1849 	2808,
   1850 	2804,
   1851 	2800,
   1852 	2796,
   1853 	2792,
   1854 	2788,
   1855 	2784,
   1856 	2780,
   1857 	2776,
   1858 	2772,
   1859 	2768,
   1860 	2764,
   1861 	2760,
   1862 	2756,
   1863 	2752,
   1864 	2748,
   1865 	2744,
   1866 	2740,
   1867 	2736,
   1868 	2732,
   1869 	2728,
   1870 	2724,
   1871 	2720,
   1872 	2716,
   1873 	2712,
   1874 	2708,
   1875 	2704,
   1876 	2700,
   1877 	2696,
   1878 	2692,
   1879 	2688,
   1880 	2684,
   1881 	2680,
   1882 	2676,
   1883 	2672,
   1884 	2668,
   1885 	2664,
   1886 	2660,
   1887 	2656,
   1888 	2652,
   1889 	2648,
   1890 	2644,
   1891 	2640,
   1892 	2636,
   1893 	2632,
   1894 	2628,
   1895 	2624,
   1896 	2620,
   1897 	2616,
   1898 	2612,
   1899 	2608,
   1900 	2604,
   1901 	2600,
   1902 	2596,
   1903 	2592,
   1904 	2588,
   1905 	2584,
   1906 	2580,
   1907 	2576,
   1908 	2572,
   1909 	2568,
   1910 	2564,
   1911 	2560,
   1912 	2556,
   1913 	2552,
   1914 	2548,
   1915 	2544,
   1916 	2540,
   1917 	2536,
   1918 	2532,
   1919 	2528,
   1920 	2524,
   1921 	2520,
   1922 	2516,
   1923 	2512,
   1924 	2508,
   1925 	2504,
   1926 	2500,
   1927 	2496,
   1928 	2492,
   1929 	2488,
   1930 	2484,
   1931 	2480,
   1932 	2476,
   1933 	2472,
   1934 	2468,
   1935 	2464,
   1936 	2460,
   1937 	2456,
   1938 	2452,
   1939 	2448,
   1940 	2444,
   1941 	2440,
   1942 	2436,
   1943 	2432,
   1944 	2428,
   1945 	2424,
   1946 	2420,
   1947 	2416,
   1948 	2412,
   1949 	2408,
   1950 	2404,
   1951 	2400,
   1952 	2396,
   1953 	2392,
   1954 	2388,
   1955 	2384,
   1956 	2380,
   1957 	2376,
   1958 	2372,
   1959 	2368,
   1960 	2364,
   1961 	2360,
   1962 	2356,
   1963 	2352,
   1964 	2348,
   1965 	2344,
   1966 	2340,
   1967 	2336,
   1968 	2332,
   1969 	2328,
   1970 	2324,
   1971 	2320,
   1972 	2316,
   1973 	2312,
   1974 	2308,
   1975 	2304,
   1976 	2300,
   1977 	2296,
   1978 	2292,
   1979 	2288,
   1980 	2284,
   1981 	2280,
   1982 	2276,
   1983 	2272,
   1984 	2268,
   1985 	2264,
   1986 	2260,
   1987 	2256,
   1988 	2252,
   1989 	2248,
   1990 	2244,
   1991 	2240,
   1992 	2236,
   1993 	2232,
   1994 	2228,
   1995 	2224,
   1996 	2220,
   1997 	2216,
   1998 	2212,
   1999 	2208,
   2000 	2204,
   2001 	2200,
   2002 	2196,
   2003 	2192,
   2004 	2188,
   2005 	2184,
   2006 	2180,
   2007 	2176,
   2008 	2172,
   2009 	2168,
   2010 	2164,
   2011 	2160,
   2012 	2156,
   2013 	2152,
   2014 	2148,
   2015 	2144,
   2016 	2140,
   2017 	2136,
   2018 	2132,
   2019 	2128,
   2020 	2124,
   2021 	2120,
   2022 	2116,
   2023 	2112,
   2024 	2108,
   2025 	2104,
   2026 	2100,
   2027 	2096,
   2028 	2092,
   2029 	2088,
   2030 	2084,
   2031 	2080,
   2032 	2076,
   2033 	2072,
   2034 	2068,
   2035 	2064,
   2036 	2060,
   2037 	2056,
   2038 	2052,
   2039 	2048,
   2040 	2044,
   2041 	2040,
   2042 	2036,
   2043 	2032,
   2044 	2028,
   2045 	2024,
   2046 	2020,
   2047 	2016,
   2048 	2012,
   2049 	2008,
   2050 	2004,
   2051 	2000,
   2052 	1996,
   2053 	1992,
   2054 	1988,
   2055 	1984,
   2056 	1980,
   2057 	1976,
   2058 	1972,
   2059 	1968,
   2060 	1964,
   2061 	1960,
   2062 	1956,
   2063 	1952,
   2064 	1948,
   2065 	1944,
   2066 	1940,
   2067 	1936,
   2068 	1932,
   2069 	1928,
   2070 	1924,
   2071 	1920,
   2072 	1916,
   2073 	1912,
   2074 	1908,
   2075 	1904,
   2076 	1900,
   2077 	1896,
   2078 	1892,
   2079 	1888,
   2080 	1884,
   2081 	1880,
   2082 	1876,
   2083 	1872,
   2084 	1868,
   2085 	1864,
   2086 	1860,
   2087 	1856,
   2088 	1852,
   2089 	1848,
   2090 	1844,
   2091 	1840,
   2092 	1836,
   2093 	1832,
   2094 	1828,
   2095 	1824,
   2096 	1820,
   2097 	1816,
   2098 	1812,
   2099 	1808,
   2100 	1804,
   2101 	1800,
   2102 	1796,
   2103 	1792,
   2104 	1788,
   2105 	1784,
   2106 	1780,
   2107 	1776,
   2108 	1772,
   2109 	1768,
   2110 	1764,
   2111 	1760,
   2112 	1756,
   2113 	1752,
   2114 	1748,
   2115 	1744,
   2116 	1740,
   2117 	1736,
   2118 	1732,
   2119 	1728,
   2120 	1724,
   2121 	1720,
   2122 	1716,
   2123 	1712,
   2124 	1708,
   2125 	1704,
   2126 	1700,
   2127 	1696,
   2128 	1692,
   2129 	1688,
   2130 	1684,
   2131 	1680,
   2132 	1676,
   2133 	1672,
   2134 	1668,
   2135 	1664,
   2136 	1660,
   2137 	1656,
   2138 	1652,
   2139 	1648,
   2140 	1644,
   2141 	1640,
   2142 	1636,
   2143 	1632,
   2144 	1628,
   2145 	1624,
   2146 	1620,
   2147 	1616,
   2148 	1612,
   2149 	1608,
   2150 	1604,
   2151 	1600,
   2152 	1596,
   2153 	1592,
   2154 	1588,
   2155 	1584,
   2156 	1580,
   2157 	1576,
   2158 	1572,
   2159 	1568,
   2160 	1564,
   2161 	1560,
   2162 	1556,
   2163 	1552,
   2164 	1548,
   2165 	1544,
   2166 	1540,
   2167 	1536,
   2168 	1532,
   2169 	1528,
   2170 	1524,
   2171 	1520,
   2172 	1516,
   2173 	1512,
   2174 	1508,
   2175 	1504,
   2176 	1500,
   2177 	1496,
   2178 	1492,
   2179 	1488,
   2180 	1484,
   2181 	1480,
   2182 	1476,
   2183 	1472,
   2184 	1468,
   2185 	1464,
   2186 	1460,
   2187 	1456,
   2188 	1452,
   2189 	1448,
   2190 	1444,
   2191 	1440,
   2192 	1436,
   2193 	1432,
   2194 	1428,
   2195 	1424,
   2196 	1420,
   2197 	1416,
   2198 	1412,
   2199 	1408,
   2200 	1404,
   2201 	1400,
   2202 	1396,
   2203 	1392,
   2204 	1388,
   2205 	1384,
   2206 	1380,
   2207 	1376,
   2208 	1372,
   2209 	1368,
   2210 	1364,
   2211 	1360,
   2212 	1356,
   2213 	1352,
   2214 	1348,
   2215 	1344,
   2216 	1340,
   2217 	1336,
   2218 	1332,
   2219 	1328,
   2220 	1324,
   2221 	1320,
   2222 	1316,
   2223 	1312,
   2224 	1308,
   2225 	1304,
   2226 	1300,
   2227 	1296,
   2228 	1292,
   2229 	1288,
   2230 	1284,
   2231 	1280,
   2232 	1276,
   2233 	1272,
   2234 	1268,
   2235 	1264,
   2236 	1260,
   2237 	1256,
   2238 	1252,
   2239 	1248,
   2240 	1244,
   2241 	1240,
   2242 	1236,
   2243 	1232,
   2244 	1228,
   2245 	1224,
   2246 	1220,
   2247 	1216,
   2248 	1212,
   2249 	1208,
   2250 	1204,
   2251 	1200,
   2252 	1196,
   2253 	1192,
   2254 	1188,
   2255 	1184,
   2256 	1180,
   2257 	1176,
   2258 	1172,
   2259 	1168,
   2260 	1164,
   2261 	1160,
   2262 	1156,
   2263 	1152,
   2264 	1148,
   2265 	1144,
   2266 	1140,
   2267 	1136,
   2268 	1132,
   2269 	1128,
   2270 	1124,
   2271 	1120,
   2272 	1116,
   2273 	1112,
   2274 	1108,
   2275 	1104,
   2276 	1100,
   2277 	1096,
   2278 	1092,
   2279 	1088,
   2280 	1084,
   2281 	1080,
   2282 	1076,
   2283 	1072,
   2284 	1068,
   2285 	1064,
   2286 	1060,
   2287 	1056,
   2288 	1052,
   2289 	1048,
   2290 	1044,
   2291 	1040,
   2292 	1036,
   2293 	1032,
   2294 	1028,
   2295 	1024,
   2296 	1020,
   2297 	1016,
   2298 	1012,
   2299 	1008,
   2300 	1004,
   2301 	1000,
   2302 	996,
   2303 	992,
   2304 	988,
   2305 	984,
   2306 	980,
   2307 	976,
   2308 	972,
   2309 	968,
   2310 	964,
   2311 	960,
   2312 	956,
   2313 	952,
   2314 	948,
   2315 	944,
   2316 	940,
   2317 	936,
   2318 	932,
   2319 	928,
   2320 	924,
   2321 	920,
   2322 	916,
   2323 	912,
   2324 	908,
   2325 	904,
   2326 	900,
   2327 	896,
   2328 	892,
   2329 	888,
   2330 	884,
   2331 	880,
   2332 	876,
   2333 	872,
   2334 	868,
   2335 	864,
   2336 	860,
   2337 	856,
   2338 	852,
   2339 	848,
   2340 	844,
   2341 	840,
   2342 	836,
   2343 	832,
   2344 	828,
   2345 	824,
   2346 	820,
   2347 	816,
   2348 	812,
   2349 	808,
   2350 	804,
   2351 	800,
   2352 	796,
   2353 	792,
   2354 	788,
   2355 	784,
   2356 	780,
   2357 	776,
   2358 	772,
   2359 	768,
   2360 	764,
   2361 	760,
   2362 	756,
   2363 	752,
   2364 	748,
   2365 	744,
   2366 	740,
   2367 	736,
   2368 	732,
   2369 	728,
   2370 	724,
   2371 	720,
   2372 	716,
   2373 	712,
   2374 	708,
   2375 	704,
   2376 	700,
   2377 	696,
   2378 	692,
   2379 	688,
   2380 	684,
   2381 	680,
   2382 	676,
   2383 	672,
   2384 	668,
   2385 	664,
   2386 	660,
   2387 	656,
   2388 	652,
   2389 	648,
   2390 	644,
   2391 	640,
   2392 	636,
   2393 	632,
   2394 	628,
   2395 	624,
   2396 	620,
   2397 	616,
   2398 	612,
   2399 	608,
   2400 	604,
   2401 	600,
   2402 	596,
   2403 	592,
   2404 	588,
   2405 	584,
   2406 	580,
   2407 	576,
   2408 	572,
   2409 	568,
   2410 	564,
   2411 	560,
   2412 	556,
   2413 	552,
   2414 	548,
   2415 	544,
   2416 	540,
   2417 	536,
   2418 	532,
   2419 	528,
   2420 	524,
   2421 	520,
   2422 	516,
   2423 	512,
   2424 	508,
   2425 	504,
   2426 	500,
   2427 	496,
   2428 	492,
   2429 	488,
   2430 	484,
   2431 	480,
   2432 	476,
   2433 	472,
   2434 	468,
   2435 	464,
   2436 	460,
   2437 	456,
   2438 	452,
   2439 	448,
   2440 	444,
   2441 	440,
   2442 	436,
   2443 	432,
   2444 	428,
   2445 	424,
   2446 	420,
   2447 	416,
   2448 	412,
   2449 	408,
   2450 	404,
   2451 	400,
   2452 	396,
   2453 	392,
   2454 	388,
   2455 	384,
   2456 	380,
   2457 	376,
   2458 	372,
   2459 	368,
   2460 	364,
   2461 	360,
   2462 	356,
   2463 	352,
   2464 	348,
   2465 	344,
   2466 	340,
   2467 	336,
   2468 	332,
   2469 	328,
   2470 	324,
   2471 	320,
   2472 	316,
   2473 	312,
   2474 	308,
   2475 	304,
   2476 	300,
   2477 	296,
   2478 	292,
   2479 	288,
   2480 	284,
   2481 	280,
   2482 	276,
   2483 	272,
   2484 	268,
   2485 	264,
   2486 	260,
   2487 	256,
   2488 	252,
   2489 	248,
   2490 	244,
   2491 	240,
   2492 	236,
   2493 	232,
   2494 	228,
   2495 	224,
   2496 	220,
   2497 	216,
   2498 	212,
   2499 	208,
   2500 	204,
   2501 	200,
   2502 	196,
   2503 	192,
   2504 	188,
   2505 	184,
   2506 	180,
   2507 	176,
   2508 	172,
   2509 	168,
   2510 	164,
   2511 	160,
   2512 	156,
   2513 	152,
   2514 	148,
   2515 	144,
   2516 	140,
   2517 	136,
   2518 	132,
   2519 	128,
   2520 	124,
   2521 	120,
   2522 	116,
   2523 	112,
   2524 	108,
   2525 	104,
   2526 	100,
   2527 	96,
   2528 	92,
   2529 	88,
   2530 	84,
   2531 	80,
   2532 	76,
   2533 	72,
   2534 	68,
   2535 	64,
   2536 	60,
   2537 	56,
   2538 	52,
   2539 	48,
   2540 	44,
   2541 	40,
   2542 	36,
   2543 	32,
   2544 	28,
   2545 	24,
   2546 	20,
   2547 	16,
   2548 	12,
   2549 	8,
   2550 	4
   2551 };
   2552 
   2553 static const uint16_t amiga_period_lut[1936] = {
   2554 	29024,
   2555 	28912,
   2556 	28800,
   2557 	28704,
   2558 	28608,
   2559 	28496,
   2560 	28384,
   2561 	28288,
   2562 	28192,
   2563 	28096,
   2564 	28000,
   2565 	27888,
   2566 	27776,
   2567 	27680,
   2568 	27584,
   2569 	27488,
   2570 	27392,
   2571 	27296,
   2572 	27200,
   2573 	27104,
   2574 	27008,
   2575 	26912,
   2576 	26816,
   2577 	26720,
   2578 	26624,
   2579 	26528,
   2580 	26432,
   2581 	26336,
   2582 	26240,
   2583 	26144,
   2584 	26048,
   2585 	25952,
   2586 	25856,
   2587 	25760,
   2588 	25664,
   2589 	25568,
   2590 	25472,
   2591 	25392,
   2592 	25312,
   2593 	25216,
   2594 	25120,
   2595 	25024,
   2596 	24928,
   2597 	24848,
   2598 	24768,
   2599 	24672,
   2600 	24576,
   2601 	24480,
   2602 	24384,
   2603 	24304,
   2604 	24224,
   2605 	24144,
   2606 	24064,
   2607 	23968,
   2608 	23872,
   2609 	23792,
   2610 	23712,
   2611 	23632,
   2612 	23552,
   2613 	23456,
   2614 	23360,
   2615 	23280,
   2616 	23200,
   2617 	23120,
   2618 	23040,
   2619 	22960,
   2620 	22880,
   2621 	22784,
   2622 	22688,
   2623 	22608,
   2624 	22528,
   2625 	22448,
   2626 	22368,
   2627 	22288,
   2628 	22208,
   2629 	22128,
   2630 	22048,
   2631 	21968,
   2632 	21888,
   2633 	21792,
   2634 	21696,
   2635 	21648,
   2636 	21600,
   2637 	21520,
   2638 	21440,
   2639 	21360,
   2640 	21280,
   2641 	21200,
   2642 	21120,
   2643 	21040,
   2644 	20960,
   2645 	20896,
   2646 	20832,
   2647 	20752,
   2648 	20672,
   2649 	20576,
   2650 	20480,
   2651 	20416,
   2652 	20352,
   2653 	20288,
   2654 	20224,
   2655 	20160,
   2656 	20096,
   2657 	20016,
   2658 	19936,
   2659 	19872,
   2660 	19808,
   2661 	19728,
   2662 	19648,
   2663 	19584,
   2664 	19520,
   2665 	19424,
   2666 	19328,
   2667 	19280,
   2668 	19232,
   2669 	19168,
   2670 	19104,
   2671 	19024,
   2672 	18944,
   2673 	18880,
   2674 	18816,
   2675 	18752,
   2676 	18688,
   2677 	18624,
   2678 	18560,
   2679 	18480,
   2680 	18400,
   2681 	18320,
   2682 	18240,
   2683 	18192,
   2684 	18144,
   2685 	18080,
   2686 	18016,
   2687 	17952,
   2688 	17888,
   2689 	17824,
   2690 	17760,
   2691 	17696,
   2692 	17632,
   2693 	17568,
   2694 	17504,
   2695 	17440,
   2696 	17376,
   2697 	17296,
   2698 	17216,
   2699 	17168,
   2700 	17120,
   2701 	17072,
   2702 	17024,
   2703 	16960,
   2704 	16896,
   2705 	16832,
   2706 	16768,
   2707 	16704,
   2708 	16640,
   2709 	16576,
   2710 	16512,
   2711 	16464,
   2712 	16416,
   2713 	16336,
   2714 	16256,
   2715 	16208,
   2716 	16160,
   2717 	16112,
   2718 	16064,
   2719 	16000,
   2720 	15936,
   2721 	15872,
   2722 	15808,
   2723 	15760,
   2724 	15712,
   2725 	15648,
   2726 	15584,
   2727 	15536,
   2728 	15488,
   2729 	15424,
   2730 	15360,
   2731 	15312,
   2732 	15264,
   2733 	15216,
   2734 	15168,
   2735 	15104,
   2736 	15040,
   2737 	14992,
   2738 	14944,
   2739 	14880,
   2740 	14816,
   2741 	14768,
   2742 	14720,
   2743 	14672,
   2744 	14624,
   2745 	14568,
   2746 	14512,
   2747 	14456,
   2748 	14400,
   2749 	14352,
   2750 	14304,
   2751 	14248,
   2752 	14192,
   2753 	14144,
   2754 	14096,
   2755 	14048,
   2756 	14000,
   2757 	13944,
   2758 	13888,
   2759 	13840,
   2760 	13792,
   2761 	13744,
   2762 	13696,
   2763 	13648,
   2764 	13600,
   2765 	13552,
   2766 	13504,
   2767 	13456,
   2768 	13408,
   2769 	13360,
   2770 	13312,
   2771 	13264,
   2772 	13216,
   2773 	13168,
   2774 	13120,
   2775 	13072,
   2776 	13024,
   2777 	12976,
   2778 	12928,
   2779 	12880,
   2780 	12832,
   2781 	12784,
   2782 	12736,
   2783 	12696,
   2784 	12656,
   2785 	12608,
   2786 	12560,
   2787 	12512,
   2788 	12464,
   2789 	12424,
   2790 	12384,
   2791 	12336,
   2792 	12288,
   2793 	12240,
   2794 	12192,
   2795 	12152,
   2796 	12112,
   2797 	12072,
   2798 	12032,
   2799 	11984,
   2800 	11936,
   2801 	11896,
   2802 	11856,
   2803 	11816,
   2804 	11776,
   2805 	11728,
   2806 	11680,
   2807 	11640,
   2808 	11600,
   2809 	11560,
   2810 	11520,
   2811 	11480,
   2812 	11440,
   2813 	11392,
   2814 	11344,
   2815 	11304,
   2816 	11264,
   2817 	11224,
   2818 	11184,
   2819 	11144,
   2820 	11104,
   2821 	11064,
   2822 	11024,
   2823 	10984,
   2824 	10944,
   2825 	10896,
   2826 	10848,
   2827 	10824,
   2828 	10800,
   2829 	10760,
   2830 	10720,
   2831 	10680,
   2832 	10640,
   2833 	10600,
   2834 	10560,
   2835 	10520,
   2836 	10480,
   2837 	10448,
   2838 	10416,
   2839 	10376,
   2840 	10336,
   2841 	10288,
   2842 	10240,
   2843 	10208,
   2844 	10176,
   2845 	10144,
   2846 	10112,
   2847 	10080,
   2848 	10048,
   2849 	10008,
   2850 	9968,
   2851 	9936,
   2852 	9904,
   2853 	9864,
   2854 	9824,
   2855 	9792,
   2856 	9760,
   2857 	9712,
   2858 	9664,
   2859 	9640,
   2860 	9616,
   2861 	9584,
   2862 	9552,
   2863 	9512,
   2864 	9472,
   2865 	9440,
   2866 	9408,
   2867 	9376,
   2868 	9344,
   2869 	9312,
   2870 	9280,
   2871 	9240,
   2872 	9200,
   2873 	9160,
   2874 	9120,
   2875 	9096,
   2876 	9072,
   2877 	9040,
   2878 	9008,
   2879 	8976,
   2880 	8944,
   2881 	8912,
   2882 	8880,
   2883 	8848,
   2884 	8816,
   2885 	8784,
   2886 	8752,
   2887 	8720,
   2888 	8688,
   2889 	8648,
   2890 	8608,
   2891 	8584,
   2892 	8560,
   2893 	8536,
   2894 	8512,
   2895 	8480,
   2896 	8448,
   2897 	8416,
   2898 	8384,
   2899 	8352,
   2900 	8320,
   2901 	8288,
   2902 	8256,
   2903 	8232,
   2904 	8208,
   2905 	8168,
   2906 	8128,
   2907 	8104,
   2908 	8080,
   2909 	8056,
   2910 	8032,
   2911 	8000,
   2912 	7968,
   2913 	7936,
   2914 	7904,
   2915 	7880,
   2916 	7856,
   2917 	7824,
   2918 	7792,
   2919 	7768,
   2920 	7744,
   2921 	7712,
   2922 	7680,
   2923 	7656,
   2924 	7632,
   2925 	7608,
   2926 	7584,
   2927 	7552,
   2928 	7520,
   2929 	7496,
   2930 	7472,
   2931 	7440,
   2932 	7408,
   2933 	7384,
   2934 	7360,
   2935 	7336,
   2936 	7312,
   2937 	7284,
   2938 	7256,
   2939 	7228,
   2940 	7200,
   2941 	7176,
   2942 	7152,
   2943 	7124,
   2944 	7096,
   2945 	7072,
   2946 	7048,
   2947 	7024,
   2948 	7000,
   2949 	6972,
   2950 	6944,
   2951 	6920,
   2952 	6896,
   2953 	6872,
   2954 	6848,
   2955 	6824,
   2956 	6800,
   2957 	6776,
   2958 	6752,
   2959 	6728,
   2960 	6704,
   2961 	6680,
   2962 	6656,
   2963 	6632,
   2964 	6608,
   2965 	6584,
   2966 	6560,
   2967 	6536,
   2968 	6512,
   2969 	6488,
   2970 	6464,
   2971 	6440,
   2972 	6416,
   2973 	6392,
   2974 	6368,
   2975 	6348,
   2976 	6328,
   2977 	6304,
   2978 	6280,
   2979 	6256,
   2980 	6232,
   2981 	6212,
   2982 	6192,
   2983 	6168,
   2984 	6144,
   2985 	6120,
   2986 	6096,
   2987 	6076,
   2988 	6056,
   2989 	6036,
   2990 	6016,
   2991 	5992,
   2992 	5968,
   2993 	5948,
   2994 	5928,
   2995 	5908,
   2996 	5888,
   2997 	5864,
   2998 	5840,
   2999 	5820,
   3000 	5800,
   3001 	5780,
   3002 	5760,
   3003 	5740,
   3004 	5720,
   3005 	5696,
   3006 	5672,
   3007 	5652,
   3008 	5632,
   3009 	5612,
   3010 	5592,
   3011 	5572,
   3012 	5552,
   3013 	5532,
   3014 	5512,
   3015 	5492,
   3016 	5472,
   3017 	5448,
   3018 	5424,
   3019 	5412,
   3020 	5400,
   3021 	5380,
   3022 	5360,
   3023 	5340,
   3024 	5320,
   3025 	5300,
   3026 	5280,
   3027 	5260,
   3028 	5240,
   3029 	5224,
   3030 	5208,
   3031 	5188,
   3032 	5168,
   3033 	5144,
   3034 	5120,
   3035 	5104,
   3036 	5088,
   3037 	5072,
   3038 	5056,
   3039 	5040,
   3040 	5024,
   3041 	5004,
   3042 	4984,
   3043 	4968,
   3044 	4952,
   3045 	4932,
   3046 	4912,
   3047 	4896,
   3048 	4880,
   3049 	4856,
   3050 	4832,
   3051 	4820,
   3052 	4808,
   3053 	4792,
   3054 	4776,
   3055 	4756,
   3056 	4736,
   3057 	4720,
   3058 	4704,
   3059 	4688,
   3060 	4672,
   3061 	4656,
   3062 	4640,
   3063 	4620,
   3064 	4600,
   3065 	4580,
   3066 	4560,
   3067 	4548,
   3068 	4536,
   3069 	4520,
   3070 	4504,
   3071 	4488,
   3072 	4472,
   3073 	4456,
   3074 	4440,
   3075 	4424,
   3076 	4408,
   3077 	4392,
   3078 	4376,
   3079 	4360,
   3080 	4344,
   3081 	4324,
   3082 	4304,
   3083 	4292,
   3084 	4280,
   3085 	4268,
   3086 	4256,
   3087 	4240,
   3088 	4224,
   3089 	4208,
   3090 	4192,
   3091 	4176,
   3092 	4160,
   3093 	4144,
   3094 	4128,
   3095 	4116,
   3096 	4104,
   3097 	4084,
   3098 	4064,
   3099 	4052,
   3100 	4040,
   3101 	4028,
   3102 	4016,
   3103 	4000,
   3104 	3984,
   3105 	3968,
   3106 	3952,
   3107 	3940,
   3108 	3928,
   3109 	3912,
   3110 	3896,
   3111 	3884,
   3112 	3872,
   3113 	3856,
   3114 	3840,
   3115 	3828,
   3116 	3816,
   3117 	3804,
   3118 	3792,
   3119 	3776,
   3120 	3760,
   3121 	3748,
   3122 	3736,
   3123 	3720,
   3124 	3704,
   3125 	3692,
   3126 	3680,
   3127 	3668,
   3128 	3656,
   3129 	3642,
   3130 	3628,
   3131 	3614,
   3132 	3600,
   3133 	3588,
   3134 	3576,
   3135 	3562,
   3136 	3548,
   3137 	3536,
   3138 	3524,
   3139 	3512,
   3140 	3500,
   3141 	3486,
   3142 	3472,
   3143 	3460,
   3144 	3448,
   3145 	3436,
   3146 	3424,
   3147 	3412,
   3148 	3400,
   3149 	3388,
   3150 	3376,
   3151 	3364,
   3152 	3352,
   3153 	3340,
   3154 	3328,
   3155 	3316,
   3156 	3304,
   3157 	3292,
   3158 	3280,
   3159 	3268,
   3160 	3256,
   3161 	3244,
   3162 	3232,
   3163 	3220,
   3164 	3208,
   3165 	3196,
   3166 	3184,
   3167 	3174,
   3168 	3164,
   3169 	3152,
   3170 	3140,
   3171 	3128,
   3172 	3116,
   3173 	3106,
   3174 	3096,
   3175 	3084,
   3176 	3072,
   3177 	3060,
   3178 	3048,
   3179 	3038,
   3180 	3028,
   3181 	3018,
   3182 	3008,
   3183 	2996,
   3184 	2984,
   3185 	2974,
   3186 	2964,
   3187 	2954,
   3188 	2944,
   3189 	2932,
   3190 	2920,
   3191 	2910,
   3192 	2900,
   3193 	2890,
   3194 	2880,
   3195 	2870,
   3196 	2860,
   3197 	2848,
   3198 	2836,
   3199 	2826,
   3200 	2816,
   3201 	2806,
   3202 	2796,
   3203 	2786,
   3204 	2776,
   3205 	2766,
   3206 	2756,
   3207 	2746,
   3208 	2736,
   3209 	2724,
   3210 	2712,
   3211 	2706,
   3212 	2700,
   3213 	2690,
   3214 	2680,
   3215 	2670,
   3216 	2660,
   3217 	2650,
   3218 	2640,
   3219 	2630,
   3220 	2620,
   3221 	2612,
   3222 	2604,
   3223 	2594,
   3224 	2584,
   3225 	2572,
   3226 	2560,
   3227 	2552,
   3228 	2544,
   3229 	2536,
   3230 	2528,
   3231 	2520,
   3232 	2512,
   3233 	2502,
   3234 	2492,
   3235 	2484,
   3236 	2476,
   3237 	2466,
   3238 	2456,
   3239 	2448,
   3240 	2440,
   3241 	2428,
   3242 	2416,
   3243 	2410,
   3244 	2404,
   3245 	2396,
   3246 	2388,
   3247 	2378,
   3248 	2368,
   3249 	2360,
   3250 	2352,
   3251 	2344,
   3252 	2336,
   3253 	2328,
   3254 	2320,
   3255 	2310,
   3256 	2300,
   3257 	2290,
   3258 	2280,
   3259 	2274,
   3260 	2268,
   3261 	2260,
   3262 	2252,
   3263 	2244,
   3264 	2236,
   3265 	2228,
   3266 	2220,
   3267 	2212,
   3268 	2204,
   3269 	2196,
   3270 	2188,
   3271 	2180,
   3272 	2172,
   3273 	2162,
   3274 	2152,
   3275 	2146,
   3276 	2140,
   3277 	2134,
   3278 	2128,
   3279 	2120,
   3280 	2112,
   3281 	2104,
   3282 	2096,
   3283 	2088,
   3284 	2080,
   3285 	2072,
   3286 	2064,
   3287 	2058,
   3288 	2052,
   3289 	2042,
   3290 	2032,
   3291 	2026,
   3292 	2020,
   3293 	2014,
   3294 	2008,
   3295 	2000,
   3296 	1992,
   3297 	1984,
   3298 	1976,
   3299 	1970,
   3300 	1964,
   3301 	1956,
   3302 	1948,
   3303 	1942,
   3304 	1936,
   3305 	1928,
   3306 	1920,
   3307 	1914,
   3308 	1908,
   3309 	1902,
   3310 	1896,
   3311 	1888,
   3312 	1880,
   3313 	1874,
   3314 	1868,
   3315 	1860,
   3316 	1852,
   3317 	1846,
   3318 	1840,
   3319 	1834,
   3320 	1828,
   3321 	1821,
   3322 	1814,
   3323 	1807,
   3324 	1800,
   3325 	1794,
   3326 	1788,
   3327 	1781,
   3328 	1774,
   3329 	1768,
   3330 	1762,
   3331 	1756,
   3332 	1750,
   3333 	1743,
   3334 	1736,
   3335 	1730,
   3336 	1724,
   3337 	1718,
   3338 	1712,
   3339 	1706,
   3340 	1700,
   3341 	1694,
   3342 	1688,
   3343 	1682,
   3344 	1676,
   3345 	1670,
   3346 	1664,
   3347 	1658,
   3348 	1652,
   3349 	1646,
   3350 	1640,
   3351 	1634,
   3352 	1628,
   3353 	1622,
   3354 	1616,
   3355 	1610,
   3356 	1604,
   3357 	1598,
   3358 	1592,
   3359 	1587,
   3360 	1582,
   3361 	1576,
   3362 	1570,
   3363 	1564,
   3364 	1558,
   3365 	1553,
   3366 	1548,
   3367 	1542,
   3368 	1536,
   3369 	1530,
   3370 	1524,
   3371 	1519,
   3372 	1514,
   3373 	1509,
   3374 	1504,
   3375 	1498,
   3376 	1492,
   3377 	1487,
   3378 	1482,
   3379 	1477,
   3380 	1472,
   3381 	1466,
   3382 	1460,
   3383 	1455,
   3384 	1450,
   3385 	1445,
   3386 	1440,
   3387 	1435,
   3388 	1430,
   3389 	1424,
   3390 	1418,
   3391 	1413,
   3392 	1408,
   3393 	1403,
   3394 	1398,
   3395 	1393,
   3396 	1388,
   3397 	1383,
   3398 	1378,
   3399 	1373,
   3400 	1368,
   3401 	1362,
   3402 	1356,
   3403 	1353,
   3404 	1350,
   3405 	1345,
   3406 	1340,
   3407 	1335,
   3408 	1330,
   3409 	1325,
   3410 	1320,
   3411 	1315,
   3412 	1310,
   3413 	1306,
   3414 	1302,
   3415 	1297,
   3416 	1292,
   3417 	1286,
   3418 	1280,
   3419 	1276,
   3420 	1272,
   3421 	1268,
   3422 	1264,
   3423 	1260,
   3424 	1256,
   3425 	1251,
   3426 	1246,
   3427 	1242,
   3428 	1238,
   3429 	1233,
   3430 	1228,
   3431 	1224,
   3432 	1220,
   3433 	1214,
   3434 	1208,
   3435 	1205,
   3436 	1202,
   3437 	1198,
   3438 	1194,
   3439 	1189,
   3440 	1184,
   3441 	1180,
   3442 	1176,
   3443 	1172,
   3444 	1168,
   3445 	1164,
   3446 	1160,
   3447 	1155,
   3448 	1150,
   3449 	1145,
   3450 	1140,
   3451 	1137,
   3452 	1134,
   3453 	1130,
   3454 	1126,
   3455 	1122,
   3456 	1118,
   3457 	1114,
   3458 	1110,
   3459 	1106,
   3460 	1102,
   3461 	1098,
   3462 	1094,
   3463 	1090,
   3464 	1086,
   3465 	1081,
   3466 	1076,
   3467 	1073,
   3468 	1070,
   3469 	1067,
   3470 	1064,
   3471 	1060,
   3472 	1056,
   3473 	1052,
   3474 	1048,
   3475 	1044,
   3476 	1040,
   3477 	1036,
   3478 	1032,
   3479 	1029,
   3480 	1026,
   3481 	1021,
   3482 	1016,
   3483 	1013,
   3484 	1010,
   3485 	1007,
   3486 	1004,
   3487 	1000,
   3488 	996,
   3489 	992,
   3490 	988,
   3491 	985,
   3492 	982,
   3493 	978,
   3494 	974,
   3495 	971,
   3496 	968,
   3497 	964,
   3498 	960,
   3499 	957,
   3500 	954,
   3501 	951,
   3502 	948,
   3503 	944,
   3504 	940,
   3505 	937,
   3506 	934,
   3507 	930,
   3508 	926,
   3509 	923,
   3510 	920,
   3511 	917,
   3512 	914,
   3513 	910,
   3514 	907,
   3515 	903,
   3516 	900,
   3517 	897,
   3518 	894,
   3519 	890,
   3520 	887,
   3521 	884,
   3522 	881,
   3523 	878,
   3524 	875,
   3525 	871,
   3526 	868,
   3527 	865,
   3528 	862,
   3529 	859,
   3530 	856,
   3531 	853,
   3532 	850,
   3533 	847,
   3534 	844,
   3535 	841,
   3536 	838,
   3537 	835,
   3538 	832,
   3539 	829,
   3540 	826,
   3541 	823,
   3542 	820,
   3543 	817,
   3544 	814,
   3545 	811,
   3546 	808,
   3547 	805,
   3548 	802,
   3549 	799,
   3550 	796,
   3551 	793,
   3552 	791,
   3553 	788,
   3554 	785,
   3555 	782,
   3556 	779,
   3557 	776,
   3558 	774,
   3559 	771,
   3560 	768,
   3561 	765,
   3562 	762,
   3563 	759,
   3564 	757,
   3565 	754,
   3566 	752,
   3567 	749,
   3568 	746,
   3569 	743,
   3570 	741,
   3571 	738,
   3572 	736,
   3573 	733,
   3574 	730,
   3575 	727,
   3576 	725,
   3577 	722,
   3578 	720,
   3579 	717,
   3580 	715,
   3581 	712,
   3582 	709,
   3583 	706,
   3584 	704,
   3585 	701,
   3586 	699,
   3587 	696,
   3588 	694,
   3589 	691,
   3590 	689,
   3591 	686,
   3592 	684,
   3593 	681,
   3594 	678,
   3595 	676,
   3596 	675,
   3597 	672,
   3598 	670,
   3599 	667,
   3600 	665,
   3601 	662,
   3602 	660,
   3603 	657,
   3604 	655,
   3605 	653,
   3606 	651,
   3607 	648,
   3608 	646,
   3609 	643,
   3610 	640,
   3611 	638,
   3612 	636,
   3613 	634,
   3614 	632,
   3615 	630,
   3616 	628,
   3617 	625,
   3618 	623,
   3619 	621,
   3620 	619,
   3621 	616,
   3622 	614,
   3623 	612,
   3624 	610,
   3625 	607,
   3626 	604,
   3627 	602,
   3628 	601,
   3629 	599,
   3630 	597,
   3631 	594,
   3632 	592,
   3633 	590,
   3634 	588,
   3635 	586,
   3636 	584,
   3637 	582,
   3638 	580,
   3639 	577,
   3640 	575,
   3641 	572,
   3642 	570,
   3643 	568,
   3644 	567,
   3645 	565,
   3646 	563,
   3647 	561,
   3648 	559,
   3649 	557,
   3650 	555,
   3651 	553,
   3652 	551,
   3653 	549,
   3654 	547,
   3655 	545,
   3656 	543,
   3657 	540,
   3658 	538,
   3659 	536,
   3660 	535,
   3661 	533,
   3662 	532,
   3663 	530,
   3664 	528,
   3665 	526,
   3666 	524,
   3667 	522,
   3668 	520,
   3669 	518,
   3670 	516,
   3671 	514,
   3672 	513,
   3673 	510,
   3674 	508,
   3675 	506,
   3676 	505,
   3677 	503,
   3678 	502,
   3679 	500,
   3680 	498,
   3681 	496,
   3682 	494,
   3683 	492,
   3684 	491,
   3685 	489,
   3686 	487,
   3687 	485,
   3688 	484,
   3689 	482,
   3690 	480,
   3691 	478,
   3692 	477,
   3693 	475,
   3694 	474,
   3695 	472,
   3696 	470,
   3697 	468,
   3698 	467,
   3699 	465,
   3700 	463,
   3701 	461,
   3702 	460,
   3703 	458,
   3704 	457,
   3705 	455,
   3706 	453,
   3707 	451,
   3708 	450,
   3709 	448,
   3710 	447,
   3711 	445,
   3712 	443,
   3713 	441,
   3714 	440,
   3715 	438,
   3716 	437,
   3717 	435,
   3718 	434,
   3719 	432,
   3720 	431,
   3721 	429,
   3722 	428,
   3723 	426,
   3724 	425,
   3725 	423,
   3726 	422,
   3727 	420,
   3728 	419,
   3729 	417,
   3730 	416,
   3731 	414,
   3732 	413,
   3733 	411,
   3734 	410,
   3735 	408,
   3736 	407,
   3737 	405,
   3738 	404,
   3739 	402,
   3740 	401,
   3741 	399,
   3742 	398,
   3743 	396,
   3744 	395,
   3745 	393,
   3746 	392,
   3747 	390,
   3748 	389,
   3749 	388,
   3750 	387,
   3751 	385,
   3752 	384,
   3753 	382,
   3754 	381,
   3755 	379,
   3756 	378,
   3757 	377,
   3758 	376,
   3759 	374,
   3760 	373,
   3761 	371,
   3762 	370,
   3763 	369,
   3764 	368,
   3765 	366,
   3766 	365,
   3767 	363,
   3768 	362,
   3769 	361,
   3770 	360,
   3771 	358,
   3772 	357,
   3773 	355,
   3774 	354,
   3775 	353,
   3776 	352,
   3777 	350,
   3778 	349,
   3779 	348,
   3780 	347,
   3781 	345,
   3782 	344,
   3783 	343,
   3784 	342,
   3785 	340,
   3786 	339,
   3787 	338,
   3788 	337,
   3789 	336,
   3790 	335,
   3791 	333,
   3792 	332,
   3793 	331,
   3794 	330,
   3795 	328,
   3796 	327,
   3797 	326,
   3798 	325,
   3799 	324,
   3800 	323,
   3801 	321,
   3802 	320,
   3803 	319,
   3804 	318,
   3805 	317,
   3806 	316,
   3807 	315,
   3808 	314,
   3809 	312,
   3810 	311,
   3811 	310,
   3812 	309,
   3813 	308,
   3814 	307,
   3815 	306,
   3816 	305,
   3817 	303,
   3818 	302,
   3819 	301,
   3820 	300,
   3821 	299,
   3822 	298,
   3823 	297,
   3824 	296,
   3825 	295,
   3826 	294,
   3827 	293,
   3828 	292,
   3829 	291,
   3830 	290,
   3831 	288,
   3832 	287,
   3833 	286,
   3834 	285,
   3835 	284,
   3836 	283,
   3837 	282,
   3838 	281,
   3839 	280,
   3840 	279,
   3841 	278,
   3842 	277,
   3843 	276,
   3844 	275,
   3845 	274,
   3846 	273,
   3847 	272,
   3848 	271,
   3849 	270,
   3850 	269,
   3851 	268,
   3852 	267,
   3853 	266,
   3854 	266,
   3855 	265,
   3856 	264,
   3857 	263,
   3858 	262,
   3859 	261,
   3860 	260,
   3861 	259,
   3862 	258,
   3863 	257,
   3864 	256,
   3865 	255,
   3866 	254,
   3867 	253,
   3868 	252,
   3869 	251,
   3870 	251,
   3871 	250,
   3872 	249,
   3873 	248,
   3874 	247,
   3875 	246,
   3876 	245,
   3877 	244,
   3878 	243,
   3879 	242,
   3880 	242,
   3881 	241,
   3882 	240,
   3883 	239,
   3884 	238,
   3885 	237,
   3886 	237,
   3887 	236,
   3888 	235,
   3889 	234,
   3890 	233,
   3891 	232,
   3892 	231,
   3893 	230,
   3894 	230,
   3895 	229,
   3896 	228,
   3897 	227,
   3898 	227,
   3899 	226,
   3900 	225,
   3901 	224,
   3902 	223,
   3903 	222,
   3904 	222,
   3905 	221,
   3906 	220,
   3907 	219,
   3908 	219,
   3909 	218,
   3910 	217,
   3911 	216,
   3912 	215,
   3913 	214,
   3914 	214,
   3915 	213,
   3916 	212,
   3917 	211,
   3918 	211,
   3919 	210,
   3920 	209,
   3921 	208,
   3922 	208,
   3923 	207,
   3924 	206,
   3925 	205,
   3926 	205,
   3927 	204,
   3928 	203,
   3929 	202,
   3930 	202,
   3931 	201,
   3932 	200,
   3933 	199,
   3934 	199,
   3935 	198,
   3936 	198,
   3937 	197,
   3938 	196,
   3939 	195,
   3940 	195,
   3941 	194,
   3942 	193,
   3943 	192,
   3944 	192,
   3945 	191,
   3946 	190,
   3947 	189,
   3948 	189,
   3949 	188,
   3950 	188,
   3951 	187,
   3952 	186,
   3953 	185,
   3954 	185,
   3955 	184,
   3956 	184,
   3957 	183,
   3958 	182,
   3959 	181,
   3960 	181,
   3961 	180,
   3962 	180,
   3963 	179,
   3964 	179,
   3965 	178,
   3966 	177,
   3967 	176,
   3968 	176,
   3969 	175,
   3970 	175,
   3971 	174,
   3972 	173,
   3973 	172,
   3974 	172,
   3975 	171,
   3976 	171,
   3977 	170,
   3978 	169,
   3979 	169,
   3980 	169,
   3981 	168,
   3982 	167,
   3983 	166,
   3984 	166,
   3985 	165,
   3986 	165,
   3987 	164,
   3988 	164,
   3989 	163,
   3990 	163,
   3991 	162,
   3992 	161,
   3993 	160,
   3994 	160,
   3995 	159,
   3996 	159,
   3997 	158,
   3998 	158,
   3999 	157,
   4000 	157,
   4001 	156,
   4002 	156,
   4003 	155,
   4004 	155,
   4005 	154,
   4006 	153,
   4007 	152,
   4008 	152,
   4009 	151,
   4010 	151,
   4011 	150,
   4012 	150,
   4013 	149,
   4014 	149,
   4015 	148,
   4016 	148,
   4017 	147,
   4018 	147,
   4019 	146,
   4020 	146,
   4021 	145,
   4022 	145,
   4023 	144,
   4024 	144,
   4025 	143,
   4026 	142,
   4027 	142,
   4028 	142,
   4029 	141,
   4030 	141,
   4031 	140,
   4032 	140,
   4033 	139,
   4034 	139,
   4035 	138,
   4036 	138,
   4037 	137,
   4038 	137,
   4039 	136,
   4040 	136,
   4041 	135,
   4042 	134,
   4043 	134,
   4044 	134,
   4045 	133,
   4046 	133,
   4047 	132,
   4048 	132,
   4049 	131,
   4050 	131,
   4051 	130,
   4052 	130,
   4053 	129,
   4054 	129,
   4055 	128,
   4056 	128,
   4057 	127,
   4058 	127,
   4059 	126,
   4060 	126,
   4061 	125,
   4062 	125,
   4063 	124,
   4064 	124,
   4065 	123,
   4066 	123,
   4067 	123,
   4068 	123,
   4069 	122,
   4070 	122,
   4071 	121,
   4072 	121,
   4073 	120,
   4074 	120,
   4075 	119,
   4076 	119,
   4077 	118,
   4078 	118,
   4079 	117,
   4080 	117,
   4081 	117,
   4082 	117,
   4083 	116,
   4084 	116,
   4085 	115,
   4086 	115,
   4087 	114,
   4088 	114,
   4089 	113,
   4090 	113,
   4091 	112,
   4092 	112,
   4093 	112,
   4094 	112,
   4095 	111,
   4096 	111,
   4097 	110,
   4098 	110,
   4099 	109,
   4100 	109,
   4101 	108,
   4102 	108,
   4103 	108,
   4104 	108,
   4105 	107,
   4106 	107,
   4107 	106,
   4108 	106,
   4109 	105,
   4110 	105,
   4111 	105,
   4112 	105,
   4113 	104,
   4114 	104,
   4115 	103,
   4116 	103,
   4117 	102,
   4118 	102,
   4119 	102,
   4120 	102,
   4121 	101,
   4122 	101,
   4123 	100,
   4124 	100,
   4125 	99,
   4126 	99,
   4127 	99,
   4128 	99,
   4129 	98,
   4130 	98,
   4131 	97,
   4132 	97,
   4133 	97,
   4134 	97,
   4135 	96,
   4136 	96,
   4137 	95,
   4138 	95,
   4139 	95,
   4140 	95,
   4141 	94,
   4142 	94,
   4143 	93,
   4144 	93,
   4145 	93,
   4146 	93,
   4147 	92,
   4148 	92,
   4149 	91,
   4150 	91,
   4151 	91,
   4152 	91,
   4153 	90,
   4154 	90,
   4155 	89,
   4156 	89,
   4157 	89,
   4158 	89,
   4159 	88,
   4160 	88,
   4161 	87,
   4162 	87,
   4163 	87,
   4164 	87,
   4165 	86,
   4166 	86,
   4167 	85,
   4168 	85,
   4169 	85,
   4170 	85,
   4171 	84,
   4172 	84,
   4173 	84,
   4174 	84,
   4175 	83,
   4176 	83,
   4177 	82,
   4178 	82,
   4179 	82,
   4180 	82,
   4181 	81,
   4182 	81,
   4183 	81,
   4184 	81,
   4185 	80,
   4186 	80,
   4187 	79,
   4188 	79,
   4189 	79,
   4190 	79,
   4191 	78,
   4192 	78,
   4193 	78,
   4194 	78,
   4195 	77,
   4196 	77,
   4197 	77,
   4198 	77,
   4199 	76,
   4200 	76,
   4201 	75,
   4202 	75,
   4203 	75,
   4204 	75,
   4205 	75,
   4206 	75,
   4207 	74,
   4208 	74,
   4209 	73,
   4210 	73,
   4211 	73,
   4212 	73,
   4213 	72,
   4214 	72,
   4215 	72,
   4216 	72,
   4217 	71,
   4218 	71,
   4219 	71,
   4220 	71,
   4221 	70,
   4222 	70,
   4223 	70,
   4224 	70,
   4225 	69,
   4226 	69,
   4227 	69,
   4228 	69,
   4229 	68,
   4230 	68,
   4231 	68,
   4232 	68,
   4233 	67,
   4234 	67,
   4235 	67,
   4236 	67,
   4237 	66,
   4238 	66,
   4239 	66,
   4240 	66,
   4241 	65,
   4242 	65,
   4243 	65,
   4244 	65,
   4245 	64,
   4246 	64,
   4247 	64,
   4248 	64,
   4249 	63,
   4250 	63,
   4251 	63,
   4252 	63,
   4253 	63,
   4254 	63,
   4255 	62,
   4256 	62,
   4257 	62,
   4258 	62,
   4259 	61,
   4260 	61,
   4261 	61,
   4262 	61,
   4263 	60,
   4264 	60,
   4265 	60,
   4266 	60,
   4267 	60,
   4268 	60,
   4269 	59,
   4270 	59,
   4271 	59,
   4272 	59,
   4273 	58,
   4274 	58,
   4275 	58,
   4276 	58,
   4277 	57,
   4278 	57,
   4279 	57,
   4280 	57,
   4281 	57,
   4282 	57,
   4283 	56,
   4284 	56,
   4285 	56,
   4286 	56,
   4287 	55,
   4288 	55,
   4289 	55,
   4290 	55,
   4291 	55,
   4292 	55,
   4293 	54,
   4294 	54,
   4295 	54,
   4296 	54,
   4297 	53,
   4298 	53,
   4299 	53,
   4300 	53,
   4301 	53,
   4302 	53,
   4303 	52,
   4304 	52,
   4305 	52,
   4306 	52,
   4307 	52,
   4308 	52,
   4309 	51,
   4310 	51,
   4311 	51,
   4312 	51,
   4313 	50,
   4314 	50,
   4315 	50,
   4316 	50,
   4317 	50,
   4318 	50,
   4319 	49,
   4320 	49,
   4321 	49,
   4322 	49,
   4323 	49,
   4324 	49,
   4325 	48,
   4326 	48,
   4327 	48,
   4328 	48,
   4329 	48,
   4330 	48,
   4331 	47,
   4332 	47,
   4333 	47,
   4334 	47,
   4335 	47,
   4336 	47,
   4337 	46,
   4338 	46,
   4339 	46,
   4340 	46,
   4341 	46,
   4342 	46,
   4343 	45,
   4344 	45,
   4345 	45,
   4346 	45,
   4347 	45,
   4348 	45,
   4349 	44,
   4350 	44,
   4351 	44,
   4352 	44,
   4353 	44,
   4354 	44,
   4355 	43,
   4356 	43,
   4357 	43,
   4358 	43,
   4359 	43,
   4360 	43,
   4361 	42,
   4362 	42,
   4363 	42,
   4364 	42,
   4365 	42,
   4366 	42,
   4367 	42,
   4368 	42,
   4369 	41,
   4370 	41,
   4371 	41,
   4372 	41,
   4373 	41,
   4374 	41,
   4375 	40,
   4376 	40,
   4377 	40,
   4378 	40,
   4379 	40,
   4380 	40,
   4381 	39,
   4382 	39,
   4383 	39,
   4384 	39,
   4385 	39,
   4386 	39,
   4387 	39,
   4388 	39,
   4389 	38,
   4390 	38,
   4391 	38,
   4392 	38,
   4393 	38,
   4394 	38,
   4395 	38,
   4396 	38,
   4397 	37,
   4398 	37,
   4399 	37,
   4400 	37,
   4401 	37,
   4402 	37,
   4403 	36,
   4404 	36,
   4405 	36,
   4406 	36,
   4407 	36,
   4408 	36,
   4409 	36,
   4410 	36,
   4411 	35,
   4412 	35,
   4413 	35,
   4414 	35,
   4415 	35,
   4416 	35,
   4417 	35,
   4418 	35,
   4419 	34,
   4420 	34,
   4421 	34,
   4422 	34,
   4423 	34,
   4424 	34,
   4425 	34,
   4426 	34,
   4427 	33,
   4428 	33,
   4429 	33,
   4430 	33,
   4431 	33,
   4432 	33,
   4433 	33,
   4434 	33,
   4435 	32,
   4436 	32,
   4437 	32,
   4438 	32,
   4439 	32,
   4440 	32,
   4441 	32,
   4442 	32,
   4443 	32,
   4444 	32,
   4445 	31,
   4446 	31,
   4447 	31,
   4448 	31,
   4449 	31,
   4450 	31,
   4451 	31,
   4452 	31,
   4453 	30,
   4454 	30,
   4455 	30,
   4456 	30,
   4457 	30,
   4458 	30,
   4459 	30,
   4460 	30,
   4461 	30,
   4462 	30,
   4463 	29,
   4464 	29,
   4465 	29,
   4466 	29,
   4467 	29,
   4468 	29,
   4469 	29,
   4470 	29,
   4471 	29,
   4472 	29,
   4473 	22,
   4474 	16,
   4475 	8,
   4476 	0,
   4477 	16,
   4478 	32,
   4479 	24,
   4480 	16,
   4481 	8,
   4482 	0,
   4483 	16,
   4484 	32,
   4485 	24,
   4486 	16,
   4487 	8,
   4488 	0,
   4489 	0
   4490 };
   4491 
   4492 static const uint32_t log_tab[4*12*16] = {
   4493 	16777216,
   4494 	16792365,
   4495 	16807527,
   4496 	16822704,
   4497 	16837894,
   4498 	16853097,
   4499 	16868315,
   4500 	16883546,
   4501 	16898791,
   4502 	16914049,
   4503 	16929322,
   4504 	16944608,
   4505 	16959908,
   4506 	16975222,
   4507 	16990549,
   4508 	17005891,
   4509 	17021246,
   4510 	17036615,
   4511 	17051999,
   4512 	17067396,
   4513 	17082806,
   4514 	17098231,
   4515 	17113670,
   4516 	17129123,
   4517 	17144589,
   4518 	17160070,
   4519 	17175564,
   4520 	17191073,
   4521 	17206595,
   4522 	17222132,
   4523 	17237683,
   4524 	17253247,
   4525 	17268826,
   4526 	17284419,
   4527 	17300026,
   4528 	17315646,
   4529 	17331282,
   4530 	17346931,
   4531 	17362594,
   4532 	17378271,
   4533 	17393963,
   4534 	17409669,
   4535 	17425389,
   4536 	17441123,
   4537 	17456871,
   4538 	17472634,
   4539 	17488410,
   4540 	17504202,
   4541 	17520007,
   4542 	17535826,
   4543 	17551660,
   4544 	17567508,
   4545 	17583371,
   4546 	17599248,
   4547 	17615139,
   4548 	17631044,
   4549 	17646964,
   4550 	17662898,
   4551 	17678847,
   4552 	17694810,
   4553 	17710787,
   4554 	17726779,
   4555 	17742785,
   4556 	17758806,
   4557 	17774841,
   4558 	17790891,
   4559 	17806955,
   4560 	17823034,
   4561 	17839127,
   4562 	17855235,
   4563 	17871357,
   4564 	17887494,
   4565 	17903645,
   4566 	17919811,
   4567 	17935992,
   4568 	17952187,
   4569 	17968397,
   4570 	17984621,
   4571 	18000860,
   4572 	18017114,
   4573 	18033382,
   4574 	18049665,
   4575 	18065963,
   4576 	18082276,
   4577 	18098603,
   4578 	18114945,
   4579 	18131302,
   4580 	18147673,
   4581 	18164060,
   4582 	18180461,
   4583 	18196877,
   4584 	18213307,
   4585 	18229753,
   4586 	18246213,
   4587 	18262689,
   4588 	18279179,
   4589 	18295684,
   4590 	18312204,
   4591 	18328739,
   4592 	18345288,
   4593 	18361853,
   4594 	18378433,
   4595 	18395028,
   4596 	18411637,
   4597 	18428262,
   4598 	18444902,
   4599 	18461556,
   4600 	18478226,
   4601 	18494911,
   4602 	18511611,
   4603 	18528325,
   4604 	18545056,
   4605 	18561801,
   4606 	18578561,
   4607 	18595336,
   4608 	18612127,
   4609 	18628932,
   4610 	18645753,
   4611 	18662589,
   4612 	18679441,
   4613 	18696307,
   4614 	18713189,
   4615 	18730086,
   4616 	18746998,
   4617 	18763925,
   4618 	18780868,
   4619 	18797826,
   4620 	18814800,
   4621 	18831788,
   4622 	18848792,
   4623 	18865812,
   4624 	18882846,
   4625 	18899897,
   4626 	18916962,
   4627 	18934043,
   4628 	18951139,
   4629 	18968251,
   4630 	18985378,
   4631 	19002521,
   4632 	19019679,
   4633 	19036853,
   4634 	19054042,
   4635 	19071247,
   4636 	19088467,
   4637 	19105703,
   4638 	19122954,
   4639 	19140221,
   4640 	19157504,
   4641 	19174802,
   4642 	19192116,
   4643 	19209445,
   4644 	19226790,
   4645 	19244151,
   4646 	19261527,
   4647 	19278919,
   4648 	19296327,
   4649 	19313750,
   4650 	19331190,
   4651 	19348645,
   4652 	19366115,
   4653 	19383602,
   4654 	19401104,
   4655 	19418622,
   4656 	19436156,
   4657 	19453706,
   4658 	19471271,
   4659 	19488853,
   4660 	19506450,
   4661 	19524063,
   4662 	19541692,
   4663 	19559337,
   4664 	19576998,
   4665 	19594675,
   4666 	19612368,
   4667 	19630077,
   4668 	19647802,
   4669 	19665543,
   4670 	19683300,
   4671 	19701072,
   4672 	19718861,
   4673 	19736666,
   4674 	19754488,
   4675 	19772325,
   4676 	19790178,
   4677 	19808047,
   4678 	19825933,
   4679 	19843835,
   4680 	19861752,
   4681 	19879686,
   4682 	19897637,
   4683 	19915603,
   4684 	19933586,
   4685 	19951585,
   4686 	19969600,
   4687 	19987631,
   4688 	20005679,
   4689 	20023743,
   4690 	20041823,
   4691 	20059920,
   4692 	20078033,
   4693 	20096162,
   4694 	20114308,
   4695 	20132470,
   4696 	20150648,
   4697 	20168843,
   4698 	20187054,
   4699 	20205282,
   4700 	20223526,
   4701 	20241787,
   4702 	20260064,
   4703 	20278358,
   4704 	20296668,
   4705 	20314995,
   4706 	20333338,
   4707 	20351698,
   4708 	20370074,
   4709 	20388467,
   4710 	20406877,
   4711 	20425303,
   4712 	20443746,
   4713 	20462206,
   4714 	20480682,
   4715 	20499175,
   4716 	20517684,
   4717 	20536211,
   4718 	20554754,
   4719 	20573313,
   4720 	20591890,
   4721 	20610483,
   4722 	20629093,
   4723 	20647720,
   4724 	20666364,
   4725 	20685025,
   4726 	20703702,
   4727 	20722396,
   4728 	20741107,
   4729 	20759835,
   4730 	20778580,
   4731 	20797342,
   4732 	20816121,
   4733 	20834917,
   4734 	20853729,
   4735 	20872559,
   4736 	20891406,
   4737 	20910270,
   4738 	20929150,
   4739 	20948048,
   4740 	20966963,
   4741 	20985895,
   4742 	21004844,
   4743 	21023810,
   4744 	21042794,
   4745 	21061794,
   4746 	21080812,
   4747 	21099846,
   4748 	21118898,
   4749 	21137968,
   4750 	21157054,
   4751 	21176158,
   4752 	21195278,
   4753 	21214417,
   4754 	21233572,
   4755 	21252745,
   4756 	21271935,
   4757 	21291142,
   4758 	21310367,
   4759 	21329609,
   4760 	21348868,
   4761 	21368145,
   4762 	21387439,
   4763 	21406751,
   4764 	21426080,
   4765 	21445426,
   4766 	21464790,
   4767 	21484172,
   4768 	21503571,
   4769 	21522987,
   4770 	21542421,
   4771 	21561873,
   4772 	21581342,
   4773 	21600829,
   4774 	21620333,
   4775 	21639855,
   4776 	21659395,
   4777 	21678952,
   4778 	21698527,
   4779 	21718119,
   4780 	21737729,
   4781 	21757357,
   4782 	21777003,
   4783 	21796666,
   4784 	21816348,
   4785 	21836046,
   4786 	21855763,
   4787 	21875498,
   4788 	21895250,
   4789 	21915020,
   4790 	21934808,
   4791 	21954614,
   4792 	21974438,
   4793 	21994279,
   4794 	22014139,
   4795 	22034016,
   4796 	22053912,
   4797 	22073825,
   4798 	22093757,
   4799 	22113706,
   4800 	22133674,
   4801 	22153659,
   4802 	22173663,
   4803 	22193684,
   4804 	22213724,
   4805 	22233781,
   4806 	22253857,
   4807 	22273951,
   4808 	22294063,
   4809 	22314194,
   4810 	22334342,
   4811 	22354509,
   4812 	22374693,
   4813 	22394897,
   4814 	22415118,
   4815 	22435357,
   4816 	22455615,
   4817 	22475891,
   4818 	22496186,
   4819 	22516499,
   4820 	22536830,
   4821 	22557179,
   4822 	22577547,
   4823 	22597933,
   4824 	22618338,
   4825 	22638761,
   4826 	22659202,
   4827 	22679662,
   4828 	22700141,
   4829 	22720638,
   4830 	22741153,
   4831 	22761687,
   4832 	22782240,
   4833 	22802811,
   4834 	22823400,
   4835 	22844009,
   4836 	22864635,
   4837 	22885281,
   4838 	22905945,
   4839 	22926628,
   4840 	22947329,
   4841 	22968049,
   4842 	22988788,
   4843 	23009546,
   4844 	23030322,
   4845 	23051117,
   4846 	23071931,
   4847 	23092764,
   4848 	23113615,
   4849 	23134485,
   4850 	23155374,
   4851 	23176282,
   4852 	23197209,
   4853 	23218155,
   4854 	23239120,
   4855 	23260103,
   4856 	23281106,
   4857 	23302127,
   4858 	23323168,
   4859 	23344227,
   4860 	23365306,
   4861 	23386403,
   4862 	23407520,
   4863 	23428656,
   4864 	23449810,
   4865 	23470984,
   4866 	23492177,
   4867 	23513389,
   4868 	23534620,
   4869 	23555871,
   4870 	23577140,
   4871 	23598429,
   4872 	23619737,
   4873 	23641065,
   4874 	23662411,
   4875 	23683777,
   4876 	23705162,
   4877 	23726566,
   4878 	23747990,
   4879 	23769433,
   4880 	23790896,
   4881 	23812377,
   4882 	23833879,
   4883 	23855399,
   4884 	23876939,
   4885 	23898499,
   4886 	23920078,
   4887 	23941676,
   4888 	23963294,
   4889 	23984932,
   4890 	24006589,
   4891 	24028265,
   4892 	24049962,
   4893 	24071677,
   4894 	24093413,
   4895 	24115168,
   4896 	24136942,
   4897 	24158736,
   4898 	24180550,
   4899 	24202384,
   4900 	24224237,
   4901 	24246111,
   4902 	24268003,
   4903 	24289916,
   4904 	24311848,
   4905 	24333801,
   4906 	24355773,
   4907 	24377765,
   4908 	24399776,
   4909 	24421808,
   4910 	24443859,
   4911 	24465931,
   4912 	24488022,
   4913 	24510133,
   4914 	24532265,
   4915 	24554416,
   4916 	24576587,
   4917 	24598778,
   4918 	24620990,
   4919 	24643221,
   4920 	24665472,
   4921 	24687744,
   4922 	24710036,
   4923 	24732347,
   4924 	24754679,
   4925 	24777031,
   4926 	24799403,
   4927 	24821796,
   4928 	24844209,
   4929 	24866641,
   4930 	24889095,
   4931 	24911568,
   4932 	24934062,
   4933 	24956576,
   4934 	24979110,
   4935 	25001665,
   4936 	25024240,
   4937 	25046835,
   4938 	25069451,
   4939 	25092088,
   4940 	25114744,
   4941 	25137421,
   4942 	25160119,
   4943 	25182837,
   4944 	25205576,
   4945 	25228335,
   4946 	25251115,
   4947 	25273915,
   4948 	25296736,
   4949 	25319578,
   4950 	25342440,
   4951 	25365322,
   4952 	25388226,
   4953 	25411150,
   4954 	25434095,
   4955 	25457060,
   4956 	25480047,
   4957 	25503054,
   4958 	25526081,
   4959 	25549130,
   4960 	25572199,
   4961 	25595290,
   4962 	25618401,
   4963 	25641533,
   4964 	25664686,
   4965 	25687859,
   4966 	25711054,
   4967 	25734270,
   4968 	25757506,
   4969 	25780764,
   4970 	25804042,
   4971 	25827342,
   4972 	25850662,
   4973 	25874004,
   4974 	25897367,
   4975 	25920751,
   4976 	25944156,
   4977 	25967582,
   4978 	25991029,
   4979 	26014497,
   4980 	26037987,
   4981 	26061498,
   4982 	26085030,
   4983 	26108583,
   4984 	26132158,
   4985 	26155754,
   4986 	26179371,
   4987 	26203009,
   4988 	26226669,
   4989 	26250350,
   4990 	26274053,
   4991 	26297777,
   4992 	26321522,
   4993 	26345289,
   4994 	26369077,
   4995 	26392887,
   4996 	26416718,
   4997 	26440571,
   4998 	26464445,
   4999 	26488341,
   5000 	26512259,
   5001 	26536198,
   5002 	26560158,
   5003 	26584141,
   5004 	26608145,
   5005 	26632170,
   5006 	26656218,
   5007 	26680287,
   5008 	26704377,
   5009 	26728490,
   5010 	26752624,
   5011 	26776780,
   5012 	26800958,
   5013 	26825158,
   5014 	26849380,
   5015 	26873623,
   5016 	26897888,
   5017 	26922176,
   5018 	26946485,
   5019 	26970816,
   5020 	26995169,
   5021 	27019544,
   5022 	27043941,
   5023 	27068360,
   5024 	27092802,
   5025 	27117265,
   5026 	27141750,
   5027 	27166258,
   5028 	27190787,
   5029 	27215339,
   5030 	27239913,
   5031 	27264509,
   5032 	27289127,
   5033 	27313768,
   5034 	27338430,
   5035 	27363116,
   5036 	27387823,
   5037 	27412552,
   5038 	27437304,
   5039 	27462079,
   5040 	27486875,
   5041 	27511695,
   5042 	27536536,
   5043 	27561400,
   5044 	27586286,
   5045 	27611195,
   5046 	27636126,
   5047 	27661080,
   5048 	27686057,
   5049 	27711056,
   5050 	27736077,
   5051 	27761121,
   5052 	27786188,
   5053 	27811277,
   5054 	27836389,
   5055 	27861524,
   5056 	27886681,
   5057 	27911861,
   5058 	27937064,
   5059 	27962290,
   5060 	27987538,
   5061 	28012809,
   5062 	28038103,
   5063 	28063420,
   5064 	28088760,
   5065 	28114122,
   5066 	28139508,
   5067 	28164916,
   5068 	28190347,
   5069 	28215802,
   5070 	28241279,
   5071 	28266779,
   5072 	28292302,
   5073 	28317849,
   5074 	28343418,
   5075 	28369011,
   5076 	28394626,
   5077 	28420265,
   5078 	28445927,
   5079 	28471612,
   5080 	28497320,
   5081 	28523052,
   5082 	28548806,
   5083 	28574584,
   5084 	28600385,
   5085 	28626210,
   5086 	28652058,
   5087 	28677929,
   5088 	28703823,
   5089 	28729741,
   5090 	28755683,
   5091 	28781647,
   5092 	28807636,
   5093 	28833647,
   5094 	28859682,
   5095 	28885741,
   5096 	28911823,
   5097 	28937929,
   5098 	28964058,
   5099 	28990211,
   5100 	29016388,
   5101 	29042588,
   5102 	29068811,
   5103 	29095059,
   5104 	29121330,
   5105 	29147625,
   5106 	29173944,
   5107 	29200286,
   5108 	29226652,
   5109 	29253042,
   5110 	29279456,
   5111 	29305894,
   5112 	29332355,
   5113 	29358841,
   5114 	29385350,
   5115 	29411883,
   5116 	29438441,
   5117 	29465022,
   5118 	29491627,
   5119 	29518256,
   5120 	29544910,
   5121 	29571587,
   5122 	29598288,
   5123 	29625014,
   5124 	29651764,
   5125 	29678538,
   5126 	29705336,
   5127 	29732158,
   5128 	29759004,
   5129 	29785875,
   5130 	29812770,
   5131 	29839689,
   5132 	29866633,
   5133 	29893600,
   5134 	29920593,
   5135 	29947609,
   5136 	29974650,
   5137 	30001716,
   5138 	30028805,
   5139 	30055920,
   5140 	30083059,
   5141 	30110222,
   5142 	30137410,
   5143 	30164622,
   5144 	30191859,
   5145 	30219120,
   5146 	30246407,
   5147 	30273717,
   5148 	30301053,
   5149 	30328413,
   5150 	30355798,
   5151 	30383207,
   5152 	30410642,
   5153 	30438101,
   5154 	30465584,
   5155 	30493093,
   5156 	30520627,
   5157 	30548185,
   5158 	30575768,
   5159 	30603377,
   5160 	30631010,
   5161 	30658668,
   5162 	30686351,
   5163 	30714059,
   5164 	30741792,
   5165 	30769550,
   5166 	30797333,
   5167 	30825141,
   5168 	30852975,
   5169 	30880833,
   5170 	30908717,
   5171 	30936625,
   5172 	30964559,
   5173 	30992519,
   5174 	31020503,
   5175 	31048513,
   5176 	31076548,
   5177 	31104608,
   5178 	31132694,
   5179 	31160805,
   5180 	31188941,
   5181 	31217103,
   5182 	31245290,
   5183 	31273503,
   5184 	31301741,
   5185 	31330005,
   5186 	31358294,
   5187 	31386609,
   5188 	31414949,
   5189 	31443315,
   5190 	31471707,
   5191 	31500124,
   5192 	31528567,
   5193 	31557035,
   5194 	31585529,
   5195 	31614049,
   5196 	31642595,
   5197 	31671166,
   5198 	31699764,
   5199 	31728387,
   5200 	31757036,
   5201 	31785710,
   5202 	31814411,
   5203 	31843138,
   5204 	31871890,
   5205 	31900669,
   5206 	31929473,
   5207 	31958304,
   5208 	31987160,
   5209 	32016043,
   5210 	32044951,
   5211 	32073886,
   5212 	32102847,
   5213 	32131834,
   5214 	32160847,
   5215 	32189887,
   5216 	32218952,
   5217 	32248044,
   5218 	32277162,
   5219 	32306307,
   5220 	32335478,
   5221 	32364675,
   5222 	32393898,
   5223 	32423148,
   5224 	32452424,
   5225 	32481727,
   5226 	32511056,
   5227 	32540412,
   5228 	32569794,
   5229 	32599202,
   5230 	32628638,
   5231 	32658099,
   5232 	32687588,
   5233 	32717103,
   5234 	32746645,
   5235 	32776213,
   5236 	32805808,
   5237 	32835430,
   5238 	32865078,
   5239 	32894754,
   5240 	32924456,
   5241 	32954184,
   5242 	32983940,
   5243 	33013723,
   5244 	33043532,
   5245 	33073369,
   5246 	33103232,
   5247 	33133122,
   5248 	33163040,
   5249 	33192984,
   5250 	33222955,
   5251 	33252954,
   5252 	33282979,
   5253 	33313032,
   5254 	33343112,
   5255 	33373219,
   5256 	33403353,
   5257 	33433514,
   5258 	33463703,
   5259 	33493919,
   5260 	33524162
   5261 };
   5262 
   5263 static void reset_volumes(channel_t *ch) {
   5264 	ch->real_vol = ch->old_vol;
   5265 	ch->out_vol = ch->old_vol;
   5266 	ch->out_pan = ch->old_pan;
   5267 
   5268 	ch->status |= CS_UPDATE_VOL + CS_UPDATE_PAN + CS_USE_QUICK_VOLRAMP;
   5269 }
   5270 
   5271 static void trigger_instrument(channel_t *ch) {
   5272 	if (!(ch->vib_trem_ctrl & 0x04)) {
   5273 		ch->vibrato_pos = 0;
   5274 	}
   5275 
   5276 	if (!(ch->vib_trem_ctrl & 0x40)) {
   5277 		ch->tremolo_pos = 0;
   5278 	}
   5279 
   5280 	ch->note_retrig_counter = 0;
   5281 	ch->tremor_pos = 0;
   5282 	ch->key_off = false;
   5283 
   5284 	instr_t *ins = ch->instr_ptr;
   5285 	if (ins != NULL) {
   5286 		if (ins->vol_env_flags & ENV_ENABLED) {
   5287 			ch->vol_env_tick = 65535;
   5288 			ch->vol_env_pos = 0;
   5289 		}
   5290 
   5291 		if (ins->pan_env_flags & ENV_ENABLED) {
   5292 			ch->pan_env_tick = 65535;
   5293 			ch->pan_env_pos = 0;
   5294 		}
   5295 
   5296 		ch->fadeout_speed = ins->fadeout;
   5297 		ch->fadeout_vol = 32768;
   5298 
   5299 		if (ins->auto_vib_depth > 0) {
   5300 			ch->auto_vib_pos = 0;
   5301 
   5302 			if (ins->auto_vib_sweep > 0) {
   5303 				ch->auto_vib_amp = 0;
   5304 				ch->auto_vib_sweep = (ins->auto_vib_depth << 8) / ins->auto_vib_sweep;
   5305 			} else {
   5306 				ch->auto_vib_amp = ins->auto_vib_depth << 8;
   5307 				ch->auto_vib_sweep = 0;
   5308 			}
   5309 		}
   5310 	}
   5311 }
   5312 
   5313 static void key_off(channel_t *ch) {
   5314 	ch->key_off = true;
   5315 
   5316 	instr_t *ins = ch->instr_ptr;
   5317 
   5318 	if (ins->vol_env_flags & ENV_ENABLED) {
   5319 		if (ch->vol_env_tick >= (uint16_t)ins->vol_env_points[ch->vol_env_pos][0]) {
   5320 			ch->vol_env_tick = ins->vol_env_points[ch->vol_env_pos][0] - 1;
   5321 		}
   5322 	} else {
   5323 		ch->real_vol = 0;
   5324 		ch->out_vol = 0;
   5325 		ch->status |= CS_UPDATE_VOL + CS_USE_QUICK_VOLRAMP;
   5326 	}
   5327 
   5328 	if (!(ins->pan_env_flags & ENV_ENABLED)) {
   5329 		if (ch->pan_env_tick >= (uint16_t)ins->pan_env_points[ch->pan_env_pos][0]) {
   5330 			ch->pan_env_tick = ins->pan_env_points[ch->pan_env_pos][0] - 1;
   5331 		}
   5332 	}
   5333 }
   5334 
   5335 static void trigger_note(uint8_t note, uint8_t efx, uint8_t efx_data, channel_t *ch) {
   5336 	if (note == NOTE_OFF) {
   5337 		key_off(ch);
   5338 		return;
   5339 	}
   5340 
   5341 	if (note == 0) {
   5342 		note = ch->note_num;
   5343 		if (note == 0) {
   5344 			return;
   5345 		}
   5346 	}
   5347 
   5348 	ch->note_num = note;
   5349 	instr_t *ins = instr[ch->instr_num];
   5350 	if (ins == NULL) {
   5351 		ins = instr[0];
   5352 	}
   5353 
   5354 	ch->instr_ptr = ins;
   5355 	ch->mute = ins->mute;
   5356 
   5357 	if (note > 96) {
   5358 		note = 96;
   5359 	}
   5360 
   5361 	ch->smp_num = ins->note2_sample_lut[note-1] & 0xF;
   5362 	sample_t *s = &ins->smp[ch->smp_num];
   5363 
   5364 	ch->smp_ptr = s;
   5365 	ch->relative_note = s->relative_note;
   5366 
   5367 	note += ch->relative_note;
   5368 	if (note >= 10*12) {
   5369 		return;
   5370 	}
   5371 
   5372 	ch->old_vol = s->volume;
   5373 	ch->old_pan = s->panning;
   5374 
   5375 	if (efx == 0xE && (efx_data & 0xF0) == 0x50) {
   5376 		ch->finetune = ((efx_data & 0x0F) * 16) - 128;
   5377 	} else {
   5378 		ch->finetune = s->finetune;
   5379 	}
   5380 
   5381 	if (note != 0) {
   5382 		const uint16_t note_index = ((note-1) * 16) + (((int8_t)ch->finetune >> 3) + 16);
   5383 		ch->out_period = ch->real_period = note2_period_lut[note_index];
   5384 	}
   5385 
   5386 	ch->status |= CF_UPDATE_PERIOD + CS_UPDATE_VOL + CS_UPDATE_PAN + CS_TRIGGER_VOICE + CS_USE_QUICK_VOLRAMP;
   5387 
   5388 	if (efx == 9) {
   5389 		if (efx_data > 0) {
   5390 			ch->sample_offset = ch->efx_data;
   5391 		}
   5392 
   5393 		ch->smp_start_pos = ch->sample_offset << 8;
   5394 	} else {
   5395 		ch->smp_start_pos = 0;
   5396 	}
   5397 }
   5398 
   5399 static void vol_slide(channel_t *ch, uint8_t param);
   5400 static void do_vibrato(channel_t *ch);
   5401 static void portamento(channel_t *ch, uint8_t param);
   5402 
   5403 static void dummy(channel_t *ch, uint8_t param) {
   5404 	(void)ch;
   5405 	(void)param;
   5406 	return;
   5407 }
   5408 
   5409 static void fine_pitch_slide_up(channel_t *ch, uint8_t param) {
   5410 	if (param == 0) {
   5411 		param = ch->f_pitch_slide_up_speed;
   5412 	}
   5413 
   5414 	ch->f_pitch_slide_up_speed = param;
   5415 
   5416 	ch->real_period -= param * 4;
   5417 	if ((int16_t)ch->real_period < 1) {
   5418 		ch->real_period = 1;
   5419 	}
   5420 
   5421 	ch->out_period = ch->real_period;
   5422 	ch->status |= CF_UPDATE_PERIOD;
   5423 }
   5424 
   5425 static void fine_pitch_slide_down(channel_t *ch, uint8_t param) {
   5426 	if (param == 0) {
   5427 		param = ch->f_pitch_slide_down_speed;
   5428 	}
   5429 
   5430 	ch->f_pitch_slide_down_speed = param;
   5431 
   5432 	ch->real_period += param * 4;
   5433 	if ((int16_t)ch->real_period >= 32000) {
   5434 		ch->real_period = 32000-1;
   5435 	}
   5436 
   5437 	ch->out_period = ch->real_period;
   5438 	ch->status |= CF_UPDATE_PERIOD;
   5439 }
   5440 
   5441 static void set_portamento_ctrl(channel_t *ch, uint8_t param) {
   5442 	ch->semitone_porta_mode = (param != 0);
   5443 }
   5444 
   5445 static void set_vibrato_ctrl(channel_t *ch, uint8_t param) {
   5446 	ch->vib_trem_ctrl = (ch->vib_trem_ctrl & 0xF0) | param;
   5447 }
   5448 
   5449 static void pattern_loop(channel_t *ch, uint8_t param) {
   5450 	if (param == 0) {
   5451 		ch->pattern_loop_start_row = song.row & 0xFF;
   5452 	} else if (ch->pattern_loop_counter == 0) {
   5453 		ch->pattern_loop_counter = param;
   5454 
   5455 		song.p_break_pos = ch->pattern_loop_start_row;
   5456 		song.p_break_flag = true;
   5457 	} else if (--ch->pattern_loop_counter > 0) {
   5458 		song.p_break_pos = ch->pattern_loop_start_row;
   5459 		song.p_break_flag = true;
   5460 	}
   5461 }
   5462 
   5463 static void set_tremolo_ctrl(channel_t *ch, uint8_t param) {
   5464 	ch->vib_trem_ctrl = (param << 4) | (ch->vib_trem_ctrl & 0x0F);
   5465 }
   5466 
   5467 static void fine_vol_slide_up(channel_t *ch, uint8_t param) {
   5468 	if (param == 0) {
   5469 		param = ch->f_vol_slide_up_speed;
   5470 	}
   5471 
   5472 	ch->f_vol_slide_up_speed = param;
   5473 
   5474 	ch->real_vol += param;
   5475 	if (ch->real_vol > 64) {
   5476 		ch->real_vol = 64;
   5477 	}
   5478 
   5479 	ch->out_vol = ch->real_vol;
   5480 	ch->status |= CS_UPDATE_VOL;
   5481 }
   5482 
   5483 static void fine_vol_fine_down(channel_t *ch, uint8_t param) {
   5484 	if (param == 0) {
   5485 		param = ch->f_vol_slide_down_speed;
   5486 	}
   5487 
   5488 	ch->f_vol_slide_down_speed = param;
   5489 
   5490 	ch->real_vol -= param;
   5491 	if ((int8_t)ch->real_vol < 0) {
   5492 		ch->real_vol = 0;
   5493 	}
   5494 
   5495 	ch->out_vol = ch->real_vol;
   5496 	ch->status |= CS_UPDATE_VOL;
   5497 }
   5498 
   5499 static void note_cut0(channel_t *ch, uint8_t param) {
   5500 	if (param == 0) {
   5501 		ch->real_vol = 0;
   5502 		ch->out_vol = 0;
   5503 		ch->status |= CS_UPDATE_VOL + CS_USE_QUICK_VOLRAMP;
   5504 	}
   5505 }
   5506 
   5507 static void pattern_delay(channel_t *ch, uint8_t param) {
   5508 	if (song.patt_del_time2 == 0) {
   5509 		song.patt_del_time = param + 1;
   5510 	}
   5511 
   5512 	(void)ch;
   5513 }
   5514 
   5515 static const efx_routine e_jump_tab_tick_zero[16] = {
   5516 	dummy,
   5517 	fine_pitch_slide_up,
   5518 	fine_pitch_slide_down,
   5519 	set_portamento_ctrl,
   5520 	set_vibrato_ctrl,
   5521 	dummy,
   5522 	pattern_loop,
   5523 	set_tremolo_ctrl,
   5524 	dummy,
   5525 	dummy,
   5526 	fine_vol_slide_up,
   5527 	fine_vol_fine_down,
   5528 	note_cut0,
   5529 	dummy,
   5530 	pattern_delay,
   5531 	dummy
   5532 };
   5533 
   5534 static void e_effects_tick_zero(channel_t *ch, uint8_t param) {
   5535 	const uint8_t efx = param >> 4;
   5536 	param &= 0x0F;
   5537 
   5538 	if (ch->channel_off) {
   5539 		if (efx == 0x6) {
   5540 			pattern_loop(ch, param);
   5541 		} else if (efx == 0xE) {
   5542 			pattern_delay(ch, param);
   5543 		}
   5544 
   5545 		return;
   5546 	}
   5547 
   5548 	e_jump_tab_tick_zero[efx](ch, param);
   5549 }
   5550 
   5551 static void position_jump(channel_t *ch, uint8_t param) {
   5552 	const int16_t pos = (int16_t)param - 1;
   5553 	if (pos < 0 || pos >= song.song_length) {
   5554 		bxx_overflow = true;
   5555 	} else {
   5556 		song.song_pos = pos;
   5557 	}
   5558 
   5559 	song.p_break_pos = 0;
   5560 	song.pos_jump_flag = true;
   5561 
   5562 	(void)ch;
   5563 }
   5564 
   5565 static void pattern_break(channel_t *ch, uint8_t param) {
   5566 	param = ((param >> 4) * 10) + (param & 0x0F);
   5567 	if (param <= 63) {
   5568 		song.p_break_pos = param;
   5569 	} else {
   5570 		song.p_break_pos = 0;
   5571 	}
   5572 
   5573 	song.pos_jump_flag = true;
   5574 
   5575 	(void)ch;
   5576 }
   5577 
   5578 static void set_speed(channel_t *ch, uint8_t param) {
   5579 	if (param >= 32) {
   5580 		song.bpm = param;
   5581 		set_mixer_bpm(song.bpm);
   5582 	} else {
   5583 		song.tick = song.speed = param;
   5584 	}
   5585 
   5586 	(void)ch;
   5587 }
   5588 
   5589 static void set_global_volume(channel_t *ch, uint8_t param) {
   5590 	if (param > 64) {
   5591 		param = 64;
   5592 	}
   5593 
   5594 	song.global_volume = param;
   5595 
   5596 	channel_t *c = channel;
   5597 	for (int32_t i = 0; i < song.num_channels; i++, c++) {
   5598 		c->status |= CS_UPDATE_VOL;
   5599 	}
   5600 
   5601 	(void)ch;
   5602 }
   5603 
   5604 static void set_envelope_pos(channel_t *ch, uint8_t param) {
   5605 	bool env_update;
   5606 	int8_t point;
   5607 	int32_t tick;
   5608 
   5609 	instr_t *ins = ch->instr_ptr;
   5610 
   5611 	if (ins->vol_env_flags & ENV_ENABLED) {
   5612 		ch->vol_env_tick = param - 1;
   5613 
   5614 		point = 0;
   5615 		env_update = true;
   5616 		tick = param;
   5617 
   5618 		if (ins->vol_env_length > 1) {
   5619 			point++;
   5620 			for (int32_t i = 0; i < ins->vol_env_length-1; i++) {
   5621 				if (tick < ins->vol_env_points[point][0]) {
   5622 					point--;
   5623 
   5624 					tick -= ins->vol_env_points[point][0];
   5625 					if (tick == 0) {
   5626 						env_update = false;
   5627 						break;
   5628 					}
   5629 
   5630 					const int16_t x0 = ins->vol_env_points[point+0][0];
   5631 					const int16_t x1 = ins->vol_env_points[point+1][0];
   5632 
   5633 					const int16_t x_diff = x1 - x0;
   5634 					if (x_diff <= 0) {
   5635 						env_update = true;
   5636 						break;
   5637 					}
   5638 
   5639 					const int16_t y0 = ins->vol_env_points[point+0][1];
   5640 					const int16_t y1 = ins->vol_env_points[point+1][1];
   5641 
   5642 					const int8_t y_diff = (int8_t)(y1 - y0);
   5643 					ch->vol_env_delta = (y_diff * 256) / x_diff;
   5644 
   5645 					ch->vol_env_value = (((int8_t)y0 * 256)) + (int16_t)(ch->vol_env_delta * (tick-1));
   5646 
   5647 					point++;
   5648 
   5649 					env_update = false;
   5650 					break;
   5651 				}
   5652 
   5653 				point++;
   5654 			}
   5655 
   5656 			if (env_update) {
   5657 				point--;
   5658 			}
   5659 		}
   5660 
   5661 		if (env_update) {
   5662 			ch->vol_env_delta = 0;
   5663 			ch->vol_env_value = ((int8_t)ins->vol_env_points[point][1] * 256);
   5664 		}
   5665 
   5666 		if (point >= ins->vol_env_length) {
   5667 			point = ins->vol_env_length - 1;
   5668 			if (point < 0) {
   5669 				point = 0;
   5670 			}
   5671 		}
   5672 
   5673 		ch->vol_env_pos = point;
   5674 	}
   5675 
   5676 	if (ins->vol_env_flags & ENV_SUSTAIN) {
   5677 		ch->pan_env_tick = param - 1;
   5678 
   5679 		point = 0;
   5680 		env_update = true;
   5681 		tick = param;
   5682 
   5683 		if (ins->pan_env_length > 1) {
   5684 			point++;
   5685 			for (int32_t i = 0; i < ins->pan_env_length-1; i++) {
   5686 				if (tick < ins->pan_env_points[point][0]) {
   5687 					point--;
   5688 
   5689 					tick -= ins->pan_env_points[point][0];
   5690 					if (tick == 0) {
   5691 						env_update = false;
   5692 						break;
   5693 					}
   5694 
   5695 					const int16_t x0 = ins->pan_env_points[point+0][0];
   5696 					const int16_t x1 = ins->pan_env_points[point+1][0];
   5697 
   5698 					const int16_t x_diff = x1 - x0;
   5699 					if (x_diff <= 0) {
   5700 						env_update = true;
   5701 						break;
   5702 					}
   5703 
   5704 					const int16_t y0 = ins->pan_env_points[point+0][1];
   5705 					const int16_t y1 = ins->pan_env_points[point+1][1];
   5706 
   5707 					const int8_t y_diff = (int8_t)(y1 - y0);
   5708 					ch->pan_env_delta = (y_diff * 256) / x_diff;
   5709 
   5710 					ch->pan_env_value = (((int8_t)y0 * 256)) + (int16_t)(ch->pan_env_delta * (tick-1));
   5711 
   5712 					point++;
   5713 
   5714 					env_update = false;
   5715 					break;
   5716 				}
   5717 
   5718 				point++;
   5719 			}
   5720 
   5721 			if (env_update) {
   5722 				point--;
   5723 			}
   5724 		}
   5725 
   5726 		if (env_update) {
   5727 			ch->pan_env_delta = 0;
   5728 			ch->pan_env_value = ((int8_t)ins->pan_env_points[point][1] * 256);
   5729 		}
   5730 
   5731 		if (point >= ins->pan_env_length) {
   5732 			point = ins->pan_env_length - 1;
   5733 			if (point < 0) {
   5734 				point = 0;
   5735 			}
   5736 		}
   5737 
   5738 		ch->pan_env_pos = point;
   5739 	}
   5740 }
   5741 
   5742 static const efx_routine jump_tab_tick_zero[36] = {
   5743 	dummy,
   5744 	dummy,
   5745 	dummy,
   5746 	dummy,
   5747 	dummy,
   5748 	dummy,
   5749 	dummy,
   5750 	dummy,
   5751 	dummy,
   5752 	dummy,
   5753 	dummy,
   5754 	position_jump,
   5755 	dummy,
   5756 	pattern_break,
   5757 	e_effects_tick_zero,
   5758 	set_speed,
   5759 	set_global_volume,
   5760 	dummy,
   5761 	dummy,
   5762 	dummy,
   5763 	dummy,
   5764 	set_envelope_pos,
   5765 	dummy,
   5766 	dummy,
   5767 	dummy,
   5768 	dummy,
   5769 	dummy,
   5770 	dummy,
   5771 	dummy,
   5772 	dummy,
   5773 	dummy,
   5774 	dummy,
   5775 	dummy,
   5776 	dummy,
   5777 	dummy,
   5778 	dummy
   5779 };
   5780 
   5781 static void handle_more_effects_tick_zero(channel_t *ch) {
   5782 	if (ch->efx > 35) {
   5783 		return;
   5784 	}
   5785 
   5786 	jump_tab_tick_zero[ch->efx](ch, ch->efx_data);
   5787 }
   5788 
   5789 static void v_set_vib_speed(channel_t *ch, uint8_t *vol_column_data) {
   5790 	*vol_column_data = (ch->vol_column_vol & 0x0F) * 4;
   5791 	if (*vol_column_data != 0) {
   5792 		ch->vibrato_speed = *vol_column_data;
   5793 	}
   5794 }
   5795 
   5796 static void v_set_volume(channel_t *ch, uint8_t *vol_column_data) {
   5797 	*vol_column_data -= 16;
   5798 	if (*vol_column_data > 64) {
   5799 		*vol_column_data = 64;
   5800 	}
   5801 
   5802 	ch->out_vol = ch->real_vol = *vol_column_data;
   5803 	ch->status |= CS_UPDATE_VOL + CS_USE_QUICK_VOLRAMP;
   5804 }
   5805 
   5806 static void v_fine_vol_slide_down(channel_t *ch, uint8_t *vol_column_data) {
   5807 	*vol_column_data = (uint8_t)(0 - (ch->vol_column_vol & 0x0F)) + ch->real_vol;
   5808 	if ((int8_t)*vol_column_data < 0) {
   5809 		*vol_column_data = 0;
   5810 	}
   5811 
   5812 	ch->out_vol = ch->real_vol = *vol_column_data;
   5813 	ch->status |= CS_UPDATE_VOL;
   5814 }
   5815 
   5816 static void v_fine_vol_slide_up(channel_t *ch, uint8_t *vol_column_data) {
   5817 	*vol_column_data = (ch->vol_column_vol & 0x0F) + ch->real_vol;
   5818 	if (*vol_column_data > 64) {
   5819 		*vol_column_data = 64;
   5820 	}
   5821 
   5822 	ch->out_vol = ch->real_vol = *vol_column_data;
   5823 	ch->status |= CS_UPDATE_VOL;
   5824 }
   5825 
   5826 static void v_set_pan(channel_t *ch, uint8_t *vol_column_data) {
   5827 	*vol_column_data <<= 4;
   5828 
   5829 	ch->out_pan = *vol_column_data;
   5830 	ch->status |= CS_UPDATE_PAN;
   5831 }
   5832 
   5833 static void v_vol_slide_down(channel_t *ch) {
   5834 	uint8_t new_vol = (uint8_t)(0 - (ch->vol_column_vol & 0x0F)) + ch->real_vol;
   5835 	if ((int8_t)new_vol < 0) {
   5836 		new_vol = 0;
   5837 	}
   5838 
   5839 	ch->out_vol = ch->real_vol = new_vol;
   5840 	ch->status |= CS_UPDATE_VOL;
   5841 }
   5842 
   5843 static void v_vol_slide_up(channel_t *ch) {
   5844 	uint8_t new_vol = (ch->vol_column_vol & 0x0F) + ch->real_vol;
   5845 	if (new_vol > 64) {
   5846 		new_vol = 64;
   5847 	}
   5848 
   5849 	ch->out_vol = ch->real_vol = new_vol;
   5850 	ch->status |= CS_UPDATE_VOL;
   5851 }
   5852 
   5853 static void v_vibrato(channel_t *ch) {
   5854 	const uint8_t param = ch->vol_column_vol & 0xF;
   5855 	if (param > 0) {
   5856 		ch->vibrato_depth = param;
   5857 	}
   5858 
   5859 	do_vibrato(ch);
   5860 }
   5861 
   5862 static void v_pan_slide_left(channel_t *ch) {
   5863 	uint16_t tmp16 = ch->out_pan + (uint8_t)(0 - (ch->vol_column_vol & 0x0F));
   5864 	if (tmp16 < 256) {
   5865 		tmp16 = 0;
   5866 	}
   5867 
   5868 	ch->out_pan = (uint8_t)tmp16;
   5869 	ch->status |= CS_UPDATE_PAN;
   5870 }
   5871 
   5872 static void v_pan_slide_right(channel_t *ch) {
   5873 	uint16_t tmp16 = ch->out_pan + (ch->vol_column_vol & 0x0F);
   5874 	if (tmp16 > 255) {
   5875 		tmp16 = 255;
   5876 	}
   5877 
   5878 	ch->out_pan = (uint8_t)tmp16;
   5879 	ch->status |= CS_UPDATE_PAN;
   5880 }
   5881 
   5882 static void v_portamento(channel_t *ch) {
   5883 	portamento(ch, 0);
   5884 }
   5885 
   5886 static void v_dummy(channel_t *ch) {
   5887 	(void)ch;
   5888 	return;
   5889 }
   5890 
   5891 static void v_dummy2(channel_t *ch, uint8_t *vol_column_data) {
   5892 	(void)ch;
   5893 	(void)vol_column_data;
   5894 	return;
   5895 }
   5896 
   5897 static const vol_column_efx_routine v_jump_tab_tick_non_zero[16] = {
   5898 	v_dummy,
   5899 	v_dummy,
   5900 	v_dummy,
   5901 	v_dummy,
   5902 	v_dummy,
   5903 	v_dummy,
   5904 	v_vol_slide_down,
   5905 	v_vol_slide_up,
   5906 	v_dummy,
   5907 	v_dummy,
   5908 	v_dummy,
   5909 	v_vibrato,
   5910 	v_dummy,
   5911 	v_pan_slide_left,
   5912 	v_pan_slide_right,
   5913 	v_portamento
   5914 };
   5915 
   5916 static const vol_column_efx_routine2 v_jump_tab_tick_zero[16] = {
   5917 	v_dummy2,
   5918 	v_set_volume,
   5919 	v_set_volume,
   5920 	v_set_volume,
   5921 	v_set_volume,
   5922 	v_set_volume,
   5923 	v_dummy2,
   5924 	v_dummy2,
   5925 	v_fine_vol_slide_down,
   5926 	v_fine_vol_slide_up,
   5927 	v_set_vib_speed,
   5928 	v_dummy2,
   5929 	v_set_pan,
   5930 	v_dummy2,
   5931 	v_dummy2,
   5932 	v_dummy2
   5933 };
   5934 
   5935 static void set_pan(channel_t *ch, uint8_t param) {
   5936 	ch->out_pan = param;
   5937 	ch->status |= CS_UPDATE_PAN;
   5938 }
   5939 
   5940 static void set_vol(channel_t *ch, uint8_t param) {
   5941 	if (param > 64) {
   5942 		param = 64;
   5943 	}
   5944 
   5945 	ch->out_vol = ch->real_vol = param;
   5946 	ch->status |= CS_UPDATE_VOL + CS_USE_QUICK_VOLRAMP;
   5947 }
   5948 
   5949 static void extra_fine_pitch_slide(channel_t *ch, uint8_t param) {
   5950 	const uint8_t slide_type = param >> 4;
   5951 	param &= 0x0F;
   5952 
   5953 	if (slide_type == 1) {
   5954 		if (param == 0) {
   5955 			param = ch->ef_pitch_slide_up_speed;
   5956 		}
   5957 
   5958 		ch->ef_pitch_slide_up_speed = param;
   5959 
   5960 		uint16_t new_period = ch->real_period;
   5961 
   5962 		new_period -= param;
   5963 		if ((int16_t)new_period < 1) {
   5964 			new_period = 1;
   5965 		}
   5966 
   5967 		ch->out_period = ch->real_period = new_period;
   5968 		ch->status |= CF_UPDATE_PERIOD;
   5969 	} else if (slide_type == 2)  {
   5970 		if (param == 0) {
   5971 			param = ch->ef_pitch_slide_down_speed;
   5972 		}
   5973 
   5974 		ch->ef_pitch_slide_down_speed = param;
   5975 
   5976 		uint16_t new_period = ch->real_period;
   5977 
   5978 		new_period += param;
   5979 		if ((int16_t)new_period >= 32000) {
   5980 			new_period = 32000-1;
   5981 		}
   5982 
   5983 		ch->out_period = ch->real_period = new_period;
   5984 		ch->status |= CF_UPDATE_PERIOD;
   5985 	}
   5986 }
   5987 
   5988 static void do_multi_note_retrig(channel_t *ch, uint8_t param) {
   5989 	uint8_t cnt = ch->note_retrig_counter + 1;
   5990 	if (cnt < ch->note_retrig_speed) {
   5991 		ch->note_retrig_counter = cnt;
   5992 		return;
   5993 	}
   5994 
   5995 	ch->note_retrig_counter = 0;
   5996 
   5997 	int16_t vol = ch->real_vol;
   5998 	switch (ch->note_retrig_vol) {
   5999 		case 0x1: {
   6000 			vol -= 1;
   6001 			break;
   6002 		}
   6003 		case 0x2: {
   6004 			vol -= 2;
   6005 			break;
   6006 		}
   6007 		case 0x3: {
   6008 			vol -= 4;
   6009 			break;
   6010 		}
   6011 		case 0x4: {
   6012 			vol -= 8;
   6013 			break;
   6014 		}
   6015 		case 0x5: {
   6016 			vol -= 16;
   6017 			break;
   6018 		}
   6019 		case 0x6: {
   6020 			vol = (vol >> 1) + (vol >> 3) + (vol >> 4);
   6021 			break;
   6022 		}
   6023 		case 0x7: {
   6024 			vol >>= 1;
   6025 			break;
   6026 		}
   6027 		case 0x8: {
   6028 			break;
   6029 		}
   6030 		case 0x9: {
   6031 			vol += 1;
   6032 			break;
   6033 		}
   6034 		case 0xA: {
   6035 			vol += 2;
   6036 			break;
   6037 		}
   6038 		case 0xB: {
   6039 			vol += 4;
   6040 			break;
   6041 		}
   6042 		case 0xC: {
   6043 			vol += 8;
   6044 			break;
   6045 		}
   6046 		case 0xD: {
   6047 			vol += 16;
   6048 			break;
   6049 		}
   6050 		case 0xE: {
   6051 			vol = (vol >> 1) + vol;
   6052 			break;
   6053 		}
   6054 		case 0xF: {
   6055 			vol += vol;
   6056 			break;
   6057 		}
   6058 		default: {
   6059 			break;
   6060 		}
   6061 	}
   6062 	vol = CLAMP(vol, 0, 64);
   6063 
   6064 	ch->real_vol = (uint8_t)vol;
   6065 	ch->out_vol = ch->real_vol;
   6066 
   6067 	if (ch->vol_column_vol >= 0x10 && ch->vol_column_vol <= 0x50) {
   6068 		ch->out_vol = ch->vol_column_vol - 0x10;
   6069 		ch->real_vol = ch->out_vol;
   6070 	} else if (ch->vol_column_vol >= 0xC0 && ch->vol_column_vol <= 0xCF) {
   6071 		ch->out_pan = (ch->vol_column_vol & 0x0F) << 4;
   6072 	}
   6073 
   6074 	trigger_note(0, 0, 0, ch);
   6075 
   6076 	(void)param;
   6077 }
   6078 
   6079 static void multi_note_retrig(channel_t *ch, uint8_t param, uint8_t volume_column_data) {
   6080 	uint8_t tmp_param;
   6081 
   6082 	tmp_param = param & 0x0F;
   6083 	if (tmp_param == 0) {
   6084 		tmp_param = ch->note_retrig_speed;
   6085 	}
   6086 
   6087 	ch->note_retrig_speed = tmp_param;
   6088 
   6089 	tmp_param = param >> 4;
   6090 	if (tmp_param == 0) {
   6091 		tmp_param = ch->note_retrig_vol;
   6092 	}
   6093 
   6094 	ch->note_retrig_vol = tmp_param;
   6095 
   6096 	if (volume_column_data == 0) {
   6097 		do_multi_note_retrig(ch, 0);
   6098 	}
   6099 }
   6100 
   6101 static void handle_effects_tick_zero(channel_t *ch) {
   6102 	uint8_t new_vol_col = ch->vol_column_vol;
   6103 
   6104 	v_jump_tab_tick_zero[ch->vol_column_vol >> 4](ch, &new_vol_col);
   6105 
   6106 	const uint8_t param = ch->efx_data;
   6107 	if (ch->efx == 0 && param == 0) {
   6108 		return;
   6109 	}
   6110 
   6111 	if (ch->efx ==  8) {
   6112 		set_pan(ch, param);
   6113 	} else if (ch->efx == 12) {
   6114 		set_vol(ch, param);
   6115 	} else if (ch->efx == 27) {
   6116 		multi_note_retrig(ch, param, new_vol_col);
   6117 	} else if (ch->efx == 33) {
   6118 		extra_fine_pitch_slide(ch, param);
   6119 	}
   6120 
   6121 	handle_more_effects_tick_zero(ch);
   6122 }
   6123 
   6124 static void prepare_portamento(channel_t *ch, const note_t *p, uint8_t inst) {
   6125 	if (p->note > 0) {
   6126 		if (p->note == NOTE_OFF) {
   6127 			key_off(ch);
   6128 		} else {
   6129 			const uint16_t note = (((p->note-1) + ch->relative_note) * 16) + (((int8_t)ch->finetune >> 3) + 16);
   6130 			if (note < MAX_NOTES) {
   6131 				ch->portamento_target_period = note2_period_lut[note];
   6132 
   6133 				if (ch->portamento_target_period == ch->real_period) {
   6134 					ch->portamento_direction = 0;
   6135 				} else if (ch->portamento_target_period > ch->real_period) {
   6136 					ch->portamento_direction = 1;
   6137 				} else {
   6138 					ch->portamento_direction = 2;
   6139 				}
   6140 			}
   6141 		}
   6142 	}
   6143 
   6144 	if (inst > 0) {
   6145 		reset_volumes(ch);
   6146 		if (p->note != NOTE_OFF) {
   6147 			trigger_instrument(ch);
   6148 		}
   6149 	}
   6150 }
   6151 
   6152 static void get_new_note(channel_t *ch, const note_t *p) {
   6153 	ch->vol_column_vol = p->vol;
   6154 
   6155 	if (ch->efx == 0) {
   6156 		if (ch->efx_data > 0) {
   6157 			ch->out_period = ch->real_period;
   6158 			ch->status |= CF_UPDATE_PERIOD;
   6159 		}
   6160 	} else {
   6161 		if ((ch->efx == 4 || ch->efx == 6) && (p->efx != 4 && p->efx != 6)) {
   6162 			ch->out_period = ch->real_period;
   6163 			ch->status |= CF_UPDATE_PERIOD;
   6164 		}
   6165 	}
   6166 
   6167 	ch->efx = p->efx;
   6168 	ch->efx_data = p->efx_data;
   6169 	ch->copy_of_instr_and_note = (p->instr << 8) | p->note;
   6170 
   6171 	if (ch->channel_off) {
   6172 		handle_more_effects_tick_zero(ch);
   6173 		return;
   6174 	}
   6175 
   6176 	uint8_t inst = p->instr;
   6177 	if (inst > 0) {
   6178 		if (inst <= MAX_INST) {
   6179 			ch->instr_num = inst;
   6180 		} else {
   6181 			inst = 0;
   6182 		}
   6183 	}
   6184 
   6185 	if (p->efx == 0x0E && p->efx_data >= 0xD1 && p->efx_data <= 0xDF) {
   6186 		return;
   6187 	}
   6188 
   6189 	if (p->efx != 0x0E || p->efx_data != 0x90) {
   6190 		if ((ch->vol_column_vol & 0xF0) == 0xF0) {
   6191 			const uint8_t param = ch->vol_column_vol & 0x0F;
   6192 			if (param > 0) {
   6193 				ch->portamento_speed = (param << 4) * 4;
   6194 			}
   6195 
   6196 			prepare_portamento(ch, p, inst);
   6197 			handle_effects_tick_zero(ch);
   6198 			
   6199 			return;
   6200 		}
   6201 
   6202 		if (p->efx == 3 || p->efx == 5) {
   6203 			if (p->efx != 5 && p->efx_data != 0) {
   6204 				ch->portamento_speed = p->efx_data * 4;
   6205 			}
   6206 
   6207 			prepare_portamento(ch, p, inst);
   6208 			handle_effects_tick_zero(ch);
   6209 			
   6210 			return;
   6211 		}
   6212 
   6213 		if (p->efx == 0x14 && p->efx_data == 0) {
   6214 			key_off(ch);
   6215 
   6216 			if (inst) {
   6217 				reset_volumes(ch);
   6218 			}
   6219 
   6220 			handle_effects_tick_zero(ch);
   6221 			
   6222 			return;
   6223 		}
   6224 
   6225 		if (p->note == 0) {
   6226 			if (inst > 0) {
   6227 				reset_volumes(ch);
   6228 				trigger_instrument(ch);
   6229 			}
   6230 
   6231 			handle_effects_tick_zero(ch);
   6232 			
   6233 			return;
   6234 		}
   6235 	}
   6236 
   6237 	if (p->note == NOTE_OFF) {
   6238 		key_off(ch);
   6239 	} else {
   6240 		trigger_note(p->note, p->efx, p->efx_data, ch);
   6241 	}
   6242 
   6243 	if (inst > 0) {
   6244 		reset_volumes(ch);
   6245 		if (p->note != NOTE_OFF) {
   6246 			trigger_instrument(ch);
   6247 		}
   6248 	}
   6249 
   6250 	handle_effects_tick_zero(ch);
   6251 }
   6252 
   6253 static void update_vol_pan_auto_vib(channel_t *ch) {
   6254 	bool env_interpolate_flag, env_did_interpolate;
   6255 	uint8_t env_pos;
   6256 	int16_t env_val;
   6257 	uint32_t f_vol;
   6258 
   6259 	instr_t *ins = ch->instr_ptr;
   6260 
   6261 	if (ch->key_off) {
   6262 		if (ch->fadeout_speed > ch->fadeout_vol) {
   6263 			ch->fadeout_vol = 0;
   6264 			ch->fadeout_speed = 0;
   6265 		} else {
   6266 			ch->fadeout_vol -= ch->fadeout_speed;
   6267 		}
   6268 
   6269 		ch->status |= CS_UPDATE_VOL;
   6270 	}
   6271 
   6272 	if (!ch->mute) {
   6273 		env_val = 0;
   6274 		if (ins->vol_env_flags & ENV_ENABLED) {
   6275 			env_did_interpolate = false;
   6276 			env_pos = ch->vol_env_pos;
   6277 
   6278 			ch->vol_env_tick++;
   6279 
   6280 			if (ch->vol_env_tick == ins->vol_env_points[env_pos][0]) {
   6281 				ch->vol_env_value = ((int8_t)ins->vol_env_points[env_pos][1] * 256);
   6282 
   6283 				env_pos++;
   6284 				if (ins->vol_env_flags & ENV_LOOP) {
   6285 					env_pos--;
   6286 
   6287 					if (env_pos == ins->vol_env_loop_end) {
   6288 						if (!(ins->vol_env_flags & ENV_SUSTAIN) || env_pos != ins->vol_env_sustain || !ch->key_off) {
   6289 							env_pos = ins->vol_env_loop_start;
   6290 							ch->vol_env_tick = ins->vol_env_points[env_pos][0];
   6291 							ch->vol_env_value = ((int8_t)ins->vol_env_points[env_pos][1] * 256);
   6292 						}
   6293 					}
   6294 
   6295 					env_pos++;
   6296 				}
   6297 
   6298 				if (env_pos < ins->vol_env_length) {
   6299 					env_interpolate_flag = true;
   6300 					if ((ins->vol_env_flags & ENV_SUSTAIN) && !ch->key_off) {
   6301 						if (env_pos-1 == ins->vol_env_sustain) {
   6302 							env_pos--;
   6303 							ch->vol_env_delta = 0;
   6304 							env_interpolate_flag = false;
   6305 						}
   6306 					}
   6307 
   6308 					if (env_interpolate_flag) {
   6309 						ch->vol_env_pos = env_pos;
   6310 
   6311 						const int16_t x0 = ins->vol_env_points[env_pos-1][0];
   6312 						const int16_t x1 = ins->vol_env_points[env_pos-0][0];
   6313 
   6314 						const int16_t x_diff = x1 - x0;
   6315 						if (x_diff > 0) {
   6316 							const int16_t y0 = ins->vol_env_points[env_pos-1][1];
   6317 							const int16_t y1 = ins->vol_env_points[env_pos-0][1];
   6318 
   6319 							const int8_t y_diff = (int8_t)(y1 - y0);
   6320 							ch->vol_env_delta = (y_diff * 256) / x_diff;
   6321 
   6322 							env_val = ch->vol_env_value;
   6323 							env_did_interpolate = true;
   6324 						} else {
   6325 							ch->vol_env_delta = 0;
   6326 						}
   6327 					}
   6328 				} else {
   6329 					ch->vol_env_delta = 0;
   6330 				}
   6331 			}
   6332 
   6333 			if (!env_did_interpolate) {
   6334 				ch->vol_env_value += ch->vol_env_delta;
   6335 				env_val = ch->vol_env_value;
   6336 
   6337 				const uint8_t env_hi_byte = (uint8_t)(env_val >> 8);
   6338 				if (env_hi_byte > 64) {
   6339 					if (env_hi_byte <= 160) {
   6340 						env_val = 64*256;
   6341 					} else {
   6342 						env_val = 0;
   6343 					}
   6344 
   6345 					ch->vol_env_delta = 0;
   6346 				}
   6347 			}
   6348 
   6349 			uint32_t env_scaled = (uint16_t)env_val;
   6350 			if (env_scaled > 16384) {
   6351 				env_scaled = 16384;
   6352 			}
   6353 
   6354 			const uint32_t vol = song.global_volume * ch->out_vol * ch->fadeout_vol;
   6355 			f_vol = ((vol >> 12) * env_scaled) >> 15;
   6356 
   6357 			ch->status |= CS_UPDATE_VOL;
   6358 		} else {
   6359 			const uint32_t vol = song.global_volume * ch->out_vol * ch->fadeout_vol;
   6360 			f_vol = vol >> 13;
   6361 		}
   6362 
   6363 		if (f_vol > 16384) {
   6364 			f_vol = 16384;
   6365 		}
   6366 
   6367 		ch->final_vol = (uint16_t)f_vol;
   6368 	} else {
   6369 		ch->final_vol = 0;
   6370 	}
   6371 
   6372 	env_val = 0;
   6373 	if (ins->pan_env_flags & ENV_ENABLED) {
   6374 		env_did_interpolate = false;
   6375 		env_pos = ch->pan_env_pos;
   6376 
   6377 		ch->pan_env_tick++;
   6378 
   6379 		if (ch->pan_env_tick == ins->pan_env_points[env_pos][0]) {
   6380 			ch->pan_env_value = ((int8_t)ins->pan_env_points[env_pos][1] * 256);
   6381 
   6382 			env_pos++;
   6383 			if (ins->pan_env_flags & ENV_LOOP) {
   6384 				env_pos--;
   6385 
   6386 				if (env_pos == ins->pan_env_loop_end) {
   6387 					if (!(ins->pan_env_flags & ENV_SUSTAIN) || env_pos != ins->pan_env_sustain || !ch->key_off) {
   6388 						env_pos = ins->pan_env_loop_start;
   6389 
   6390 						ch->pan_env_tick = ins->pan_env_points[env_pos][0];
   6391 						ch->pan_env_value = ((int8_t)ins->pan_env_points[env_pos][1] * 256);
   6392 					}
   6393 				}
   6394 
   6395 				env_pos++;
   6396 			}
   6397 
   6398 			if (env_pos < ins->pan_env_length) {
   6399 				env_interpolate_flag = true;
   6400 				if ((ins->pan_env_flags & ENV_SUSTAIN) && !ch->key_off) {
   6401 					if (env_pos-1 == ins->pan_env_sustain) {
   6402 						env_pos--;
   6403 						ch->pan_env_delta = 0;
   6404 						env_interpolate_flag = false;
   6405 					}
   6406 				}
   6407 
   6408 				if (env_interpolate_flag) {
   6409 					ch->pan_env_pos = env_pos;
   6410 
   6411 					const int16_t x0 = ins->pan_env_points[env_pos-1][0];
   6412 					const int16_t x1 = ins->pan_env_points[env_pos-0][0];
   6413 
   6414 					const int16_t x_diff = x1 - x0;
   6415 					if (x_diff > 0) {
   6416 						const int16_t y0 = ins->pan_env_points[env_pos-1][1];
   6417 						const int16_t y1 = ins->pan_env_points[env_pos-0][1];
   6418 
   6419 						const int8_t y_diff = (int8_t)(y1 - y0);
   6420 						ch->pan_env_delta = (y_diff * 256) / x_diff;
   6421 
   6422 						env_val = ch->pan_env_value;
   6423 						env_did_interpolate = true;
   6424 					} else {
   6425 						ch->pan_env_delta = 0;
   6426 					}
   6427 				}
   6428 			} else {
   6429 				ch->pan_env_delta = 0;
   6430 			}
   6431 		}
   6432 
   6433 		if (!env_did_interpolate) {
   6434 			ch->pan_env_value += ch->pan_env_delta;
   6435 			env_val = ch->pan_env_value;
   6436 
   6437 			const uint8_t env_hi_byte = (uint8_t)(env_val >> 8);
   6438 			if (env_hi_byte > 64) {
   6439 				if (env_hi_byte <= 160) {
   6440 					env_val = 64*256;
   6441 				} else {
   6442 					env_val = 0;
   6443 				}
   6444 
   6445 				ch->pan_env_delta = 0;
   6446 			}
   6447 		}
   6448 
   6449 		int16_t pan_mul = ch->out_pan - 128;
   6450 		if (pan_mul >= 0) {
   6451 			pan_mul = 0 - pan_mul;
   6452 		}
   6453 		pan_mul += 128;
   6454 		pan_mul <<= 3;
   6455 
   6456 		env_val -= 32*256;
   6457 		const int8_t pan_add = (int8_t)((env_val * pan_mul) >> 16);
   6458 
   6459 		ch->final_pan = (uint8_t)(ch->out_pan + pan_add);
   6460 		ch->status |= CS_UPDATE_PAN;
   6461 	} else {
   6462 		ch->final_pan = ch->out_pan;
   6463 	}
   6464 
   6465 	if (ins->auto_vib_depth > 0) {
   6466 		uint16_t auto_vib_amp;
   6467 
   6468 		if (ch->auto_vib_sweep > 0) {
   6469 			auto_vib_amp = ch->auto_vib_sweep;
   6470 			if (!ch->key_off) {
   6471 				auto_vib_amp += ch->auto_vib_amp;
   6472 				if ((auto_vib_amp >> 8) > ins->auto_vib_depth) {
   6473 					auto_vib_amp = ins->auto_vib_depth << 8;
   6474 					ch->auto_vib_sweep = 0;
   6475 				}
   6476 
   6477 				ch->auto_vib_amp = auto_vib_amp;
   6478 			}
   6479 		} else {
   6480 			auto_vib_amp = ch->auto_vib_amp;
   6481 		}
   6482 
   6483 		ch->auto_vib_pos += ins->auto_vib_rate;
   6484 
   6485 		int16_t auto_vib_val;
   6486 		if (ins->auto_vib_type == 1) {
   6487 			auto_vib_val = (ch->auto_vib_pos > 127) ? 64 : -64;
   6488 		} else if (ins->auto_vib_type == 2) {
   6489 			auto_vib_val = (((ch->auto_vib_pos >> 1) + 64) & 127) - 64;
   6490 		} else if (ins->auto_vib_type == 3) {
   6491 			auto_vib_val = ((-(ch->auto_vib_pos >> 1) + 64) & 127) - 64;
   6492 		} else {
   6493 			auto_vib_val = auto_vib_sine_tab[ch->auto_vib_pos];
   6494 		}
   6495 
   6496 		auto_vib_val = (auto_vib_val * (int16_t)auto_vib_amp) >> (6+8);
   6497 
   6498 		uint16_t tmp_period = ch->out_period + auto_vib_val;
   6499 		if (tmp_period >= 32000) {
   6500 			tmp_period = 0;
   6501 		}
   6502 
   6503 		ch->final_period = tmp_period;
   6504 		ch->status |= CF_UPDATE_PERIOD;
   6505 	} else {
   6506 		ch->final_period = ch->out_period;
   6507 	}
   6508 }
   6509 
   6510 static uint16_t period2_note_period(uint16_t period, uint8_t output_note_offset, channel_t *ch) {
   6511 	int32_t tmp_period;
   6512 
   6513 	const int32_t fine_tune = ((int8_t)ch->finetune >> 3) + 16;
   6514 
   6515 	int32_t hi_period = 8*12*16;
   6516 
   6517 	int32_t lo_period = 0;
   6518 
   6519 	for (int32_t i = 0; i < 8; i++) {
   6520 		tmp_period = (((lo_period + hi_period) >> 1) & ~15) + fine_tune;
   6521 
   6522 		int32_t look_up = tmp_period - 8;
   6523 		if (look_up < 0) {
   6524 			look_up = 0;
   6525 		}
   6526 
   6527 		if (period >= note2_period_lut[look_up]) {
   6528 			hi_period = (tmp_period - fine_tune) & ~15;
   6529 		} else {
   6530 			lo_period = (tmp_period - fine_tune) & ~15;
   6531 		}
   6532 	}
   6533 
   6534 	tmp_period = lo_period + fine_tune + (output_note_offset << 4);
   6535 	if (tmp_period >= (8*12*16+15)-1) {
   6536 		tmp_period = (8*12*16+16)-1;
   6537 	}
   6538 
   6539 	return note2_period_lut[tmp_period];
   6540 }
   6541 
   6542 static void do_vibrato(channel_t *ch) {
   6543 	uint8_t tmp_vib = (ch->vibrato_pos >> 2) & 0x1F;
   6544 
   6545 	switch (ch->vib_trem_ctrl & 3) {
   6546 		case 0: {
   6547 			tmp_vib = vibrato_tab[tmp_vib];
   6548 			break;
   6549 		}
   6550 
   6551 		case 1: {
   6552 			tmp_vib <<= 3;
   6553 			if ((int8_t)ch->vibrato_pos < 0) {
   6554 				tmp_vib = ~tmp_vib;
   6555 			}
   6556 		}
   6557 		break;
   6558 
   6559 		default: {
   6560 			tmp_vib = 255;
   6561 			break;
   6562 		}
   6563 	}
   6564 
   6565 	tmp_vib = (tmp_vib * ch->vibrato_depth) >> 5;
   6566 
   6567 	if ((int8_t)ch->vibrato_pos < 0) {
   6568 		ch->out_period = ch->real_period - tmp_vib;
   6569 	} else {
   6570 		ch->out_period = ch->real_period + tmp_vib;
   6571 	}
   6572 
   6573 	ch->status |= CF_UPDATE_PERIOD;
   6574 	ch->vibrato_pos += ch->vibrato_speed;
   6575 }
   6576 
   6577 static void arpeggio(channel_t *ch, uint8_t param) {
   6578 	uint8_t note_offset;
   6579 
   6580 	const uint8_t tick = arpeggio_tab[song.tick & 31];
   6581 	if (tick == 0) {
   6582 		ch->out_period = ch->real_period;
   6583 	} else {
   6584 		if (tick == 1) {
   6585 			note_offset = param >> 4;
   6586 		} else {
   6587 			note_offset = param & 0x0F;
   6588 		}
   6589 
   6590 		ch->out_period = period2_note_period(ch->real_period, note_offset, ch);
   6591 	}
   6592 
   6593 	ch->status |= CF_UPDATE_PERIOD;
   6594 }
   6595 
   6596 static void pitch_slide_up(channel_t *ch, uint8_t param) {
   6597 	if (param == 0) {
   6598 		param = ch->pitch_slide_up_speed;
   6599 	}
   6600 
   6601 	ch->pitch_slide_up_speed = param;
   6602 
   6603 	ch->real_period -= param * 4;
   6604 	if ((int16_t)ch->real_period < 1) {
   6605 		ch->real_period = 1;
   6606 	}
   6607 
   6608 	ch->out_period = ch->real_period;
   6609 	ch->status |= CF_UPDATE_PERIOD;
   6610 }
   6611 
   6612 static void pitch_slide_down(channel_t *ch, uint8_t param) {
   6613 	if (param == 0) {
   6614 		param = ch->pitch_slide_down_speed;
   6615 	}
   6616 
   6617 	ch->pitch_slide_down_speed = param;
   6618 
   6619 	ch->real_period += param * 4;
   6620 	if ((int16_t)ch->real_period >= 32000) {
   6621 		ch->real_period = 32000-1;
   6622 	}
   6623 
   6624 	ch->out_period = ch->real_period;
   6625 	ch->status |= CF_UPDATE_PERIOD;
   6626 }
   6627 
   6628 static void portamento(channel_t *ch, uint8_t param) {
   6629 	if (ch->portamento_direction == 0) {
   6630 		return;
   6631 	}
   6632 
   6633 	if (ch->portamento_direction > 1) {
   6634 		ch->real_period -= ch->portamento_speed;
   6635 		if ((int16_t)ch->real_period <= (int16_t)ch->portamento_target_period) {
   6636 			ch->portamento_direction = 1;
   6637 			ch->real_period = ch->portamento_target_period;
   6638 		}
   6639 	} else {
   6640 		ch->real_period += ch->portamento_speed;
   6641 		if (ch->real_period >= ch->portamento_target_period) {
   6642 			ch->portamento_direction = 1;
   6643 			ch->real_period = ch->portamento_target_period;
   6644 		}
   6645 	}
   6646 
   6647 	if (ch->semitone_porta_mode) {
   6648 		ch->out_period = period2_note_period(ch->real_period, 0, ch);
   6649 	} else {
   6650 		ch->out_period = ch->real_period;
   6651 	}
   6652 
   6653 	ch->status |= CF_UPDATE_PERIOD;
   6654 
   6655 	(void)param;
   6656 }
   6657 
   6658 static void vibrato(channel_t *ch, uint8_t param) {
   6659 	if (param > 0) {
   6660 		const uint8_t vibrato_depth = param & 0x0F;
   6661 		if (vibrato_depth > 0) {
   6662 			ch->vibrato_depth = vibrato_depth;
   6663 		}
   6664 
   6665 		const uint8_t vibrato_speed = (param & 0xF0) >> 2;
   6666 		if (vibrato_speed > 0) {
   6667 			ch->vibrato_speed = vibrato_speed;
   6668 		}
   6669 	}
   6670 
   6671 	do_vibrato(ch);
   6672 }
   6673 
   6674 static void portamento_plus_vol_slide(channel_t *ch, uint8_t param) {
   6675 	portamento(ch, 0);
   6676 	vol_slide(ch, param);
   6677 
   6678 	(void)param;
   6679 }
   6680 
   6681 static void vibrato_plus_vol_slide(channel_t *ch, uint8_t param) {
   6682 	do_vibrato(ch);
   6683 	vol_slide(ch, param);
   6684 
   6685 	(void)param;
   6686 }
   6687 
   6688 static void tremolo(channel_t *ch, uint8_t param) {
   6689 	int16_t trem_vol;
   6690 
   6691 	if (param > 0) {
   6692 		uint8_t tremolo_depth = param & 0x0F;
   6693 		if (tremolo_depth > 0) {
   6694 			ch->tremolo_depth = tremolo_depth;
   6695 		}
   6696 
   6697 		uint8_t tremolo_speed = (param & 0xF0) >> 2;
   6698 		if (tremolo_speed > 0) {
   6699 			ch->tremolo_speed = tremolo_speed;
   6700 		}
   6701 	}
   6702 
   6703 	uint8_t tmp_trem = (ch->tremolo_pos >> 2) & 0x1F;
   6704 	switch ((ch->vib_trem_ctrl >> 4) & 3) {
   6705 		case 0: {
   6706 			tmp_trem = vibrato_tab[tmp_trem];
   6707 			break;
   6708 		}
   6709 		case 1: {
   6710 			tmp_trem <<= 3;
   6711 			if ((int8_t)ch->vibrato_pos < 0) {
   6712 				tmp_trem = ~tmp_trem;
   6713 			}
   6714 			break;
   6715 		}
   6716 		default: {
   6717 			tmp_trem = 255;
   6718 			break;
   6719 		}
   6720 	}
   6721 	tmp_trem = (tmp_trem * ch->tremolo_depth) >> 6;
   6722 
   6723 	if ((int8_t)ch->tremolo_pos < 0) {
   6724 		trem_vol = ch->real_vol - tmp_trem;
   6725 		if (trem_vol < 0) {
   6726 			trem_vol = 0;
   6727 		}
   6728 	} else {
   6729 		trem_vol = ch->real_vol + tmp_trem;
   6730 		if (trem_vol > 64) {
   6731 			trem_vol = 64;
   6732 		}
   6733 	}
   6734 
   6735 	ch->out_vol = (uint8_t)trem_vol;
   6736 	ch->status |= CS_UPDATE_VOL;
   6737 
   6738 	ch->tremolo_pos += ch->tremolo_speed;
   6739 }
   6740 
   6741 static void vol_slide(channel_t *ch, uint8_t param) {
   6742 	if (param == 0) {
   6743 		param = ch->vol_slide_speed;
   6744 	}
   6745 
   6746 	ch->vol_slide_speed = param;
   6747 
   6748 	uint8_t new_vol = ch->real_vol;
   6749 	if ((param & 0xF0) == 0) {
   6750 		new_vol -= param;
   6751 		if ((int8_t)new_vol < 0) {
   6752 			new_vol = 0;
   6753 		}
   6754 	} else {
   6755 		param >>= 4;
   6756 
   6757 		new_vol += param;
   6758 		if (new_vol > 64) {
   6759 			new_vol = 64;
   6760 		}
   6761 	}
   6762 
   6763 	ch->out_vol = ch->real_vol = new_vol;
   6764 	ch->status |= CS_UPDATE_VOL;
   6765 }
   6766 
   6767 static void global_vol_slide(channel_t *ch, uint8_t param) {
   6768 	if (param == 0) {
   6769 		param = ch->glob_vol_slide_speed;
   6770 	}
   6771 
   6772 	ch->glob_vol_slide_speed = param;
   6773 
   6774 	uint8_t new_vol = (uint8_t)song.global_volume;
   6775 	if ((param & 0xF0) == 0) {
   6776 		new_vol -= param;
   6777 		if ((int8_t)new_vol < 0) {
   6778 			new_vol = 0;
   6779 		}
   6780 	} else {
   6781 		param >>= 4;
   6782 
   6783 		new_vol += param;
   6784 		if (new_vol > 64) {
   6785 			new_vol = 64;
   6786 		}
   6787 	}
   6788 
   6789 	song.global_volume = new_vol;
   6790 
   6791 	channel_t *c = channel;
   6792 	for (int32_t i = 0; i < song.num_channels; i++, c++) {
   6793 		c->status |= CS_UPDATE_VOL;
   6794 	}
   6795 }
   6796 
   6797 static void key_off_cmd(channel_t *ch, uint8_t param) {
   6798 	if ((uint8_t)(song.speed-song.tick) == (param & 31)) {
   6799 		key_off(ch);
   6800 	}
   6801 }
   6802 
   6803 static void panning_slide(channel_t *ch, uint8_t param) {
   6804 	if (param == 0) {
   6805 		param = ch->panning_slide_speed;
   6806 	}
   6807 
   6808 	ch->panning_slide_speed = param;
   6809 
   6810 	int16_t new_pan = (int16_t)ch->out_pan;
   6811 	if ((param & 0xF0) == 0) {
   6812 		new_pan -= param;
   6813 		if (new_pan < 0) {
   6814 			new_pan = 0;
   6815 		}
   6816 	} else {
   6817 		param >>= 4;
   6818 
   6819 		new_pan += param;
   6820 		if (new_pan > 255) {
   6821 			new_pan = 255;
   6822 		}
   6823 	}
   6824 
   6825 	ch->out_pan = (uint8_t)new_pan;
   6826 	ch->status |= CS_UPDATE_PAN;
   6827 }
   6828 
   6829 static void tremor(channel_t *ch, uint8_t param) {
   6830 	if (param == 0) {
   6831 		param = ch->tremor_param;
   6832 	}
   6833 
   6834 	ch->tremor_param = param;
   6835 
   6836 	uint8_t tremor_sign = ch->tremor_pos & 0x80;
   6837 	uint8_t tremor_data = ch->tremor_pos & 0x7F;
   6838 
   6839 	tremor_data--;
   6840 	if ((int8_t)tremor_data < 0) {
   6841 		if (tremor_sign == 0x80) {
   6842 			tremor_sign = 0x00;
   6843 			tremor_data = param & 0x0F;
   6844 		} else {
   6845 			tremor_sign = 0x80;
   6846 			tremor_data = param >> 4;
   6847 		}
   6848 	}
   6849 
   6850 	ch->tremor_pos = tremor_sign | tremor_data;
   6851 	ch->out_vol = (tremor_sign == 0x80) ? ch->real_vol : 0;
   6852 	ch->status |= CS_UPDATE_VOL + CS_USE_QUICK_VOLRAMP;
   6853 }
   6854 
   6855 static void retrig_note(channel_t *ch, uint8_t param) {
   6856 	if (param == 0) {
   6857 		return;
   6858 	}
   6859 
   6860 	if ((song.speed-song.tick) % param == 0) {
   6861 		trigger_note(0, 0, 0, ch);
   6862 		trigger_instrument(ch);
   6863 	}
   6864 }
   6865 
   6866 static void note_cut(channel_t *ch, uint8_t param) {
   6867 	if ((uint8_t)(song.speed-song.tick) == param) {
   6868 		ch->out_vol = ch->real_vol = 0;
   6869 		ch->status |= CS_UPDATE_VOL + CS_USE_QUICK_VOLRAMP;
   6870 	}
   6871 }
   6872 
   6873 static void note_delay(channel_t *ch, uint8_t param) {
   6874 	if ((uint8_t)(song.speed-song.tick) == param) {
   6875 		const uint8_t note = ch->copy_of_instr_and_note & 0x00FF;
   6876 		trigger_note(note, 0, 0, ch);
   6877 
   6878 		const uint8_t instrument = ch->copy_of_instr_and_note >> 8;
   6879 		if (instrument > 0) {
   6880 			reset_volumes(ch);
   6881 		}
   6882 
   6883 		trigger_instrument(ch);
   6884 
   6885 		if (ch->vol_column_vol >= 0x10 && ch->vol_column_vol <= 0x50) {
   6886 			ch->out_vol = ch->vol_column_vol - 16;
   6887 			ch->real_vol = ch->out_vol;
   6888 		} else if (ch->vol_column_vol >= 0xC0 && ch->vol_column_vol <= 0xCF) {
   6889 			ch->out_pan = (ch->vol_column_vol & 0x0F) << 4;
   6890 		}
   6891 	}
   6892 }
   6893 
   6894 static const efx_routine e_jump_tab_tick_non_zero[16] = {
   6895 	dummy,
   6896 	dummy,
   6897 	dummy,
   6898 	dummy,
   6899 	dummy,
   6900 	dummy,
   6901 	dummy,
   6902 	dummy,
   6903 	dummy,
   6904 	retrig_note,
   6905 	dummy,
   6906 	dummy,
   6907 	note_cut,
   6908 	note_delay,
   6909 	dummy,
   6910 	dummy
   6911 };
   6912 
   6913 static void e_effects_tick_non_zero(channel_t *ch, uint8_t param) {
   6914 	e_jump_tab_tick_non_zero[param >> 4](ch, param & 0xF);
   6915 }
   6916 
   6917 static const efx_routine jump_tab_tick_non_zero[36] = {
   6918 	arpeggio,
   6919 	pitch_slide_up,
   6920 	pitch_slide_down,
   6921 	portamento,
   6922 	vibrato,
   6923 	portamento_plus_vol_slide,
   6924 	vibrato_plus_vol_slide,
   6925 	tremolo,
   6926 	dummy,
   6927 	dummy,
   6928 	vol_slide,
   6929 	dummy,
   6930 	dummy,
   6931 	dummy,
   6932 	e_effects_tick_non_zero,
   6933 	dummy,
   6934 	dummy,
   6935 	global_vol_slide,
   6936 	dummy,
   6937 	dummy,
   6938 	key_off_cmd,
   6939 	dummy,
   6940 	dummy,
   6941 	dummy,
   6942 	dummy,
   6943 	panning_slide,
   6944 	dummy,
   6945 	do_multi_note_retrig,
   6946 	dummy,
   6947 	tremor,
   6948 	dummy,
   6949 	dummy,
   6950 	dummy,
   6951 	dummy,
   6952 	dummy,
   6953 	dummy
   6954 };
   6955 
   6956 static void handle_effects_tick_non_zero(channel_t *ch) {
   6957 	if (ch->channel_off) {
   6958 		return;
   6959 	}
   6960 
   6961 	v_jump_tab_tick_non_zero[ch->vol_column_vol >> 4](ch);
   6962 
   6963 	if ((ch->efx == 0 && ch->efx_data == 0) || ch->efx > 35) {
   6964 		return;
   6965 	}
   6966 
   6967 	jump_tab_tick_non_zero[ch->efx](ch, ch->efx_data);
   6968 }
   6969 
   6970 static void get_next_pos(void) {
   6971 	if (song.tick != 1) {
   6972 		return;
   6973 	}
   6974 
   6975 	song.row++;
   6976 
   6977 	if (song.patt_del_time > 0) {
   6978 		song.patt_del_time2 = song.patt_del_time;
   6979 		song.patt_del_time = 0;
   6980 	}
   6981 
   6982 	if (song.patt_del_time2 > 0) {
   6983 		song.patt_del_time2--;
   6984 		if (song.patt_del_time2 > 0) {
   6985 			song.row--;
   6986 		}
   6987 	}
   6988 
   6989 	if (song.p_break_flag) {
   6990 		song.p_break_flag = false;
   6991 		song.row = song.p_break_pos;
   6992 	}
   6993 
   6994 	if (song.row >= song.curr_num_rows || song.pos_jump_flag) {
   6995 		song.row = song.p_break_pos;
   6996 		song.p_break_pos = 0;
   6997 		song.pos_jump_flag = false;
   6998 
   6999 		if (bxx_overflow) {
   7000 			song.song_pos = 0;
   7001 			bxx_overflow = false;
   7002 		} else if (++song.song_pos >= song.song_length) {
   7003 			song.song_pos = song.song_loop_start;
   7004 		}
   7005 
   7006 		song.patt_num = song.orders[song.song_pos & 0xFF];
   7007 		song.curr_num_rows = pattern_num_rows[song.patt_num & 0xFF];
   7008 
   7009 		if (song.row >= song.curr_num_rows) {
   7010 			song.row = 0;
   7011 		}
   7012 	}
   7013 }
   7014 
   7015 static void tick_replayer(void) {
   7016 	channel_t *ch;
   7017 
   7018 	bool tick_zero = false;
   7019 	if (--song.tick == 0) {
   7020 		song.tick = song.speed;
   7021 		tick_zero = true;
   7022 	}
   7023 
   7024 	const bool read_new_note = tick_zero && (song.patt_del_time2 == 0);
   7025 	if (read_new_note) {
   7026 		const note_t *p = nil_pattern_line;
   7027 		if (pattern[song.patt_num] != NULL) {
   7028 			p = &pattern[song.patt_num][song.row * song.num_channels];
   7029 		}
   7030 
   7031 		ch = channel;
   7032 		for (int32_t i = 0; i < song.num_channels; i++, ch++, p++) {
   7033 			get_new_note(ch, p);
   7034 			update_vol_pan_auto_vib(ch);
   7035 		}
   7036 	} else {
   7037 		ch = channel;
   7038 		for (int32_t i = 0; i < song.num_channels; i++, ch++) {
   7039 			handle_effects_tick_non_zero(ch);
   7040 			update_vol_pan_auto_vib(ch);
   7041 		}
   7042 	}
   7043 
   7044 	get_next_pos();
   7045 }
   7046 
   7047 static uint32_t period2_voice_delta(uint32_t period) {
   7048 	uint32_t delta;
   7049 
   7050 	period &= 0xFFFF;
   7051 
   7052 	if (period == 0) {
   7053 		return 0;
   7054 	}
   7055 
   7056 	if (linear_periods_flag) {
   7057 		const uint32_t inv_period = ((12 * 192 * 4) - period) & 0xFFFF;
   7058 
   7059 		const uint32_t quotient = inv_period / (12 * 16 * 4);
   7060 		const uint32_t remainder = inv_period % (12 * 16 * 4);
   7061 		const uint8_t shift_value = (14 - quotient) & 31;
   7062 
   7063 		delta = (uint32_t)(((uint64_t)log_tab[remainder] * FREQUENCY_MUL_FACTOR) >> 24);
   7064 		delta >>= shift_value;
   7065 	} else {
   7066 		delta = FREQUENCY_DIV_FACTOR / period;
   7067 	}
   7068 
   7069 	if (delta > MAX_VOICE_DELTA) {
   7070 		delta = MAX_VOICE_DELTA;
   7071 	}
   7072 
   7073 	return delta;
   7074 }
   7075 
   7076 static uint32_t integer_sqrt(uint64_t n) {
   7077 	uint64_t result = 0;
   7078 	uint64_t bit = (uint64_t)1 << 62;
   7079 
   7080 	while (bit > n) {
   7081 		bit >>= 2;
   7082 	}
   7083 
   7084 	while (bit != 0) {
   7085 		if (n >= result + bit) {
   7086 			n -= result + bit;
   7087 			result = (result >> 1) + bit;
   7088 		} else {
   7089 			result >>= 1;
   7090 		}
   7091 
   7092 		bit >>= 2;
   7093 	}
   7094 
   7095 	return (uint32_t)result;
   7096 }
   7097 
   7098 static void build_panning_table(void) {
   7099 	if (panning_table_ready) {
   7100 		return;
   7101 	}
   7102 
   7103 	for (uint32_t i = 0; i <= 256; i++) {
   7104 		panning_table[i] = (integer_sqrt((uint64_t)i << 26) + 1) >> 1;
   7105 	}
   7106 
   7107 	panning_table_ready = true;
   7108 }
   7109 
   7110 static void clear_voice(voice_t *v) {
   7111 	*v = (voice_t){ 0 };
   7112 	v->panning = 128;
   7113 }
   7114 
   7115 static void stop_voice(int32_t i) {
   7116 	clear_voice(&voice[i]);
   7117 	clear_voice(&voice[MAX_CHANNELS + i]);
   7118 }
   7119 
   7120 static void reset_channels(void) {
   7121 	for (int32_t i = 0; i < MAX_CHANNELS; i++) {
   7122 		channel_t *ch = &channel[i];
   7123 
   7124 		*ch = (channel_t){ 0 };
   7125 		ch->instr_ptr = instr[0];
   7126 		ch->status = CS_UPDATE_VOL;
   7127 		ch->old_pan = 128;
   7128 		ch->out_pan = 128;
   7129 		ch->final_pan = 128;
   7130 	}
   7131 }
   7132 
   7133 static void stop_voices(void) {
   7134 	channel_t *ch = channel;
   7135 	for (int32_t i = 0; i < MAX_CHANNELS; i++, ch++) {
   7136 		ch->copy_of_instr_and_note = 0;
   7137 		ch->relative_note = 0;
   7138 		ch->smp_num = 0;
   7139 		ch->smp_ptr = NULL;
   7140 		ch->instr_num = 0;
   7141 		ch->instr_ptr = instr[0];
   7142 		ch->status = CS_UPDATE_VOL;
   7143 		ch->real_vol = ch->out_vol = ch->old_vol = 0;
   7144 		ch->final_vol = 0;
   7145 		ch->old_pan = ch->out_pan = ch->final_pan = 128;
   7146 		ch->vibrato_depth = 0;
   7147 		ch->portamento_direction = 0;
   7148 
   7149 		stop_voice(i);
   7150 	}
   7151 }
   7152 
   7153 static void reset_replayer_state(void) {
   7154 	song.patt_del_time = song.patt_del_time2 = 0;
   7155 	song.pos_jump_flag = false;
   7156 	song.p_break_pos = 0;
   7157 	song.p_break_flag = false;
   7158 	song.global_volume = 64;
   7159 
   7160 	channel_t *ch = channel;
   7161 	for (int32_t i = 0; i < song.num_channels; i++, ch++) {
   7162 		ch->pattern_loop_start_row = 0;
   7163 		ch->pattern_loop_counter = 0;
   7164 		ch->status |= CS_UPDATE_VOL;
   7165 	}
   7166 }
   7167 
   7168 static void set_song_pos(int16_t song_pos, int16_t row) {
   7169 	song.song_pos = song_pos;
   7170 	if (song.song_length > 0 && song.song_pos >= song.song_length) {
   7171 		song.song_pos = song.song_length - 1;
   7172 	}
   7173 
   7174 	song.patt_num = song.orders[song.song_pos];
   7175 	song.curr_num_rows = pattern_num_rows[song.patt_num];
   7176 
   7177 	song.row = row;
   7178 	if (song.row >= song.curr_num_rows) {
   7179 		song.row = song.curr_num_rows - 1;
   7180 	}
   7181 
   7182 	song.tick = 1;
   7183 }
   7184 
   7185 static void voice_update_volumes(int32_t i, uint8_t status) {
   7186 	voice_t *v = &voice[i];
   7187 
   7188 	v->target_volume_l = (int32_t)(((uint32_t)v->volume * panning_table[256 - v->panning]) >> 16) << 8;
   7189 	v->target_volume_r = (int32_t)(((uint32_t)v->volume * panning_table[v->panning]) >> 16) << 8;
   7190 
   7191 	const bool voice_trigger_flag = !!(status & CS_TRIGGER_VOICE);
   7192 	if (voice_trigger_flag) {
   7193 		if (v->curr_volume_l > 0 || v->curr_volume_r > 0) {
   7194 			voice_t *f = &voice[MAX_CHANNELS + i];
   7195 
   7196 			*f = *v;
   7197 
   7198 			f->target_volume_l = 0;
   7199 			f->target_volume_r = 0;
   7200 			f->volume_ramp_length = QUICK_VOLRAMP_SAMPLES;
   7201 			f->volume_delta_l = (0 - f->curr_volume_l) / (int32_t)QUICK_VOLRAMP_SAMPLES;
   7202 			f->volume_delta_r = (0 - f->curr_volume_r) / (int32_t)QUICK_VOLRAMP_SAMPLES;
   7203 			f->is_fade_out_voice = true;
   7204 		}
   7205 
   7206 		v->curr_volume_l = v->curr_volume_r = 0;
   7207 	}
   7208 
   7209 	if (!voice_trigger_flag && v->target_volume_l == v->curr_volume_l && v->target_volume_r == v->curr_volume_r) {
   7210 		v->volume_ramp_length = 0;
   7211 	} else {
   7212 		const int32_t volume_l_diff = v->target_volume_l - v->curr_volume_l;
   7213 		const int32_t volume_r_diff = v->target_volume_r - v->curr_volume_r;
   7214 
   7215 		if (status & CS_USE_QUICK_VOLRAMP) {
   7216 			v->volume_ramp_length = QUICK_VOLRAMP_SAMPLES;
   7217 		} else {
   7218 			v->volume_ramp_length = samples_per_tick;
   7219 		}
   7220 
   7221 		v->volume_delta_l = volume_l_diff / (int32_t)v->volume_ramp_length;
   7222 		v->volume_delta_r = volume_r_diff / (int32_t)v->volume_ramp_length;
   7223 	}
   7224 }
   7225 
   7226 static void voice_trigger(int32_t ch, sample_t *s, int32_t position) {
   7227 	voice_t *v = &voice[ch];
   7228 
   7229 	const int32_t length = s->length;
   7230 	const int32_t loop_start = s->loop_start;
   7231 	const int32_t loop_length = s->loop_length;
   7232 	const int32_t loop_end = s->loop_start + s->loop_length;
   7233 	uint8_t loop_type = GET_LOOPTYPE(s->flags);
   7234 
   7235 	if (s->data_ptr == NULL || length < 1) {
   7236 		v->active = false;
   7237 		return;
   7238 	}
   7239 
   7240 	if (loop_length < 1) {
   7241 		loop_type = LOOP_DISABLED;
   7242 	}
   7243 
   7244 	v->data = s->data_ptr;
   7245 	v->is_16bit = !!(s->flags & SAMPLE_16BIT);
   7246 	v->sampling_backwards = false;
   7247 	v->loop_type = loop_type;
   7248 	v->sample_end = (loop_type == LOOP_DISABLED) ? (uint32_t)length : (uint32_t)loop_end;
   7249 	v->loop_start = (uint32_t)loop_start;
   7250 	v->loop_length = (uint32_t)loop_length;
   7251 	v->position = (uint32_t)position;
   7252 	v->position_frac = 0;
   7253 	v->is_fade_out_voice = false;
   7254 
   7255 	if (v->position >= v->sample_end) {
   7256 		v->active = false;
   7257 		return;
   7258 	}
   7259 
   7260 	v->active = true;
   7261 }
   7262 
   7263 static void update_voices(void) {
   7264 	channel_t *ch = channel;
   7265 	voice_t *v = voice;
   7266 
   7267 	for (int32_t i = 0; i < song.num_channels; i++, ch++, v++) {
   7268 		const uint8_t status = ch->status;
   7269 		if (status == 0) {
   7270 			continue;
   7271 		}
   7272 
   7273 		ch->status = 0;
   7274 
   7275 		if (status & CS_UPDATE_VOL) {
   7276 			v->volume = ch->final_vol;
   7277 		}
   7278 
   7279 		if (status & CS_UPDATE_PAN) {
   7280 			v->panning = ch->final_pan;
   7281 		}
   7282 
   7283 		if (status & (CS_UPDATE_VOL | CS_UPDATE_PAN)) {
   7284 			voice_update_volumes(i, status);
   7285 		}
   7286 
   7287 		if (status & CF_UPDATE_PERIOD) {
   7288 			v->delta = period2_voice_delta(ch->final_period);
   7289 		}
   7290 
   7291 		if (status & CS_TRIGGER_VOICE) {
   7292 			voice_trigger(i, ch->smp_ptr, ch->smp_start_pos);
   7293 		}
   7294 	}
   7295 }
   7296 
   7297 static void reset_ramp_volumes(void) {
   7298 	voice_t *v = voice;
   7299 	for (int32_t i = 0; i < song.num_channels; i++, v++) {
   7300 		v->curr_volume_l = v->target_volume_l;
   7301 		v->curr_volume_r = v->target_volume_r;
   7302 		v->volume_ramp_length = 0;
   7303 	}
   7304 }
   7305 
   7306 static void voice_wrap(voice_t *v) {
   7307 	if (v->loop_type == LOOP_DISABLED) {
   7308 		v->active = false;
   7309 		return;
   7310 	}
   7311 
   7312 	const uint32_t overflow = v->position - v->sample_end;
   7313 
   7314 	if (v->loop_type == LOOP_FORWARD) {
   7315 		if (v->loop_length >= 2) {
   7316 			v->position = v->loop_start + ((overflow < v->loop_length) ? overflow : (overflow % v->loop_length));
   7317 		} else {
   7318 			v->position = v->loop_start;
   7319 		}
   7320 	} else {
   7321 		if (v->loop_length >= 2) {
   7322 			const uint32_t cycles = (overflow < v->loop_length) ? 0 : (overflow / v->loop_length);
   7323 			const uint32_t phase = (overflow < v->loop_length) ? overflow : (overflow % v->loop_length);
   7324 
   7325 			v->position = v->loop_start + phase;
   7326 			if (!(cycles & 1)) {
   7327 				v->sampling_backwards = !v->sampling_backwards;
   7328 			}
   7329 		} else {
   7330 			v->position = v->loop_start;
   7331 		}
   7332 	}
   7333 }
   7334 
   7335 static void advance_voice(voice_t *v, uint32_t count) {
   7336 	const uint64_t advance = (uint64_t)v->delta * count + v->position_frac;
   7337 
   7338 	v->position_frac = (uint32_t)(advance & 0xFFFF);
   7339 	v->position += (uint32_t)(advance >> 16);
   7340 
   7341 	if (v->position >= v->sample_end) {
   7342 		voice_wrap(v);
   7343 	}
   7344 }
   7345 
   7346 static void fetch_samples(const voice_t *v, int32_t *s0, int32_t *s1) {
   7347 	uint32_t index0, index1;
   7348 	bool has_next = true;
   7349 
   7350 	if (!v->sampling_backwards) {
   7351 		index0 = v->position;
   7352 		index1 = index0 + 1;
   7353 
   7354 		if (index1 >= v->sample_end) {
   7355 			if (v->loop_type == LOOP_FORWARD) {
   7356 				index1 = v->loop_start;
   7357 			} else if (v->loop_type == LOOP_PINGPONG) {
   7358 				index1 = index0;
   7359 			} else {
   7360 				has_next = false;
   7361 			}
   7362 		}
   7363 	} else {
   7364 		index0 = v->loop_start + v->loop_start + v->loop_length - 1 - v->position;
   7365 		index1 = (index0 > v->loop_start) ? (index0 - 1) : index0;
   7366 	}
   7367 
   7368 	if (v->is_16bit) {
   7369 		const int16_t *data16 = (const int16_t *)v->data;
   7370 
   7371 		*s0 = data16[index0];
   7372 		*s1 = has_next ? data16[index1] : 0;
   7373 	} else {
   7374 		*s0 = (int32_t)v->data[index0] * 256;
   7375 		*s1 = has_next ? ((int32_t)v->data[index1] * 256) : 0;
   7376 	}
   7377 }
   7378 
   7379 static void mix_span(voice_t *v, int32_t *dl, int32_t *dr, uint32_t count, bool ramping) {
   7380 	int32_t curr_l = v->curr_volume_l;
   7381 	int32_t curr_r = v->curr_volume_r;
   7382 
   7383 	for (uint32_t i = 0; i < count; i++) {
   7384 		int32_t s0, s1;
   7385 		fetch_samples(v, &s0, &s1);
   7386 
   7387 		const int32_t sample = s0 + (((s1 - s0) * (int32_t)(v->position_frac >> 1)) >> 15);
   7388 
   7389 		dl[i] += (sample * (curr_l >> 8)) >> 14;
   7390 		dr[i] += (sample * (curr_r >> 8)) >> 14;
   7391 
   7392 		if (ramping) {
   7393 			curr_l += v->volume_delta_l;
   7394 			curr_r += v->volume_delta_r;
   7395 		}
   7396 
   7397 		v->position_frac += v->delta;
   7398 		v->position += v->position_frac >> 16;
   7399 		v->position_frac &= 0xFFFF;
   7400 
   7401 		if (v->position >= v->sample_end) {
   7402 			voice_wrap(v);
   7403 			if (!v->active) {
   7404 				break;
   7405 			}
   7406 		}
   7407 	}
   7408 
   7409 	v->curr_volume_l = curr_l;
   7410 	v->curr_volume_r = curr_r;
   7411 }
   7412 
   7413 static void mix_voice(voice_t *v, int32_t *dl, int32_t *dr, uint32_t total) {
   7414 	while (total > 0 && v->active) {
   7415 		uint32_t count = total;
   7416 		const bool ramping = v->volume_ramp_length > 0;
   7417 
   7418 		if (ramping && count > v->volume_ramp_length) {
   7419 			count = v->volume_ramp_length;
   7420 		}
   7421 
   7422 		if (!ramping && v->curr_volume_l == 0 && v->curr_volume_r == 0) {
   7423 			advance_voice(v, count);
   7424 		} else {
   7425 			mix_span(v, dl, dr, count, ramping);
   7426 		}
   7427 
   7428 		if (ramping) {
   7429 			v->volume_ramp_length -= count;
   7430 			if (v->volume_ramp_length == 0) {
   7431 				v->curr_volume_l = v->target_volume_l;
   7432 				v->curr_volume_r = v->target_volume_r;
   7433 
   7434 				if (v->is_fade_out_voice) {
   7435 					v->active = false;
   7436 				}
   7437 			}
   7438 		}
   7439 
   7440 		dl += count;
   7441 		dr += count;
   7442 		total -= count;
   7443 	}
   7444 }
   7445 
   7446 static void render_chunk(uint32_t count) {
   7447 	for (uint32_t i = 0; i < count; i++) {
   7448 		mix_l[i] = mix_r[i] = 0;
   7449 	}
   7450 
   7451 	for (int32_t i = 0; i < song.num_channels; i++) {
   7452 		if (voice[i].active) {
   7453 			mix_voice(&voice[i], mix_l, mix_r, count);
   7454 		}
   7455 
   7456 		if (voice[MAX_CHANNELS + i].active) {
   7457 			mix_voice(&voice[MAX_CHANNELS + i], mix_l, mix_r, count);
   7458 		}
   7459 	}
   7460 }
   7461 
   7462 static uint32_t next_random(void) {
   7463 	dither_state ^= dither_state << 13;
   7464 	dither_state ^= dither_state >> 17;
   7465 	dither_state ^= dither_state << 5;
   7466 
   7467 	return dither_state;
   7468 }
   7469 
   7470 static int8_t to_sample8(int32_t accumulated) {
   7471 	const uint32_t random = next_random();
   7472 	const int32_t triangular = (int32_t)(random & 1023) + (int32_t)((random >> 10) & 1023) - 1023;
   7473 
   7474 	int32_t out = (accumulated + (1 << (OUTPUT_SHIFT - 1)) + triangular) >> OUTPUT_SHIFT;
   7475 	if (out > 127) {
   7476 		out = 127;
   7477 	} else if (out < -128) {
   7478 		out = -128;
   7479 	}
   7480 
   7481 	return (int8_t)out;
   7482 }
   7483 
   7484 static void run_tick(void) {
   7485 	reset_ramp_volumes();
   7486 	tick_replayer();
   7487 
   7488 	const uint32_t numerator = SAMPLES_PER_TICK_NUMERATOR + tick_remainder;
   7489 	samples_per_tick = numerator / mixer_bpm;
   7490 	tick_remainder = numerator % mixer_bpm;
   7491 
   7492 	update_voices();
   7493 
   7494 	tick_samples_left = samples_per_tick;
   7495 }
   7496 
   7497 static void audio_source(void *user) {
   7498 	(void)user;
   7499 
   7500 	if (!song_playing || audio_left < 0) {
   7501 		return;
   7502 	}
   7503 
   7504 	uint32_t space = ta_audio_free(audio_left);
   7505 	if (audio_right >= 0) {
   7506 		const uint32_t space_right = ta_audio_free(audio_right);
   7507 		if (space_right < space) {
   7508 			space = space_right;
   7509 		}
   7510 	}
   7511 
   7512 	while (space > 0) {
   7513 		if (tick_samples_left == 0) {
   7514 			run_tick();
   7515 		}
   7516 
   7517 		uint32_t count = space;
   7518 		if (count > tick_samples_left) {
   7519 			count = tick_samples_left;
   7520 		}
   7521 		if (count > MIX_CHUNK) {
   7522 			count = MIX_CHUNK;
   7523 		}
   7524 
   7525 		render_chunk(count);
   7526 
   7527 		if (audio_right >= 0) {
   7528 			for (uint32_t i = 0; i < count; i++) {
   7529 				out_l[i] = to_sample8(mix_l[i]);
   7530 				out_r[i] = to_sample8(mix_r[i]);
   7531 			}
   7532 
   7533 			ta_audio_write(audio_left, out_l, count);
   7534 			ta_audio_write(audio_right, out_r, count);
   7535 		} else {
   7536 			for (uint32_t i = 0; i < count; i++) {
   7537 				out_l[i] = to_sample8((mix_l[i] + mix_r[i]) >> 1);
   7538 			}
   7539 
   7540 			ta_audio_write(audio_left, out_l, count);
   7541 		}
   7542 
   7543 		tick_samples_left -= count;
   7544 		space -= count;
   7545 	}
   7546 }
   7547 
   7548 typedef struct reader_t {
   7549 	const uint8_t *data;
   7550 	uint32_t size;
   7551 	uint32_t pos;
   7552 } reader_t;
   7553 
   7554 static uint8_t read8(reader_t *r) {
   7555 	if (r->pos >= r->size) {
   7556 		return 0;
   7557 	}
   7558 
   7559 	return r->data[r->pos++];
   7560 }
   7561 
   7562 static uint16_t read16(reader_t *r) {
   7563 	const uint16_t low = read8(r);
   7564 	const uint16_t high = read8(r);
   7565 
   7566 	return (uint16_t)(low | (high << 8));
   7567 }
   7568 
   7569 static void skip_bytes(reader_t *r, uint32_t count) {
   7570 	if (count > r->size - r->pos) {
   7571 		r->pos = r->size;
   7572 	} else {
   7573 		r->pos += count;
   7574 	}
   7575 }
   7576 
   7577 static uint16_t le16(const uint8_t *p) {
   7578 	return (uint16_t)(p[0] | (p[1] << 8));
   7579 }
   7580 
   7581 static uint32_t le32(const uint8_t *p) {
   7582 	return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
   7583 }
   7584 
   7585 static uint32_t extra_sample_lengths[32 - MAX_SMP_PER_INST];
   7586 
   7587 static instr_t *allocate_instr(int32_t ins_num) {
   7588 	if (instr[ins_num] != NULL) {
   7589 		return instr[ins_num];
   7590 	}
   7591 
   7592 	instr_t *p = (instr_t *)malloc(sizeof (instr_t));
   7593 	if (p == NULL) {
   7594 		return NULL;
   7595 	}
   7596 
   7597 	*p = (instr_t){ 0 };
   7598 
   7599 	for (int32_t i = 0; i < MAX_SMP_PER_INST; i++) {
   7600 		p->smp[i].panning = 128;
   7601 		p->smp[i].volume = 64;
   7602 	}
   7603 
   7604 	instr[ins_num] = p;
   7605 	
   7606 	return p;
   7607 }
   7608 
   7609 static void free_song(void) {
   7610 	for (int32_t i = 0; i < MAX_PATTERNS; i++) {
   7611 		free(pattern[i]);
   7612 		pattern[i] = NULL;
   7613 		pattern_num_rows[i] = 64;
   7614 	}
   7615 
   7616 	for (int32_t i = 0; i <= MAX_INST; i++) {
   7617 		if (instr[i] != NULL) {
   7618 			for (int32_t j = 0; j < MAX_SMP_PER_INST; j++) {
   7619 				free(instr[i]->smp[j].data_ptr);
   7620 			}
   7621 
   7622 			free(instr[i]);
   7623 			instr[i] = NULL;
   7624 		}
   7625 	}
   7626 
   7627 	song_loaded = false;
   7628 }
   7629 
   7630 static void sanitize_instrument(instr_t *ins) {
   7631 	if (ins->auto_vib_depth > 0x0F) {
   7632 		ins->auto_vib_depth = 0x0F;
   7633 	}
   7634 	if (ins->auto_vib_rate > 0x3F) {
   7635 		ins->auto_vib_rate = 0x3F;
   7636 	}
   7637 	if (ins->auto_vib_type > 3) {
   7638 		ins->auto_vib_type = 0;
   7639 	}
   7640 
   7641 	for (int32_t i = 0; i < 96; i++) {
   7642 		if (ins->note2_sample_lut[i] >= MAX_SMP_PER_INST) {
   7643 			ins->note2_sample_lut[i] = MAX_SMP_PER_INST - 1;
   7644 		}
   7645 	}
   7646 
   7647 	if (ins->vol_env_length > 12) {
   7648 		ins->vol_env_length = 12;
   7649 	}
   7650 	if (ins->vol_env_loop_start > 11) {
   7651 		ins->vol_env_loop_start = 11;
   7652 	}
   7653 	if (ins->vol_env_loop_end > 11) {
   7654 		ins->vol_env_loop_end = 11;
   7655 	}
   7656 	if (ins->vol_env_sustain > 11) {
   7657 		ins->vol_env_sustain = 11;
   7658 	}
   7659 	if (ins->pan_env_length > 12) {
   7660 		ins->pan_env_length = 12;
   7661 	}
   7662 	if (ins->pan_env_loop_start > 11) {
   7663 		ins->pan_env_loop_start = 11;
   7664 	}
   7665 	if (ins->pan_env_loop_end > 11) {
   7666 		ins->pan_env_loop_end = 11;
   7667 	}
   7668 	if (ins->pan_env_sustain > 11) {
   7669 		ins->pan_env_sustain = 11;
   7670 	}
   7671 
   7672 	for (int32_t i = 0; i < 12; i++) {
   7673 		if ((uint16_t)ins->vol_env_points[i][0] > 32767) {
   7674 			ins->vol_env_points[i][0] = 32767;
   7675 		}
   7676 		if ((uint16_t)ins->pan_env_points[i][0] > 32767) {
   7677 			ins->pan_env_points[i][0] = 32767;
   7678 		}
   7679 		if ((uint16_t)ins->vol_env_points[i][1] > 64) {
   7680 			ins->vol_env_points[i][1] = 64;
   7681 		}
   7682 		if ((uint16_t)ins->pan_env_points[i][1] > 63) {
   7683 			ins->pan_env_points[i][1] = 63;
   7684 		}
   7685 	}
   7686 }
   7687 
   7688 static void sanitize_sample(sample_t *s) {
   7689 	if (GET_LOOPTYPE(s->flags) == (LOOP_FORWARD | LOOP_PINGPONG)) {
   7690 		s->flags &= ~LOOP_FORWARD;
   7691 	}
   7692 
   7693 	if (s->volume > 64) {
   7694 		s->volume = 64;
   7695 	}
   7696 
   7697 	s->relative_note = CLAMP(s->relative_note, -48, 71);
   7698 	s->length = CLAMP(s->length, 0, MAX_SAMPLE_LEN);
   7699 
   7700 	if (s->loop_start < 0 || s->loop_length <= 0 || s->loop_start + s->loop_length > s->length) {
   7701 		s->loop_start = 0;
   7702 		s->loop_length = 0;
   7703 		DISABLE_LOOP(s->flags);
   7704 	}
   7705 }
   7706 
   7707 static bool load_instr_header(reader_t *r, int32_t ins_num) {
   7708 	uint8_t ih[INSTR_HEADER_SIZE];
   7709 
   7710 	for (int32_t i = 0; i < INSTR_HEADER_SIZE; i++) {
   7711 		ih[i] = 0;
   7712 	}
   7713 
   7714 	if (r->size - r->pos < 4) {
   7715 		return false;
   7716 	}
   7717 
   7718 	const uint32_t instr_size = le32(&r->data[r->pos]);
   7719 
   7720 	uint32_t read_size = instr_size;
   7721 	if (read_size == 0 || read_size > INSTR_HEADER_SIZE) {
   7722 		read_size = INSTR_HEADER_SIZE;
   7723 	}
   7724 
   7725 	for (uint32_t i = 0; i < read_size; i++) {
   7726 		ih[i] = read8(r);
   7727 	}
   7728 
   7729 	if (instr_size > INSTR_HEADER_SIZE) {
   7730 		skip_bytes(r, instr_size - INSTR_HEADER_SIZE);
   7731 	}
   7732 
   7733 	const int16_t num_samples = (int16_t)le16(&ih[27]);
   7734 	if (num_samples < 0 || num_samples > 32) {
   7735 		return false;
   7736 	}
   7737 
   7738 	for (int32_t i = 0; i < 32 - MAX_SMP_PER_INST; i++) {
   7739 		extra_sample_lengths[i] = 0;
   7740 	}
   7741 
   7742 	if (num_samples == 0) {
   7743 		return true;
   7744 	}
   7745 
   7746 	instr_t *ins = allocate_instr(ins_num);
   7747 	if (ins == NULL) {
   7748 		return false;
   7749 	}
   7750 
   7751 	for (int32_t i = 0; i < 96; i++) {
   7752 		ins->note2_sample_lut[i] = ih[33 + i];
   7753 	}
   7754 
   7755 	for (int32_t i = 0; i < 12; i++) {
   7756 		ins->vol_env_points[i][0] = (int16_t)le16(&ih[129 + i * 4]);
   7757 		ins->vol_env_points[i][1] = (int16_t)le16(&ih[129 + i * 4 + 2]);
   7758 		ins->pan_env_points[i][0] = (int16_t)le16(&ih[177 + i * 4]);
   7759 		ins->pan_env_points[i][1] = (int16_t)le16(&ih[177 + i * 4 + 2]);
   7760 	}
   7761 
   7762 	ins->vol_env_length = ih[225];
   7763 	ins->pan_env_length = ih[226];
   7764 	ins->vol_env_sustain = ih[227];
   7765 	ins->vol_env_loop_start = ih[228];
   7766 	ins->vol_env_loop_end = ih[229];
   7767 	ins->pan_env_sustain = ih[230];
   7768 	ins->pan_env_loop_start = ih[231];
   7769 	ins->pan_env_loop_end = ih[232];
   7770 	ins->vol_env_flags = ih[233];
   7771 	ins->pan_env_flags = ih[234];
   7772 	ins->auto_vib_type = ih[235];
   7773 	ins->auto_vib_sweep = ih[236];
   7774 	ins->auto_vib_depth = ih[237];
   7775 	ins->auto_vib_rate = ih[238];
   7776 	ins->fadeout = le16(&ih[239]);
   7777 	ins->mute = (ih[247] == 1);
   7778 	ins->num_samples = num_samples;
   7779 
   7780 	int32_t headers_to_read = num_samples;
   7781 	if (headers_to_read > MAX_SMP_PER_INST) {
   7782 		headers_to_read = MAX_SMP_PER_INST;
   7783 	}
   7784 
   7785 	if (r->size - r->pos < (uint32_t)num_samples * 40) {
   7786 		return false;
   7787 	}
   7788 
   7789 	for (int32_t i = 0; i < headers_to_read; i++) {
   7790 		const uint8_t *h = &r->data[r->pos + i * 40];
   7791 		sample_t *s = &ins->smp[i];
   7792 
   7793 		s->length = (int32_t)le32(&h[0]);
   7794 		s->loop_start = (int32_t)le32(&h[4]);
   7795 		s->loop_length = (int32_t)le32(&h[8]);
   7796 		s->volume = h[12];
   7797 		s->finetune = (int8_t)h[13];
   7798 		s->flags = h[14];
   7799 		s->panning = h[15];
   7800 		s->relative_note = (int8_t)h[16];
   7801 
   7802 		if (h[17] == 0xAD && !(h[14] & (SAMPLE_16BIT | SAMPLE_STEREO))) {
   7803 			s->flags |= SAMPLE_ADPCM;
   7804 		}
   7805 	}
   7806 
   7807 	for (int32_t i = MAX_SMP_PER_INST; i < num_samples; i++) {
   7808 		extra_sample_lengths[i - MAX_SMP_PER_INST] = le32(&r->data[r->pos + i * 40]);
   7809 	}
   7810 
   7811 	skip_bytes(r, (uint32_t)num_samples * 40);
   7812 
   7813 	return true;
   7814 }
   7815 
   7816 static bool skip_instrument(reader_t *r) {
   7817 	uint8_t ih[INSTR_HEADER_SIZE];
   7818 
   7819 	for (int32_t i = 0; i < INSTR_HEADER_SIZE; i++) {
   7820 		ih[i] = 0;
   7821 	}
   7822 
   7823 	if (r->size - r->pos < 4) {
   7824 		return false;
   7825 	}
   7826 
   7827 	const uint32_t instr_size = le32(&r->data[r->pos]);
   7828 
   7829 	uint32_t read_size = instr_size;
   7830 	if (read_size == 0 || read_size > INSTR_HEADER_SIZE) {
   7831 		read_size = INSTR_HEADER_SIZE;
   7832 	}
   7833 
   7834 	for (uint32_t i = 0; i < read_size; i++) {
   7835 		ih[i] = read8(r);
   7836 	}
   7837 
   7838 	if (instr_size > INSTR_HEADER_SIZE) {
   7839 		skip_bytes(r, instr_size - INSTR_HEADER_SIZE);
   7840 	}
   7841 
   7842 	const int16_t num_samples = (int16_t)le16(&ih[27]);
   7843 	if (num_samples < 0 || num_samples > 32) {
   7844 		return false;
   7845 	}
   7846 
   7847 	if (num_samples == 0) {
   7848 		return true;
   7849 	}
   7850 
   7851 	if (r->size - r->pos < (uint32_t)num_samples * 40) {
   7852 		return false;
   7853 	}
   7854 
   7855 	uint32_t data_bytes = 0;
   7856 	for (int32_t i = 0; i < num_samples; i++) {
   7857 		data_bytes += le32(&r->data[r->pos + i * 40]);
   7858 	}
   7859 
   7860 	skip_bytes(r, (uint32_t)num_samples * 40);
   7861 	skip_bytes(r, data_bytes);
   7862 
   7863 	return true;
   7864 }
   7865 
   7866 static void load_adpcm_sample(reader_t *r, sample_t *s) {
   7867 	int8_t delta_lut[16];
   7868 	for (int32_t i = 0; i < 16; i++) {
   7869 		delta_lut[i] = (int8_t)read8(r);
   7870 	}
   7871 
   7872 	int8_t *data_ptr = s->data_ptr;
   7873 	const int32_t data_length = (s->length + 1) / 2;
   7874 
   7875 	int8_t curr_sample = 0;
   7876 	for (int32_t i = 0; i < data_length; i++) {
   7877 		const uint8_t nibbles = read8(r);
   7878 
   7879 		curr_sample += delta_lut[nibbles & 0x0F];
   7880 		*data_ptr++ = curr_sample;
   7881 
   7882 		curr_sample += delta_lut[nibbles >> 4];
   7883 		*data_ptr++ = curr_sample;
   7884 	}
   7885 }
   7886 
   7887 static void decode_delta8(int8_t *p, int32_t length, bool stereo) {
   7888 	if (stereo) {
   7889 		length >>= 1;
   7890 
   7891 		int8_t *left = p;
   7892 		int8_t *right = p + length;
   7893 		int8_t old_left = 0;
   7894 		int8_t old_right = 0;
   7895 
   7896 		for (int32_t i = 0; i < length; i++) {
   7897 			old_left = (int8_t)(left[i] + old_left);
   7898 			old_right = (int8_t)(right[i] + old_right);
   7899 			left[i] = (int8_t)((old_left + old_right) >> 1);
   7900 		}
   7901 	} else {
   7902 		int8_t old = 0;
   7903 
   7904 		for (int32_t i = 0; i < length; i++) {
   7905 			old = (int8_t)(p[i] + old);
   7906 			p[i] = old;
   7907 		}
   7908 	}
   7909 }
   7910 
   7911 static void decode_delta16(int16_t *p, int32_t length, bool stereo) {
   7912 	if (stereo) {
   7913 		length >>= 1;
   7914 
   7915 		int16_t *left = p;
   7916 		int16_t *right = p + length;
   7917 		int16_t old_left = 0;
   7918 		int16_t old_right = 0;
   7919 
   7920 		for (int32_t i = 0; i < length; i++) {
   7921 			old_left = (int16_t)(left[i] + old_left);
   7922 			old_right = (int16_t)(right[i] + old_right);
   7923 			left[i] = (int16_t)((old_left + old_right) >> 1);
   7924 		}
   7925 	} else {
   7926 		int16_t old = 0;
   7927 
   7928 		for (int32_t i = 0; i < length; i++) {
   7929 			old = (int16_t)(p[i] + old);
   7930 			p[i] = old;
   7931 		}
   7932 	}
   7933 }
   7934 
   7935 static bool load_instr_samples(reader_t *r, int32_t ins_num) {
   7936 	instr_t *ins = instr[ins_num];
   7937 	if (ins == NULL) {
   7938 		return true;
   7939 	}
   7940 
   7941 	int32_t num_samples = ins->num_samples;
   7942 	if (num_samples > MAX_SMP_PER_INST) {
   7943 		num_samples = MAX_SMP_PER_INST;
   7944 	}
   7945 
   7946 	sample_t *s = ins->smp;
   7947 	for (int32_t i = 0; i < num_samples; i++, s++) {
   7948 		if (s->length <= 0) {
   7949 			s->length = 0;
   7950 			s->loop_start = 0;
   7951 			s->loop_length = 0;
   7952 			s->flags = 0;
   7953 			continue;
   7954 		}
   7955 
   7956 		const int32_t length_in_file = s->length;
   7957 		const bool sample_16bit = !!(s->flags & SAMPLE_16BIT);
   7958 		const bool stereo_sample = !!(s->flags & SAMPLE_STEREO);
   7959 		const bool adpcm_sample = !!(s->flags & SAMPLE_ADPCM);
   7960 
   7961 		if (sample_16bit) {
   7962 			s->length >>= 1;
   7963 			s->loop_start >>= 1;
   7964 			s->loop_length >>= 1;
   7965 		}
   7966 
   7967 		if (s->length > MAX_SAMPLE_LEN) {
   7968 			s->length = MAX_SAMPLE_LEN;
   7969 		}
   7970 
   7971 		const size_t bytes = (size_t)s->length * (sample_16bit ? 2 : 1) + 2;
   7972 		s->data_ptr = (int8_t *)malloc(bytes);
   7973 		if (s->data_ptr == NULL) {
   7974 			return false;
   7975 		}
   7976 
   7977 		for (size_t j = 0; j < bytes; j++) {
   7978 			s->data_ptr[j] = 0;
   7979 		}
   7980 
   7981 		if (adpcm_sample) {
   7982 			load_adpcm_sample(r, s);
   7983 		} else {
   7984 			const uint32_t start_pos = r->pos;
   7985 
   7986 			if (sample_16bit) {
   7987 				int16_t *p16 = (int16_t *)s->data_ptr;
   7988 				for (int32_t j = 0; j < s->length; j++) {
   7989 					p16[j] = (int16_t)read16(r);
   7990 				}
   7991 
   7992 				decode_delta16(p16, s->length, stereo_sample);
   7993 			} else {
   7994 				for (int32_t j = 0; j < s->length; j++) {
   7995 					s->data_ptr[j] = (int8_t)read8(r);
   7996 				}
   7997 
   7998 				decode_delta8(s->data_ptr, s->length, stereo_sample);
   7999 			}
   8000 
   8001 			const uint32_t consumed = r->pos - start_pos;
   8002 			if (consumed < (uint32_t)length_in_file) {
   8003 				skip_bytes(r, (uint32_t)length_in_file - consumed);
   8004 			}
   8005 
   8006 			if (stereo_sample) {
   8007 				s->length >>= 1;
   8008 				s->loop_start >>= 1;
   8009 				s->loop_length >>= 1;
   8010 			}
   8011 		}
   8012 
   8013 		s->flags &= ~(SAMPLE_STEREO | SAMPLE_ADPCM);
   8014 	}
   8015 
   8016 	for (int32_t i = 0; i < ins->num_samples - MAX_SMP_PER_INST; i++) {
   8017 		skip_bytes(r, extra_sample_lengths[i]);
   8018 	}
   8019 
   8020 	return true;
   8021 }
   8022 
   8023 static bool load_patterns(reader_t *r, int32_t num_patterns, uint16_t xm_version, int32_t file_channels) {
   8024 	for (int32_t i = 0; i < num_patterns; i++) {
   8025 		if (r->size - r->pos < 8) {
   8026 			return false;
   8027 		}
   8028 
   8029 		const int32_t header_size = (int32_t)le32(&r->data[r->pos]);
   8030 		skip_bytes(r, 4);
   8031 		skip_bytes(r, 1);
   8032 
   8033 		int32_t num_rows;
   8034 		if (xm_version == 0x0102) {
   8035 			num_rows = read8(r) + 1;
   8036 		} else {
   8037 			num_rows = read16(r);
   8038 		}
   8039 
   8040 		const uint32_t data_size = read16(r);
   8041 
   8042 		if (xm_version == 0x0102) {
   8043 			if (header_size > 8) {
   8044 				skip_bytes(r, (uint32_t)header_size - 8);
   8045 			}
   8046 		} else {
   8047 			if (header_size > 9) {
   8048 				skip_bytes(r, (uint32_t)header_size - 9);
   8049 			}
   8050 		}
   8051 
   8052 		if (num_rows <= 0) {
   8053 			num_rows = 64;
   8054 		}
   8055 
   8056 		if (num_rows > MAX_PATT_LEN) {
   8057 			num_rows = MAX_PATT_LEN;
   8058 		}
   8059 
   8060 		pattern_num_rows[i] = (int16_t)num_rows;
   8061 
   8062 		if (data_size == 0) {
   8063 			pattern_num_rows[i] = 64;
   8064 			continue;
   8065 		}
   8066 
   8067 		if (data_size > r->size - r->pos) {
   8068 			return false;
   8069 		}
   8070 
   8071 		note_t *notes = (note_t *)malloc((size_t)num_rows * (size_t)song.num_channels * sizeof (note_t));
   8072 		if (notes == NULL) {
   8073 			return false;
   8074 		}
   8075 
   8076 		for (int32_t j = 0; j < num_rows * song.num_channels; j++) {
   8077 			notes[j] = (note_t){ 0 };
   8078 		}
   8079 
   8080 		reader_t pr = {
   8081 			r->data + r->pos,
   8082 			data_size,
   8083 			0
   8084 		};
   8085 
   8086 		bool any_note = false;
   8087 
   8088 		for (int32_t row = 0; row < num_rows; row++) {
   8089 			for (int32_t c = 0; c < file_channels; c++) {
   8090 				note_t n = { 0 };
   8091 				const uint8_t packed = read8(&pr);
   8092 
   8093 				if (packed & 128) {
   8094 					n.note = (packed & 1) ? read8(&pr) : 0;
   8095 					n.instr = (packed & 2) ? read8(&pr) : 0;
   8096 					n.vol = (packed & 4) ? read8(&pr) : 0;
   8097 					n.efx = (packed & 8) ? read8(&pr) : 0;
   8098 					n.efx_data = (packed & 16) ? read8(&pr) : 0;
   8099 				} else {
   8100 					n.note = packed;
   8101 					n.instr = read8(&pr);
   8102 					n.vol = read8(&pr);
   8103 					n.efx = read8(&pr);
   8104 					n.efx_data = read8(&pr);
   8105 				}
   8106 
   8107 				if (n.note > 97) {
   8108 					n.note = 0;
   8109 				}
   8110 
   8111 				if (n.instr > MAX_INST) {
   8112 					n.instr = 0;
   8113 				}
   8114 
   8115 				if (n.efx > 35) {
   8116 					n.efx = 0;
   8117 					n.efx_data = 0;
   8118 				}
   8119 
   8120 				if (c < song.num_channels) {
   8121 					notes[row * song.num_channels + c] = n;
   8122 
   8123 					if (n.note != 0 || n.instr != 0 || n.vol != 0 || n.efx != 0 || n.efx_data != 0) {
   8124 						any_note = true;
   8125 					}
   8126 				}
   8127 			}
   8128 		}
   8129 
   8130 		skip_bytes(r, data_size);
   8131 
   8132 		if (any_note) {
   8133 			pattern[i] = notes;
   8134 		} else {
   8135 			free(notes);
   8136 			pattern_num_rows[i] = 64;
   8137 		}
   8138 	}
   8139 
   8140 	return true;
   8141 }
   8142 
   8143 static bool load_xm(const uint8_t *data, uint32_t size) {
   8144 	static const char xm_id[17] = {
   8145 		'E',
   8146 		'x',
   8147 		't',
   8148 		'e',
   8149 		'n',
   8150 		'd',
   8151 		'e',
   8152 		'd',
   8153 		' ',
   8154 		'M',
   8155 		'o',
   8156 		'd',
   8157 		'u',
   8158 		'l',
   8159 		'e',
   8160 		':',
   8161 		' '
   8162 	};
   8163 
   8164 	if (data == NULL || size < 336) {
   8165 		return false;
   8166 	}
   8167 
   8168 	for (int32_t i = 0; i < 17; i++) {
   8169 		if ((char)data[i] != xm_id[i]) {
   8170 			return false;
   8171 		}
   8172 	}
   8173 
   8174 	const uint16_t version = le16(&data[58]);
   8175 	const uint32_t header_size = le32(&data[60]);
   8176 	const uint16_t num_orders = le16(&data[64]);
   8177 	const uint16_t song_loop_start = le16(&data[66]);
   8178 	const uint16_t file_channels_raw = le16(&data[68]);
   8179 	const uint16_t num_patterns = le16(&data[70]);
   8180 	const uint16_t num_instr = le16(&data[72]);
   8181 	const uint16_t flags = le16(&data[74]);
   8182 	const uint16_t speed = le16(&data[76]);
   8183 	const uint16_t bpm = le16(&data[78]);
   8184 
   8185 	if (version < 0x0102 || version > 0x0104) {
   8186 		return false;
   8187 	}
   8188 
   8189 	if (num_orders > MAX_ORDERS || num_patterns > MAX_PATTERNS) {
   8190 		return false;
   8191 	}
   8192 
   8193 	if (file_channels_raw == 0 || num_instr > 256) {
   8194 		return false;
   8195 	}
   8196 
   8197 	if (header_size > size - 60) {
   8198 		return false;
   8199 	}
   8200 
   8201 	for (int32_t i = 0; i < MAX_PATTERNS; i++) {
   8202 		pattern_num_rows[i] = 64;
   8203 	}
   8204 
   8205 	song = (song_t){ 0 };
   8206 
   8207 	song.song_length = num_orders;
   8208 	song.song_loop_start = song_loop_start;
   8209 	song.bpm = bpm;
   8210 	song.speed = speed;
   8211 	linear_periods_flag = !!(flags & 1);
   8212 	note2_period_lut = linear_periods_flag ? linear_period_lut : amiga_period_lut;
   8213 
   8214 	if (song.song_length == 0) {
   8215 		song.song_length = 1;
   8216 	} else {
   8217 		for (int32_t i = 0; i < song.song_length; i++) {
   8218 			song.orders[i] = data[80 + i];
   8219 		}
   8220 	}
   8221 
   8222 	for (int32_t i = 255; i >= 0; i--) {
   8223 		if (song.orders[i] != 0xFF) {
   8224 			break;
   8225 		}
   8226 
   8227 		if (song.song_length > i) {
   8228 			song.song_length = (uint16_t)i;
   8229 		}
   8230 	}
   8231 
   8232 	if (song.song_length > 255) {
   8233 		song.song_length = 255;
   8234 	}
   8235 
   8236 	const int32_t file_channels = file_channels_raw;
   8237 	int32_t num_channels = file_channels;
   8238 	if (num_channels > MAX_CHANNELS) {
   8239 		num_channels = MAX_CHANNELS;
   8240 	}
   8241 
   8242 	if (num_channels & 1) {
   8243 		num_channels++;
   8244 	}
   8245 
   8246 	song.num_channels = CLAMP(num_channels, 2, MAX_CHANNELS);
   8247 
   8248 	reader_t reader = {
   8249 		data,
   8250 		size,
   8251 		60 + header_size
   8252 	};
   8253 
   8254 	reader_t *r = &reader;
   8255 
   8256 	if (!allocate_instr(0)) {
   8257 		return false;
   8258 	}
   8259 
   8260 	instr[0]->smp[0].volume = 0;
   8261 
   8262 	if (version < 0x0104) {
   8263 		if (num_instr > MAX_INST) {
   8264 			return false;
   8265 		}
   8266 
   8267 		for (int32_t i = 0; i < num_instr; i++) {
   8268 			if (!load_instr_header(r, 1 + i)) {
   8269 				return false;
   8270 			}
   8271 		}
   8272 
   8273 		if (!load_patterns(r, num_patterns, version, file_channels)) {
   8274 			return false;
   8275 		}
   8276 
   8277 		for (int32_t i = 0; i < num_instr; i++) {
   8278 			if (!load_instr_samples(r, 1 + i)) {
   8279 				return false;
   8280 			}
   8281 		}
   8282 	} else {
   8283 		if (!load_patterns(r, num_patterns, version, file_channels)) {
   8284 			return false;
   8285 		}
   8286 
   8287 		for (int32_t i = 0; i < num_instr; i++) {
   8288 			const int32_t slot = (i < MAX_INST) ? (1 + i) : MAX_INST + 1;
   8289 
   8290 			if (slot > MAX_INST) {
   8291 				if (!skip_instrument(r)) {
   8292 					return false;
   8293 				}
   8294 
   8295 				continue;
   8296 			}
   8297 
   8298 			if (!load_instr_header(r, slot)) {
   8299 				return false;
   8300 			}
   8301 
   8302 			if (!load_instr_samples(r, slot)) {
   8303 				return false;
   8304 			}
   8305 		}
   8306 	}
   8307 
   8308 	for (int32_t i = 1; i <= MAX_INST; i++) {
   8309 		if (instr[i] == NULL) {
   8310 			continue;
   8311 		}
   8312 
   8313 		sanitize_instrument(instr[i]);
   8314 		for (int32_t j = 0; j < MAX_SMP_PER_INST; j++) {
   8315 			sanitize_sample(&instr[i]->smp[j]);
   8316 		}
   8317 	}
   8318 
   8319 	song.bpm = CLAMP(song.bpm, MIN_BPM, MAX_BPM);
   8320 	song.initial_bpm = song.bpm;
   8321 	song.initial_speed = song.speed = CLAMP(song.speed, 1, MAX_SPEED);
   8322 
   8323 	if (song.song_loop_start >= song.song_length) {
   8324 		song.song_loop_start = 0;
   8325 	}
   8326 
   8327 	song.global_volume = 64;
   8328 
   8329 	return true;
   8330 }
   8331 
   8332 static void release_audio(void) {
   8333 	if (audio_left >= 0) {
   8334 		ta_audio_unlock(audio_left);
   8335 	}
   8336 
   8337 	if (audio_right >= 0) {
   8338 		ta_audio_unlock(audio_right);
   8339 	}
   8340 
   8341 	audio_left = -1;
   8342 	audio_right = -1;
   8343 }
   8344 
   8345 bool ta_ft2_init(const uint8_t *data, uint32_t length) {
   8346 	ta_ft2_destroy();
   8347 	build_panning_table();
   8348 
   8349 	if (!load_xm(data, length)) {
   8350 		free_song();
   8351 		return false;
   8352 	}
   8353 
   8354 	reset_channels();
   8355 	stop_voices();
   8356 	song_loaded = true;
   8357 
   8358 	return true;
   8359 }
   8360 
   8361 bool ta_ft2_play(void) {
   8362 	if (!song_loaded) {
   8363 		return false;
   8364 	}
   8365 
   8366 	if (audio_left < 0) {
   8367 		audio_left = ta_audio_lock(audio_source, NULL);
   8368 		if (audio_left < 0) {
   8369 			return false;
   8370 		}
   8371 
   8372 		audio_right = ta_audio_lock(audio_source, NULL);
   8373 
   8374 		if (audio_right >= 0) {
   8375 			ta_audio_set_pan(audio_left, TA_AUDIO_PAN_LEFT);
   8376 			ta_audio_set_pan(audio_right, TA_AUDIO_PAN_RIGHT);
   8377 		}
   8378 	}
   8379 
   8380 	ta_audio_clear(audio_left);
   8381 	if (audio_right >= 0) {
   8382 		ta_audio_clear(audio_right);
   8383 	}
   8384 
   8385 	reset_channels();
   8386 	stop_voices();
   8387 
   8388 	song.speed = song.initial_speed;
   8389 	song.bpm = song.initial_bpm;
   8390 	set_mixer_bpm(song.bpm);
   8391 
   8392 	set_song_pos(0, 0);
   8393 	reset_replayer_state();
   8394 	bxx_overflow = false;
   8395 	tick_samples_left = 0;
   8396 	tick_remainder = 0;
   8397 	song_playing = true;
   8398 
   8399 	return true;
   8400 }
   8401 
   8402 void ta_ft2_stop(void) {
   8403 	song_playing = false;
   8404 	release_audio();
   8405 
   8406 	if (song_loaded) {
   8407 		stop_voices();
   8408 	}
   8409 }
   8410 
   8411 bool ta_ft2_is_playing(void) {
   8412 	return song_playing;
   8413 }
   8414 
   8415 void ta_ft2_destroy(void) {
   8416 	ta_ft2_stop();
   8417 	free_song();
   8418 }