EasyReader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. EasyReader = function(args, finish) {
  2. this.finish = finish;
  3. this.args = args;
  4. this.init = jQuery.proxy(this.init, this);
  5. this.isEnable = jQuery.proxy(this.isEnable, this);
  6. this.readable = jQuery.proxy(this.readable, this);
  7. this.read = jQuery.proxy(this.read, this);
  8. this.pause = jQuery.proxy(this.pause, this);
  9. this.resume = jQuery.proxy(this.resume, this);
  10. this.stop = jQuery.proxy(this.stop, this);
  11. this.volumePlus = jQuery.proxy(this.volumePlus, this);
  12. this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
  13. this.volume = jQuery.proxy(this.volume, this);
  14. this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
  15. this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
  16. this.ratePlus = jQuery.proxy(this.ratePlus, this);
  17. this.rateMinus = jQuery.proxy(this.rateMinus, this);
  18. this.rate = jQuery.proxy(this.rate, this);
  19. this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
  20. this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
  21. this.onFinish = jQuery.proxy(this.onFinish, this);
  22. this.init();
  23. }
  24. EasyReader.readableExp = new RegExp("[a-zA-Z0-9\u4E00-\u9FA5\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A]");
  25. EasyReader.readable = function (text) {
  26. return EasyReader.readableExp.test(text);
  27. }
  28. EasyReader.prototype = {
  29. init: function() {
  30. try {
  31. this._reader = new EasyReader.MSTTS(this.args, this.onFinish);
  32. }
  33. catch (e) {
  34. this._reader = new EasyReader.ServerTTS(this.args, this.onFinish);
  35. }
  36. },
  37. isEnable: function() {
  38. return !!this._reader;
  39. },
  40. readable: function(text) {
  41. return EasyReader.readableExp.test(text);
  42. },
  43. read: function(text) {
  44. text += "";
  45. if (this._reader && text && EasyReader.readableExp.test(text)) {
  46. this._reader.read(text);
  47. }
  48. },
  49. pause: function() {
  50. if (this._reader) {
  51. this._reader.pause();
  52. }
  53. },
  54. resume: function() {
  55. if (this._reader) {
  56. this._reader.resume();
  57. }
  58. },
  59. stop: function() {
  60. if (this._reader) {
  61. this._reader.stop();
  62. }
  63. },
  64. volumePlus: function() {
  65. if (this._reader) {
  66. this._reader.volumePlus();
  67. }
  68. },
  69. volumeMinus: function() {
  70. if (this._reader) {
  71. this._reader.volumeMinus();
  72. }
  73. },
  74. volume: function(v) {
  75. if (this._reader) {
  76. return this._reader.volume(v);
  77. }
  78. },
  79. canVolumePlus: function() {
  80. if (this._reader) {
  81. return this._reader.canVolumePlus();
  82. }
  83. else {
  84. return false;
  85. }
  86. },
  87. canVolumeMinus: function() {
  88. if (this._reader) {
  89. return this._reader.canVolumeMinus();
  90. }
  91. else {
  92. return false;
  93. }
  94. },
  95. ratePlus: function() {
  96. if (this._reader) {
  97. this._reader.ratePlus();
  98. }
  99. },
  100. rateMinus: function() {
  101. if (this._reader) {
  102. this._reader.rateMinus();
  103. }
  104. },
  105. rate: function(v) {
  106. if (this._reader) {
  107. return this._reader.rate(v);
  108. }
  109. },
  110. canRatePlus: function() {
  111. if (this._reader) {
  112. return this._reader.canRatePlus();
  113. }
  114. else {
  115. return false;
  116. }
  117. },
  118. canRateMinus: function() {
  119. if (this._reader) {
  120. return this._reader.canRateMinus();
  121. }
  122. else {
  123. return false;
  124. }
  125. },
  126. onFinish: function() {
  127. if (jQuery.isFunction(this.finish)) {
  128. this.finish();
  129. }
  130. }
  131. }
  132. EasyReader.MSTTS = function(args, finish) {
  133. this._args = args;
  134. this._finish = finish;
  135. this._timer = null;
  136. this.init = jQuery.proxy(this.init, this);
  137. this.read = jQuery.proxy(this.read, this);
  138. this.pause = jQuery.proxy(this.pause, this);
  139. this.resume = jQuery.proxy(this.resume, this);
  140. this.stop = jQuery.proxy(this.stop, this);
  141. this.volumePlus = jQuery.proxy(this.volumePlus, this);
  142. this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
  143. this.volume = jQuery.proxy(this.volume, this);
  144. this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
  145. this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
  146. this.ratePlus = jQuery.proxy(this.ratePlus, this);
  147. this.rateMinus = jQuery.proxy(this.rateMinus, this);
  148. this.rate = jQuery.proxy(this.rate, this);
  149. this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
  150. this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
  151. this.checkState = jQuery.proxy(this.checkState, this);
  152. this.init();
  153. }
  154. EasyReader.MSTTS.prototype = {
  155. init: function () {
  156. this._reader = new ActiveXObject("Sapi.SpVoice");
  157. this._reader.Voice = this._reader.GetVoices("Name = Microsoft Lili").Item(0);
  158. if (this._args && this._args.speed && this._args.speed == "slow") {
  159. this.rate(-3);
  160. }
  161. },
  162. checkState: function () {
  163. clearTimeout(this._timer);
  164. if (this._reader.Status.RunningState == 2) {
  165. this._timer = setTimeout(this.checkState, 200);
  166. }
  167. else {
  168. clearTimeout(this._timer);
  169. this._finish();
  170. }
  171. },
  172. read: function (text) {
  173. this._reader.Speak(text, 1);
  174. this._timer = setTimeout(this.checkState, 200);
  175. },
  176. pause: function () {
  177. this._reader.Pause();
  178. },
  179. resume: function () {
  180. this._reader.Resume();
  181. },
  182. stop: function () {
  183. clearTimeout(this._timer);
  184. this._reader.Speak("", 2);
  185. },
  186. volumePlus: function () {
  187. if (this._reader.Volume >= 100) {
  188. this._reader.Volume = 100;
  189. }
  190. else {
  191. this._reader.Volume += 20;
  192. }
  193. },
  194. volumeMinus: function () {
  195. if (this._reader.Volume <= 0) {
  196. this._reader.Volume = 0;
  197. }
  198. else {
  199. this._reader.Volume -= 20;
  200. }
  201. },
  202. volume: function (v) {
  203. if (v) {
  204. if (v > 1) { v = 1 }
  205. if (v < 0) { v = 0 }
  206. this._reader.Volume = v * 100;
  207. }
  208. else {
  209. return this._reader.Volume / 100;
  210. }
  211. },
  212. canVolumePlus: function () {
  213. return this._reader.Volume < 100;
  214. },
  215. canVolumeMinus: function () {
  216. return this._reader.Volume > 0;
  217. },
  218. ratePlus: function () {
  219. if (this._reader.Rate >= 10) {
  220. this._reader.Rate = 10;
  221. }
  222. else {
  223. this._reader.Rate += 1;
  224. }
  225. },
  226. rateMinus: function () {
  227. if (this._reader.Rate <= -10) {
  228. this._reader.Rate = -10;
  229. }
  230. else {
  231. this._reader.Rate -= 1;
  232. }
  233. },
  234. rate: function (v) {
  235. if (v) {
  236. if (v > 10) { v = 10 }
  237. if (v < -10) { v = -10 }
  238. this._reader.Rate = v;
  239. }
  240. else {
  241. return this._reader.Rate;
  242. }
  243. },
  244. canRatePlus: function () {
  245. return this._reader.Rate < 10;
  246. },
  247. canRateMinus: function () {
  248. return this._reader.Rate > -10;
  249. }
  250. }
  251. EasyReader.ServerTTS = function(args, finish) {
  252. this._args = args;
  253. this._finish = finish;
  254. this.readerVolume = 1;
  255. this.init = jQuery.proxy(this.init, this);
  256. this.read = jQuery.proxy(this.read, this);
  257. this.pause = jQuery.proxy(this.pause, this);
  258. this.resume = jQuery.proxy(this.resume, this);
  259. this.stop = jQuery.proxy(this.stop, this);
  260. this.volumePlus = jQuery.proxy(this.volumePlus, this);
  261. this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
  262. this.volume = jQuery.proxy(this.volume, this);
  263. this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
  264. this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
  265. this.ratePlus = jQuery.proxy(this.ratePlus, this);
  266. this.rateMinus = jQuery.proxy(this.rateMinus, this);
  267. this.rate = jQuery.proxy(this.rate, this);
  268. this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
  269. this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
  270. this.onFinish = jQuery.proxy(this.onFinish, this);
  271. this.onError = jQuery.proxy(this.onError, this);
  272. this.init();
  273. }
  274. EasyReader.ServerTTS.prototype = {
  275. init: function() {
  276. this.player = jQuery("<div id='jquery_jplayer'></div>");
  277. jQuery(document.body).append(this.player);
  278. this.player.jPlayer({
  279. swfPath: this._args && this._args.playerPath ? this._args.playerPath : "/js/",
  280. supplied: "mp3",
  281. wmode: "window",
  282. volume: this.readerVolume
  283. })
  284. this.player.bind(jQuery.jPlayer.event.ended, this.onFinish);
  285. this.player.bind(jQuery.jPlayer.event.error, this.onError);
  286. },
  287. onFinish: function() {
  288. if (jQuery.isFunction(this._finish)) {
  289. this._finish();
  290. }
  291. },
  292. onError: function() {
  293. if (this.mp3) {
  294. try {
  295. this.player.jPlayer("setMedia", { mp3: this.mp3 + "&time=" + new Date().getTime() }).jPlayer("play");
  296. this.mp3 = "";
  297. }
  298. catch (e) { }
  299. }
  300. },
  301. read: function(text) {
  302. var md5 = jQuery.md5.hex_md5(text);
  303. //this.mp3 = ((this._args && this._args.mp3Path) ? this._args.mp3Path : "/tts/host/") + md5 + ".tts.mp3?text=" + encodeURIComponent(text);
  304. //this.mp3 = "http://218.12.44.24/cgi-bin/index.pl?voice=female&cache=1&rate=1&text=" + encodeURIComponent(text);
  305. //this.mp3 = "http://192.168.0.184/perl/ekho_agent.pl?cache=1&mtts=1&text=" + encodeURIComponent(text);
  306. this.mp3="http://tsn.baidu.com/text2audio?tex=" + encodeURIComponent(text) + "&lan=zh&cuid=123qwe&ctp=1&tok="+opt.baidu_access_token;
  307. try {
  308. this.player.jPlayer("setMedia", { mp3: this.mp3 }).jPlayer("play");
  309. }
  310. catch (e) { }
  311. },
  312. pause: function() {
  313. this.player.jPlayer("pause");
  314. },
  315. resume: function() {
  316. this.player.jPlayer("play");
  317. },
  318. stop: function() {
  319. try {
  320. this.player.jPlayer("stop");
  321. }
  322. catch (e) { }
  323. },
  324. volumePlus: function() {
  325. this.readerVolume += 0.1;
  326. if (this.readerVolume >= 0.99) {
  327. this.readerVolume = 1;
  328. }
  329. this.player.jPlayer("volume", this.readerVolume);
  330. },
  331. volumeMinus: function() {
  332. this.readerVolume -= 0.1;
  333. if (this.readerVolume <= 0.01) {
  334. this.readerVolume = 0;
  335. }
  336. this.player.jPlayer("volume", this.readerVolume);
  337. },
  338. volume: function(v) {
  339. if (v) {
  340. if (v > 1) { v = 1 }
  341. if (v < 0) { v = 0 }
  342. this.player.jPlayer("volume", v);
  343. }
  344. else {
  345. return this.readerVolume;
  346. }
  347. },
  348. canVolumePlus: function() {
  349. return this.readerVolume < 1;
  350. },
  351. canVolumeMinus: function() {
  352. return this.readerVolume > 0;
  353. },
  354. ratePlus: function() {
  355. },
  356. rateMinus: function() {
  357. },
  358. rate: function(v) {
  359. },
  360. canRatePlus: function() {
  361. return false;
  362. },
  363. canRateMinus: function() {
  364. return false;
  365. }
  366. }