ノイズから母音を合成

May 5, 2020

世の中ではAIが流暢に話すのが当たり前になりつつありますが、ここでやるのは単純なノイズにフィルタを書けて母音っぽくするという単純なものです。

ノイズの生成

合成の元となるノイズです。 参考文献「https://www.jstage.jst.go.jp/article/jasj/73/4/73_230/_article/-char/ja/」の pulse_train.wav に似せて作りました。

https://soundcloud.com/user-255261297-724174099/pulse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import numpy as np
from scipy.io import wavfile

A = 1
fs = 8000
duration = 1

t = np.arange(0, fs * duration) / fs

margin_ms = 8
margin_size = int(len(t) * margin_ms / (duration * 1000))
y = np.array([A if i % margin_size == 0 else 0 for i in np.arange(len(t))])

wavfile.write(f'output/pulse.wav', fs, (y * 32767).astype(np.int16))

ローパスフィルタ

FIRフィルタの練習のために、Low passフィルタ(LPF)をかけてみます。

https://soundcloud.com/user-255261297-724174099/lowpass

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np
from scipy.io import wavfile
from scipy import signal

A = 1
fs = 8000
duration = 1

t = np.arange(0, fs * duration) / fs

margin_ms = 8
margin_size = int(len(t) * margin_ms / (duration * 1000))
y = np.array([A if i % margin_size == 0 else 0 for i in np.arange(len(t))])

numtaps = 21
dt = 1 / fs
cutoff = 250
b = signal.firwin(numtaps, cutoff, fs=1 / dt)
gain = 2

filtered = signal.lfilter(b, 1, y) * gain

wavfile.write(f'output/lowpass.wav', fs, (filtered * 32767).astype(np.int16))

「あ」の合成

Band passフィルタ(BPF)で「あ」っぽく聞こえるようにしてみます。

https://soundcloud.com/user-255261297-724174099/a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np
from functools import reduce
from scipy.io import wavfile
from scipy import signal

def bpf(noise, dt, cutoff):
    numtaps = 51
    b = signal.firwin(numtaps, [cutoff - 50, cutoff + 50], fs=1 / dt, pass_zero=False)
    return signal.lfilter(b, 1, noise)

A = 1
fs = 8000
duration = 1

t = np.arange(0, fs * duration) / fs

margin_ms = 8
margin_size = int(len(t) * margin_ms / (duration * 1000))
noise = np.array([1 if i % margin_size == 0 else 0 for i in np.arange(len(t))])

dt = 1 / fs
cutoff_list = [800, 1200, 2500, 3500]

gain = 2
y = reduce(lambda x, cutoff: x + bpf(noise, dt, cutoff), cutoff_list, np.zeros_like(t)) * gain

wavfile.write(f'output/a.wav', fs, (y * 32767).astype(np.int16))

「あいうえお」の合成

「あ」がうまくいったので他の母音も続けてみます。

https://soundcloud.com/user-255261297-724174099/aiueo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
from functools import reduce
from scipy.io import wavfile
from scipy import signal

def bpf(noise, dt, cutoff):
    numtaps = 51
    b = signal.firwin(numtaps, [cutoff - 50, cutoff + 50], fs=1 / dt, pass_zero=False)
    return signal.lfilter(b, 1, noise)

def vowel(fs, i, cutoffs):
    A = 1
    duration = 0.4
    offset = i * duration
    t = np.arange(offset, offset + fs * duration) / fs

    margin_ms = 8
    margin_size = int(len(t) * margin_ms / (duration * 1000))
    noise = np.array([A if i % margin_size == 0 else 0 for i in np.arange(len(t))])

    dt = 1 / fs
    gain = 2

    return reduce(lambda x, cutoff: x + bpf(noise, dt, cutoff), cutoffs, np.zeros_like(t)) * gain

def deemphasis(x):
    y = np.zeros_like(x)
    for i in np.arange(len(y)):
        if i > 0:
            y[i] = 0.98 * y[i - 1] + x[i]
        else:
            y[i] = x[0]
    return y

fs = 8000
cutoff_list = [
        [800, 1200, 2500, 3500],
        [300, 2300, 2900, 3500],
        [300, 1200, 2500, 3500],
        [500, 1900, 2500, 2500],
        [500, 800, 2500, 2500],
        ]
y = deemphasis(reduce(lambda x, cutoffs: np.append(x, vowel(fs, len(x), cutoffs)), cutoff_list, []))

wavfile.write(f'output/aiueo.wav', fs, (y * 32767).astype(np.int16))

予想より母音っぽく聞こえてテンションがあがりました。

参考

https://www.jstage.jst.go.jp/article/jasj/73/4/73_230/_article/-char/ja/

https://helve-python.hatenablog.jp/entry/2018/06/18/000000