Skip to content

Commit

Permalink
Fix: Windowsでworldlineが使えないのを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed May 30, 2024
1 parent 822579e commit 7b17afc
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 33 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/worldline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ crate-type = ["rlib"]

[dependencies]
anyhow = "1.0.86"
tokio = "1.37.0"
dlopen2 = "0.7.0"
once_cell = "1.19.0"
tokio = { version = "1.37.0", features = ["rt"] }
tracing = "0.1.40"

[build-dependencies]
Expand Down
10 changes: 3 additions & 7 deletions crates/worldline/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ fn main() {
std::env::var("CARGO_MANIFEST_DIR").unwrap(),
);
eprintln!("Building cpp code in {}", cpp_path);
if duct::cmd!("bazelisk", "build", "//worldline")
if let Err(e) = duct::cmd!("bazelisk", "build", "//worldline")
.dir(cpp_path)
.run()
.is_err()
{
if std::env::var("PROFILE").unwrap() == "release" {
panic!("Failed to build cpp code");
panic!("Failed to build cpp code: {:?}", e);
}
// rust-analyzerだとなぜかエラーが出るので握りつぶす。
// TODO: ちゃんと直す
eprintln!("Failed to build cpp code");
eprintln!("Failed to build cpp code: {:?}", e);
std::process::exit(0);
}

Expand All @@ -43,7 +42,4 @@ fn main() {
// TODO: もっといい方法があれば変える
let binary = std::fs::read(&out_lib_path).unwrap();
std::fs::write(&target_lib_path, binary).unwrap();

println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-lib=dylib=worldline");
}
38 changes: 32 additions & 6 deletions crates/worldline/src/phrase_synth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
use super::sys;
use dlopen2::wrapper::Container;
use once_cell::sync::Lazy;
use tracing::info;

static LIB: Lazy<Container<sys::WorldlineSys>> = Lazy::new(|| {
let lib_name = if cfg!(target_os = "windows") {
"worldline.dll"
} else if cfg!(target_os = "macos") {
"libworldline.dylib"
} else {
"libworldline.so"
};
let exe_path = std::env::current_exe().unwrap();
let lib_paths = [
exe_path.parent().unwrap().join(lib_name),
exe_path.parent().unwrap().parent().unwrap().join(lib_name),
];
let lib_path = lib_paths
.iter()
.find(|p| p.exists())
.expect("Failed to find libworldline.so");

unsafe {
let lib = Container::load(lib_path);
lib.unwrap()
}
});

