// nyblcore v0.5 generated 2023-07-04 18:16:07.407885071 +0000 UTC
// m=+4765605.172245669 bpm150_4.wav 5 slices

// nyblcore v1.0.0
//
// ATtiny85 pinout:
//
//         +--u--+
//      NC |1   8| VCC
//     InA |2   7| InK
//     InB |3   6| Audio out
//     GND |4   5| LED out
//         +-----+
//

// <sample>
#ifndef SAMPLE_TABLE_H_
#define SAMPLE_TABLE_H_

#define NUM_SAMPLES 5
#define SAMPLE_SIZE 998
#define TOTAL_SAMPLES 4990

const word retrigs[] = {7984, 3992, 2994, 1996, 998, 499, 332};
const word pos[] = {0, 998, 1996, 2994, 3992, 4990};
const byte SAMPLE_TABLE[] PROGMEM = {
    128, 128, 128, 128, 128, 128, 127, 128, 129, 127, 130, 119, 127, 135, 133,
    129, 127, 141, 129, 123, 124, 126, 124, 132, 126, 122, 123, 122, 122, 132,
    133, 129, 147, 121, 124, 114, 129, 131, 126, 175, 189, 170, 134, 100, 81,
    90,  79,  79,  86,  112, 145, 154, 173, 189, 194, 186, 167, 126, 103, 87,
    77,  44,  49,  91,  106, 158, 171, 190, 184, 127, 126, 145, 152, 143, 131,
    138, 144, 134, 128, 106, 114, 127, 135, 140, 138, 136, 133, 126, 121, 115,
    114, 127, 112, 97,  84,  90,  94,  105, 119, 138, 152, 156, 165, 182, 187,
    185, 190, 193, 186, 158, 133, 100, 76,  62,  56,  46,  43,  51,  68,  89,
    117, 145, 170, 185, 190, 184, 168, 156, 142, 133, 128, 123, 121, 123, 124,
    124, 128, 117, 117, 122, 121, 126, 122, 120, 121, 120, 119, 120, 119, 108,
    97,  86,  95,  108, 129, 149, 160, 173, 184, 182, 184, 179, 176, 168, 148,
    123, 94,  80,  69,  75,  90,  109, 116, 130, 127, 123, 124, 116, 109, 103,
    115, 111, 116, 126, 136, 145, 151, 152, 159, 177, 180, 186, 191, 186, 169,
    161, 147, 130, 121, 105, 95,  82,  74,  66,  61,  66,  70,  81,  88,  100,
    110, 123, 129, 127, 134, 142, 146, 150, 157, 152, 152, 154, 150, 156, 163,
    160, 159, 158, 149, 133, 122, 115, 119, 118, 112, 113, 111, 114, 111, 105,
    115, 128, 138, 152, 155, 149, 148, 139, 137, 140, 142, 147, 156, 157, 154,
    134, 120, 111, 102, 105, 100, 104, 107, 108, 98,  98,  94,  92,  94,  87,
    90,  98,  110, 112, 120, 131, 125, 131, 148, 156, 165, 171, 166, 169, 167,
    156, 151, 144, 145, 144, 141, 138, 131, 134, 136, 131, 139, 137, 145, 151,
    156, 158, 155, 151, 144, 136, 129, 117, 105, 97,  87,  81,  76,  71,  79,
    85,  87,  88,  94,  103, 114, 128, 142, 144, 144, 138, 126, 119, 109, 112,
    117, 126, 144, 156, 161, 157, 162, 162, 163, 155, 155, 152, 145, 148, 144,
    136, 132, 124, 123, 127, 128, 130, 127, 128, 127, 120, 121, 119, 122, 123,
    124, 121, 121, 125, 122, 116, 110, 105, 107, 117, 123, 122, 116, 110, 112,
    112, 114, 120, 126, 129, 136, 146, 145, 149, 150, 145, 146, 143, 143, 143,
    138, 135, 132, 131, 133, 132, 133, 123, 120, 122, 120, 120, 116, 119, 123,
    129, 131, 128, 131, 130, 137, 135, 127, 129, 122, 122, 107, 104, 103, 106,
    112, 122, 133, 139, 142, 135, 130, 122, 122, 116, 118, 121, 134, 140, 146,
    153, 149, 148, 146, 146, 140, 130, 122, 124, 121, 125, 120, 118, 115, 118,
    122, 129, 133, 143, 143, 140, 146, 146, 143, 146, 141, 132, 131, 119, 109,
    106, 101, 98,  101, 103, 110, 116, 115, 118, 122, 130, 138, 148, 151, 147,
    139, 133, 123, 123, 118, 117, 121, 124, 132, 140, 153, 150, 157, 155, 153,
    151, 144, 139, 123, 122, 118, 118, 119, 114, 110, 101, 100, 107, 107, 106,
    105, 110, 115, 123, 136, 139, 137, 142, 144, 147, 144, 138, 132, 125, 123,
    122, 123, 125, 127, 135, 142, 144, 151, 150, 145, 143, 140, 140, 134, 128,
    123, 123, 121, 113, 110, 115, 116, 119, 123, 127, 123, 126, 123, 121, 122,
    120, 117, 113, 115, 116, 121, 126, 124, 125, 128, 135, 137, 143, 141, 147,
    147, 143, 138, 127, 124, 119, 123, 127, 126, 120, 122, 118, 118, 121, 128,
    137, 137, 145, 142, 146, 143, 141, 140, 130, 129, 126, 129, 120, 117, 110,
    108, 110, 117, 127, 132, 137, 138, 134, 127, 124, 117, 116, 114, 113, 122,
    121, 127, 133, 133, 132, 128, 125, 127, 124, 125, 132, 137, 140, 134, 134,
    137, 139, 140, 137, 135, 132, 132, 132, 129, 136, 132, 136, 139, 136, 138,
    131, 127, 124, 122, 118, 116, 109, 111, 114, 117, 122, 122, 126, 124, 123,
    117, 114, 108, 112, 112, 121, 127, 130, 127, 123, 127, 131, 141, 141, 147,
    150, 151, 151, 147, 139, 132, 132, 132, 131, 134, 132, 133, 133, 135, 133,
    131, 128, 131, 131, 131, 139, 133, 132, 125, 123, 115, 113, 111, 105, 101,
    104, 110, 106, 112, 106, 112, 116, 123, 131, 139, 145, 143, 147, 145, 147,
    141, 137, 135, 132, 132, 130, 129, 128, 123, 125, 125, 128, 134, 135, 138,
    138, 138, 139, 139, 137, 135, 130, 123, 118, 115, 114, 111, 112, 112, 108,
    110, 112, 116, 117, 118, 124, 130, 138, 137, 142, 134, 133, 126, 125, 127,
    125, 125, 121, 125, 123, 127, 126, 129, 129, 134, 141, 143, 150, 151, 147,
    143, 140, 138, 137, 129, 128, 124, 123, 126, 120, 114, 108, 105, 107, 108,
    114, 117, 120, 125, 130, 129, 123, 122, 118, 124, 124, 128, 128, 127, 126,
    123, 122, 126, 129, 132, 139, 141, 148, 146, 144, 140, 138, 138, 139, 133,
    134, 131, 132, 135, 132, 135, 135, 134, 130, 127, 124, 118, 117, 117, 117,
    119, 117, 114, 115, 119, 123, 122, 123, 122, 120, 120, 119, 121, 124, 125,
    131, 133, 133, 136, 134, 132, 129, 128, 126, 128, 130, 135, 140, 143, 144,
    141, 139, 129, 129, 127, 128, 125, 122, 126, 123, 127, 126, 130, 128, 128,
    134, 146, 125, 119, 127, 128, 124, 119, 129, 133, 129, 123, 115, 111, 111,
    114, 119, 116, 123, 126, 138, 142, 139, 131, 139, 139, 145, 146, 138, 135,
    128, 129, 118, 115, 112, 110, 126, 135, 142, 159, 152, 147, 142, 137, 124,
    123, 118, 120, 132, 141, 132, 126, 129, 118, 116, 113, 125, 130, 132, 133,
    137, 136, 126, 117, 117, 108, 120, 113, 107, 105, 111, 116, 121, 119, 120,
    131, 133, 144, 151, 155, 145, 146, 137, 138, 127, 128, 119, 125, 129, 131,
    139, 130, 135, 136, 137, 136, 140, 140, 132, 133, 133, 130, 132, 118, 111,
    103, 100, 111, 118, 125, 127, 129, 128, 128, 128, 128, 127, 129, 129, 140,
    145, 135, 131, 124, 115, 121, 123, 120, 129, 134, 132, 137, 138, 132, 132,
    133, 128, 133, 134, 131, 130, 126, 124, 122, 117, 114, 119, 122, 122, 124,
    125, 125, 132, 134, 132, 134, 135, 135, 134, 129, 128, 121, 122, 119, 120,
    125, 123, 122, 118, 122, 119, 120, 131, 133, 136, 140, 139, 139, 143, 135,
    133, 134, 132, 131, 128, 127, 126, 125, 121, 124, 125, 124, 123, 123, 129,
    131, 137, 137, 131, 127, 123, 125, 127, 127, 127, 126, 123, 124, 123, 118,
    118, 117, 125, 130, 130, 130, 124, 125, 127, 129, 125, 130, 133, 129, 131,
    131, 135, 133, 133, 132, 133, 131, 128, 133, 134, 137, 133, 134, 131, 127,
    128, 125, 129, 129, 131, 132, 130, 134, 130, 127, 125, 122, 121, 124, 122,
    123, 127, 123, 128, 125, 122, 120, 125, 124, 124, 124, 125, 134, 133, 131,
    129, 125, 120, 122, 123, 128, 129, 131, 135, 138, 133, 131, 132, 132, 137,
    137, 134, 133, 129, 122, 121, 119, 117, 120, 118, 124, 129, 130, 132, 136,
    136, 135, 137, 135, 137, 130, 128, 128, 125, 120, 120, 115, 112, 116, 118,
    124, 131, 133, 135, 134, 132, 133, 132, 132, 129, 132, 130, 133, 126, 125,
    123, 120, 126, 129, 132, 130, 131, 131, 136, 139, 136, 133, 124, 123, 123,
    124, 122, 119, 120, 120, 126, 125, 127, 126, 132, 136, 139, 140, 138, 135,
    133, 128, 123, 119, 118, 116, 119, 118, 120, 121, 122, 123, 124, 126, 130,
    135, 134, 139, 143, 142, 134, 131, 130, 128, 132, 125, 127, 122, 121, 124,
    124, 123, 122, 127, 135, 138, 135, 133, 134, 132, 132, 131, 127, 125, 122,
    123, 128, 131, 130, 130, 127, 126, 123, 119, 124, 127, 127, 131, 127, 123,
    125, 124, 126, 130, 129, 127, 129, 130, 129, 131, 124, 126, 129, 131, 134,
    131, 130, 128, 129, 129, 130, 127, 120, 118, 122, 127, 132, 132, 132, 132,
    133, 135, 132, 131, 130, 131, 128, 128, 126, 121, 119, 117, 122, 124, 127,
    131, 131, 129, 122, 124, 123, 124, 125, 124, 127, 128, 131, 129, 129, 130,
    131, 133, 130, 134, 134, 135, 135, 134, 133, 133, 130, 123, 122, 120, 121,
    124, 128, 127, 127, 127, 130, 130, 131, 135, 135, 135, 135, 130, 130, 123,
    119, 116, 115, 117, 120, 123, 125, 126, 125, 127, 128, 130, 134, 135, 140,
    135, 132, 128, 123, 118, 117, 116, 113, 119, 122, 127, 130, 127, 132, 131,
    134, 135, 133, 132, 132, 130, 129, 127, 123, 125, 124, 122, 123, 125, 128,
    128, 134, 133, 134, 133, 132, 135, 131, 133, 135, 132, 130, 125, 122, 118,
    118, 121, 126, 129, 129, 131, 133, 131, 132, 130, 129, 130, 132, 133, 133,
    128, 125, 124, 122, 125, 124, 123, 123, 125, 127, 131, 131, 129, 130, 127,
    131, 132, 128, 129, 131, 132, 133, 128, 125, 122, 122, 126, 127, 128, 128,
    130, 132, 132, 134, 129, 131, 131, 127, 126, 126, 126, 124, 125, 124, 126,
    126, 127, 130, 129, 131, 131, 130, 132, 136, 134, 132, 130, 125, 126, 125,
    124, 125, 123, 125, 126, 124, 128, 127, 130, 132, 135, 134, 130, 132, 129,
    127, 127, 123, 124, 123, 121, 121, 117, 119, 122, 126, 126, 128, 129, 127,
    130, 130, 131, 128, 129, 126, 127, 128, 127, 125, 124, 126, 126, 124, 126,
    127, 130, 132, 133, 132, 132, 134, 133, 133, 128, 124, 126, 124, 127, 128,
    127, 127, 129, 130, 130, 126, 128, 128, 131, 130, 131, 127, 129, 129, 128,
    128, 124, 124, 123, 126, 127, 129, 130, 128, 131, 131, 132, 130, 128, 130,
    133, 132, 133, 129, 125, 126, 122, 121, 123, 125, 125, 127, 128, 130, 129,
    130, 131, 131, 132, 132, 132, 129, 128, 128, 126, 123, 123, 123, 124, 126,
    124, 128, 128, 130, 131, 129, 132, 131, 131, 133, 132, 132, 128, 126, 125,
    125, 120, 122, 126, 126, 129, 131, 132, 133, 132, 130, 130, 130, 131, 130,
    129, 128, 125, 124, 124, 121, 123, 122, 124, 126, 131, 147, 130, 132, 129,
    124, 124, 124, 125, 127, 130, 125, 126, 128, 127, 132, 139, 139, 142, 137,
    136, 126, 121, 115, 119, 119, 121, 122, 120, 139, 151, 162, 125, 73,  80,
    137, 157, 184, 173, 144, 109, 79,  107, 103, 130, 151, 139, 172, 187, 169,
    130, 119, 75,  74,  98,  136, 170, 100, 126, 176, 184, 154, 147, 125, 102,
    89,  119, 146, 86,  76,  107, 160, 169, 171, 148, 86,  113, 164, 143, 100,
    106, 96,  90,  140, 140, 162, 140, 139, 177, 195, 156, 122, 93,  46,  85,
    149, 147, 86,  120, 139, 152, 157, 154, 119, 60,  61,  113, 148, 93,  108,
    135, 128, 143, 173, 154, 94,  98,  142, 150, 150, 168, 136, 81,  101, 177,
    170, 135, 112, 156, 165, 148, 167, 103, 86,  82,  127, 136, 119, 123, 120,
    116, 98,  130, 139, 74,  63,  137, 133, 113, 147, 138, 115, 138, 157, 156,
    112, 116, 114, 129, 143, 173, 159, 94,  103, 174, 192, 115, 137, 136, 105,
    149, 171, 132, 116, 110, 119, 140, 157, 142, 125, 99,  95,  145, 132, 91,
    82,  114, 118, 126, 163, 135, 95,  92,  123, 143, 137, 117, 100, 117, 120,
    168, 149, 107, 138, 152, 148, 127, 140, 125, 101, 130, 146, 151, 129, 123,
    162, 138, 122, 148, 116, 100, 136, 163, 135, 140, 144, 127, 118, 104, 121,
    106, 101, 131, 162, 142, 127, 132, 122, 112, 125, 128, 103, 123, 125, 106,
    117, 141, 120, 134, 133, 127, 134, 124, 123, 116, 119, 113, 122, 131, 136,
    141, 128, 132, 137, 156, 144, 112, 110, 113, 128, 123, 126, 132, 138, 151,
    160, 140, 118, 124, 132, 129, 137, 136, 115, 112, 137, 131, 134, 131, 128,
    142, 129, 137, 120, 123, 118, 126, 120, 125, 136, 130, 133, 130, 129, 129,
    128, 128, 127, 127, 133, 125, 128, 128, 122, 130, 125, 120, 119, 119, 129,
    135, 129, 131, 124, 118, 124, 136, 140, 142, 125, 129, 135, 119, 122, 125,
    112, 106, 114, 127, 147, 135, 131, 125, 125, 137, 137, 118, 108, 125, 138,
    139, 126, 129, 141, 140, 138, 157, 144, 142, 138, 135, 133, 122, 129, 119,
    125, 117, 115, 116, 118, 123, 120, 120, 115, 120, 126, 127, 129, 133, 141,
    135, 131, 134, 131, 131, 124, 125, 132, 141, 141, 139, 130, 123, 129, 135,
    142, 131, 124, 121, 115, 123, 122, 122, 120, 121, 121, 129, 127, 129, 131,
    128, 123, 117, 125, 126, 122, 115, 122, 119, 121, 132, 137, 130, 132, 135,
    134, 129, 130, 130, 124, 119, 119, 132, 137, 142, 142, 142, 132, 135, 146,
    144, 128, 130, 131, 128, 134, 134, 133, 122, 123, 121, 126, 123, 122, 119,
    111, 109, 119, 125, 131, 127, 126, 134, 132, 133, 131, 129, 118, 114, 117,
    117, 125, 129, 133, 127, 126, 127, 128, 131, 126, 126, 120, 112, 119, 128,
    135, 134, 124, 133, 133, 141, 141, 127, 117, 113, 113, 112, 119, 122, 130,
    127, 132, 138, 138, 143, 140, 131, 125, 125, 116, 122, 132, 124, 123, 127,
    136, 141, 141, 136, 128, 129, 125, 124, 123, 122, 121, 125, 128, 129, 138,
    140, 135, 127, 130, 122, 123, 120, 116, 120, 123, 131, 135, 139, 139, 133,
    125, 123, 119, 118, 119, 118, 122, 119, 120, 120, 132, 137, 132, 135, 135,
    137, 128, 124, 122, 119, 124, 123, 135, 139, 135, 133, 137, 138, 136, 140,
    132, 121, 115, 121, 123, 132, 135, 136, 133, 127, 125, 126, 124, 119, 127,
    125, 124, 123, 122, 121, 119, 123, 136, 141, 140, 134, 123, 113, 115, 122,
    122, 122, 128, 132, 136, 136, 132, 132, 132, 125, 128, 128, 125, 124, 126,
    129, 132, 136, 135, 131, 130, 128, 129, 126, 125, 129, 128, 125, 123, 124,
    124, 127, 129, 135, 134, 126, 125, 126, 130, 133, 127, 124, 127, 129, 133,
    133, 127, 124, 128, 128, 125, 121, 122, 125, 123, 125, 130, 126, 126, 127,
    127, 127, 126, 129, 131, 131, 131, 130, 126, 126, 125, 129, 131, 129, 133,
    136, 131, 130, 128, 127, 126, 133, 137, 137, 132, 125, 129, 129, 130, 123,
    126, 123, 123, 130, 126, 124, 117, 119, 124, 128, 130, 132, 129, 126, 128,
    130, 130, 127, 124, 122, 123, 123, 124, 125, 122, 125, 129, 135, 134, 131,
    130, 127, 129, 127, 130, 128, 128, 128, 126, 129, 129, 129, 123, 126, 126,
    131, 127, 126, 129, 127, 132, 135, 134, 130, 126, 127, 128, 129, 134, 129,
    126, 123, 130, 133, 133, 131, 130, 129, 122, 132, 127, 126, 125, 123, 123,
    124, 131, 131, 129, 123, 124, 126, 127, 129, 127, 123, 120, 121, 128, 125,
    131, 129, 125, 126, 127, 130, 127, 125, 125, 126, 128, 129, 128, 126, 130,
    129, 131, 129, 131, 129, 126, 127, 130, 134, 133, 131, 129, 130, 128, 129,
    127, 125, 125, 124, 129, 129, 125, 127, 128, 128, 133, 136, 133, 131, 123,
    128, 128, 127, 128, 126, 125, 125, 129, 128, 124, 122, 125, 130, 132, 131,
    130, 130, 126, 127, 127, 126, 125, 128, 126, 128, 126, 125, 125, 123, 124,
    128, 128, 128, 128, 129, 129, 126, 129, 131, 129, 131, 132, 131, 129, 128,
    131, 129, 127, 127, 129, 130, 133, 133, 129, 126, 129, 131, 131, 127, 124,
    126, 128, 128, 128, 129, 128, 126, 125, 126, 126, 126, 126, 125, 128, 125,
    124, 126, 128, 128, 127, 129, 140, 138, 132, 132, 123, 116, 118, 124, 131,
    126, 124, 125, 128, 135, 137, 137, 123, 130, 129, 131, 138, 136, 132, 120,
    119, 123, 135, 134, 131, 128, 125, 140, 135, 125, 125, 121, 121, 124, 121,
    129, 134, 135, 138, 136, 138, 130, 125, 129, 130, 116, 108, 114, 116, 120,
    124, 128, 146, 141, 148, 148, 137, 136, 117, 109, 109, 113, 118, 121, 125,
    134, 129, 130, 134, 134, 130, 121, 120, 123, 128, 127, 128, 127, 134, 135,
    139, 134, 129, 127, 121, 121, 132, 128, 128, 133, 132, 137, 136, 134, 136,
    132, 125, 128, 122, 121, 125, 120, 123, 132, 131, 133, 133, 130, 128, 125,
    125, 125, 125, 117, 123, 125, 125, 127, 129, 133, 129, 124, 126, 128, 126,
    128, 132, 124, 122, 129, 126, 130, 130, 130, 131, 127, 126, 135, 131, 123,
    126, 123, 123, 132, 132, 134, 136, 129, 130, 132, 127, 124, 129, 124, 129,
    127, 127, 131, 127, 127, 128, 131, 135, 137, 128, 125, 124, 120, 123, 125,
    121, 124, 124, 125, 128, 132, 134, 132, 128, 126, 125, 121, 120, 121, 124,
    125, 131, 135, 133, 134, 129, 130, 127, 125, 126, 127, 122, 125, 131, 127,
    131, 132, 132, 127, 127, 128, 127, 129, 127, 127, 126, 126, 129, 129, 127,
    133, 135, 135, 131, 133, 127, 122, 118, 121, 126, 128, 129, 130, 132, 129,
    135, 133, 133, 130, 127, 126, 124, 122, 124, 125, 128, 132, 133, 133, 132,
    133, 127, 126, 124, 125, 123, 119, 127, 128, 131, 138, 136, 131, 130, 128,
    124, 124, 118, 122, 124, 121, 128, 128, 131, 132, 136, 134, 132, 131, 125,
    123, 121, 122, 127, 127, 127, 134, 134, 131, 135, 131, 125, 127, 125, 127,
    129, 127, 127, 131, 128, 128, 128, 128, 131, 133, 129, 128, 126, 127, 127,
    124, 123, 126, 128, 130, 137, 133, 131, 129, 126, 122, 123, 121, 123, 123,
    125, 132, 131, 129, 128, 133, 130, 128, 125, 131, 129, 132, 130, 129, 130,
    124, 121, 122, 117, 116, 122, 125, 128, 132, 131, 129, 130, 127, 128, 128,
    124, 125, 127, 128, 129, 129, 127, 128, 128, 127, 129, 130, 130, 131, 126,
    126, 127, 123, 124, 128, 128, 128, 133, 132, 132, 130, 127, 127, 124, 123,
    125, 127, 129, 130, 132, 130, 129, 129, 129, 128, 128, 128, 127, 125, 124,
    126, 127, 130, 133, 132, 132, 131, 129, 129, 126, 127, 124, 125, 126, 127,
    127, 130, 129, 130, 129, 128, 128, 125, 124, 125, 127, 127, 129, 128, 128,
    128, 131, 132, 132, 128, 126, 127, 117, 95,  94,  129, 160, 185, 179, 156,
    130, 97,  93,  83,  63,  62,  91,  126, 164, 173, 169, 155, 129, 138, 136,
    115, 100, 80,  101, 115, 138, 139, 125, 136, 138, 172, 158, 158, 131, 108,
    98,  95,  103, 101, 115, 136, 173, 170, 174, 154, 120, 103, 113, 116, 115,
    101, 96,  127, 142, 164, 164, 141, 126, 120, 134, 124, 111, 112, 107, 126,
    134, 153, 142, 137, 129, 137, 136, 131, 135, 101, 102, 117, 133, 129, 129,
    119, 125, 132, 146, 147, 131, 129, 101, 102, 123, 132, 124, 125, 135, 140,
    151, 127, 102, 120, 124, 130, 130, 132, 135, 125, 125, 141, 145, 137, 130,
    125, 127, 135, 136, 126, 123, 110, 120, 128, 136, 132, 126, 133, 134, 142,
    120, 101, 116, 120, 130, 130, 132, 135, 123, 122, 138, 143, 137, 130, 121,
    123, 128, 127, 119, 118, 119, 138, 134, 140, 141, 125, 130, 125, 121, 116,
    106, 110, 119, 138, 148, 135, 130, 124, 126, 132, 128, 123, 120, 114, 119,
    122, 129, 133, 134, 131, 141, 135, 134, 131, 119, 122, 122, 131, 126, 126,
    123, 124, 133, 138, 134, 131, 122, 127, 126, 127, 126, 123, 123, 126, 137,
    136, 134, 128, 129, 133, 134, 132, 121, 126, 126, 122, 127, 122, 126, 126,
    125, 131, 134, 136, 127, 122, 124, 127, 127, 123, 119, 126, 132, 133, 132,
    134, 130, 124, 134, 132, 129, 124, 118, 124, 130, 134, 130, 126, 127, 128,
    126, 133, 131, 127, 128, 127, 126, 128, 127, 123, 123, 128, 133, 130, 127,
    128, 128, 131, 132, 126, 128, 124, 121, 131, 132, 131, 133, 128, 125, 126,
    127, 124, 121, 124, 121, 123, 132, 129, 130, 130, 126, 124, 127, 126, 128,
    125, 123, 128, 132, 132, 125, 126, 127, 125, 126, 126, 123, 124, 127, 127,
    130, 131, 128, 123, 127, 130, 134, 130, 131, 125, 124, 131, 126, 127, 125,
    126, 126, 129, 134, 131, 129, 129, 129, 132, 130, 124, 123, 123, 129, 127,
    130, 130, 127, 130, 128, 130, 132, 127, 125, 127, 128, 128, 128, 127, 126,
    129, 130, 131, 130, 129, 127, 128, 130, 128, 126, 123, 124, 128, 128, 125,
    123, 138, 132, 135, 138, 133, 129, 117, 115, 118, 120, 122, 130, 123, 132,
    142, 146, 153, 125, 121, 128, 123, 122, 113, 92,  86,  89,  121, 151, 167,
    180, 183, 181, 167, 134, 106, 91,  69,  58,  64,  83,  105, 133, 152, 158,
    162, 164, 159, 154, 152, 153, 152, 144, 136, 129, 122, 115, 114, 110, 113,
    111, 105, 115, 123, 122, 119, 115, 112, 112, 107, 107, 105, 106, 113, 124,
    134, 149, 161, 176, 192, 201, 204, 204, 204, 193, 176, 150, 123, 95,  70,
    54,  51,  44,  53,  56,  65,  79,  93,  110, 133, 149, 157, 162, 153, 143,
    136, 131, 125, 128, 130, 138, 147, 152, 157, 155, 152, 148, 145, 144, 134,
    124, 115, 114, 113, 113, 116, 114, 117, 119, 125, 132, 137, 139, 143, 143,
    147, 152, 150, 146, 144, 139, 134, 127, 119, 113, 105, 91,  89,  91,  92,
    100, 100, 106, 108, 112, 113, 115, 120, 123, 129, 134, 140, 145, 153, 159,
    169, 169, 172, 175, 170, 169, 168, 162, 152, 150, 146, 142, 132, 123, 119,
    117, 115, 108, 100, 95,  88,  86,  85,  90,  90,  91,  94,  103, 108, 113,
    120, 125, 132, 132, 131, 127, 124, 121, 115, 117, 120, 128, 130, 133, 136,
    135, 141, 140, 144, 146, 147, 148, 154, 155, 160, 160, 158, 158, 161, 161,
    160, 156, 149, 151, 145, 137, 135, 132, 127, 124, 117, 113, 108, 97,  88,
    82,  78,  79,  80,  81,  83,  84,  93,  94,  104, 106, 112, 117, 123, 129,
    129, 134, 143, 147, 152, 155, 157, 161, 160, 160, 164, 160, 156, 158, 149,
    145, 143, 140, 143, 144, 141, 145, 148, 144, 142, 141, 133, 131, 124, 123,
    125, 121, 124, 120, 121, 114, 110, 107, 105, 108, 105, 102, 101, 98,  95,
    89,  79,  83,  87,  91,  99,  104, 114, 123, 126, 137, 146, 154, 163, 169,
    170, 175, 177, 175, 178, 173, 170, 165, 163, 156, 152, 150, 142, 136, 132,
    125, 123, 118, 109, 105, 102, 99,  98,  100, 104, 109, 108, 111, 113, 121,
    129, 138, 145, 155, 162, 169, 170, 170, 172, 169, 170, 168, 167, 164, 157,
    152, 142, 135, 130, 120, 116, 116, 110, 108, 106, 104, 103, 102, 95,  98,
    99,  98,  103, 105, 107, 111, 113, 116, 120, 121, 124, 127, 125, 127, 128,
    129, 131, 130, 133, 134, 138, 138, 139, 140, 141, 143, 144, 143, 143, 141,
    141, 142, 143, 144, 146, 147, 149, 147, 145, 144, 139, 137, 132, 128, 125,
    127, 114, 110, 104, 109, 114, 105, 117, 141, 143, 124, 98,  84,  79,  79,
    83,  83,  101, 102, 134, 156, 180, 202, 199, 200, 185, 155, 134, 123, 117,
    98,  80,  108, 128, 158, 185, 203, 186, 168, 165, 160, 156, 150, 153, 148,
    139, 138, 123, 105, 103, 96,  111, 112, 105, 115, 110, 117, 111, 99,  97,
    88,  76,  66,  61,  59,  77,  90,  109, 131, 143, 161, 176, 197, 206, 208,
    213, 217, 203, 186, 159, 125, 100, 73,  66,  58,  52,  65,  72,  91,  111,
    128, 148, 168, 175, 172, 157, 136, 124, 114, 109, 109, 112, 121, 129, 141,
    145, 141, 139, 137, 132, 127, 118, 112, 109, 109, 109, 113, 111, 108, 104,
    98,  97,  103, 108, 124, 136, 149, 160, 167, 173, 176, 176, 174, 171, 164,
    156, 146, 135, 120, 109, 96,  93,  91,  94,  103, 114, 120, 122, 127, 125,
    118, 108, 101, 103, 107, 104, 109, 127, 143, 158, 172, 171, 170, 162, 151,
    139, 131, 128, 130, 133, 134, 128, 116, 98,  82,  80,  75,  68,  70,  73,
    76,  84,  88,  93,  98,  101, 105, 110, 115, 121, 123, 126, 133, 130, 132,
    139, 144, 152, 156, 165, 175, 181, 179, 171, 157, 150, 136, 125, 114, 111,
    111, 113, 122, 121, 130, 134, 141, 142, 143, 139, 137, 135, 136, 144, 145,
    143, 141, 138, 130, 130, 127, 120, 119, 127, 130, 138, 135, 124, 112, 101,
    86,  81,  83,  80,  84,  88,  94,  99,  100, 96,  99,  105, 112, 113, 119,
    120, 124, 130, 139, 145, 151, 158, 159, 165, 168, 170, 170, 165, 156, 153,
    152, 140, 136, 130, 129, 129, 133, 137, 139, 143, 143, 146, 145, 142, 135,
    131, 119, 111, 111, 106, 103, 100, 105, 108, 116, 119, 123, 128, 127, 130,
    131, 130, 128, 126, 121, 120, 121, 127, 126, 125, 124, 131, 135, 142, 143,
    147, 155, 151, 149, 145, 141, 136, 142, 149, 156, 162, 162, 156, 154, 147,
    139, 133, 125, 118, 116, 115, 111, 109, 104, 100, 98,  99,  101, 103, 103,
    106, 110, 111, 113, 111, 112, 111, 110, 112, 112, 114, 120, 127, 137, 141,
    145, 148, 147, 149, 141, 139, 135, 131, 129, 127, 125, 127, 125, 126, 126,
    129, 127, 132, 131, 134, 134, 133, 135, 139, 137, 108, 117, 122, 155, 186,
    169, 157, 138, 125, 92,  61,  72,  104, 132, 153, 197, 194, 166, 132, 104,
    75,  62,  72,  80,  99,  124, 172, 171, 137, 140, 157, 118, 143, 144, 94,
    98,  103, 128, 171, 152, 174, 172, 144, 184, 160, 142, 107, 112, 100, 80,
    85,  102, 107, 136, 167, 160, 175, 154, 134, 88,  77,  110, 117, 85,  86,
    139, 152, 161, 175, 150, 123, 118, 119, 105, 86,  106, 122, 120, 130, 164,
    146, 135, 142, 146, 155, 120, 126, 112, 101, 108, 156, 147, 157, 143, 144,
    142, 118, 148, 134, 110, 91,  128, 124, 101, 105, 121, 112, 132, 152, 145,
    118, 112, 99,  114, 103, 97,  134, 124, 143, 139, 171, 108, 100, 118, 123,
    112, 157, 180, 126, 149, 153, 167, 135, 160, 145, 139, 108, 117, 114, 89,
    122, 126, 157, 144, 156, 117, 101, 110, 145, 136, 110, 99,  107, 123, 108,
    140, 151, 141, 126, 138, 131, 117, 106, 119, 99,  109, 134, 135, 128, 131,
    158, 150, 131, 135, 128, 93,  111, 134, 131, 123, 122, 132, 149, 125, 145,
    153, 121, 117, 137, 137, 115, 128, 132, 124, 116, 145, 124, 104, 130, 132,
    149, 135, 125, 159, 142, 128, 148, 124, 104, 109, 118, 111, 113, 119, 127,
    104, 131, 143, 122, 128, 114, 139, 128, 125, 122, 130, 113, 118, 141, 130,
    140, 145, 141, 125, 127, 131, 128, 109, 136, 136, 127, 126, 142, 137, 135,
    138, 132, 119, 106, 133, 126, 125, 130, 144, 136, 128, 138, 144, 138, 133,
    134, 126, 99,  110, 120, 115, 117, 128, 142, 124, 133, 128, 129, 113, 124,
    134, 126, 128, 128, 133, 126, 132, 135, 122, 113, 119, 122, 115, 119, 132,
    130, 131, 131, 131, 138, 128, 132, 129, 125, 124, 123, 113, 108, 122, 127,
    132, 140, 160, 149, 150, 148, 143, 138, 134, 129, 118, 119, 121, 121, 129,
    125, 130, 142, 128, 132, 134, 131, 115, 122, 126, 121, 125, 132, 129, 117,
    123, 123, 126, 115, 118, 114, 112, 113, 124, 122, 124, 136, 150, 148, 136,
    138, 136, 134, 125, 133, 124, 115, 126, 128, 126, 131, 133, 131, 133, 130,
    131, 128, 121, 124, 126, 129, 128, 132, 129, 129, 131, 135, 134, 122, 127,
    124, 124, 123, 126, 124, 128, 132, 133, 135, 128, 131, 130, 122, 121, 118,
    111, 117, 125, 125, 131, 140, 141, 146, 141, 145, 138, 130, 122, 118, 119,
    116, 120, 123, 125, 125, 131, 132, 129, 124, 125, 125, 125, 127, 126, 125,
    124, 130, 132, 134, 139, 143, 144, 136, 126, 124, 127, 121, 126, 126, 131,
    134, 136, 141, 134, 134, 122, 118, 114, 115, 114, 115, 118, 121, 130, 138,
    137, 136, 135, 131, 133, 132, 128, 116, 109, 110, 113, 122, 124, 125, 132,
    142, 139, 139, 141, 130, 131, 136, 132, 131, 128, 127, 129, 129, 125, 125,
    125, 128, 128, 131, 131, 127, 129, 132, 131, 131, 134, 131, 131, 127, 126,
    124, 118, 119, 119, 124, 127, 127, 131, 129, 131, 133, 139, 134, 133, 132,
    129, 122, 115, 110, 113, 117, 118, 128, 129, 129, 137, 140, 140, 137, 136,
    131, 125, 123, 116, 116, 111, 113, 121, 128, 135, 136, 138, 139, 136, 136,
    134, 131, 129, 126, 124, 126, 125, 127, 130, 129, 126, 124, 119, 119, 122,
    126, 130, 133, 141, 145, 144, 136, 127, 127, 125, 122, 122, 117, 120, 123,
    126, 130, 129, 130, 130, 131, 130, 133, 128, 125, 121, 122, 121, 122, 121,
    120, 122, 124, 131, 130, 133, 130, 130, 132, 133, 133, 133, 132, 139, 139,
    136, 127, 123, 123, 118, 120, 120, 122, 123, 128, 128, 129, 126, 129, 134,
    131, 133, 133, 132, 127, 131, 126, 125, 129, 131, 134, 134, 134, 135, 135,
    130, 125, 119, 119, 122, 125, 129, 132, 134, 132, 130, 124, 121, 120, 121,
    122, 128, 133, 134, 132, 128, 123, 119, 124, 125, 124, 125, 125, 127, 130,
    132, 132, 129, 130, 129, 130, 131, 128, 129, 129, 130, 128, 130, 131, 128,
    127, 128, 125, 126, 126, 125, 128, 130, 131, 130, 129, 129, 129, 131, 130,
    127, 129, 128, 126, 126, 131, 131, 131, 129, 128, 128, 124, 122, 124, 122,
    125, 129, 133, 132, 132, 134, 130, 134, 129, 127, 125, 125, 122, 121, 123,
    120, 124, 126, 129, 129, 130, 127, 129, 129, 126, 128, 127, 126, 126, 131,
    131, 132, 130, 126, 124, 123, 129, 129, 133, 131, 134, 136, 135, 138, 136,
    142, 131, 134, 127, 122, 121, 113, 120, 118, 127, 125, 129, 131, 131, 131,
    131, 133, 134, 130, 125, 124, 125, 126, 128, 128};
