123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- EasyReader = function(args, finish) {
- this.finish = finish;
- this.args = args;
- this.init = jQuery.proxy(this.init, this);
- this.isEnable = jQuery.proxy(this.isEnable, this);
- this.readable = jQuery.proxy(this.readable, this);
- this.read = jQuery.proxy(this.read, this);
- this.pause = jQuery.proxy(this.pause, this);
- this.resume = jQuery.proxy(this.resume, this);
- this.stop = jQuery.proxy(this.stop, this);
- this.volumePlus = jQuery.proxy(this.volumePlus, this);
- this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
- this.volume = jQuery.proxy(this.volume, this);
- this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
- this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
- this.ratePlus = jQuery.proxy(this.ratePlus, this);
- this.rateMinus = jQuery.proxy(this.rateMinus, this);
- this.rate = jQuery.proxy(this.rate, this);
- this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
- this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
- this.onFinish = jQuery.proxy(this.onFinish, this);
- this.init();
- }
- EasyReader.readableExp = new RegExp("[a-zA-Z0-9\u4E00-\u9FA5\uFF10-\uFF19\uFF21-\uFF3A\uFF41-\uFF5A]");
- EasyReader.readable = function (text) {
- return EasyReader.readableExp.test(text);
- }
- EasyReader.prototype = {
- init: function() {
- try {
- this._reader = new EasyReader.MSTTS(this.args, this.onFinish);
- }
- catch (e) {
- this._reader = new EasyReader.ServerTTS(this.args, this.onFinish);
- }
- },
- isEnable: function() {
- return !!this._reader;
- },
- readable: function(text) {
- return EasyReader.readableExp.test(text);
- },
- read: function(text) {
- text += "";
- if (this._reader && text && EasyReader.readableExp.test(text)) {
- this._reader.read(text);
- }
- },
- pause: function() {
- if (this._reader) {
- this._reader.pause();
- }
- },
- resume: function() {
- if (this._reader) {
- this._reader.resume();
- }
- },
- stop: function() {
- if (this._reader) {
- this._reader.stop();
- }
- },
- volumePlus: function() {
- if (this._reader) {
- this._reader.volumePlus();
- }
- },
- volumeMinus: function() {
- if (this._reader) {
- this._reader.volumeMinus();
- }
- },
- volume: function(v) {
- if (this._reader) {
- return this._reader.volume(v);
- }
- },
- canVolumePlus: function() {
- if (this._reader) {
- return this._reader.canVolumePlus();
- }
- else {
- return false;
- }
- },
- canVolumeMinus: function() {
- if (this._reader) {
- return this._reader.canVolumeMinus();
- }
- else {
- return false;
- }
- },
- ratePlus: function() {
- if (this._reader) {
- this._reader.ratePlus();
- }
- },
- rateMinus: function() {
- if (this._reader) {
- this._reader.rateMinus();
- }
- },
- rate: function(v) {
- if (this._reader) {
- return this._reader.rate(v);
- }
- },
- canRatePlus: function() {
- if (this._reader) {
- return this._reader.canRatePlus();
- }
- else {
- return false;
- }
- },
- canRateMinus: function() {
- if (this._reader) {
- return this._reader.canRateMinus();
- }
- else {
- return false;
- }
- },
- onFinish: function() {
- if (jQuery.isFunction(this.finish)) {
- this.finish();
- }
- }
- }
- EasyReader.MSTTS = function(args, finish) {
- this._args = args;
- this._finish = finish;
- this._timer = null;
- this.init = jQuery.proxy(this.init, this);
- this.read = jQuery.proxy(this.read, this);
- this.pause = jQuery.proxy(this.pause, this);
- this.resume = jQuery.proxy(this.resume, this);
- this.stop = jQuery.proxy(this.stop, this);
- this.volumePlus = jQuery.proxy(this.volumePlus, this);
- this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
- this.volume = jQuery.proxy(this.volume, this);
- this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
- this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
- this.ratePlus = jQuery.proxy(this.ratePlus, this);
- this.rateMinus = jQuery.proxy(this.rateMinus, this);
- this.rate = jQuery.proxy(this.rate, this);
- this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
- this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
- this.checkState = jQuery.proxy(this.checkState, this);
- this.init();
- }
- EasyReader.MSTTS.prototype = {
- init: function () {
- this._reader = new ActiveXObject("Sapi.SpVoice");
- this._reader.Voice = this._reader.GetVoices("Name = Microsoft Lili").Item(0);
- if (this._args && this._args.speed && this._args.speed == "slow") {
- this.rate(-3);
- }
- },
- checkState: function () {
- clearTimeout(this._timer);
- if (this._reader.Status.RunningState == 2) {
- this._timer = setTimeout(this.checkState, 200);
- }
- else {
- clearTimeout(this._timer);
- this._finish();
- }
- },
- read: function (text) {
- this._reader.Speak(text, 1);
- this._timer = setTimeout(this.checkState, 200);
- },
- pause: function () {
- this._reader.Pause();
- },
- resume: function () {
- this._reader.Resume();
- },
- stop: function () {
- clearTimeout(this._timer);
- this._reader.Speak("", 2);
- },
- volumePlus: function () {
- if (this._reader.Volume >= 100) {
- this._reader.Volume = 100;
- }
- else {
- this._reader.Volume += 20;
- }
- },
- volumeMinus: function () {
- if (this._reader.Volume <= 0) {
- this._reader.Volume = 0;
- }
- else {
- this._reader.Volume -= 20;
- }
- },
- volume: function (v) {
- if (v) {
- if (v > 1) { v = 1 }
- if (v < 0) { v = 0 }
- this._reader.Volume = v * 100;
- }
- else {
- return this._reader.Volume / 100;
- }
- },
- canVolumePlus: function () {
- return this._reader.Volume < 100;
- },
- canVolumeMinus: function () {
- return this._reader.Volume > 0;
- },
- ratePlus: function () {
- if (this._reader.Rate >= 10) {
- this._reader.Rate = 10;
- }
- else {
- this._reader.Rate += 1;
- }
- },
- rateMinus: function () {
- if (this._reader.Rate <= -10) {
- this._reader.Rate = -10;
- }
- else {
- this._reader.Rate -= 1;
- }
- },
- rate: function (v) {
- if (v) {
- if (v > 10) { v = 10 }
- if (v < -10) { v = -10 }
- this._reader.Rate = v;
- }
- else {
- return this._reader.Rate;
- }
- },
- canRatePlus: function () {
- return this._reader.Rate < 10;
- },
- canRateMinus: function () {
- return this._reader.Rate > -10;
- }
- }
- EasyReader.ServerTTS = function(args, finish) {
- this._args = args;
- this._finish = finish;
- this.readerVolume = 1;
- this.init = jQuery.proxy(this.init, this);
- this.read = jQuery.proxy(this.read, this);
- this.pause = jQuery.proxy(this.pause, this);
- this.resume = jQuery.proxy(this.resume, this);
- this.stop = jQuery.proxy(this.stop, this);
- this.volumePlus = jQuery.proxy(this.volumePlus, this);
- this.volumeMinus = jQuery.proxy(this.volumeMinus, this);
- this.volume = jQuery.proxy(this.volume, this);
- this.canVolumePlus = jQuery.proxy(this.canVolumePlus, this);
- this.canVolumeMinus = jQuery.proxy(this.canVolumeMinus, this);
- this.ratePlus = jQuery.proxy(this.ratePlus, this);
- this.rateMinus = jQuery.proxy(this.rateMinus, this);
- this.rate = jQuery.proxy(this.rate, this);
- this.canRatePlus = jQuery.proxy(this.canRatePlus, this);
- this.canRateMinus = jQuery.proxy(this.canRateMinus, this);
- this.onFinish = jQuery.proxy(this.onFinish, this);
- this.onError = jQuery.proxy(this.onError, this);
- this.init();
- }
- EasyReader.ServerTTS.prototype = {
- init: function() {
- this.player = jQuery("<div id='jquery_jplayer'></div>");
- jQuery(document.body).append(this.player);
- this.player.jPlayer({
- swfPath: this._args && this._args.playerPath ? this._args.playerPath : "/js/",
- supplied: "mp3",
- wmode: "window",
- volume: this.readerVolume
- })
- this.player.bind(jQuery.jPlayer.event.ended, this.onFinish);
- this.player.bind(jQuery.jPlayer.event.error, this.onError);
- },
- onFinish: function() {
- if (jQuery.isFunction(this._finish)) {
- this._finish();
- }
- },
- onError: function() {
- if (this.mp3) {
- try {
- this.player.jPlayer("setMedia", { mp3: this.mp3 + "&time=" + new Date().getTime() }).jPlayer("play");
- this.mp3 = "";
- }
- catch (e) { }
- }
- },
- read: function(text) {
- var md5 = jQuery.md5.hex_md5(text);
- //this.mp3 = ((this._args && this._args.mp3Path) ? this._args.mp3Path : "/tts/host/") + md5 + ".tts.mp3?text=" + encodeURIComponent(text);
- //this.mp3 = "http://218.12.44.24/cgi-bin/index.pl?voice=female&cache=1&rate=1&text=" + encodeURIComponent(text);
- //this.mp3 = "http://192.168.0.184/perl/ekho_agent.pl?cache=1&mtts=1&text=" + encodeURIComponent(text);
- this.mp3="http://tsn.baidu.com/text2audio?tex=" + encodeURIComponent(text) + "&lan=zh&cuid=123qwe&ctp=1&tok="+opt.baidu_access_token;
- try {
- this.player.jPlayer("setMedia", { mp3: this.mp3 }).jPlayer("play");
- }
- catch (e) { }
- },
- pause: function() {
- this.player.jPlayer("pause");
- },
- resume: function() {
- this.player.jPlayer("play");
- },
- stop: function() {
- try {
- this.player.jPlayer("stop");
- }
- catch (e) { }
- },
- volumePlus: function() {
- this.readerVolume += 0.1;
- if (this.readerVolume >= 0.99) {
- this.readerVolume = 1;
- }
- this.player.jPlayer("volume", this.readerVolume);
- },
- volumeMinus: function() {
- this.readerVolume -= 0.1;
- if (this.readerVolume <= 0.01) {
- this.readerVolume = 0;
- }
- this.player.jPlayer("volume", this.readerVolume);
- },
- volume: function(v) {
- if (v) {
- if (v > 1) { v = 1 }
- if (v < 0) { v = 0 }
- this.player.jPlayer("volume", v);
- }
- else {
- return this.readerVolume;
- }
- },
- canVolumePlus: function() {
- return this.readerVolume < 1;
- },
- canVolumeMinus: function() {
- return this.readerVolume > 0;
- },
- ratePlus: function() {
- },
- rateMinus: function() {
- },
- rate: function(v) {
- },
- canRatePlus: function() {
- return false;
- },
- canRateMinus: function() {
- return false;
- }
- }
|