Cleanup, fix compile warnings

This commit is contained in:
Brandon Thomas committed 2016-10-02 16:32:56 -04:00
1 parent f86f6aa3ea
commit c7224d4838
12 files changed
+37 -76

No files matched your search

-1
View File
@@ -3,7 +3,6 @@
use error::SynthError;
use hound::WavReader;
use hound::WavSpec;
use hound::WavWriter;
use std::path::Path;
use std::path::PathBuf;
+1 -1
View File
@@ -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();
+1 -1
View File
@@ -5,7 +5,7 @@ pub fn generate_pause(length: u16) -> Vec<i16> {
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
}
+1 -1
View File
@@ -7,7 +7,7 @@
*/
pub fn change_speed(waveform: Vec<i16>, speed: f32) -> Vec<i16> {
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.
-2
View File
@@ -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. */
+13 -47
View File
@@ -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<Response> {
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<RwLock<Synthesizer>>, config: Config,
directory: &str) -> AudioSynthHandler {
pub fn new(synthesizer: Arc<RwLock<Synthesizer>>,
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<PathBuf, io::Error> {
// 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<u16> {
params.get(param_name)
+1 -2
View File
@@ -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<Arpabet, SynthError> {
let mut f = try!(File::open(filename));
let f = try!(File::open(filename));
let mut reader = BufReader::new(f);
let mut map = HashMap::new();
+2
View File
@@ -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
+2 -2
View File
@@ -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 ");
+2 -3
View File
@@ -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));
+3 -5
View File
@@ -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<String, io::Error> {
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()),
+11 -11
View File
@@ -70,7 +70,7 @@ impl Synthesizer {
-> Result<WavBytes, SynthError> {
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<Vec<SampleBytes>> {
@@ -343,7 +343,7 @@ impl Synthesizer {
let mut debug : Vec<Option<String>> =
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<String> = debug.into_iter()
let debug_str : Vec<String> = debug.into_iter()
.map(|x| if x.is_some() { x.unwrap() } else { "None".to_string() })
.collect();