#endif

// </sample>

#include <avr/eeprom.h>

// THE FOLLOWING 308 LINES OF CODE FOLLOWS THE MIT LICENSE
//
// MIT License
//
// Copyright (c) 2019 Strick Yak
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#ifndef WHICH_PWM
#define WHICH_PWM 1 /* 1 or 4 */
#endif

#ifndef WHICH_LED
#define WHICH_LED 0
#endif

extern void Setup(void);
extern void Loop(void);

namespace nyblcore_internal {

// Timer/Counter 1 PWM Output OC1A (PB1)
struct FastPwm1Base {
  static void SetupPLL() {
    // de https://github.com/viking/attiny85-player //
    PLLCSR |= _BV(PLLE);  // Enable 64 MHz PLL (p94)
    // SpinDelay(1);         /// delayMicroseconds(100);            // Stabilize
    byte spin_tmp = 0;
    for (byte j = 0; j < 100; j++) {
      spin_tmp++;
    }
    while (!(PLLCSR & _BV(PLOCK)))
      ;                   // Wait for it... (p94)
    PLLCSR |= _BV(PCKE);  // Timer1 source = PLL
  };
};
struct FastPwm1A : public FastPwm1Base {
  static void Setup() {
    SetupPLL();

    // Set up Timer/Counter1 for PWM output
    TIMSK = 0;             // Timer interrupts OFF (p92)
    TCCR1 = _BV(PWM1A)     // Pulse Width Modulator A Enable. (p89)
            | _BV(COM1A1)  // Clear OC1A on match; set on count $00.
            | _BV(CS10);   // 1:1 prescale (p89)
    GTCCR = 0;             // Do not enable PW Modulater B. (p90)
    OCR1C = 255;           // Full 8-bit PWM cycle (p92)
    OCR1B = 0;             // Not used.
    OCR1A = 128;           // 50% duty at start

    pinMode(1, OUTPUT);  // Enable PWM output OC1A on pin PB1.
  }
  static void Output(int x) {
    OCR1A = x;  // (p92)
  }
};

struct FastPwm1B : public FastPwm1Base {
  static void Setup() {
    SetupPLL();

    // Set up Timer/Counter1 for PWM output
    TIMSK = 0;              // Timer interrupts OFF (p92)
    GTCCR = _BV(PWM1B)      // Pulse Width Modulator B Enable. (p89)
            | _BV(COM1B1);  // Clear OC1B on match; set on count $00 (p86).
    TCCR1 = _BV(CS10);      // Do not enable PW Modulater A. 1:1 prescale. (p89)
    OCR1C = 255;            // Full 8-bit PWM cycle (p92)
    OCR1A = 128;            // Not used.
    OCR1B = 128;            // 50% duty at start

    pinMode(4, OUTPUT);  // Enable PWM output OC1B on pin PB4.
  }
  static void Output(int x) {
    OCR1B = x;  // (p92)
  }
};

struct AnalogIn {
  static void NextInputA() {
    ADMUX = 0 |           // Use Vcc for Reference; disconnect from PB0 (p134)
            _BV(ADLAR) |  // Left-Adjust the ADC Result in ADCH (p134)
            3;            // ADC3 is PB3 is A.
  }
  static void NextInputB() {
    ADMUX = 0 |           // Use Vcc for Reference; disconnect from PB0 (p134)
            _BV(ADLAR) |  // Left-Adjust the ADC Result in ADCH (p134)
            2;            // ADC2 is PB4 is B.
  }
  static void NextInputK() {
    ADMUX = 0 |           // Use Vcc for Reference; disconnect from PB0 (p134)
            _BV(ADLAR) |  // Left-Adjust the ADC Result in ADCH (p134)
            1;            // ADC1 is PB2 is K.
  }
  static void Setup() {
    ADCSRA = _BV(ADEN);  // Enable ADC first.

    NextInputA();

    ADCSRB = 0;  // free-running ADC; not Bipolar; not reversed. (p137)

    // (p125) max ADC res: clock 50kHz to 200kHz
    // If less than 10 bits, 200khz to 1MHz.
    // We use internal 16MHz clock, so try dividing by 32.
    // 1->/2 2->/4 3->/8 4->/16 5->/32 6->/64 7->/128  (p136)
    // 7->4.88kHz   6->9.75kHz  5->19.53kHz Nyquist (freq of LED 2xToggle)
    // Use 6:   16MHz / 64 => 250kHz.
    // 250kHz / 13 => 19,230 samples per sec; nyquist 9615 Hz
    // Use 5: Can do two samples, 9615 Hz.
#define DIVISOR 5
    byte tmp = _BV(ADEN) |   // Enable the Analog-to-Digital converter (p136)
               _BV(ADATE) |  // Enable ADC auto trigger
               _BV(ADIE) |   // Enable interrupt on Conversion Complete
               DIVISOR;
    // ADCSRA = tmp;
    ADCSRA = tmp | _BV(ADSC);  // Start the fist conversion
  }
  static byte Input() {
    return ADCH;  // Just 8-bit.  Assumes ADLAR is 1.
  }
};

#if WHICH_PWM == 1
FastPwm1A pwm;
#else
FastPwm1B pwm;
#endif

volatile byte AnalogA, AnalogB, AnalogK;
AnalogIn in;
volatile byte adc_counter;
volatile bool adc_switch;

ISR(ADC_vect) {
  if (adc_switch) {
    ++adc_counter;
    adc_switch = false;
    // One time out of 256 we sample K.  The rest we sample B.
    if (adc_counter == 2) {
      AnalogB = ADCH;  // on 2, we still save B, but request K next.
      AnalogIn::NextInputK();
    } else if (adc_counter == 3) {
      AnalogK = ADCH;  // on 3, we save K, but request B again.
      AnalogIn::NextInputB();
    } else {
      AnalogB = ADCH;
      AnalogIn::NextInputB();
    }
  } else {
    adc_switch = true;
    AnalogA = ADCH;
    AnalogIn::NextInputA();
  }
}

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(WHICH_LED, OUTPUT);
  pinMode(WHICH_PWM, OUTPUT);

