diff --git a/src/handlers/audio_synth_handler.rs b/src/handlers/audio_synth_handler.rs index 78e131ce9c..e88a37eecd 100644 --- a/src/handlers/audio_synth_handler.rs +++ b/src/handlers/audio_synth_handler.rs @@ -8,7 +8,6 @@ use iron::Handler; use iron::headers::{ETag, EntityTag, Headers, IfNoneMatch}; use iron::mime::Mime; use iron::prelude::*; -//use params::Params; use iron::status; use router::Router; use rustc_serialize::json; @@ -23,6 +22,7 @@ use std::io::Cursor; use std::io::Read; use std::io; use std::path::{Path, PathBuf}; +use std::str::FromStr; use std::sync::Arc; use std::sync::RwLock; use super::error_filter::build_error; @@ -30,8 +30,10 @@ use synthesizer::Synthesizer; use urlencoded::UrlEncodedQuery; use words::split_sentence; -const SPEAKER_PARAM : &'static str = "v"; const SENTENCE_PARAM : &'static str = "s"; +const SPEAKER_PARAM : &'static str = "v"; +const USE_PHONEMES_PARAM: &'static str = "up"; +const USE_WORDS_PARAM: &'static str = "uw"; const VOLUME_PARAM : &'static str = "vol"; /// Represents a request to this endpoint. @@ -45,11 +47,11 @@ struct SpeakRequest { /** An optional volume multiplier. */ pub volume: Option, - /** Whether to use words. */ - pub use_words: bool, - /** Whether to use phonemes. */ pub use_phonemes: bool, + + /** Whether to use words. */ + pub use_words: bool, } enum SpeakerRequestError { @@ -78,7 +80,7 @@ impl SpeakRequest { // Get the request sentence and speaker. // TODO: Cleanup - let (sentence, speaker, volume) = match http_request.get_ref::() { + match http_request.get_ref::() { Err(_) => { return sentence_error; }, Ok(ref map) => { let sen = match map.get(SENTENCE_PARAM) { @@ -116,34 +118,45 @@ impl SpeakRequest { }, }; - (sen, spk, volume) - }, - }; - - /*let (use_words, use_phonemes) = match http_request.get_ref::() { - None => { - (false, false) - }, - Some(map) => { - match map.find("use_words") { - Some(_e) => { - println!(">>> FOUND"); + let use_phonemes = match map.get(USE_PHONEMES_PARAM) { + None => { true }, + Some(list) => { + match list.get(0) { + None => { return speaker_error; }, + Some(s) => { + match FromStr::from_str(s) { + Ok(b) => b, + Err(_) => { return speaker_error; }, + } + }, + } }, - _ => { - println!(">>> NOT FOUND"); - }, - } - (true, true) - }, - };*/ + }; - Ok(SpeakRequest { - sentence: sentence, - speaker: speaker, - volume: volume, - use_words: false, - use_phonemes: true, - }) + let use_words = match map.get(USE_WORDS_PARAM) { + None => { true }, + Some(list) => { + match list.get(0) { + None => { return speaker_error; }, + Some(s) => { + match FromStr::from_str(s) { + Ok(b) => b, + Err(_) => { return speaker_error; }, + } + }, + } + }, + }; + + Ok(SpeakRequest { + sentence: sen, + speaker: spk, + volume: volume, + use_phonemes: use_phonemes, + use_words: use_words, + }) + }, + } } } diff --git a/src/main.rs b/src/main.rs index 634393dacd..cdcceccc4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,6 @@ extern crate hound; extern crate iron; #[macro_use] extern crate log; -//extern crate params; extern crate regex; extern crate resolve; extern crate router; diff --git a/src/web/js/events.js b/src/web/js/events.js index 0144f5e157..3463681196 100644 --- a/src/web/js/events.js +++ b/src/web/js/events.js @@ -40,7 +40,11 @@ speaker = $('select').val(), volume = $('input#vol').val(), query = encodeURIComponent(sentence), - url = '/speak?v=' + speaker + '&s=' + query + '&vol=' + volume; + url = '/speak?v=' + speaker + + '&s=' + query + + '&vol=' + volume + + '&up=' + $('#use_phonemes').prop('checked') + + '&uw=' + $('#use_words').prop('checked'); ev.preventDefault(); diff --git a/src/web/js/initialize.js b/src/web/js/initialize.js index 645fed803a..b4e1a0540c 100644 --- a/src/web/js/initialize.js +++ b/src/web/js/initialize.js @@ -29,6 +29,9 @@ $('input#vol').val(volume); + $('#use_phonemes').prop('use_phonemes', Url.getUsePhonemes()); + $('#use_words').prop('use_words', Url.getUseWords()); + Library.load(function(library) { Ui.libraryLoadCallback(library); }); Sound.install(); diff --git a/src/web/js/url.js b/src/web/js/url.js index 1fd35c3575..1312213c7d 100644 --- a/src/web/js/url.js +++ b/src/web/js/url.js @@ -13,6 +13,12 @@ /** JSON key for the `volume` param. */ _VOLUME_KEY: 'vol', + /** JSON key for the `use_phonemes` param. */ + _USE_PHONEMES_KEY: 'up', + + /** JSON key for the `use_words` param. */ + _USE_WORDS_KEY: 'uw', + /** Get the sentence from the `window.location`. */ getSentence: function() { var state = this.parseState(window.location), @@ -45,16 +51,36 @@ } }, + /** Get the "use phonemes" from the `window.location`. */ + getUsePhonemes: function() { + var state = this.parseState(window.location); + if (!state || !(this._USE_PHONEMES_KEY in state)) { + return null; + } else { + return state[this._USE_PHONEMES_KEY]; + } + }, + + /** Get the "use words" from the `window.location`. */ + getUseWords: function() { + var state = this.parseState(window.location); + if (!state || !(this._USE_WORDS_KEY in state)) { + return null; + } else { + return state[this._USE_WORDS_KEY]; + } + }, + // TODO: Independent functions to set both speaker and sentence separately. - /** Set the `window.history` speaker, volume, and sentence. */ - setState: function(speaker, rawSentence, volume) { - var urlHash = this.fromParams(speaker, rawSentence, volume); + /** Set the `window.history` speaker, sentence, phoneme use, and word use. */ + setState: function(speaker, rawSentence, volume, usePhonemes, useWords) { + var urlHash = this.fromParams(speaker, rawSentence, volume, usePhonemes, useWords); console.log('set state, urlhash = ', urlHash); window.history.replaceState(null, null, urlHash); }, /** Encode a speaker and sentence in a URL hash. */ - fromParams: function(speaker, rawSentence, volume) { + fromParams: function(speaker, rawSentence, volume, usePhonemes, useWords) { var cleanedSentence = SentenceHelper.cleanSentence(rawSentence), cleanedSpeaker = speaker, // TODO: Check against speakers. cleanedVolume = volume, // TODO: Filter invalid values. @@ -65,6 +91,8 @@ state[this._SPEAKER_KEY] = cleanedSpeaker; state[this._SENTENCE_KEY] = cleanedSentence; state[this._VOLUME_KEY] = cleanedVolume; + state[this._USE_PHONEMES_KEY] = !!usePhonemes; + state[this._USE_WORDS_KEY] = !!useWords; json = JSON.stringify(state);