From c7224d4838da3f7d1b89ef30adb8bd87f7f6575d Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 2 Oct 2016 15:44:33 -0400 Subject: [PATCH] Cleanup, fix compile warnings --- src/audiobank.rs | 1 - src/config.rs | 2 +- src/effects/pause.rs | 2 +- src/effects/speed.rs | 2 +- src/error.rs | 2 - src/handlers/audio_synth_handler.rs | 60 +++++++---------------------- src/lang/arpabet.rs | 3 +- src/lang/dictionary.rs | 2 + src/lang/tokenizer.rs | 4 +- src/main.rs | 5 +-- src/old_dictionary.rs | 8 ++-- src/synthesizer.rs | 22 +++++------ 12 files changed, 37 insertions(+), 76 deletions(-) diff --git a/src/audiobank.rs b/src/audiobank.rs index bc8dce23d0..fccd5cf27b 100644 --- a/src/audiobank.rs +++ b/src/audiobank.rs @@ -3,7 +3,6 @@ use error::SynthError; use hound::WavReader; use hound::WavSpec; -use hound::WavWriter; use std::path::Path; use std::path::PathBuf; diff --git a/src/config.rs b/src/config.rs index 1ca1596050..67b8974fce 100644 --- a/src/config.rs +++ b/src/config.rs @@ -36,7 +36,7 @@ impl Config { Err(_) => None, Ok(contents) => { let value = toml::Parser::new(&contents).parse().unwrap(); - let mut config = toml::decode(toml::Value::Table(value)); + let config = toml::decode(toml::Value::Table(value)); config.map(|mut c: Config| { c.hostname = get_hostname(); diff --git a/src/effects/pause.rs b/src/effects/pause.rs index 646110d47f..9c02005948 100644 --- a/src/effects/pause.rs +++ b/src/effects/pause.rs @@ -5,7 +5,7 @@ pub fn generate_pause(length: u16) -> Vec { let mut sound = Vec::new(); let len = length * 2; // number of channels - for i in 0..len { sound.push(0); } + for _ in 0..len { sound.push(0); } sound } diff --git a/src/effects/speed.rs b/src/effects/speed.rs index 445b854588..d04734800b 100644 --- a/src/effects/speed.rs +++ b/src/effects/speed.rs @@ -7,7 +7,7 @@ */ pub fn change_speed(waveform: Vec, speed: f32) -> Vec { let change = 1.0 / speed; - let mut size = (waveform.len() as f32 * change).round() as usize; + let size = (waveform.len() as f32 * change).round() as usize; // FIXME: Use memory of caller. // Caller can be more efficient with memory management. diff --git a/src/error.rs b/src/error.rs index f8c1894724..1697a8438d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,8 +2,6 @@ use hound::Error as HoundError; use std::convert::From; -use std::error; -use std::fmt; use std::io; /** The common error type used throughout the synthesizer. */ diff --git a/src/handlers/audio_synth_handler.rs b/src/handlers/audio_synth_handler.rs index 63a383c84c..418eb0f03f 100644 --- a/src/handlers/audio_synth_handler.rs +++ b/src/handlers/audio_synth_handler.rs @@ -4,28 +4,20 @@ use config::Config; use crypto::digest::Digest; use crypto::sha1::Sha1; -use hound::{WavReader, WavSpec, WavWriter}; use iron::Handler; -use iron::headers::{ETag, EntityTag, Headers, IfNoneMatch}; +use iron::Plugin; +use iron::headers::ETag; +use iron::headers::EntityTag; +use iron::headers::IfNoneMatch; use iron::mime::Mime; -use iron::prelude::*; +use iron::prelude::IronResult; +use iron::prelude::Request; +use iron::prelude::Response; use iron::status; -use old_words::split_sentence; -use router::Router; -use rustc_serialize::json; use speaker::Speaker; use std::collections::HashMap; -use std::error::Error; -use std::fmt::{self, Debug}; -use std::fs::File; -use std::fs; use std::i16; -use std::io::BufReader; -use std::io::BufWriter; -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; @@ -97,11 +89,11 @@ struct SpeakRequest { } enum SpeakerRequestError { - SentenceMissing, + //SentenceMissing, SentenceInvalid, - SpeakerMissing, + //SpeakerMissing, SpeakerInvalid, - VolumeInvalid, + //VolumeInvalid, } /// Synthesizes audio from input. @@ -111,10 +103,6 @@ pub struct AudioSynthHandler { /// Server configs. config: Config, - - /// Root of where files can be served from. - /// A PathBuf since `Path` can only be created as a borrow. - directory: PathBuf, } impl SpeakRequest { @@ -293,9 +281,6 @@ impl Handler for AudioSynthHandler { fn handle(&self, req: &mut Request) -> IronResult { info!("GET /speak"); - let speaker_error = build_error(status::BadRequest, - &format!("Missing `{}` parameter.", SPEAKER_PARAM)); - let sentence_error = build_error(status::BadRequest, &format!("Missing `{}` parameter.", SENTENCE_PARAM)); @@ -310,7 +295,7 @@ impl Handler for AudioSynthHandler { // TODO: Cleanup let request = match SpeakRequest::parse(req) { Ok(s) => s, - Err(e) => { + Err(_) => { return sentence_error; }, }; @@ -344,12 +329,11 @@ impl Handler for AudioSynthHandler { } impl AudioSynthHandler { - pub fn new(synthesizer: Arc>, config: Config, - directory: &str) -> AudioSynthHandler { + pub fn new(synthesizer: Arc>, + config: Config) -> AudioSynthHandler { AudioSynthHandler { synthesizer: synthesizer, config: config, - directory: Path::new(directory).to_path_buf(), } } @@ -415,26 +399,8 @@ impl AudioSynthHandler { hasher.result_str().to_string() } - - fn get_speaker_path(&self, speaker: &str) -> Result { - // FIXME: Hack so I can release tonight. Rewrite this whole controller plz. - if speaker.contains("..") || - speaker.contains("/") || - speaker.contains("$") || - speaker.contains("~") { - return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid path")); - } - - let speaker_path = self.directory.as_path().join(Path::new(speaker)); - // FIXME: This is not the security measure you think it is: - if !speaker_path.starts_with(&self.directory) { - return Err(io::Error::new(io::ErrorKind::InvalidInput, "invalid path")); - } - Ok(speaker_path) - } } - /** Parse an optional u16 out of query parameters. */ fn get_u16(params: &QueryParams, param_name: &str) -> Option { params.get(param_name) diff --git a/src/lang/arpabet.rs b/src/lang/arpabet.rs index 61442b490c..50a910cd48 100644 --- a/src/lang/arpabet.rs +++ b/src/lang/arpabet.rs @@ -8,7 +8,6 @@ use std::collections::HashSet; use std::fs::File; use std::io::BufRead; use std::io::BufReader; -use std::io::Read; pub type Word = String; pub type Phoneme = String; @@ -24,7 +23,7 @@ impl Arpabet { /// Read CMU's Arpabet Dictionary. /// See http://www.speech.cs.cmu.edu/cgi-bin/cmudict pub fn load_from_file(filename: &str) -> Result { - let mut f = try!(File::open(filename)); + let f = try!(File::open(filename)); let mut reader = BufReader::new(f); let mut map = HashMap::new(); diff --git a/src/lang/dictionary.rs b/src/lang/dictionary.rs index 1572449535..ce268837f5 100644 --- a/src/lang/dictionary.rs +++ b/src/lang/dictionary.rs @@ -32,6 +32,8 @@ impl Dictionary { self.words.contains(word) } + /// For testing only. + #[allow(dead_code)] fn insert(mut self, word: &str) -> Self { self.words.insert(word.to_string()); self diff --git a/src/lang/tokenizer.rs b/src/lang/tokenizer.rs index 80537e5746..cfcad21176 100644 --- a/src/lang/tokenizer.rs +++ b/src/lang/tokenizer.rs @@ -4,7 +4,7 @@ use regex::Regex; use speaker::Speaker; -use std::sync::Arc; +//use std::sync::Arc; //use super::dictionary::UniversalDictionary; lazy_static! { @@ -41,7 +41,7 @@ impl Tokenizer { Tokenizer {} } - pub fn convert(&self, speaker: &Speaker, raw_sentence: &str) -> String { + pub fn convert(&self, _speaker: &Speaker, raw_sentence: &str) -> String { let mut sentence = raw_sentence.to_string(); sentence = RE_ALPHA_COLON.replace_all(&sentence, "$1 "); diff --git a/src/main.rs b/src/main.rs index 564a7ac237..3d3ffec6d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,7 +64,7 @@ fn main() { let config = Config::read("./config.toml").unwrap(); VocabularyLibrary::read_from_directory( - Path::new(&config.get_sound_path())); + Path::new(&config.get_sound_path())).unwrap(); get_hostname(); @@ -95,8 +95,7 @@ fn start_server(config: &Config, port: u16, synthesizer: Synthesizer) { // TODO: Cross-cutting filter installation let mut router = Router::new(); let mut chain = Chain::new(AudioSynthHandler::new(async_synth.clone(), - config.clone(), - audio_path)); + config.clone())); chain.link_after(ErrorFilter); router.get("/speak", chain); router.get("/words", VocabListHandler::new(audio_path)); diff --git a/src/old_dictionary.rs b/src/old_dictionary.rs index 7f6984364d..8153de85cf 100644 --- a/src/old_dictionary.rs +++ b/src/old_dictionary.rs @@ -2,11 +2,9 @@ use std::collections::HashMap; use std::fs::DirEntry; -use std::fs::File; use std::fs; -use std::io::Read; use std::io; -use std::path::{Path, PathBuf}; +use std::path::Path; pub type VoiceName = String; @@ -49,7 +47,7 @@ impl VocabularyLibrary { } // FIXME: Opening FS reads in a loop is bad. Feel bad. - let mut vocabulary = try!(Vocabulary::read_from_directory( + let vocabulary = try!(Vocabulary::read_from_directory( entry.path().as_path())); if vocabulary.words.is_empty() { @@ -98,7 +96,7 @@ impl Vocabulary { fn get_filename(entry: &DirEntry) -> Result { match entry.file_name().into_string() { - Err(err) => { + Err(_) => { Err(io::Error::new(io::ErrorKind::Other, "Error reading filename")) }, Ok(filename) => Ok(filename.to_string()), diff --git a/src/synthesizer.rs b/src/synthesizer.rs index e7153f94e4..d617c250d1 100644 --- a/src/synthesizer.rs +++ b/src/synthesizer.rs @@ -70,7 +70,7 @@ impl Synthesizer { -> Result { let prepared = self.tokenizer.convert(speaker, sentence); - let mut words = split_sentence(&prepared); + let words = split_sentence(&prepared); if words.len() == 0 { return Err(SynthError::BadInput { @@ -108,13 +108,13 @@ impl Synthesizer { } if !word_added && use_phonemes { // Old phoneme algorithm - word_added = self.concatenate_polyphone(&mut concatenated_waveform, - speaker, - word, - use_diphones, - monophone_padding_start, - monophone_padding_end, - polyphone_padding_end); + self.concatenate_polyphone(&mut concatenated_waveform, + speaker, + word, + use_diphones, + monophone_padding_start, + monophone_padding_end, + polyphone_padding_end); } } @@ -324,7 +324,7 @@ impl Synthesizer { fn get_n_phone_samples(&self, speaker: &Speaker, word: &str, - sample_preference: SamplePreference, + _sample_preference: SamplePreference, use_ends: bool) -> Option> { @@ -343,7 +343,7 @@ impl Synthesizer { let mut debug : Vec> = Vec::with_capacity(polyphone.len()); - for i in 0..polyphone.len() { + for _ in 0..polyphone.len() { fulfilled.push(false); chunks.push(None); debug.push(None); @@ -485,7 +485,7 @@ impl Synthesizer { } } - let mut debug_str : Vec = debug.into_iter() + let debug_str : Vec = debug.into_iter() .map(|x| if x.is_some() { x.unwrap() } else { "None".to_string() }) .collect();