  pwm.Setup();
  in.Setup();

  ::Setup();  // Call user's Setup.
}

void loop() {
  // no need to return; just loop here.

  while (true) {
    // Wait for the next Analog sample in.
    {
      byte old_counter = adc_counter;
      byte c;
      do {
        c = adc_counter;
      } while (old_counter == c);
      if (byte(old_counter + 1) == c) {
      } else {
        // Fault on overruns.  ignore
      }
      old_counter = c;
    }

    ::Loop();  // Call user's Loop.
  }
}

}  // namespace nyblcore_internal

void setup() { nyblcore_internal::setup(); }
void loop() { nyblcore_internal::loop(); }

// public wrappers
inline byte InA() { return nyblcore_internal::AnalogA; }
inline byte InB() { return nyblcore_internal::AnalogB; }
inline byte InR() {
  return nyblcore_internal::AnalogK;
}  // R was old name for K.
inline byte InK() { return nyblcore_internal::AnalogK; }
inline void OutF(byte b) { nyblcore_internal::pwm.Output(b); }

#define IN_A() (nyblcore_internal::AnalogA)
#define IN_B() (nyblcore_internal::AnalogB)
#define IN_R() (nyblcore_internal::AnalogK)  // R was old name for K.
#define IN_K() (nyblcore_internal::AnalogK)
#define OUT_F(B) (nyblcore_internal::pwm.Output(B))

