Browse Source

without numbers

master
Avital 3 years ago
parent
commit
f267028af1

+ 52
- 0
sketches/avital/asta_ammm/asta_ammm.ino
File diff suppressed because it is too large
View File


+ 308
- 0
sketches/avital/asta_ammm/talkie.cpp View File

@@ -0,0 +1,308 @@
1
+// Talkie library 
2
+// Copyright 2011 Peter Knight
3
+// Adafruit library modified by Jean-Luc Deladrière
4
+// This code is released under GPLv2 license.
5
+
6
+
7
+// Though the Wave Shield DAC pins are configurable, much else in this code
8
+// is still very Uno-specific; the timers and timer control registers, the
9
+// PWM output pin, etc.  Compatibility with other boards is not guaranteed.
10
+
11
+#include "talkie.h"
12
+
13
+#define PIEZO         // If set, connect piezo on pins 3 & 11, is louder
14
+
15
+//#define FS    8000      // Speech engine sample rate
16
+#define FS map(analogRead(1),0,1023,4000,16000)
17
+
18
+//#define TICKS (FS / 40) // Speech data rate
19
+#define TICKS 20 // Speech data rate
20
+//#define TICKS map(analogRead(0),0,700,20,800)
21
+
22
+// Some of these variables could go in the Talkie object, but the hardware
23
+// specificity (reliance on certain timers and/or PWM pins) kills any point
24
+// in multiple instances; there can be only one.  So they're declared as
25
+// static here to keep the header simple and self-documenting.
26
+//#if TICKS < 255
27
+//static volatile uint8_t  interruptCount;
28
+//#else
29
+static volatile uint16_t interruptCount;
30
+static volatile uint16_t slower;
31
+//#endif
32
+static volatile uint8_t  *csPort, *clkPort, *datPort;
33
+static volatile uint16_t synthEnergy;
34
+static volatile int16_t  synthK1, synthK2;
35
+static volatile int8_t   synthK3, synthK4, synthK5, synthK6,
36
+                         synthK7, synthK8, synthK9, synthK10;
37
+static uint16_t          buf, synthRand = 1;
38
+static int16_t           x0, x1, x2, x3, x4, x5, x6, x7, x8, x9;
39
+static uint8_t           periodCounter, nextPwm = 0x80,
40
+                         synthPeriod, bufBits,
41
+                         csBitMask, clkBitMask, datBitMask;
42
+static const uint8_t     *ptrAddr;
43
+
44
+static const int16_t PROGMEM
45
+  tmsK1[]     = {0x82C0,0x8380,0x83C0,0x8440,0x84C0,0x8540,0x8600,0x8780,
46
+                 0x8880,0x8980,0x8AC0,0x8C00,0x8D40,0x8F00,0x90C0,0x92C0,
47
+                 0x9900,0xA140,0xAB80,0xB840,0xC740,0xD8C0,0xEBC0,0x0000,
48
+                 0x1440,0x2740,0x38C0,0x47C0,0x5480,0x5EC0,0x6700,0x6D40},
49
+  tmsK2[]     = {0xAE00,0xB480,0xBB80,0xC340,0xCB80,0xD440,0xDDC0,0xE780,
50
+                 0xF180,0xFBC0,0x0600,0x1040,0x1A40,0x2400,0x2D40,0x3600,
51
+                 0x3E40,0x45C0,0x4CC0,0x5300,0x5880,0x5DC0,0x6240,0x6640,
52
+                 0x69C0,0x6CC0,0x6F80,0x71C0,0x73C0,0x7580,0x7700,0x7E80};
53
+
54
+static const int8_t PROGMEM
55
+  tmsK3[]     = {0x92,0x9F,0xAD,0xBA,0xC8,0xD5,0xE3,0xF0,
56
+                 0xFE,0x0B,0x19,0x26,0x34,0x41,0x4F,0x5C},
57
+  tmsK4[]     = {0xAE,0xBC,0xCA,0xD8,0xE6,0xF4,0x01,0x0F,
58
+                 0x1D,0x2B,0x39,0x47,0x55,0x63,0x71,0x7E},
59
+  tmsK5[]     = {0xAE,0xBA,0xC5,0xD1,0xDD,0xE8,0xF4,0xFF,
60
+                 0x0B,0x17,0x22,0x2E,0x39,0x45,0x51,0x5C},
61
+  tmsK6[]     = {0xC0,0xCB,0xD6,0xE1,0xEC,0xF7,0x03,0x0E,
62
+                 0x19,0x24,0x2F,0x3A,0x45,0x50,0x5B,0x66},
63
+  tmsK7[]     = {0xB3,0xBF,0xCB,0xD7,0xE3,0xEF,0xFB,0x07,
64
+                 0x13,0x1F,0x2B,0x37,0x43,0x4F,0x5A,0x66},
65
+  tmsK8[]     = {0xC0,0xD8,0xF0,0x07,0x1F,0x37,0x4F,0x66},
66
+  tmsK9[]     = {0xC0,0xD4,0xE8,0xFC,0x10,0x25,0x39,0x4D},
67
+  tmsK10[]    = {0xCD,0xDF,0xF1,0x04,0x16,0x20,0x3B,0x4D},
68
+  chirp[]     = {0x00,0x2A,0xD4,0x32,0xB2,0x12,0x25,0x14,
69
+                 0x02,0xE1,0xC5,0x02,0x5F,0x5A,0x05,0x0F,
70
+                 0x26,0xFC,0xA5,0xA5,0xD6,0xDD,0xDC,0xFC,
71
+                 0x25,0x2B,0x22,0x21,0x0F,0xFF,0xF8,0xEE,
72
+                 0xED,0xEF,0xF7,0xF6,0xFA,0x00,0x03,0x02,0x01};
73
+
74
+static const uint8_t PROGMEM
75
+  tmsEnergy[] = {0x00,0x02,0x03,0x04,0x05,0x07,0x0A,0x0F,
76
+                 0x14,0x20,0x29,0x39,0x51,0x72,0xA1,0xFF},
77
+  tmsPeriod[] = {0x00,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
78
+                 0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,
79
+                 0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,
80
+                 0x27,0x28,0x29,0x2A,0x2B,0x2D,0x2F,0x31,
81
+                 0x33,0x35,0x36,0x39,0x3B,0x3D,0x3F,0x42,
82
+                 0x45,0x47,0x49,0x4D,0x4F,0x51,0x55,0x57,
83
+                 0x5C,0x5F,0x63,0x66,0x6A,0x6E,0x73,0x77,
84
+                 0x7B,0x80,0x85,0x8A,0x8F,0x95,0x9A,0xA0};
85
+
86
+// Constructor for PWM mode
87
+Talkie::Talkie(void) {
88
+#if defined(__AVR_ATmega32U4__)
89
+	pinMode(5, OUTPUT);  // !OC4A
90
+#else
91
+	pinMode(3, OUTPUT);  // OC2B
92
+#endif
93
+#ifdef PIEZO
94
+	pinMode(11, OUTPUT); // OC2A
95
+#endif
96
+	csBitMask = 0;       // DAC not in use
97
+}
98
+
99
+// Constructor for DAC mode
100
+Talkie::Talkie(uint8_t cs, uint8_t clk, uint8_t dat) {
101
+	csPort     = portOutputRegister(digitalPinToPort(cs));
102
+	csBitMask  = digitalPinToBitMask(cs);
103
+	clkPort    = portOutputRegister(digitalPinToPort(clk));
104
+	clkBitMask = digitalPinToBitMask(clk);
105
+	datPort    = portOutputRegister(digitalPinToPort(dat));
106
+	datBitMask = digitalPinToBitMask(dat);
107
+	pinMode(cs , OUTPUT);
108
+	pinMode(clk, OUTPUT);
109
+	pinMode(dat, OUTPUT);
110
+	*csPort   |=  csBitMask;  // Deselect
111
+	*clkPort  &= ~clkBitMask; // Clock low
112
+}
113
+
114
+void Talkie::say(const uint8_t *addr, boolean block) {
115
+
116
+	// Enable the speech system whenever say() is called.
117
+
118
+	if(!csBitMask) {
119
+#if defined(__AVR_ATmega32U4__)
120
+		// Set up Timer4 for fast PWM on !OC4A
121
+		PLLFRQ = (PLLFRQ & 0xCF) | 0x30;   // Route PLL to async clk
122
+		TCCR4A = _BV(COM4A0) | _BV(PWM4A); // Clear on match, PWMA on
123
+		TCCR4B = _BV(PWM4X)  |_BV(CS40);   // PWM invert, 1:1 prescale
124
+		TCCR4D = 0;                        // Fast PWM mode
125
+		TCCR4E = 0;                        // Not enhanced mode
126
+		TC4H   = 0;                        // Not 10-bit mode
127
+		DT4    = 0;                        // No dead time
128
+		OCR4C  = 255;                      // TOP
129
+		OCR4A  = 127;                      // 50% duty to start
130
+#else
131
+		// Set up Timer2 for 8-bit, 62500 Hz PWM on OC2B
132
+		TCCR2A  = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
133
+		TCCR2B  = _BV(CS20); // No prescale
134
+		TIMSK2  = 0;         // No interrupt
135
+		OCR2B   = 0x80;      // 50% duty cycle
136
+#ifdef PIEZO
137
+		OCR2A   = 0x80;
138
+		TCCR2A |= _BV(COM2A1) | _BV(COM2A0); // OC2A inverting mode
139
+#endif
140
+#endif
141
+	}
142
+
143
+	// Reset synth state and 'ROM' reader
144
+	x0 = x1 = x2 = x3 = x4 = x5 = x6 = x7 = x8 =
145
+	  periodCounter = buf = bufBits = 0;
146
+	ptrAddr        = addr;
147
+
148
+interruptCount = TICKS; // modif rate	
149
+
150
+
151
+	// Set up Timer1 to trigger periodic synth calc at 'FS' Hz
152
+	TCCR1A = 0;                             // No output
153
+	TCCR1B = _BV(WGM12) | _BV(CS10);        // CTC mode, no prescale
154
+	//OCR1A  = ((F_CPU + (FS / 2)) / FS) - 1; // 'FS' Hz (w/rounding)
155
+	OCR1A = F_CPU / FS;
156
+	TCNT1  = 0;                             // Reset counter
157
+	TIMSK1 = _BV(OCIE1A);                   // Compare match interrupt on
158
+
159
+	if(block) while(TIMSK1 & _BV(OCIE1A));
160
+  slower= map(analogRead(0),0,1023,600,0);
161
+}
162
+
163
+boolean Talkie::talking(void) {
164
+	return TIMSK1 & _BV(OCIE1A);
165
+}
166
+
167
+static inline uint8_t rev(uint8_t a) { // Reverse bit sequence in 8-bit value
168
+	a = ( a         >> 4) | ( a         << 4); // 76543210 -> 32107654
169
+	a = ((a & 0xCC) >> 2) | ((a & 0x33) << 2); // 32107654 -> 10325476
170
+	a = ((a & 0xAA) >> 1) | ((a & 0x55) << 1); // 10325476 -> 01234567
171
+	return a;
172
+}
173
+
174
+static uint8_t getBits(uint8_t bits) {
175
+	uint8_t value;
176
+	if(bits > bufBits) {
177
+		buf     |= rev(pgm_read_byte(ptrAddr)) << (8 - bufBits);
178
+		bufBits += 8;
179
+		ptrAddr++; // Don't post-inc in pgm_read_byte! Is a macro.
180
+	}
181
+	value    = buf >> (16 - bits);
182
+	buf    <<= bits;
183
+	bufBits -= bits;
184
+	return value;
185
+}
186
+
187
+static void dacOut(uint8_t value) {
188
+	uint8_t bit;
189
+
190
+	*csPort  &= ~csBitMask; // Select DAC
191
+
192
+	// Clock out 4 bits DAC config (not in loop because it's constant)
193
+	*datPort &= ~datBitMask; // 0 = Select DAC A, unbuffered
194
+	*clkPort |=  clkBitMask; *clkPort &= ~clkBitMask;
195
+	*clkPort |=  clkBitMask; *clkPort &= ~clkBitMask;
196
+	*datPort |=  datBitMask; // 1X gain, enable = 1
197
+	*clkPort |=  clkBitMask; *clkPort &= ~clkBitMask;
198
+	*clkPort |=  clkBitMask; *clkPort &= ~clkBitMask;
199
+
200
+	// Output is expanded from 8 to 12 bits for DAC.  Perhaps the
201
+	// synthesizer math could be fiddled to generate 12-bit values.
202
+	for(bit=0x80; bit; bit>>=1) { // Clock out first 8 bits of data
203
+		if(value & bit) *datPort |=  datBitMask;
204
+		else            *datPort &= ~datBitMask;
205
+		*clkPort |= clkBitMask; *clkPort &= ~clkBitMask;
206
+	}
207
+	for(bit=0x80; bit >= 0x10; bit>>=1) { // Low 4 bits = repeat hi 4
208
+		if(value & bit) *datPort |=  datBitMask;
209
+		else            *datPort &= ~datBitMask;
210
+		*clkPort |= clkBitMask; *clkPort &= ~clkBitMask;
211
+	}
212
+	*csPort  |=  csBitMask; // Unselect DAC
213
+}
214
+
215
+#define read8(base, bits)  pgm_read_byte(&base[getBits(bits)]);
216
+#define read16(base, bits) pgm_read_word(&base[getBits(bits)]);
217
+
218
+ISR(TIMER1_COMPA_vect) {
219
+	int16_t u0;
220
+
221
+	if(csBitMask) dacOut(nextPwm);
222
+#if defined(__AVR_ATmega32U4__)
223
+	else          OCR4A = nextPwm;
224
+#else
225
+#ifdef PIEZO
226
+	else          OCR2A = OCR2B = nextPwm;
227
+#else
228
+	else          OCR2B = nextPwm;
229
+#endif
230
+#endif
231
+
232
+
233
+	if(++interruptCount >= TICKS+slower){
234
+		// Read speech data, processing the variable size frames
235
+		uint8_t energy;
236
+		if((energy = getBits(4)) == 0) {  // Rest frame
237
+			synthEnergy = 0;
238
+		} else if(energy == 0xF) {        // Stop frame; silence
239
+			TIMSK1 &= ~_BV(OCIE1A);   // Stop interrupt
240
+			nextPwm = 0x80;           // Neutral
241
+			if(csBitMask) {
242
+				dacOut(nextPwm);
243
+			} else {
244
+				// Stop PWM out:
245
+#if defined(__AVR_ATmega32U4__)
246
+				TCCR4A = 0;
247
+#else
248
+			//TCCR2A = 0; // to avoid "Plop" between speech
249
+#endif
250
+			}
251
+			return;
252
+		} else {
253
+			synthEnergy    = pgm_read_byte(&tmsEnergy[energy]);
254
+			uint8_t repeat = getBits(1);
255
+			synthPeriod    = pgm_read_byte(&tmsPeriod[getBits(6)]);
256
+			if(!repeat) { // A repeat frame uses last coefficients
257
+				synthK1 = read16(tmsK1, 5); // All frames
258
+				synthK2 = read16(tmsK2, 5); // use the first
259
+				synthK3 = read8( tmsK3, 4); // 4 coefficients
260
+				synthK4 = read8( tmsK4, 4);
261
+				if(synthPeriod) {
262
+					synthK5  = read8(tmsK5 , 4); // Voiced
263
+					synthK6  = read8(tmsK6 , 4); // frames
264
+					synthK7  = read8(tmsK7 , 4); // use
265
+					synthK8  = read8(tmsK8 , 3); // six
266
+					synthK9  = read8(tmsK9 , 3); // extra
267
+					synthK10 = read8(tmsK10, 3); // coeffs
268
+				}
269
+			}
270
+		}
271
+		interruptCount = 0;
272
+	}
273
+
274
+	if(synthPeriod) { // Voiced source
275
+		if(++periodCounter >= synthPeriod) periodCounter = 0;
276
+		u0 = (periodCounter >= sizeof(chirp)) ? 0 :
277
+		     (pgm_read_byte(&chirp[periodCounter]) *
278
+                     (uint32_t)synthEnergy) >> 8;
279
+	} else {          // Unvoiced source
280
+		synthRand = (synthRand >> 1) ^ ((synthRand & 1) ? 0xB800 : 0);
281
+		u0        = (synthRand & 1) ? synthEnergy : -synthEnergy;
282
+	}
283
+	u0     -=       ((synthK10 *          x9) +
284
+	                 (synthK9  *          x8)) >>  7;
285
+	x9      = x8  + ((synthK9  *          u0 ) >>  7);
286
+	u0     -=       ((synthK8  *          x7 ) >>  7);
287
+	x8      = x7  + ((synthK8  *          u0 ) >>  7);
288
+	u0     -=       ((synthK7  *          x6 ) >>  7);
289
+	x7      = x6  + ((synthK7  *          u0 ) >>  7);
290
+	u0     -=       ((synthK6  *          x5 ) >>  7);
291
+	x6      = x5  + ((synthK6  *          u0 ) >>  7);
292
+	u0     -=       ((synthK5  *          x4 ) >>  7);
293
+	x5      = x4  + ((synthK5  *          u0 ) >>  7);
294
+	u0     -=       ((synthK4  *          x3 ) >>  7);
295
+	x4      = x3  + ((synthK4  *          u0 ) >>  7);
296
+	u0     -=       ((synthK3  *          x2 ) >>  7);
297
+	x3      = x2  + ((synthK3  *          u0 ) >>  7);
298
+	u0     -=       ((synthK2  * (int32_t)x1 ) >> 15);
299
+	x2      = x1  + ((synthK2  * (int32_t)u0 ) >> 15);
300
+	u0     -=       ((synthK1  * (int32_t)x0 ) >> 15);
301
+	x1      = x0  + ((synthK1  * (int32_t)u0 ) >> 15);
302
+
303
+	if(     u0 >  511) u0 =  511; // Output clamp
304
+	else if(u0 < -512) u0 = -512;
305
+
306
+	x0      =  u0;
307
+	nextPwm = (u0 >> 2) + 0x80;
308
+}

+ 20
- 0
sketches/avital/asta_ammm/talkie.h View File

@@ -0,0 +1,20 @@
1
+// Talkie library 
2
+// Copyright 2011 Peter Knight
3
+// Adafruit library modified by Jean-Luc Deladrière
4
+// This code is released under GPLv2 license.
5
+
6
+
7
+#ifndef _Talkie_h_
8
+#define _Talkie_h_
9
+
10
+#include <Arduino.h>
11
+
12
+class Talkie {
13
+  public:
14
+	Talkie(void);                                 // PWM
15
+	Talkie(uint8_t cs, uint8_t clk, uint8_t dat); // DAC
16
+	void    say(const uint8_t *address, boolean block=true);
17
+	boolean talking(void); // Poll this when block=false
18
+};
19
+
20
+#endif

+ 0
- 1160
sketches/avital/avital.h
File diff suppressed because it is too large
View File


+ 0
- 0
sketches/avital/avital_sketch View File


Loading…
Cancel
Save