Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
TTWNO committed Jun 10, 2024
1 parent f0191ab commit d3470ca
Showing 1 changed file with 83 additions and 85 deletions.
168 changes: 83 additions & 85 deletions fry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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<S: AsRef<str> + Debug>(s: S, buf: &mut [PcmSample; MAX_BUFFER_SIZE]) -> Option<usize> {
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);
}
}

0 comments on commit d3470ca

Please sign in to comment.