// using nyblcore_internal::SpinDelay;
// using nyblcore_internal::SpinDelayFast;

namespace nyblcore_random {

typedef struct rc4_key {
  unsigned char state[256];
  unsigned char x;
  unsigned char y;
} rc4_key;

void swap_byte(unsigned char *a, unsigned char *b) {
  unsigned char swapByte;

  swapByte = *a;
  *a = *b;
  *b = swapByte;
}

void prepare_key(unsigned char *key_data_ptr, int key_data_len, rc4_key *key) {
  unsigned char swapByte;
  unsigned char index1;
  unsigned char index2;
  unsigned char *state;
  short counter;

  state = &key->state[0];
  for (counter = 0; counter < 256; counter++) state[counter] = counter;
  key->x = 0;
  key->y = 0;
  index1 = 0;
  index2 = 0;
  for (counter = 0; counter < 256; counter++) {
    index2 = (key_data_ptr[index1] + state[counter] + index2) % 256;
    swap_byte(&state[counter], &state[index2]);

    index1 = (index1 + 1) % key_data_len;
  }
}

void rc4(unsigned char *buffer_ptr, int buffer_len, rc4_key *key) {
  unsigned char x;
  unsigned char y;
  unsigned char *state;
  unsigned char xorIndex;
  short counter;

  x = key->x;
  y = key->y;

  state = &key->state[0];
  for (counter = 0; counter < buffer_len; counter++) {
    x = (x + 1) % 256;
    y = (state[x] + y) % 256;
    swap_byte(&state[x], &state[y]);

    xorIndex = state[x] + (state[y]) % 256;

    buffer_ptr[counter] ^= state[xorIndex];
  }
  key->x = x;
  key->y = y;
}

rc4_key Engine;

}  // namespace nyblcore_random

