CHAPTER 9 SPEECH SYNTHESIZER Figure 9-7. Shielded cable connected to female RCA line plug tted to an Arduino and plugged into an amplifier you can go to the following ding a small amplifier to the shield so you can drive a speaker directly is quite easy with the LM386 io amplifier IC. LM386 chips are very common in small audio devices, such as compact portable radios, because they're very easy to use and produce reasonable quality sound at a good volume level. They certainly won't compete with the amp in your stereo system, but for simple voice-level output in a portable device they're perfect. Once your shield is fi section “Speech Synthesizer Software” to test it out. On-Board Amplifier Ad aud Figure 9-8. Pinout of LM386 audio amplifier 149 CHAPTER 9 SPEECH SYNTHESIZER To build a simple audio amplifier with a gain of about 20 you can use the LM386 on pretty much nothing else required. However, with a few extra parts it can be configured of about 50. If necessary it can be configured for a gain of up to 200, but that's pushing it can do and since we're powering the amplifier circuit from the Arduino's 5V supply th and noise on the supply rails could become a problem. Audio amplifiers pull a lot more typical digital circuits you might be used to dealing with on an Arduino: since amps equ by volts, even a tiny 1/4W amplifier running on its own with to deliver a gain the limits of what e current drain power than the als watts divided a 5V supply can suck down 50mA of current, assuming it uations nt but rather jumps ble balance between a high output volume and low power consumption, eed to use a nd also make sure d version of the output from the SpeakJet, so it uses the w-pass filter as the line-level output and then adds the LM386 to provide an amplified version of the signal. Add the LM386 to the shield along with its associated parts, the 10K trimpot and the various resistors and capacitors, as shown in Figure 9-9. Finally, solder on the PCB-mount two-pin screw terminal to provide a handy connection for a speaker. runs at an unrealistic 100% efficiency. Even worse than the current drain, though, are the fluct that can be induced on the supply rails, since the current consumption isn't consiste around all over the place depending on the input signal. A gain of 50 gives a reasona and won't strain the Arduino's power supply. If you want more volume you'll probably n more substantial external amplifier and feed it with the line-level output connection, a the supply rails are adequately filtered. The LM386 needs to be fed with a filtere same lo Figure 9-9. Shield with LM386-based audio amplifier in place and speaker connected If you have an old speaker from a stereo system you can connect a pair of wires to it and attach them to the screw terminals. Otherwise, use a small speaker from an electronics parts shop, an old computer 150 CHAPTER 9 SPEECH SYNTHESIZER speaker, or even a speaker out of a car stereo system, and solder two wires to the speake other ends into the screw terminals on the shie r. Then insert the ld. Turn the trimpot to about halfway as a starting point so it has a reasonable volume level. Mount the shield on your Arduino, and install the software to test it. ftware phones to the ons at 9600bps we reSerial library, then specify which pins will be used for RX and TX. We t the SoftwareSerial RX (receive) pin, because all we'll be doing is sending values to the SpeakJet and not reading anything back. However, SoftwareSerial needs both to be defined so we'll set it anyway and then ignore it. ftwareSerial.h> s are stored as a series of allophones in an array, so to make it easier to read the array we'll for the "Word Pause" value that is appended after each word. The SpeakJet s allophone 6 ly put WP into the alue. #define WP 6 e” to hold the allophones for the words we want to speak. d embedding comments within each line we can make the array much lue onto the end of each word inserts allophone 6, the word pause. es it relatively simple to copy and paste different rds into place to make up your own sentences, as follows: /* name */ 141, 154, 140, WP, /* is */ 8, 129, 167, WP, /* arduino */ 152, 148, 175, 147, 128, 141, 164, WP }; Later the program will need to know the length of the message array, so rather than count the values manually and have to update it every time the message is changed we'll instead have the program count the array elements and store it for future reference. Speech Synthesizer So To test the speech synthesizer, start with a minimal program that sends a series of allo SpeakJet to sound out a simple sentence. Because the Arduino communicates with the SpeakJet using serial communicati first need to include the Softwa don't actually care abou #include <So #define rxPin 2 #define txPin 3 Then create a new software serial port object called "speakJet." SoftwareSerial speakJet = SoftwareSerial(rxPin, txPin); Word define a memorable token has six different pauses available, but the most commonly used pause between words i which has a duration of 90 milliseconds. By defining WP as the value 6 we can simp w agic vord array as if it's a single byte, and not have to remember that allophone 6 is a m Next, we'll set up an array called “messag By breaking it up visually an easier to read. Finally, tacking a WP va Working with the message array this way mak wo byte message[] = { /* hello */ 183, 007, 159, 146, 164, WP, WP, /* my */ 140, 155, WP, 151 CHAPTER 9 SPEECH SYNTHESIZER int messageSize = sizeof(message); The setup function is called once when the program starts, and configures the software serial to the SpeakJet. Since the SpeakJet runs by default at 9600bps we set up the software serial port to match. s a special case by he volume) and 21 (which sets the speaking rate). When the SpeakJet sees either of these bytes it interprets the next byte as the value for that parameter and updates se settings are stored in nonvolatile memory inside the SpeakJet. e to a value of 96 (on a scale of 0 through 127), we first send byte 20 (volume mmand) followed by a value of 96, as follows: speakJet.print(20, BYTE); TE); Setting the speech rate is done in a similar way. First we send byte 21 (speed command), followed by speakJet.print(114, BYTE); the setup function, olumes and speech rates. The setup function then pauses for one second to give the SpeakJet time to receive and process the values. 1000); he main program loop is incredibly simple. It steps through the message rray using a counter, i, and sends each value it finds to the SpeakJet via the serial connection defined nds before repeating. void loop() speakJet.print(message[i], BYTE); } delay (5000); } With a short sentence repeated at five second intervals there is plenty of time for the SpeakJet to say the whole the sentence before it is sent through again, so we are not checking for the Buffer Half Full state. connection void setup() { pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT); speakJet.begin(9600); It then sends some initialization values to the SpeakJet. Some bytes are treated a the SpeakJet, including 20 (which sets t its internal settings. The So, to set the volum co speakJet.print(96, BY a value of 114 to set the speaking rate to 114 (on a scale of 0 to 127), as follows: print( 21, BYTE); speakJet. Both those settings are actually the default values, but by putting these lines into it makes it easy to experiment with different v delay( } After all that preparation t a earlier. It then pauses for five seco { int i; for (i=0; i<messageSize; i++) { 152 CHAPTER 9 SPEECH SYNTHESIZER When you power up your Arduino and speech synthesizer shield it will immediately say "ready," even if you haven't sent any values to the SpeakJet yet. That's just the SpeakJet's internal self-test indicating that it has finished and is ready to receive commands. nds listed below allow you to create any word you want your Arduino to say hose sounds. mands and nt to the SpeakJet. Because the SpeakJet chip can also function as a multichannel sound synthesizer it has a number of commands other than the ones listed in hat are relevant to speech synthesis are shown here. Full details of all available omm ncluded in the SpeakJet datasheet. Table ommands Byte Command Create Your Own Messages The allophones and comma by breaking it up into sounds and then creating a list of the bytes that represent t The SpeakJet accepts one-byte values from 0 to 255, with some values treated as com others as allophones to sound out. Low values are used for commands se Table 9-2. Just the ones t c ands are i 9-2. SpeakJet c Pause 3 (7 Pause 4 (30ms Pause 5 (60ms) Pause 6 (90ms) 0 Pause 0 (0ms) 1 Pause 1 (100ms) 2 Pause 2 (200ms) 3 00ms) 4 ) 5 6 7 Play next sound fast 8 Play next sound slow 14 Stress next phoneme 15 Relax next phoneme 20 Volume X (range 0–127, default 96) 21 Speed X (range 0–127, default 114) 22 Pitch X (range 0–255, default 88) 153 CHAPTER 9 SPEECH SYNTHESIZER 23 Bend X (range 0–15, default 5) 30 Delay X (range 0–255 times 10ms increments) Five of the commands listed in Table 9-2 are two-byte commands. The first byte sent i command itself, and the second X byte is the value for that parameter. Sending a pair and then 56, would set the volume to a value of 56. Value s the of bytes, such as 20 s 128 and above are the allophones themselves. Spoken sounds that are used to form words are values 128 through 199, and are listed in Table 9-3 with the letter representation of the associated to show the pronunciation, the duration of the sound in milliseconds, and the type of sound. Spea yte Phonem le Words phoneme, sample words Table 9-3. kJet allophones B e Samp Duration Type 128 IY Feed Long Vowel See, Even, 70 Voiced 133 Cotten 70 Voiced 134 U Luck, Up, U 70 Voiced 135 Hot, Clock, F 70 Voiced 129 IH Sit, Fix, Pin 70 Voiced Short Vowel 130 EY Hair, Gate, Beige 70 Voiced Long Vowel 131 EH Met, Check, Red 70 Voiced Short Vowel 132 AY Hat, Fast, Fan 70 Voiced Short Vowel AX Short Vowel X ncle Short Vowel OH ox Short Vowel 136 AW Father, Fall 70 Voiced Short Vowel 137 OW Comb, Over, Hold 70 Voiced Long Vowel 138 UH Book, Could, Should 70 Voiced Short Vowel 139 UW Food, June 70 Voiced Long Vowel 140 MM Milk, Famous 70 Voiced Nasal 141 NE Nip, Danger, Thin 70 Voiced Nasal 142 NO No, Snow, On 70 Voiced Nasal 154 CHAPTER 9 SPEECH SYNTHESIZER 143 NGE , Ping iced Nasal NGO ed Nasal LE , Lapel ed Resonate 146 LO Clock, Plus, Hello 70 Voiced Resonate 147 WW Wool, Sweat 70 Voiced Resonate 148 RR Ray, Brain, Over 70 Voiced Resonate RR ear, Year Color Vowel RR r, Repair Color Vowel 51 RR Color Vowel 152 AWRR Part, Farm, Yarn 200 Voiced R Color Vowel 153 OWRR Corn, Four, Your 185 Voiced R Color Vowel 154 EYIY Gate, Ate, Ray 165 Voiced Diphthong 155 OHIY Mice, Fight, White 200 Voiced Diphthong 56 IY ice Diphthong 57 IH Five, I Diphthong 58 H Diphthong 159 EHLL Saddle, Angle, Spell 140 Voiced Diphthong 160 IYUW Cute, Few 180 Voiced Diphthong 161 AXUW Brown, Clown, Thousand 170 Voiced Diphthong 162 IHWW Two, New, Zoo 170 Voiced Diphthong 163 AYWW Our, Ouch, Owl 200 Voiced Diphthong 164 OWWW Go, Hello, Snow 131 Voiced Diphthong 165 JH Dodge, Jet, Savage 70 Voiced Affricate Think 70 Vo 144 Hung, Song 70 Voic 145 Lake, Alarm 70 Voic 149 IY Clear, H 200 Voiced R 150 EY Hair, Stai 200 Voiced R 1 AX Fir, Bird, Burn 190 Voiced R 1 OW Boy, Toy, Vo 225 Voiced 1 OH Sky, 185 Voiced 1 IYE Yes, Yarn, Million 170 Voiced 155 CHAPTER 9 SPEECH SYNTHESIZER 166 VV t, Even iced Fictive 67 ZZ iced Fictive 68 ZH asure iced Fictive 169 DH There, That, This 70 Voiced Fictive 170 BE Bear, Bird, Beed 45 Voiced Stop 171 BO Bone, Book, Brown 45 Voiced Stop 72 EB eb Voiced Stop 73 OB b, Tub Voiced Stop 74 DE , Date, Divide Voiced Stop 175 DO Do, Dust, Dog 45 Voiced Stop 176 ED Could, Bird 10 Voiced Stop 177 OD Bud, Food 10 Voiced Stop 178 GE Get, Gate, Guest 55 Voiced Stop 79 GO Stop 80 EG d Stop 81 OG g d Stop 182 CH Church, Feature, March 70 Voiceless Affricate 183 HE Help, Hand, Hair 70 Voiceless Fricative 184 HO Hoe, Hot, Hug 70 Voiceless Fricative 185 WH Who, Whale, White 70 Voiceless Fricative 186 FF Food, Effort, Off 70 Voiceless Fricative 187 SE See, Vest, Plus 40 Voiceless Fricative 188 SO So, Sweat 40 Voiceless Fricative Ves 70 Vo 1 Zoo, Zap 70 Vo 1 Azure, Tre 70 Vo 1 Cab, Crib, W 10 1 Bob, Su 10 1 Deep 45 1 Got, Glue, Goo 55 Voiced 1 Peg, Wig 55 Voice 1 Dog, Pe 55 Voice 156 CHAPTER 9 SPEECH SYNTHESIZER 189 SH iction, Leas less Fricative 90 TH onth iceless Fricative 91 TT ittle, Sit iceless Stop 192 TU To, Talk, Ten 70 Voiceless Stop 193 TS Parts, Costs, Robots 170 Voiceless Stop 194 KE Can't, Clown, Key 55 Voiceless Stop 95 KO , Fox iceless Stop 96 EK iceless Stop 197 OK Book, Took, October 45 Voiceless Stop 198 PE People, Computer 99 Voiceless Stop Ship, F h 50 Voice 1 Thin, M 40 Vo 1 Part, L 50 Vo 1 Comb, Quick 55 Vo 1 Speak, Task 55 Vo Values 200 a spec ose s s such as sound effects and DTMF tones. These are listed in Table 9-4. ab 4. SpeakJet sound effects and tones yte Phoneme Sample Wor Duration Type nd above are ial purp ound T le 9- B ds 200 R0 80 Robot 201 R1 80 Robot 202 R2 80 Robot 203 R3 80 Robot 204 R4 80 Robot 205 R5 80 Robot 206 R6 80 Robot 207 R7 80 Robot 208 R8 80 Robot 157 CHAPTER 9 SPEECH SYNTHESIZER 209 R9 80 Ro 10 A0 300 Alarm 11 A1 101 Alarm 212 A2 102 Alarm 213 A3 540 Alarm 214 A4 530 Alarm 15 A5 500 Alarm 16 A6 135 Alarm 17 A7 600 Alarm 218 A8 300 Alarm 219 A9 250 Alarm 220 B0 200 Beeps 221 B1 270 Beeps 22 B2 280 Beeps 23 B3 260 Beeps 24 B4 300 Beeps 225 B5 100 Beeps 226 B6 104 Beeps 227 B7 100 Beeps 228 B8 270 Beeps 229 B9 262 Beeps 230 C0 160 Biological 231 C1 300 Biological bot 2 2 2 2 2 2 2 2 158 . preparation t a earlier. It then pauses for five seco { int i; for (i=0; i<messageSize; i++) { 152 CHAPTER 9 SPEECH SYNTHESIZER When you power up your Arduino and speech synthesizer shield. Mount the shield on your Arduino, and install the software to test it. ftware phones to the ons at 9600bps we reSerial library, then specify which pins will be used for RX and TX. We t the. we'll for the "Word Pause" value that is appended after each word. The SpeakJet s allophone 6 ly put WP into the alue. #define WP 6 e” to hold the allophones for the words