Skip to content

Commit

Permalink
Merge pull request #3 from White-Green/setup_ci
Browse files Browse the repository at this point in the history
Create .github/workflows/test.yml
  • Loading branch information
White-Green authored Jan 6, 2024
2 parents 37c0d11 + e333aac commit 0fec809
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 25 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Test

on:
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Run tests
run: cargo test --verbose

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- uses: giraffate/clippy-action@v1
with:
reporter: 'github-pr-review'
clippy_flags: --all-targets -- -Dwarnings
fail_on_error: true
github_token: ${{ secrets.GITHUB_TOKEN }}

fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt
- name: check rustfmt
run: cargo fmt -- --check
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*
!/.github
!/.gitignore
!/.gitmodules
!/Cargo.toml
!/LICENSE
!/rustfmt.toml
!/src
!/world_sys
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
max_width = 150
use_field_init_shorthand = true
35 changes: 31 additions & 4 deletions src/signal_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ impl SignalAnalyzerBuilder {

impl SignalAnalyzer {
pub fn from_builder(builder: SignalAnalyzerBuilder, signal: Box<[f64]>) -> SignalAnalyzer {
let SignalAnalyzerBuilder { fs, harvest_option, cheaptrick_option, d4c_option } = builder;
let SignalAnalyzerBuilder {
fs,
harvest_option,
cheaptrick_option,
d4c_option,
} = builder;
assert!(signal.len() <= i32::MAX as usize);
SignalAnalyzer {
signal,
Expand Down Expand Up @@ -130,7 +135,14 @@ impl SignalAnalyzer {
let mut temporal_positions = vec![0.; samples as usize].into_boxed_slice();
let mut f0 = vec![0.; samples as usize].into_boxed_slice();
unsafe {
Harvest(self.signal.as_ptr(), self.signal.len() as i32, self.fs, self.harvest_option.as_ptr(), temporal_positions.as_mut_ptr(), f0.as_mut_ptr());
Harvest(
self.signal.as_ptr(),
self.signal.len() as i32,
self.fs,
self.harvest_option.as_ptr(),
temporal_positions.as_mut_ptr(),
f0.as_mut_ptr(),
);
}
HarvestResult { temporal_positions, f0 }
})
Expand All @@ -149,7 +161,16 @@ impl SignalAnalyzer {
let HarvestResult { f0, temporal_positions } = self.harvest_result();
let mut spectrogram = SpectrogramLike::new(f0.len(), self.cheaptrick_option.fft_size() as usize / 2 + 1);
unsafe {
CheapTrick(self.signal.as_ptr(), self.signal.len() as i32, self.fs, temporal_positions.as_ptr(), f0.as_ptr(), f0.len() as i32, self.cheaptrick_option.as_ptr(), spectrogram.as_mut_ptr());
CheapTrick(
self.signal.as_ptr(),
self.signal.len() as i32,
self.fs,
temporal_positions.as_ptr(),
f0.as_ptr(),
f0.len() as i32,
self.cheaptrick_option.as_ptr(),
spectrogram.as_mut_ptr(),
);
}
spectrogram
})
Expand Down Expand Up @@ -182,7 +203,13 @@ impl SignalAnalyzer {
}

pub fn into_result(self) -> AnalyzeResult {
let SignalAnalyzer { signal, harvest_result, spectrogram, aperiodicity, .. } = self;
let SignalAnalyzer {
signal,
harvest_result,
spectrogram,
aperiodicity,
..
} = self;
AnalyzeResult {
signal,
f0: harvest_result.into_inner().map(|HarvestResult { f0, .. }| f0),
Expand Down
14 changes: 7 additions & 7 deletions src/spectrogram_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ mod ndarray {
arr.as_standard_layout().into_owned()
};

let lines = arr
.rows_mut()
.into_iter()
.map(|row| { row }.as_mut_ptr())
.collect();
let lines = arr.rows_mut().into_iter().map(|row| { row }.as_mut_ptr()).collect();

let all = arr.into_raw_vec().into();

Expand Down Expand Up @@ -99,8 +95,12 @@ mod tests {
let mut spec = SpectrogramLike::<u32>::new(10, 5);
assert_eq!(spec.time_axis_size(), 10);
assert_eq!(spec.frequency_axis_size(), 5);
spec.lines_mut().enumerate().for_each(|(i, line)| line.iter_mut().enumerate().for_each(|(j, item)| *item = (i * 5 + j) as u32));
spec.lines().enumerate().for_each(|(i, line)| line.iter().enumerate().for_each(|(j, item)| assert_eq!(*item, (i * 5 + j) as u32)));
spec.lines_mut()
.enumerate()
.for_each(|(i, line)| line.iter_mut().enumerate().for_each(|(j, item)| *item = (i * 5 + j) as u32));
spec.lines()
.enumerate()
.for_each(|(i, line)| line.iter().enumerate().for_each(|(j, item)| assert_eq!(*item, (i * 5 + j) as u32)));
let ptr = spec.as_mut_ptr();
for i in 0..10 {
for j in 0..5 {
Expand Down
84 changes: 73 additions & 11 deletions src/synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,56 @@ impl Display for SynthesisError {

impl Error for SynthesisError {}

pub fn synthesis_to(f0: &[f64], spectrogram: &SpectrogramLike<f64>, aperiodicity: &SpectrogramLike<f64>, fft_size: Option<i32>, frame_period: f64, fs: u32, out: &mut [f64]) -> Result<(), SynthesisError> {
if f0.len() != spectrogram.time_axis_size() || spectrogram.time_axis_size() != aperiodicity.time_axis_size() || spectrogram.frequency_axis_size() != aperiodicity.frequency_axis_size() {
pub fn synthesis_to(
f0: &[f64],
spectrogram: &SpectrogramLike<f64>,
aperiodicity: &SpectrogramLike<f64>,
fft_size: Option<i32>,
frame_period: f64,
fs: u32,
out: &mut [f64],
) -> Result<(), SynthesisError> {
if f0.len() != spectrogram.time_axis_size()
|| spectrogram.time_axis_size() != aperiodicity.time_axis_size()
|| spectrogram.frequency_axis_size() != aperiodicity.frequency_axis_size()
{
return Err(SynthesisError::DifferentSizeInput);
}
if fs > i32::MAX as u32 || out.len() > i32::MAX as usize || f0.len() > i32::MAX as usize {
return Err(SynthesisError::TooLargeValue);
}
let fft_size = fft_size.ok_or(()).or_else(|_| ((spectrogram.frequency_axis_size() - 1) * 2).try_into().map_err(|_| SynthesisError::TooLargeValue))?;
let fft_size = fft_size.ok_or(()).or_else(|_| {
((spectrogram.frequency_axis_size() - 1) * 2)
.try_into()
.map_err(|_| SynthesisError::TooLargeValue)
})?;
if (fft_size / 2 + 1) as usize != spectrogram.frequency_axis_size() {
return Err(SynthesisError::InvalidFFTSize);
}
unsafe { Synthesis(f0.as_ptr(), f0.len() as i32, spectrogram.as_ptr(), aperiodicity.as_ptr(), fft_size, frame_period, fs as i32, out.len() as i32, out.as_mut_ptr()) }
unsafe {
Synthesis(
f0.as_ptr(),
f0.len() as i32,
spectrogram.as_ptr(),
aperiodicity.as_ptr(),
fft_size,
frame_period,
fs as i32,
out.len() as i32,
out.as_mut_ptr(),
)
}
Ok(())
}

pub fn synthesis(f0: &[f64], spectrogram: &SpectrogramLike<f64>, aperiodicity: &SpectrogramLike<f64>, fft_size: Option<i32>, frame_period: f64, fs: u32) -> Result<Vec<f64>, SynthesisError> {
pub fn synthesis(
f0: &[f64],
spectrogram: &SpectrogramLike<f64>,
aperiodicity: &SpectrogramLike<f64>,
fft_size: Option<i32>,
frame_period: f64,
fs: u32,
) -> Result<Vec<f64>, SynthesisError> {
let out_len = (f0.len() as f64 * frame_period * fs as f64 / 1000.).ceil() as usize;
let mut out = vec![0.; out_len];
synthesis_to(f0, spectrogram, aperiodicity, fft_size, frame_period, fs, &mut out).map(move |_| out)
Expand All @@ -60,11 +94,22 @@ impl Synthesizer {
InitializeSynthesizer(fs as i32, frame_period, fft_size, 128, 1, synthesizer.as_mut_ptr());
synthesizer.assume_init()
};
Synthesizer { synthesizer, queue: VecDeque::new() }
Synthesizer {
synthesizer,
queue: VecDeque::new(),
}
}

pub fn add(&mut self, f0: &mut [f64], spectrogram: &mut SpectrogramLike<f64>, aperiodicity: &mut SpectrogramLike<f64>) -> Result<(), SynthesisError> {
if f0.len() != spectrogram.time_axis_size() || spectrogram.time_axis_size() != aperiodicity.time_axis_size() || spectrogram.frequency_axis_size() != aperiodicity.frequency_axis_size() {
pub fn add(
&mut self,
f0: &mut [f64],
spectrogram: &mut SpectrogramLike<f64>,
aperiodicity: &mut SpectrogramLike<f64>,
) -> Result<(), SynthesisError> {
if f0.len() != spectrogram.time_axis_size()
|| spectrogram.time_axis_size() != aperiodicity.time_axis_size()
|| spectrogram.frequency_axis_size() != aperiodicity.frequency_axis_size()
{
return Err(SynthesisError::DifferentSizeInput);
}
if f0.len() > i32::MAX as usize {
Expand All @@ -73,10 +118,23 @@ impl Synthesizer {
if (self.synthesizer.fft_size / 2 + 1) as usize != spectrogram.frequency_axis_size() {
return Err(SynthesisError::InvalidFFTSize);
}
while unsafe { AddParameters(f0.as_mut_ptr(), f0.len() as i32, spectrogram.as_mut_ptr(), aperiodicity.as_mut_ptr(), &mut self.synthesizer) } == 0 {
while unsafe {
AddParameters(
f0.as_mut_ptr(),
f0.len() as i32,
spectrogram.as_mut_ptr(),
aperiodicity.as_mut_ptr(),
&mut self.synthesizer,
)
} == 0
{
unsafe {
if Synthesis2(&mut self.synthesizer) != 0 {
self.queue.extend(slice::from_raw_parts(self.synthesizer.buffer, self.synthesizer.buffer_size as usize).iter().copied());
self.queue.extend(
slice::from_raw_parts(self.synthesizer.buffer, self.synthesizer.buffer_size as usize)
.iter()
.copied(),
);
}
if IsLocked(&mut self.synthesizer) != 0 {
RefreshSynthesizer(&mut self.synthesizer);
Expand All @@ -85,7 +143,11 @@ impl Synthesizer {
}
unsafe {
while Synthesis2(&mut self.synthesizer) != 0 {
self.queue.extend(slice::from_raw_parts(self.synthesizer.buffer, self.synthesizer.buffer_size as usize).iter().copied());
self.queue.extend(
slice::from_raw_parts(self.synthesizer.buffer, self.synthesizer.buffer_size as usize)
.iter()
.copied(),
);
}
if IsLocked(&mut self.synthesizer) != 0 {
RefreshSynthesizer(&mut self.synthesizer);
Expand Down
24 changes: 21 additions & 3 deletions world_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@ use std::env;
use std::path::Path;

const WORLD_BASE_DIR: &str = "WORLD";
const WORLD_FILE_NAMES: &[&str] = &["cheaptrick", "codec", "common", "d4c", "dio", "fft", "harvest", "matlabfunctions", "stonemask", "synthesis", "synthesisrealtime"];
const WORLD_FILE_NAMES: &[&str] = &[
"cheaptrick",
"codec",
"common",
"d4c",
"dio",
"fft",
"harvest",
"matlabfunctions",
"stonemask",
"synthesis",
"synthesisrealtime",
];

fn main() {
let world_src_dir = Path::new(WORLD_BASE_DIR).join("src");
generate_bindgen(&world_src_dir);
for file_name in WORLD_FILE_NAMES {
cc::Build::new().cpp(true).file(&world_src_dir.join(file_name).with_extension("cpp")).include(&world_src_dir).compile(file_name);
cc::Build::new()
.cpp(true)
.file(&world_src_dir.join(file_name).with_extension("cpp"))
.include(&world_src_dir)
.compile(file_name);
}
}

Expand All @@ -21,7 +37,9 @@ fn generate_bindgen(world_src_dir: impl AsRef<Path>) {
let world_header_dir = world_src_dir.join("world");
WORLD_FILE_NAMES
.iter()
.fold(bindgen::builder(), |builder, &entry| builder.header(world_header_dir.join(entry).with_extension("h").to_str().unwrap()))
.fold(bindgen::builder(), |builder, &entry| {
builder.header(world_header_dir.join(entry).with_extension("h").to_str().unwrap())
})
.clang_arg(format!("-I{}", world_src_dir.display()))
.clang_arg("-fparse-all-comments")
.derive_copy(false)
Expand Down

0 comments on commit 0fec809

Please sign in to comment.