void RandomSetup() {
  nyblcore_random::prepare_key("RandomSetup", 12, &nyblcore_random::Engine);
}

byte RandomByte() {
  unsigned char buf[1] = {0};
  nyblcore_random::rc4(buf, 1, &nyblcore_random::Engine);
  return buf[0];
}

//
// THE PREVIOUS CODE FOLLOWS THE MIT LICENSE
//

#define SHIFTY 6
#define PARM1 30
#define PARM2 220

byte distortion = 0;
byte volume_reduce = 0;  // volume 0 to 6
byte volume_mod = 0;
byte thresh_counter = 0;
byte thresh_next = 3;
bool thresh_nibble = 0;
word phase_sample = 0;
word phase_sample_last = 11;
byte thresh_counter_t = 0;
byte thresh_next_t = 3;
bool thresh_nibble_t = 0;
word phase_t = 0;
byte select_sample = 0;
byte select_sample_start = 0;
byte select_sample_end = NUM_SAMPLES - 1;
bool direction = 1;  // 0 = reverse, 1 = forward
byte retrig = 4;
byte tempo = 12;
int audio_last = 0;
int audio_next = -1;
byte audio_now = 0;
byte audio_played = 0;
char audio_add = 0;
char stretch_amt = 0;
word stretch_add = 0;
byte knobB_last = 0;
byte knobA_last = 0;
byte knobK_last = 0;
byte probability = 0;
bool do_retrigger = false;
bool do_stutter = false;
bool do_stretch = false;
bool do_stretch_slow = true;
byte do_retriggerp = false;
byte do_stutterp = false;
byte do_stretchp = false;
byte bcount = 0;
byte lastMoved = 0;
bool firstrun = true;
word debounce_eeprom = 0;

