| 1 | // Talkie library |
|---|
| 2 | // Copyright 2011 Peter Knight |
|---|
| 3 | // This code is released under GPLv2 license. |
|---|
| 4 | |
|---|
| 5 | #include "Adafruit_CPlay_Speaker.h" |
|---|
| 6 | |
|---|
| 7 | #define FS 8000 // Speech engine sample rate |
|---|
| 8 | #define TICKS (FS / 40) // Speech data rate |
|---|
| 9 | #define USEC ((1000000 + (FS / 2)) / FS) // Sample period (microseconds) |
|---|
| 10 | |
|---|
| 11 | static const uint16_t PROGMEM |
|---|
| 12 | tmsK1[] = {0x82C0,0x8380,0x83C0,0x8440,0x84C0,0x8540,0x8600,0x8780, |
|---|
| 13 | 0x8880,0x8980,0x8AC0,0x8C00,0x8D40,0x8F00,0x90C0,0x92C0, |
|---|
| 14 | 0x9900,0xA140,0xAB80,0xB840,0xC740,0xD8C0,0xEBC0,0x0000, |
|---|
| 15 | 0x1440,0x2740,0x38C0,0x47C0,0x5480,0x5EC0,0x6700,0x6D40}, |
|---|
| 16 | tmsK2[] = {0xAE00,0xB480,0xBB80,0xC340,0xCB80,0xD440,0xDDC0,0xE780, |
|---|
| 17 | 0xF180,0xFBC0,0x0600,0x1040,0x1A40,0x2400,0x2D40,0x3600, |
|---|
| 18 | 0x3E40,0x45C0,0x4CC0,0x5300,0x5880,0x5DC0,0x6240,0x6640, |
|---|
| 19 | 0x69C0,0x6CC0,0x6F80,0x71C0,0x73C0,0x7580,0x7700,0x7E80}; |
|---|
| 20 | |
|---|
| 21 | static const uint8_t PROGMEM |
|---|
| 22 | tmsK3[] = {0x92,0x9F,0xAD,0xBA,0xC8,0xD5,0xE3,0xF0, |
|---|
| 23 | 0xFE,0x0B,0x19,0x26,0x34,0x41,0x4F,0x5C}, |
|---|
| 24 | tmsK4[] = {0xAE,0xBC,0xCA,0xD8,0xE6,0xF4,0x01,0x0F, |
|---|
| 25 | 0x1D,0x2B,0x39,0x47,0x55,0x63,0x71,0x7E}, |
|---|
| 26 | tmsK5[] = {0xAE,0xBA,0xC5,0xD1,0xDD,0xE8,0xF4,0xFF, |
|---|
| 27 | 0x0B,0x17,0x22,0x2E,0x39,0x45,0x51,0x5C}, |
|---|
| 28 | tmsK6[] = {0xC0,0xCB,0xD6,0xE1,0xEC,0xF7,0x03,0x0E, |
|---|
| 29 | 0x19,0x24,0x2F,0x3A,0x45,0x50,0x5B,0x66}, |
|---|
| 30 | tmsK7[] = {0xB3,0xBF,0xCB,0xD7,0xE3,0xEF,0xFB,0x07, |
|---|
| 31 | 0x13,0x1F,0x2B,0x37,0x43,0x4F,0x5A,0x66}, |
|---|
| 32 | tmsK8[] = {0xC0,0xD8,0xF0,0x07,0x1F,0x37,0x4F,0x66}, |
|---|
| 33 | tmsK9[] = {0xC0,0xD4,0xE8,0xFC,0x10,0x25,0x39,0x4D}, |
|---|
| 34 | tmsK10[] = {0xCD,0xDF,0xF1,0x04,0x16,0x20,0x3B,0x4D}, |
|---|
| 35 | chirp[] = {0x00,0x2A,0xD4,0x32,0xB2,0x12,0x25,0x14, |
|---|
| 36 | 0x02,0xE1,0xC5,0x02,0x5F,0x5A,0x05,0x0F, |
|---|
| 37 | 0x26,0xFC,0xA5,0xA5,0xD6,0xDD,0xDC,0xFC, |
|---|
| 38 | 0x25,0x2B,0x22,0x21,0x0F,0xFF,0xF8,0xEE, |
|---|
| 39 | 0xED,0xEF,0xF7,0xF6,0xFA,0x00,0x03,0x02,0x01}; |
|---|
| 40 | |
|---|
| 41 | static const uint8_t PROGMEM |
|---|
| 42 | tmsEnergy[] = {0x00,0x02,0x03,0x04,0x05,0x07,0x0A,0x0F, |
|---|
| 43 | 0x14,0x20,0x29,0x39,0x51,0x72,0xA1,0xFF}, |
|---|
| 44 | tmsPeriod[] = {0x00,0x10,0x11,0x12,0x13,0x14,0x15,0x16, |
|---|
| 45 | 0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E, |
|---|
| 46 | 0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26, |
|---|
| 47 | 0x27,0x28,0x29,0x2A,0x2B,0x2D,0x2F,0x31, |
|---|
| 48 | 0x33,0x35,0x36,0x39,0x3B,0x3D,0x3F,0x42, |
|---|
| 49 | 0x45,0x47,0x49,0x4D,0x4F,0x51,0x55,0x57, |
|---|
| 50 | 0x5C,0x5F,0x63,0x66,0x6A,0x6E,0x73,0x77, |
|---|
| 51 | 0x7B,0x80,0x85,0x8A,0x8F,0x95,0x9A,0xA0}; |
|---|
| 52 | |
|---|
| 53 | static const uint8_t *ptrAddr; |
|---|
| 54 | static uint16_t buf; |
|---|
| 55 | static uint8_t bufBits; |
|---|
| 56 | |
|---|
| 57 | static inline uint8_t rev(uint8_t a) { // Reverse bit sequence in 8-bit value |
|---|
| 58 | a = ( a >> 4) | ( a << 4); // 76543210 -> 32107654 |
|---|
| 59 | a = ((a & 0xCC) >> 2) | ((a & 0x33) << 2); // 32107654 -> 10325476 |
|---|
| 60 | a = ((a & 0xAA) >> 1) | ((a & 0x55) << 1); // 10325476 -> 01234567 |
|---|
| 61 | return a; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | static uint8_t getBits(uint8_t bits) { |
|---|
| 65 | uint8_t value; |
|---|
| 66 | if(bits > bufBits) { |
|---|
| 67 | buf |= rev(pgm_read_byte(ptrAddr)) << (8 - bufBits); |
|---|
| 68 | bufBits += 8; |
|---|
| 69 | ptrAddr++; // Don't post-inc in pgm_read_byte! Is a macro. |
|---|
| 70 | } |
|---|
| 71 | value = buf >> (16 - bits); |
|---|
| 72 | buf <<= bits; |
|---|
| 73 | bufBits -= bits; |
|---|
| 74 | return value; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | #define read8(base, bits) pgm_read_byte(&base[getBits(bits)]); |
|---|
| 78 | #define read16(base, bits) pgm_read_word(&base[getBits(bits)]); |
|---|
| 79 | |
|---|
| 80 | /**************************************************************************/ |
|---|
| 81 | /*! |
|---|
| 82 | @brief speak the data at the passed location |
|---|
| 83 | @param addr pointer to the data |
|---|
| 84 | */ |
|---|
| 85 | /**************************************************************************/ |
|---|
| 86 | void Adafruit_CPlay_Speaker::say(const uint8_t *addr) { |
|---|
| 87 | |
|---|
| 88 | int16_t x0=0, x1=0, x2=0, x3=0, x4=0, |
|---|
| 89 | x5=0, x6=0, x7=0, x8=0, x9=0, |
|---|
| 90 | synthK1, synthK2, u0; |
|---|
| 91 | uint16_t synthEnergy, synthRand = 1; |
|---|
| 92 | int8_t synthK3, synthK4, synthK5, synthK6, |
|---|
| 93 | synthK7, synthK8, synthK9, synthK10; |
|---|
| 94 | uint8_t periodCounter=0, nextPwm = 0x7F, synthPeriod; |
|---|
| 95 | uint8_t iCount = TICKS; |
|---|
| 96 | uint32_t nowTime, prevTime=0; |
|---|
| 97 | |
|---|
| 98 | if(!started) begin(); |
|---|
| 99 | |
|---|
| 100 | buf = bufBits = 0; // Reset 'ROM' reader (global stuff) |
|---|
| 101 | ptrAddr = addr; |
|---|
| 102 | |
|---|
| 103 | for(;;) { |
|---|
| 104 | while(((nowTime = micros()) - prevTime) < USEC); |
|---|
| 105 | #ifdef __AVR__ |
|---|
| 106 | OCR4A = nextPwm; |
|---|
| 107 | #else |
|---|
| 108 | analogWrite(A0, nextPwm); |
|---|
| 109 | #endif |
|---|
| 110 | prevTime = nowTime; |
|---|
| 111 | |
|---|
| 112 | if(++iCount >= TICKS) { |
|---|
| 113 | // Read speech data, processing the variable size frames |
|---|
| 114 | uint8_t energy; |
|---|
| 115 | if((energy = getBits(4)) == 0) { // Rest frame |
|---|
| 116 | synthEnergy = 0; |
|---|
| 117 | } else if(energy == 0xF) { // Stop frame; silence |
|---|
| 118 | #ifdef __AVR__ |
|---|
| 119 | TCCR4A = 0x7F; |
|---|
| 120 | #else |
|---|
| 121 | analogWrite(A0, 0x7F); |
|---|
| 122 | #endif |
|---|
| 123 | break; |
|---|
| 124 | } else { |
|---|
| 125 | synthEnergy = pgm_read_byte(&tmsEnergy[energy]); |
|---|
| 126 | uint8_t repeat = getBits(1); |
|---|
| 127 | synthPeriod = pgm_read_byte(&tmsPeriod[getBits(6)]); |
|---|
| 128 | if(!repeat) { // A repeat frame uses last coefficients |
|---|
| 129 | synthK1 = read16(tmsK1, 5); // All frames |
|---|
| 130 | synthK2 = read16(tmsK2, 5); // use the first |
|---|
| 131 | synthK3 = read8( tmsK3, 4); // 4 coefficients |
|---|
| 132 | synthK4 = read8( tmsK4, 4); |
|---|
| 133 | if(synthPeriod) { |
|---|
| 134 | synthK5 = read8(tmsK5 , 4); // Voiced |
|---|
| 135 | synthK6 = read8(tmsK6 , 4); // frames |
|---|
| 136 | synthK7 = read8(tmsK7 , 4); // use |
|---|
| 137 | synthK8 = read8(tmsK8 , 3); // six |
|---|
| 138 | synthK9 = read8(tmsK9 , 3); // extra |
|---|
| 139 | synthK10 = read8(tmsK10, 3); // coeffs |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | iCount = 0; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | if(synthPeriod) { // Voiced source |
|---|
| 147 | if(++periodCounter >= synthPeriod) periodCounter = 0; |
|---|
| 148 | u0 = (periodCounter >= sizeof(chirp)) ? 0 : |
|---|
| 149 | (pgm_read_byte(&chirp[periodCounter]) * |
|---|
| 150 | (uint32_t)synthEnergy) >> 8; |
|---|
| 151 | } else { // Unvoiced source |
|---|
| 152 | synthRand = (synthRand >> 1) ^ ((synthRand & 1) ? 0xB800 : 0); |
|---|
| 153 | u0 = (synthRand & 1) ? synthEnergy : -synthEnergy; |
|---|
| 154 | } |
|---|
| 155 | u0 -= ((synthK10 * x9) + |
|---|
| 156 | (synthK9 * x8)) >> 7; |
|---|
| 157 | x9 = x8 + ((synthK9 * u0 ) >> 7); |
|---|
| 158 | u0 -= ((synthK8 * x7 ) >> 7); |
|---|
| 159 | x8 = x7 + ((synthK8 * u0 ) >> 7); |
|---|
| 160 | u0 -= ((synthK7 * x6 ) >> 7); |
|---|
| 161 | x7 = x6 + ((synthK7 * u0 ) >> 7); |
|---|
| 162 | u0 -= ((synthK6 * x5 ) >> 7); |
|---|
| 163 | x6 = x5 + ((synthK6 * u0 ) >> 7); |
|---|
| 164 | u0 -= ((synthK5 * x4 ) >> 7); |
|---|
| 165 | x5 = x4 + ((synthK5 * u0 ) >> 7); |
|---|
| 166 | u0 -= ((synthK4 * x3 ) >> 7); |
|---|
| 167 | x4 = x3 + ((synthK4 * u0 ) >> 7); |
|---|
| 168 | u0 -= ((synthK3 * x2 ) >> 7); |
|---|
| 169 | x3 = x2 + ((synthK3 * u0 ) >> 7); |
|---|
| 170 | u0 -= ((synthK2 * (int32_t)x1 ) >> 15); |
|---|
| 171 | x2 = x1 + ((synthK2 * (int32_t)u0 ) >> 15); |
|---|
| 172 | u0 -= ((synthK1 * (int32_t)x0 ) >> 15); |
|---|
| 173 | x1 = x0 + ((synthK1 * (int32_t)u0 ) >> 15); |
|---|
| 174 | |
|---|
| 175 | if( u0 > 511) u0 = 511; // Output clamp |
|---|
| 176 | else if(u0 < -512) u0 = -512; |
|---|
| 177 | |
|---|
| 178 | x0 = u0; |
|---|
| 179 | nextPwm = (u0 >> 2) + 0x80; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|