pub struct PhraseSynth {
inner: Inner,
}
Expand All @@ -20,7 +46,7 @@ impl Default for PhraseSynth {
impl PhraseSynth {
pub fn new() -> Self {
Self {
inner: Inner(unsafe { sys::PhraseSynthNew() }),
inner: Inner(unsafe { LIB.PhraseSynthNew() }),
}
}

Expand All @@ -35,7 +61,7 @@ impl PhraseSynth {
) {
let c_request = request.into_sys();
unsafe {
sys::PhraseSynthAddRequest(
LIB.PhraseSynthAddRequest(
self.inner.0,
&c_request,
pos_ms,
Expand All @@ -57,7 +83,7 @@ impl PhraseSynth {
voicing: &[f64],
) {
unsafe {
sys::PhraseSynthSetCurves(
LIB.PhraseSynthSetCurves(
self.inner.0,
f0.as_ptr(),
gender.as_ptr(),
Expand All @@ -73,7 +99,7 @@ impl PhraseSynth {
pub fn synth(&mut self) -> Vec<f32> {
let mut y = std::ptr::null_mut();
unsafe {
let len = sys::PhraseSynthSynth(self.inner.0, &mut y, log_callback) as usize;
let len = LIB.PhraseSynthSynth(self.inner.0, &mut y, log_callback) as usize;
let y = std::slice::from_raw_parts(y, len);
y.to_vec()
}
Expand All @@ -85,7 +111,7 @@ impl PhraseSynth {
let mut y = std::ptr::null_mut();
let inner = inner;
unsafe {
let len = sys::PhraseSynthSynth(inner.0, &mut y, log_callback) as usize;
let len = LIB.PhraseSynthSynth(inner.0, &mut y, log_callback) as usize;
let y = std::slice::from_raw_parts(y, len);
y.to_vec()
}
Expand All @@ -98,7 +124,7 @@ impl PhraseSynth {
impl Drop for PhraseSynth {
fn drop(&mut self) {
unsafe {
sys::PhraseSynthDelete(self.inner.0);
LIB.PhraseSynthDelete(self.inner.0);
}
}
}
Expand Down
40 changes: 21 additions & 19 deletions crates/worldline/src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(non_snake_case, clippy::too_many_arguments)]
use dlopen2::wrapper::WrapperApi;
use std::ffi::{c_char, c_double, c_float, c_int};

#[repr(C)]
Expand Down Expand Up @@ -34,35 +36,35 @@ pub struct SynthRequest {

type LogCallback = extern "C" fn(message: *const c_char);

#[allow(dead_code)]
extern "C-unwind" {
pub fn F0(
#[derive(WrapperApi)]
pub struct WorldlineSys {
F0: unsafe extern "C" fn(
samples: *mut c_float,
length: c_int,
fs: c_int,
frame_period: c_double,
method: c_int,
f0: *mut *mut c_double,
) -> c_int;
) -> c_int,

pub fn DecodeMgc(
DecodeMgc: unsafe extern "C" fn(
f0_length: c_int,
mgc: *mut c_double,
mgc_size: c_int,
fft_size: c_int,
fs: c_int,
spectrogram: *mut *mut c_double,
) -> c_int;
) -> c_int,

pub fn DecodeBap(
DecodeBap: unsafe extern "C" fn(
f0_length: c_int,
bap: *mut c_double,
fft_size: c_int,
fs: c_int,
aperiodicity: *mut *mut c_double,
) -> c_int;
) -> c_int,

pub fn WorldSynthesis(
WorldSynthesis: unsafe extern "C" fn(
f0: *const c_double,
f0_length: c_int,
mgc_or_sp: *const c_double,
Expand All @@ -78,15 +80,15 @@ extern "C-unwind" {
tension: *const c_double,
breathiness: *const c_double,
voicing: *const c_double,
) -> c_int;
) -> c_int,

pub fn Resample(request: *const SynthRequest, y: *mut *mut c_float) -> c_int;
Resample: unsafe extern "C" fn(request: *const SynthRequest, y: *mut *mut c_float) -> c_int,

pub fn PhraseSynthNew() -> *mut PhraseSynth;
PhraseSynthNew: unsafe extern "C" fn() -> *mut PhraseSynth,

pub fn PhraseSynthDelete(phrase_synth: *mut PhraseSynth);
PhraseSynthDelete: unsafe extern "C" fn(phrase_synth: *mut PhraseSynth),

pub fn PhraseSynthAddRequest(
PhraseSynthAddRequest: unsafe extern "C" fn(
phrase_synth: *mut PhraseSynth,
request: *const SynthRequest,
pos_ms: c_double,
Expand All @@ -95,9 +97,9 @@ extern "C-unwind" {
fade_in_ms: c_double,
fade_out_ms: c_double,
logCallback: LogCallback,
);
),

pub fn PhraseSynthSetCurves(
PhraseSynthSetCurves: unsafe extern "C" fn(
phrase_synth: *mut PhraseSynth,
f0: *const c_double,
gender: *const c_double,
Expand All @@ -106,11 +108,11 @@ extern "C-unwind" {
voicing: *const c_double,
length: c_int,
logCallback: LogCallback,
);
),

pub fn PhraseSynthSynth(
PhraseSynthSynth: unsafe extern "C" fn(
phrase_synth: *mut PhraseSynth,
y: *mut *mut c_float,
logCallback: LogCallback,
) -> c_int;
) -> c_int,
}

0 comments on commit 7b17afc

Please sign in to comment.