|
3 | 3 | var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1'); |
4 | 4 | var fs = require('fs'); |
5 | 5 |
|
6 | | -var text_to_speech = new TextToSpeechV1({ |
7 | | - username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE', |
8 | | - password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE' |
| 6 | +var textToSpeech = new TextToSpeechV1({ |
| 7 | + // if left unspecified here, the SDK will fall back to the TEXT_TO_SPEECH_USERNAME and TEXT_TO_SPEECH_PASSWORD |
| 8 | + // environment properties, and then Bluemix's VCAP_SERVICES environment property |
| 9 | + //username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE', |
| 10 | + //password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE' |
9 | 11 | }); |
10 | 12 |
|
11 | | -var params = { |
| 13 | +// Synthesize speech and then pipe the results to a file |
| 14 | +textToSpeech.synthesize({ |
12 | 15 | text: 'Hello from IBM Watson', |
13 | 16 | voice: 'en-US_AllisonVoice', // Optional voice |
14 | | - accept: 'audio/wav' |
15 | | -}; |
| 17 | + accept: 'audio/wav' // default is audio/ogg; codec=opus |
| 18 | +}).pipe(fs.createWriteStream('output.wav')); |
16 | 19 |
|
17 | | -// Pipe the synthesized text to a file |
18 | | -text_to_speech.synthesize(params).pipe(fs.createWriteStream('output.wav')); |
| 20 | +// Retrieve details of all available voices |
| 21 | +textToSpeech.voices({}, function(err, res){ |
| 22 | + if (err) { |
| 23 | + return console.log(err); |
| 24 | + } |
| 25 | + console.log(JSON.stringify(res, null, 2)); |
| 26 | +}); |
| 27 | + |
| 28 | +// Retrieve details of a specific voice |
| 29 | +textToSpeech.voice({ |
| 30 | + voice: 'en-GB_KateVoice' |
| 31 | +}, function(err, res){ |
| 32 | + if (err) { |
| 33 | + return console.log(err); |
| 34 | + } |
| 35 | + console.log(JSON.stringify(res, null, 2)); |
| 36 | +}); |
| 37 | + |
| 38 | +// Pronunciation details for a word |
| 39 | +textToSpeech.pronunciation({ |
| 40 | + text: 'iPhone', |
| 41 | + format: 'spr', // 'ipa' (default) is only for english voices |
| 42 | + voice: 'de-DE_DieterVoice' // optional, defaults to en-US_MichaelVoice |
| 43 | +}, function(err, res){ |
| 44 | + if (err) { |
| 45 | + return console.log(err); |
| 46 | + } |
| 47 | + console.log(JSON.stringify(res, null, 2)); |
| 48 | +}); |
| 49 | + |
| 50 | +// create a customization model to change pronunciation of words |
| 51 | +textToSpeech.createCustomization({ |
| 52 | + name: 'my custom alt language pronunciation model', |
| 53 | + language: 'en-US', // currently, only en-US is accepted |
| 54 | + description: 'Test model to try out custom pronunciations' |
| 55 | +}, function(err, res){ |
| 56 | + if (err) { |
| 57 | + return console.log(err); |
| 58 | + } |
| 59 | + console.log(JSON.stringify(res, null, 2)); |
| 60 | + /* |
| 61 | +{ |
| 62 | + "customization_id": "6666451d-a23e-485c-9bc5-c7ce722550d6" |
| 63 | +} |
| 64 | + */ |
| 65 | +}); |
| 66 | + |
| 67 | +// update a customization model |
| 68 | +textToSpeech.updateCustomization({ |
| 69 | + customization_id: "6666451d-a23e-485c-9bc5-c7ce722550d6", |
| 70 | + name: 'new name', // optional |
| 71 | + description: 'new description', // optional |
| 72 | + words: [ // required - replaces existing words list |
| 73 | + {"word":"NCAA", "translation":"N C double A"}, |
| 74 | + {"word":"iPhone", "translation":"I phone"} |
| 75 | + ] |
| 76 | +}, function(err){ |
| 77 | + if (err) { |
| 78 | + return console.log(err); |
| 79 | + } |
| 80 | + console.log('updated'); |
| 81 | +}); |
| 82 | + |
| 83 | +// get a list of custom voice models |
| 84 | +textToSpeech.getCustomizations({ |
| 85 | + language: 'en-US' // optional filter (currently only accepts en-US) |
| 86 | +}, function(err, res){ |
| 87 | + if (err) { |
| 88 | + return console.log(err); |
| 89 | + } |
| 90 | + console.log(JSON.stringify(res, null, 2)); |
| 91 | + /* |
| 92 | +{ |
| 93 | + "customizations": [ |
| 94 | + { |
| 95 | + "last_modified": 1472067196711, |
| 96 | + "customization_id": "10ba7c7c-ce2d-447f-9724-72da0d6f1e66", |
| 97 | + "created": 1472067196711, |
| 98 | + "description": "Test model to try out custom pronunciations", |
| 99 | + "name": "my custom pronunciation model", |
| 100 | + "language": "en-US", |
| 101 | + "owner": "7f966201-2afd-48ea-b5c1-d6981d50633e" |
| 102 | + }, |
| 103 | + { |
| 104 | + "last_modified": 1465475563937, |
| 105 | + "customization_id": "e24536fd-b1b4-48f3-95fe-c2d93e7f5c45", |
| 106 | + "created": 1465475563937, |
| 107 | + "description": "a simple model for testing purposes", |
| 108 | + "name": "test model", |
| 109 | + "language": "en-US", |
| 110 | + "owner": "7f966201-2afd-48ea-b5c1-d6981d50633e" |
| 111 | + } |
| 112 | + //... |
| 113 | + ] |
| 114 | +} |
| 115 | + */ |
| 116 | +}); |
| 117 | + |
| 118 | +// get details of a custom voice model |
| 119 | +textToSpeech.getCustomization({ |
| 120 | + customization_id: "6666451d-a23e-485c-9bc5-c7ce722550d6" |
| 121 | +}, function(err, res){ |
| 122 | + if (err) { |
| 123 | + return console.log(err); |
| 124 | + } |
| 125 | + console.log(JSON.stringify(res, null, 2)); |
| 126 | + /* |
| 127 | +{ |
| 128 | + "last_modified": 1472067165812, |
| 129 | + "customization_id": "6666451d-a23e-485c-9bc5-c7ce722550d6", |
| 130 | + "created": 1472066628780, |
| 131 | + "words": [ |
| 132 | + { |
| 133 | + "word": "NCAA", |
| 134 | + "translation": "N C double A" |
| 135 | + }, |
| 136 | + { |
| 137 | + "word": "iPhone", |
| 138 | + "translation": "I phone" |
| 139 | + } |
| 140 | + ], |
| 141 | + "description": "new description", |
| 142 | + "name": "new name", |
| 143 | + "language": "en-US", |
| 144 | + "owner": "7f966201-2afd-48ea-b5c1-d6981d50633e" |
| 145 | +} |
| 146 | + */ |
| 147 | +}); |
| 148 | + |
| 149 | +// delete a custom voice model |
| 150 | +textToSpeech.deleteCustomization({ |
| 151 | + customization_id: "9d153f61-a9c4-4b73-8eaf-63951c6dd77d" |
| 152 | +}, function(err){ |
| 153 | + if (err) { |
| 154 | + return console.log(err); |
| 155 | + } |
| 156 | + console.log('deleted'); |
| 157 | +}); |
| 158 | + |
| 159 | +// add multiple words to an existing model |
| 160 | +textToSpeech.addWords({ |
| 161 | + customization_id: "7c7f8ba7-2f83-48f2-ae52-3a70825f9899", |
| 162 | + words: [ |
| 163 | + {"word":"NCAA", "translation":"N C double A"}, |
| 164 | + {"word":"iPhone", "translation":"I phone"} |
| 165 | + ] |
| 166 | +}, function(err){ |
| 167 | + if (err) { |
| 168 | + return console.log(err); |
| 169 | + } |
| 170 | + console.log('added'); |
| 171 | +}); |
| 172 | + |
| 173 | +// add a single word to an existing model |
| 174 | +textToSpeech.updateCustomization({ |
| 175 | + customization_id: "7c7f8ba7-2f83-48f2-ae52-3a70825f9899", |
| 176 | + word:"NCAA", |
| 177 | + translation:"N C double A" |
| 178 | +}, function(err){ |
| 179 | + if (err) { |
| 180 | + return console.log(err); |
| 181 | + } |
| 182 | + console.log('added'); |
| 183 | +}); |
| 184 | + |
| 185 | +// get all words in a customization |
| 186 | +textToSpeech.getWords({ |
| 187 | + customization_id: "7c7f8ba7-2f83-48f2-ae52-3a70825f9899" |
| 188 | +}, function(err, res){ |
| 189 | + if (err) { |
| 190 | + return console.log(err); |
| 191 | + } |
| 192 | + console.log(JSON.stringify(res, null, 2)); |
| 193 | + /* |
| 194 | +{ |
| 195 | + "words": [ |
| 196 | + { |
| 197 | + "word": "NCAA", |
| 198 | + "translation": "N C double A" |
| 199 | + }, |
| 200 | + { |
| 201 | + "word": "iPhone", |
| 202 | + "translation": "I phone" |
| 203 | + } |
| 204 | + ] |
| 205 | +} |
| 206 | + */ |
| 207 | +}); |
| 208 | + |
| 209 | +// get a single word from a customization |
| 210 | +textToSpeech.getWord({ |
| 211 | + customization_id: "7c7f8ba7-2f83-48f2-ae52-3a70825f9899", |
| 212 | + word: "iPhone" |
| 213 | +}, function(err, res){ |
| 214 | + if (err) { |
| 215 | + return console.log(err); |
| 216 | + } |
| 217 | + console.log(JSON.stringify(res, null, 2)); |
| 218 | + /* |
| 219 | +{ |
| 220 | + "translation": "I phone" |
| 221 | +} |
| 222 | + */ |
| 223 | +}); |
| 224 | + |
| 225 | +// delete a word from a customization |
| 226 | +textToSpeech.deleteWord({ |
| 227 | + customization_id: "7c7f8ba7-2f83-48f2-ae52-3a70825f9899", |
| 228 | + word: "iPhone" |
| 229 | +}, function(err){ |
| 230 | + if (err) { |
| 231 | + return console.log(err); |
| 232 | + } |
| 233 | + console.log('deleted word'); |
| 234 | +}); |
0 commit comments