From 77f0c504dfedd35bab0641f6ade1dec34ac3a622 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 8 Dec 2015 01:59:59 -0500 Subject: [PATCH] Work in progress on handlers --- Cargo.lock | 32 ++++++++++++ Cargo.toml | 6 ++- src/handlers/audio_synth_handler.rs | 66 ++++++++++++++++++++++++ src/handlers/mod.rs | 5 ++ src/{ => handlers}/vocab_list_handler.rs | 0 src/main.rs | 32 ++++++++---- src/words.rs | 19 +++++++ 7 files changed, 147 insertions(+), 13 deletions(-) create mode 100644 src/handlers/audio_synth_handler.rs create mode 100644 src/handlers/mod.rs rename src/{ => handlers}/vocab_list_handler.rs (100%) create mode 100644 src/words.rs diff --git a/Cargo.lock b/Cargo.lock index f9af60f344..fb78d16ff8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,7 @@ dependencies = [ "router 0.0.16 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "urlencoded 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -25,6 +26,17 @@ name = "bitflags" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bodyparser" +version = "0.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "iron 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "persistent 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "conduit-mime-types" version = "0.7.3" @@ -228,6 +240,15 @@ dependencies = [ "openssl-sys 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "persistent" +version = "0.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "iron 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "pkg-config" version = "0.3.6" @@ -359,6 +380,17 @@ dependencies = [ "uuid 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "urlencoded" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bodyparser 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "iron 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "plugin 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "url 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "uuid" version = "0.1.18" diff --git a/Cargo.toml b/Cargo.toml index f84386a8a3..9430a80e0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "trumpet" version = "0.0.1" - authors = [ "Brandon Thomas " ] + authors = [ "Brandon Thomas ", + "Brandon Thomas " ] [[bin]] name = "trumpet" @@ -9,10 +10,11 @@ [dependencies] #cpal = "*" # Hardware audio output (for testing); works well with Hound. hound = "1.0.0" # WAV codec. + hyper = "0.7.*" iron = "0.2.*" log = "0.3.*" - hyper = "0.7.*" router = "0.0.16" rustc-serialize = "0.3.15" time = "0.1.*" + urlencoded = "0.2.0" diff --git a/src/handlers/audio_synth_handler.rs b/src/handlers/audio_synth_handler.rs new file mode 100644 index 0000000000..7da26ecc90 --- /dev/null +++ b/src/handlers/audio_synth_handler.rs @@ -0,0 +1,66 @@ +// Copyright (c) 2015 Brandon Thomas + +use iron::Handler; +use iron::mime::Mime; +use iron::prelude::*; +use iron::status; +use router::Router; +use rustc_serialize::json; +use urlencoded::UrlEncodedQuery; + +use std::fs::File; +use std::fs; +use std::io::Read; +use std::path::{Path, PathBuf}; + +use words::split_sentence; + +const QUERY_PARAM : &'static str = "q"; + +/// Synthesizes audio from input. +pub struct AudioSynthHandler { + /// Root of where files can be served from. + /// A PathBuf since `Path` can only be created as a borrow. + directory: PathBuf, +} + +impl Handler for AudioSynthHandler { + fn handle(&self, req: &mut Request) -> IronResult { + let error = Ok(Response::with((status::Ok, "error"))); + + let sentence = match req.get_ref::() { + Err(_) => { return error; }, + Ok(ref map) => { + match map.get(QUERY_PARAM) { + None => { return error; }, + Some(list) => { + match list.get(0) { + None => { return error; }, + Some(s) => { s }, + } + }, + } + }, + }; + + let words = split_sentence(sentence); + + /*let words = self.list_files(); + let response = json::encode(&words).unwrap();*/ + Ok(Response::with((status::Ok, "todo"))) + } +} + +impl AudioSynthHandler { + pub fn new(directory: &str) -> AudioSynthHandler { + AudioSynthHandler { directory: Path::new(directory).to_path_buf() } + } + + // TODO: Return errors. + /// Return a list of words from the audio files in the directory. + fn list_files(&self) -> Vec { + let mut words = Vec::new(); + words + } +} + diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs new file mode 100644 index 0000000000..d2bfb31b8b --- /dev/null +++ b/src/handlers/mod.rs @@ -0,0 +1,5 @@ +// Copyright (c) 2015 Brandon Thomas + +pub mod audio_synth_handler; +pub mod vocab_list_handler; + diff --git a/src/vocab_list_handler.rs b/src/handlers/vocab_list_handler.rs similarity index 100% rename from src/vocab_list_handler.rs rename to src/handlers/vocab_list_handler.rs diff --git a/src/main.rs b/src/main.rs index e859d32be8..96b2d6234e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,10 @@ extern crate hound; extern crate iron; extern crate router; extern crate rustc_serialize; +extern crate urlencoded; -pub mod vocab_list_handler; +pub mod handlers; +pub mod words; use std::env; use std::fs::File; @@ -14,9 +16,26 @@ use iron::prelude::*; use router::Router; use hound::{WavReader, WavSpec, WavWriter}; -use vocab_list_handler::VocabListHandler; +use handlers::vocab_list_handler::VocabListHandler; +use handlers::audio_synth_handler::AudioSynthHandler; fn main() { + //create_from_argv(); + start_server(); +} + +fn start_server() { + let audio_path = "./sounds/trump"; + + let mut router = Router::new(); + router.get("/speak", AudioSynthHandler::new(audio_path)); + router.get("/words", VocabListHandler::new(audio_path)); + + println!("Starting server..."); + Iron::new(router).http("0.0.0.0:9000").unwrap(); +} + +fn create_from_argv() { let dictionary = words_from_argv(); // Note: Keeping a list of buffered file readers is stupid and is simply @@ -43,8 +62,6 @@ fn main() { let spec = get_spec(&dictionary[0]); write_file("output.wav", &spec, all_samples); - - start_server(); } fn words_from_argv() -> Vec { @@ -84,10 +101,3 @@ fn write_file(filename: &str, spec: &WavSpec, samples: Vec) { } } -fn start_server() { - println!("starting server"); - let mut router = Router::new(); - router.get("/vocab_list", VocabListHandler::new("./sounds/trump")); - Iron::new(router).http("0.0.0.0:9000").unwrap(); -} - diff --git a/src/words.rs b/src/words.rs new file mode 100644 index 0000000000..e398184167 --- /dev/null +++ b/src/words.rs @@ -0,0 +1,19 @@ +// Copyright (c) 2015 Brandon Thomas + +/// Split a sentence into words. Remove extra padding, etc. +pub fn split_sentence(sentence: &str) -> Vec { + let mut words = Vec::new(); + let mut split = sentence.split(" "); + + // TODO: Keep only \w, '-', '.', and apostrophe. + for s in split { + let trim = s.trim(); + if trim.len() == 0 { + continue; + } + words.push(trim.to_lowercase()); + } + + words +} +