diff --git a/fry/src/lib.rs b/fry/src/lib.rs index 0894a4a..af70937 100644 --- a/fry/src/lib.rs +++ b/fry/src/lib.rs @@ -1,9 +1,9 @@ #![no_std] use core::fmt::Debug; -use tracing::{instrument, debug, error}; use core::mem::size_of; use include_data::include_data; +use tracing::{debug, error, instrument}; /// The BYTE_SIZE of the files as reported by `ls -l` /// All consts defined here will be exactly this value divided by two `u16`s long. @@ -62,40 +62,43 @@ import_raw!(Z, "../../data/z.raw"); import_raw!(SPACE, "../../data/space.raw"); #[instrument] -fn letter_to_pcm(c: char) -> Option<[PcmSample; BYTE_SIZE/2]> { - match c { - 'a' => Some(A), - 'b' => Some(B), - 'c' => Some(C), - 'd' => Some(D), - 'e' => Some(E), - 'f' => Some(F), - 'g' => Some(G), - 'h' => Some(H), - 'i' => Some(I), - 'j' => Some(J), - 'k' => Some(K), - 'l' => Some(L), - 'm' => Some(M), - 'n' => Some(N), - 'o' => Some(O), - 'p' => Some(P), - 'q' => Some(Q), - 'r' => Some(R), - 's' => Some(S), - 't' => Some(T), - 'u' => Some(U), - 'v' => Some(V), - 'w' => Some(W), - 'x' => Some(X), - 'y' => Some(Y), - 'z' => Some(Z), - ' ' => Some(SPACE), - _ => { - error!("Character '{}' does not correspond to a pre-recorded sound", c); - None - } - } +fn letter_to_pcm(c: char) -> Option<[PcmSample; BYTE_SIZE / 2]> { + match c { + 'a' => Some(A), + 'b' => Some(B), + 'c' => Some(C), + 'd' => Some(D), + 'e' => Some(E), + 'f' => Some(F), + 'g' => Some(G), + 'h' => Some(H), + 'i' => Some(I), + 'j' => Some(J), + 'k' => Some(K), + 'l' => Some(L), + 'm' => Some(M), + 'n' => Some(N), + 'o' => Some(O), + 'p' => Some(P), + 'q' => Some(Q), + 'r' => Some(R), + 's' => Some(S), + 't' => Some(T), + 'u' => Some(U), + 'v' => Some(V), + 'w' => Some(W), + 'x' => Some(X), + 'y' => Some(Y), + 'z' => Some(Z), + ' ' => Some(SPACE), + _ => { + error!( + "Character '{}' does not correspond to a pre-recorded sound", + c + ); + None + } + } } /// Fill a buffer with TTS data. @@ -109,60 +112,55 @@ fn letter_to_pcm(c: char) -> Option<[PcmSample; BYTE_SIZE/2]> { /// If you want the number of bytes, multiply the v in Some(v) by `BYTE_SIZE`. #[instrument(ret, skip(buf))] pub fn tts + Debug>(s: S, buf: &mut [PcmSample; MAX_BUFFER_SIZE]) -> Option { - if s.as_ref().len() > MAX_LETTERS { - error!("The length of the string {} ({} letters) is greater than the maximum amount of letters permitted: {}", s.as_ref(), s.as_ref().len(), MAX_LETTERS); - return None; - } - Some( - s - .as_ref() - .chars() - .fold(0, |offset: usize, ch| { - letter_to_pcm(ch) - .unwrap() - .iter() - .enumerate() - .for_each(|(i, pcm)| buf[(offset*BYTE_SIZE)+i] = *pcm); - offset+1 - }) - ) + if s.as_ref().len() > MAX_LETTERS { + error!("The length of the string {} ({} letters) is greater than the maximum amount of letters permitted: {}", s.as_ref(), s.as_ref().len(), MAX_LETTERS); + return None; + } + Some(s.as_ref().chars().fold(0, |offset: usize, ch| { + letter_to_pcm(ch) + .unwrap() + .iter() + .enumerate() + .for_each(|(i, pcm)| buf[(offset * BYTE_SIZE) + i] = *pcm); + offset + 1 + })) } #[cfg(test)] mod tests { - extern crate alloc; + extern crate alloc; - use test_log::test; - use super::MAX_BUFFER_SIZE; - use super::tts; - use super::BYTE_SIZE; - use super::A; - use super::PcmSample; - use super::LETTER_SAMPLES; - use alloc::string::String; + use super::tts; + use super::PcmSample; + use super::A; + use super::BYTE_SIZE; + use super::LETTER_SAMPLES; + use super::MAX_BUFFER_SIZE; + use alloc::string::String; + use test_log::test; - #[test] - fn check_one_letter_str() { - let mut buf: [PcmSample; MAX_BUFFER_SIZE] = [0; MAX_BUFFER_SIZE]; - let conv = String::from("a"); - let bytes = tts(conv, &mut buf); - assert_eq!(bytes.unwrap(), 1); - let created_slice = &buf[0..LETTER_SAMPLES]; - assert_eq!(created_slice.len(), A.len()); - assert_eq!(created_slice, A); - } - #[test] - fn check_one_word_str() { - let mut buf: [PcmSample; MAX_BUFFER_SIZE] = [0; MAX_BUFFER_SIZE]; - let conv = String::from("hello"); - let bytes = tts(conv, &mut buf); - assert_eq!(bytes.unwrap(), 5); - } - #[test] - fn check_multi_word() { - let mut buf: [PcmSample; MAX_BUFFER_SIZE] = [0; MAX_BUFFER_SIZE]; - let conv = String::from("hello world"); - let bytes = tts(conv, &mut buf); - assert_eq!(bytes.unwrap(), 11); - } + #[test] + fn check_one_letter_str() { + let mut buf: [PcmSample; MAX_BUFFER_SIZE] = [0; MAX_BUFFER_SIZE]; + let conv = String::from("a"); + let bytes = tts(conv, &mut buf); + assert_eq!(bytes.unwrap(), 1); + let created_slice = &buf[0..LETTER_SAMPLES]; + assert_eq!(created_slice.len(), A.len()); + assert_eq!(created_slice, A); + } + #[test] + fn check_one_word_str() { + let mut buf: [PcmSample; MAX_BUFFER_SIZE] = [0; MAX_BUFFER_SIZE]; + let conv = String::from("hello"); + let bytes = tts(conv, &mut buf); + assert_eq!(bytes.unwrap(), 5); + } + #[test] + fn check_multi_word() { + let mut buf: [PcmSample; MAX_BUFFER_SIZE] = [0; MAX_BUFFER_SIZE]; + let conv = String::from("hello world"); + let bytes = tts(conv, &mut buf); + assert_eq!(bytes.unwrap(), 11); + } }