Skip to content

Commit 4392df6

Browse files
authored
feat!: Remove unstable SIMD optimization (#99)
1 parent 506c136 commit 4392df6

8 files changed

Lines changed: 119 additions & 286 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ documentation = "https://docs.rs/jbonsai"
1616
default = ["htsvoice"]
1717
binary = ["htsvoice", "dep:hound"]
1818
htsvoice = ["dep:nom"]
19-
simd = []
2019

2120
[package.metadata.docs.rs]
2221
all-features = true

README-ja.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@ jbonsai = "0.3.0"
2929

3030
<!-- x-release-please-end -->
3131

32-
### SIMD (experimental)
33-
34-
jbonsaiは、feature [portable_simd](https://github.com/rust-lang/portable-simd)による高速化をサポートしています。SIMD高速化を有効にするには、
35-
36-
- nightly ツールチェーンを使用する必要があります。
37-
- 次のように`features = ["simd"]`を指定する必要があります。
38-
<!-- x-release-please-start-version -->
39-
```toml
40-
[dependencies]
41-
jbonsai = { version = "0.3.0", features = ["simd"] }
42-
```
43-
<!-- x-release-please-end -->
44-
45-
SIMDサポートは非常に実験的であり、いつでも変更される可能性があります。
46-
4732
## 使用例
4833

4934
以下の例は,「盆栽」と読み上げる音声を生成し,`speech`変数にモノラル, 48000 HzのPCMとして格納します.

README.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@ jbonsai = "0.3.0"
2929

3030
<!-- x-release-please-end -->
3131

32-
### SIMD (experimental)
33-
34-
jbonsai supports acceleration provided by feature [portable_simd](https://github.com/rust-lang/portable-simd). In order to enable SIMD acceleration,
35-
36-
- you must use nightly toolchain.
37-
- you have to specify `features = ["simd"]` as follows:
38-
<!-- x-release-please-start-version -->
39-
```toml
40-
[dependencies]
41-
jbonsai = { version = "0.3.0", features = ["simd"] }
42-
```
43-
<!-- x-release-please-end -->
44-
45-
The SIMD support is highly experimental and may change at any time.
46-
4732
## Example
4833

4934
This example produces a mono, 48,000 Hz (typically) PCM data saying 「盆栽」(ぼんさい; bonsai) in `speech` variable.

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![deny(missing_docs)]
22
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
3-
#![cfg_attr(feature = "simd", feature(portable_simd))]
43
#![doc = include_str!("../README.md")]
54

65
mod constants;

src/vocoder/mlsa.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
use super::coefficients::Coefficients;
2+
3+
/// N == pd + 1
4+
#[derive(Debug, Clone)]
5+
pub struct MelLogSpectrumApproximation<const N: usize> {
6+
d11: [f64; N],
7+
d12: [f64; N],
8+
d21: [Vec<f64>; N],
9+
d22: [f64; N],
10+
}
11+
12+
pub trait Pade<const N: usize> {
13+
const PPADE: [f64; N];
14+
}
15+
macro_rules! impl_pade {
16+
($($i:literal: $ppade:expr),* $(,)?) => {
17+
$(
18+
impl Pade<$i> for MelLogSpectrumApproximation<$i> {
19+
const PPADE: [f64; $i] = $ppade;
20+
}
21+
)*
22+
};
23+
}
24+
25+
impl_pade!(
26+
1: [1.00000000000f64],
27+
2: [1.00000000000f64, 0.00000000000f64],
28+
3: [1.00000000000f64, 0.00000000000f64, 0.00000000000f64],
29+
4: [1.00000000000f64, 0.00000000000f64, 0.00000000000f64, 0.00000000000f64],
30+
5: [1.00000000000f64, 0.49992730000f64, 0.10670050000f64, 0.01170221000f64, 0.00056562790f64],
31+
6: [1.00000000000f64, 0.49993910000f64, 0.11070980000f64, 0.01369984000f64, 0.00095648530f64, 0.00003041721f64],
32+
);
33+
34+
impl<const N: usize> MelLogSpectrumApproximation<N>
35+
where
36+
Self: Pade<N>,
37+
{
38+
pub fn new(nmcp: usize) -> Self {
39+
Self {
40+
d11: [0.0; N],
41+
d12: [0.0; N],
42+
d21: std::array::from_fn(|_| vec![0.0; nmcp]),
43+
d22: [0.0; N],
44+
}
45+
}
46+
47+
#[inline(always)]
48+
pub fn df(&mut self, x: &mut f64, alpha: f64, coefficients: &'_ Coefficients) {
49+
self.df1(x, alpha, coefficients);
50+
self.df2(x, alpha, coefficients);
51+
}
52+
53+
#[inline(always)]
54+
fn df1(&mut self, x: &mut f64, alpha: f64, coefficients: &'_ Coefficients) {
55+
let aa = 1.0 - alpha * alpha;
56+
let mut out = 0.0;
57+
for i in (1..N).rev() {
58+
self.d11[i] = aa * self.d12[i - 1] + alpha * self.d11[i];
59+
self.d12[i] = self.d11[i] * coefficients[1];
60+
let v = self.d12[i] * Self::PPADE[i];
61+
*x += if i & 1 != 0 { v } else { -v };
62+
out += v;
63+
}
64+
self.d12[0] = *x;
65+
*x += out;
66+
}
67+
68+
#[inline(always)]
69+
fn df2(&mut self, x: &mut f64, alpha: f64, coefficients: &'_ Coefficients) {
70+
let mut out = 0.0;
71+
for i in (1..N).rev() {
72+
self.d22[i] = fir(&mut self.d21[i - 1], self.d22[i - 1], alpha, coefficients);
73+
let v = self.d22[i] * Self::PPADE[i];
74+
*x += if i & 1 != 0 { v } else { -v };
75+
out += v;
76+
}
77+
self.d22[0] = *x;
78+
*x += out;
79+
}
80+
}
81+
82+
fn fir(d: &mut [f64], x: f64, alpha: f64, coefficients: &[f64]) -> f64 {
83+
d[0] = x;
84+
85+
let a = alpha;
86+
let aa = a * a;
87+
let aaaa = aa * aa;
88+
let iaa = 1.0 - aa;
89+
let mut rem = 0.0;
90+
91+
let mut chunks = d.chunks_exact_mut(4);
92+
for chunk in chunks.by_ref() {
93+
(chunk[0], chunk[1], chunk[2], chunk[3], rem) = (
94+
iaa * rem + a * chunk[0],
95+
iaa * (-a * rem + chunk[0]) + a * chunk[1],
96+
iaa * (aa * rem + (-a * chunk[0] + chunk[1])) + a * chunk[2],
97+
iaa * (aa * (-a * rem + chunk[0]) + (-a * chunk[1] + chunk[2])) + a * chunk[3],
98+
aaaa * rem + (aa * (-a * chunk[0] + chunk[1]) + (-a * chunk[2] + chunk[3])),
99+
);
100+
}
101+
for di in chunks.into_remainder() {
102+
(*di, rem) = (iaa * rem + a * *di, -a * rem + *di);
103+
}
104+
105+
let mut y = [0.0; 8];
106+
let mut c = coefficients[2..d.len()].chunks_exact(8);
107+
let mut d = d[2..d.len()].chunks_exact(8);
108+
109+
use std::iter::zip;
110+
for (c, d) in zip(&mut c, &mut d) {
111+
for (y, (c, d)) in zip(&mut y, zip(c, d)) {
112+
*y += c * d;
113+
}
114+
}
115+
for (y, (c, d)) in zip(&mut y, zip(c.remainder(), d.remainder())) {
116+
*y += c * d;
117+
}
118+
((y[0] + y[1]) + (y[2] + y[3])) + ((y[4] + y[5]) + (y[6] + y[7]))
119+
}

src/vocoder/mlsa/fir.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/vocoder/mlsa/fir_simd.rs

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)