#define NUM_TEMPOS 19
const byte tempo_steps[] = {0xAA, 0xA9, 0x99, 0x98, 0x88, 0x87, 0x77,
                            0x76, 0x66, 0x65, 0x55, 0x54, 0x44, 0x43,
                            0x33, 0x32, 0x22, 0x21, 0x11};

void Setup() { RandomSetup(); }

void Loop() {
  byte knobA = InA();
  byte knobK = InK();
  byte knobB = InB();
  if (firstrun) {
    firstrun = false;
    if (eeprom_read_byte((uint8_t *)1) == 18) {
      tempo = eeprom_read_byte((uint8_t *)0);
      if (tempo > NUM_TEMPOS) tempo = 13;
      volume_reduce = eeprom_read_byte((uint8_t *)2);
      if (volume_reduce > 10) volume_reduce = 0;
      distortion = eeprom_read_byte((uint8_t *)3);
      if (distortion > 55) distortion = 0;
      probability = eeprom_read_byte((uint8_t *)4);
      if (probability > 128) probability = 0;
      do_stretchp = eeprom_read_byte((uint8_t *)5);
      if (do_stretchp > 128) do_stretchp = 0;
      do_retriggerp = eeprom_read_byte((uint8_t *)6);
      if (do_retriggerp > 63) do_retriggerp = 0;
      do_stutterp = eeprom_read_byte((uint8_t *)7);
      if (do_stutterp > 63) do_stutterp = 0;
    } else {
      eeprom_write_byte((uint8_t *)1, 18);
    }
    knobA_last = knobA;
    knobK_last = knobK;
    knobB_last = knobB;
  } else if (debounce_eeprom > 0) {
    debounce_eeprom--;
    if (debounce_eeprom == 0) {
      eeprom_write_byte((uint8_t *)0, tempo);
      eeprom_write_byte((uint8_t *)2, volume_reduce);
      eeprom_write_byte((uint8_t *)3, distortion);
      eeprom_write_byte((uint8_t *)4, probability);
      eeprom_write_byte((uint8_t *)5, do_stretchp);
      eeprom_write_byte((uint8_t *)6, do_retriggerp);
      eeprom_write_byte((uint8_t *)7, do_stutterp);
    }
  }
  bcount++;
  byte bthresh = knobA;
  if (lastMoved == 1) {
    bthresh = knobK;
  } else if (lastMoved == 2) {
    bthresh = knobB;
  }
  if (bcount < bthresh) {
    digitalWrite(WHICH_LED, HIGH);
  } else {
    digitalWrite(WHICH_LED, LOW);
  }
  if (bcount == 255) {
    bcount = 0;
  }

  if (phase_sample_last != phase_sample) {
    audio_last = ((int)pgm_read_byte(SAMPLE_TABLE + phase_sample)) << SHIFTY;
    if (thresh_next > thresh_counter) {
      audio_next = ((int)pgm_read_byte(SAMPLE_TABLE + phase_sample +
                                       (direction * 2 - 1)))
                   << SHIFTY;
      audio_next =
          (audio_next - audio_last) / ((int)(thresh_next - thresh_counter));
    } else {
      audio_next = 0;
    }
    audio_add = 0;
    phase_sample_last = phase_sample;
  }
  // no interpolation
  // OutF(audio_last >> SHIFTY);

  if (knobK > knobK_last + 5 || knobK < knobK_last - 5) {
    lastMoved = 1;
    knobK_last = knobK;
  }
  if (knobA > knobA_last + 5 || knobA < knobA_last - 5) {
    lastMoved = 0;
    knobA_last = knobA;
    // update the left parameter
    if (knobK < PARM1) {
      // volume
      if (knobA <= 200) {
        volume_reduce = (200 - knobA) * 10 / 200;  // 0-220 -> 10-0
        distortion = 0;
      } else {
        volume_reduce = 0;
        distortion = knobA - 200;  // 200-255 -> 0-55
      }
    } else if (knobK < PARM2) {
      probability = knobA / 2;  // 0-255 -> 0-100
    } else {
      do_stretchp = knobA / 2;
    }
    debounce_eeprom = 65534;
  }
  if (knobB > knobB_last + 5 || knobB < knobB_last - 5) {
    lastMoved = 2;
    knobB_last = knobB;
    // update the right parameter
    if (knobK < PARM1) {
      tempo = knobB * NUM_TEMPOS / 255;
    } else if (knobK < PARM2) {
      do_retriggerp = knobB / 4;
    } else {
      do_stutterp = knobB / 4;
    }
    debounce_eeprom = 65534;
  }

  // linear interpolation with shifts
  audio_now = (audio_last + audio_add) >> SHIFTY;

  // mute audio if volume_reduce==10
  if (volume_reduce == 10) audio_now = 128;

  // if not muted, make some actions
  if (audio_now != 128) {
    // distortion / wave-folding
    if (distortion > 0) {
      if (audio_now > 128) {
        if (audio_now < (255 - distortion)) {
          audio_now += distortion;
        } else {
          audio_now = 255 - distortion;
        }
        // too much cpu
        // audio_now = 128 + ((audio_now - 128) / ((distortion >> 4) + 1));
      } else {
        if (audio_now > distortion) {
          audio_now -= distortion;
        } else {
          audio_now = distortion - audio_now;
        }
        // too much cpu
        // audio_now = 128 - ((128 - audio_now) / ((distortion >> 4) + 1));
      }
    }
    // reduce volume
    if ((volume_reduce + volume_mod) > 0) {
      if (audio_now > 128) {
        audio_now = ((audio_now - 128) >> (volume_reduce + volume_mod)) + 128;
      } else {
        audio_now = 128 - ((128 - audio_now) >> (volume_reduce + volume_mod));
      }
    }
  }
  // click preventer??
  if (abs(audio_now - audio_played) > 32) {
    audio_now = (audio_now + audio_played) / 2;
    if (abs(audio_now - audio_played) > 32) {
      audio_now = (audio_now + audio_played) / 2;
    }
  }
  OutF(audio_now);
  audio_played = audio_now;
  // for linear interpolation
  audio_add = audio_add + audio_next;

  // audio sample iterations
  thresh_counter++;
  if (thresh_counter == thresh_next) {
    thresh_counter = 0;
    thresh_nibble = 1 - thresh_nibble;
    thresh_next = tempo_steps[tempo - stretch_amt];
    if (thresh_nibble) {
      thresh_next = (byte)(thresh_next & 0xF0) >> 4;
    } else {
      thresh_next = (byte)(thresh_next & 0x0F);
    }

    // determine directions
    if (direction) {
      // forward
      if (phase_sample < pos[NUM_SAMPLES]) {
        phase_sample++;
      } else {
        phase_sample = 0;
      }
    } else {
      // reverse
      if (phase_sample == 0) {
        phase_sample = pos[NUM_SAMPLES];
      } else {
        phase_sample--;
      }
    }
  }

  // tempo clocked effects
  thresh_counter_t++;
  if (thresh_counter_t == thresh_next_t) {
    thresh_counter_t = 0;
    thresh_nibble_t = 1 - thresh_nibble_t;
    thresh_next_t = tempo_steps[tempo];
    if (thresh_nibble_t) {
      thresh_next_t = (byte)(thresh_next_t & 0xF0) >> 4;
    } else {
      thresh_next_t = (byte)(thresh_next_t & 0x0F);
    }

    phase_t++;
    if (phase_t == retrigs[retrig]) {
      phase_t = 0;
      // randoms
      byte r1 = RandomByte();
      byte r2 = RandomByte();
      byte r3 = RandomByte();
      byte r4 = RandomByte();

      if (select_sample == 0) {
        do_stretch_slow = r1 < 200;
      }
      // do stretching
      if (do_stretchp > 10) {
        do_stretch = (r1 < do_stretchp);
      } else {
        do_stretch = false;
      }
      if (do_stretch) {
        if (do_stretch_slow) {
          stretch_amt++;
        } else {
          stretch_amt--;
        }
        if (tempo - 1 < stretch_amt) {
          stretch_amt = tempo - 1;
        } else if (tempo - NUM_TEMPOS + 1 > stretch_amt) {
          stretch_amt = tempo - NUM_TEMPOS + 1;
        }
      } else {
        stretch_amt = 0;
      }

      if (volume_mod > 0) {
        if (volume_mod > 3 || r3 < 200) {
          volume_mod--;
        }
      } else {
        // randomize direction
        if (direction == 1) {
          if (r1 < probability / 4) {
            direction = 0;
          }
        } else {
          if (r1 < probability || probability < 10) {
            direction = 1;
          }
        }

        // random retrig
        if (r2 < probability / 4) {
          retrig = 6;
        } else if (r2 < probability / 3) {
          retrig = 5;
        } else if (r2 < probability / 2) {
          retrig = 3;
        } else if (r2 < probability) {
          retrig = 2;
        } else {
          retrig = 4;
        }

        if (do_retrigger == false && do_stutter == false) {
          // select new sample based on direction
          if (direction == 1) select_sample++;
          if (direction == 0) {
            if (select_sample == 0) {
              select_sample = select_sample_end;
            } else {
              select_sample--;
            }
          }

          // make sure the new sample is not out of bounds
          if (select_sample < select_sample_start)
            select_sample = select_sample_end;
          if (select_sample > select_sample_end)
            select_sample = select_sample_start;

          // random jump
          if (r3 < probability / 2) {
            // thresh_next = thresh_next + (r1 * 4 / 255);
            // retrig = (RandomByte() * 6 / 255);
            select_sample = (RandomByte() * NUM_SAMPLES) / 255;
          }
        }

        if (do_retriggerp > 10) {
          do_retrigger = (r4 < do_retriggerp);
        } else {
          do_retrigger = false;
        }
        if (do_retrigger) {
          retrig = 6;
          if (r2 < 120) {
            retrig = 5;
          }
        }

        if (do_stutterp > 10) {
          do_stutter = (r3 < do_stutterp);
        } else {
          do_stutter = false;
        }
        if (do_stutter) {
          if (volume_mod == 0) {
            volume_mod = r2 * 6 / 255;
            if (r3 < 120) {
              retrig = 5;
            } else {
              retrig = 6;
            }
          }
        }
      }
      phase_sample = pos[select_sample];
    }
  }
}