1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

有名なサイトを参考にPythonとSPTKを使用してボイスチェンジャを作りたい

Last updated at Posted at 2019-12-12

前提・実現したいこと

PythonとSPTKを使いボイスチェンジャを作りたい。

以下のサイトを参考にさせていただきました。
ソースコードについては、基本的に書き換えておらずそのまま使用しています。

  • [ http://aidiary.hatenablog.com/entry/20120701/1341126474 ]
    SPTKの使い方 (1) インストール・波形描画・音声再生

  • [ http://aidiary.hatenablog.com/entry/20150225/1424863972 ]
    統計的声質変換 (2) ボイスチェンジャーを作ろう

発生している問題・エラーメッセージ


*** Now recording ... (10 sec)
*** extract pitch ...
*** extract mel cepstrum
*** modify parameters ...
'x2x' �́A�����R}���h�܂��͊O���R}���hA
����”\ȃv���O�����܂��̓ob�` t@C���Ƃ��ĔF������Ă��܂���B
'x2x' �́A�����R}���h�܂��͊O���R}���hA
����”\ȃv���O�����܂��̓ob�` t@C���Ƃ��ĔF������Ă��܂���B
'sopr' �́A�����R}���h�܂��͊O���R}���hA
����”\ȃv���O�����܂��̓ob�` t@C���Ƃ��ĔF������Ă��܂���B
*** play!
'sopr' �́A�����R}���h�܂��͊O���R}���hA
����”\ȃv���O�����܂��̓ob�` t@C���Ƃ��ĔF������Ă��܂���B
Traceback (most recent call last):
  File "C:/Users/joho-2/PycharmProjects/R sound 10/R sound 10.py", line 127, in <module>
    play("output.raw")
  File "C:/Users/joho-2/PycharmProjects/R sound 10/R sound 10.py", line 78, in play
    f = open(raw_file, "rb")
IOError: [Errno 2] No such file or directory: 'output.raw'

該当のソースコード

# coding: utf-8
import pyaudio
import struct
import subprocess

# SPTKを使った簡単なボイスチェンジャー

CHANNELS = 1
RATE = 16000
CHUNK = 1024

def record(raw_file, record_seconds=5):
    """音声ファイルを録音する
    録音時間は固定。キーボードを押すとループ終わりができなかった・・・"""
    fp = open(raw_file, "wb")
    for _ in range(0, int(RATE / CHUNK * record_seconds)):
        data = stream.read(CHUNK)
        fp.write(struct.pack('s' * CHUNK * 2, *data))
    fp.close()
    stream.stop_stream()
    stream.close()
    p.terminate()

def extract_pitch(raw_file, pitch_file):
    """ピッチパラメータの抽出"""
    cmd = "x2x +sf %s | pitch -a 1 -s 16 -p 80 > %s" % (raw_file, pitch_file)
    subprocess.call(cmd, shell=True)

def extract_mcep(raw_file, mcep_file):
    """メルケプストラムパラメータの抽出"""
    cmd = "x2x +sf %s | frame -p 80 | window | mcep -m 25 -a 0.42 > %s" % (raw_file, mcep_file)
    subprocess.call(cmd, shell=True)

def modify_pitch(m, pitch_file, mcep_file, raw_file):
    """ピッチを変形して再合成
    mが1より大きい => 低い声
    mが1より小さい => 高い声"""
    cmd = "sopr -m %f %s | excite -p 80 | mlsadf -m 25 -a 0.42 -p 80 %s | clip -y -32000 32000 | x2x +fs > %s" % (m, pitch_file, mcep_file, raw_file)
    subprocess.call(cmd, shell=True)

def modify_speed(frame_shift, pitch_file, mcep_file, raw_file):
    """話速を変形して再合成
    frame_shiftが小さい => 早口
    frame_shiftが大きい => ゆっくり"""
    cmd = "excite -p %f %s | mlsadf -m 25 -a 0.42 -p %f %s | clip -y -32000 32000 | x2x +fs > %s" % (frame_shift, pitch_file, frame_shift, mcep_file, raw_file)
    subprocess.call(cmd, shell=True)

def hoarse_voice(pitch_file, mcep_file, raw_file):
    """ささやき声"""
    modify_pitch(0, pitch_file, mcep_file, raw_file)

def robot_voice(frame_period, record_seconds, mcep_file, raw_file):
    """ロボット声
    frame_periodが小さい => 低い
    frame_periodが大きい => 高い"""
    sequence_length = record_seconds * RATE * frame_period
    cmd = "train -p %d -l %d | mlsadf -m 25 -a 0.42 -p 80 %s | clip -y -32000 32000 | x2x +fs > %s" % (frame_period, sequence_length, mcep_file, raw_file)
    subprocess.call(cmd, shell=True)

def child_voice(pitch_file, mcep_file, raw_file):
    """子供声"""
    cmd = "sopr -m 0.4 %s | excite -p 80 | mlsadf -m 25 -a 0.1 -p 80 %s | clip -y -32000 32000 | x2x +fs > %s" % (pitch_file, mcep_file, raw_file)
    subprocess.call(cmd, shell=True)

def deep_voice(pitch_file, mcep_file, raw_file):
    """太い声"""
    cmd = "sopr -m 2.0 %s | excite -p 80 | mlsadf -m 25 -a 0.6 -p 80 %s | clip -y -32000 32000 | x2x +fs > %s" % (pitch_file, mcep_file, raw_file)
    subprocess.call(cmd, shell=True)

def raw2wav(raw_file, wav_file):
    cmd = "sox -e signed-integer -c %d -b 16 -r %d %s %s" % (CHANNELS, RATE, raw_file, wav_file)
    subprocess.call(cmd, shell=True)

def play(raw_file):
    """rawファイルを再生"""
    p = pyaudio.PyAudio()
    stream = p.open(format=p.get_format_from_width(2), channels=CHANNELS, rate=RATE, output=True)
    f = open(raw_file, "rb")
    data = f.read(CHUNK)
    while data != '':
        stream.write(data)
        data = f.read(CHUNK)
    stream.stop_stream()
    stream.close()
    p.terminate()

if __name__ == "__main__":
    # 録音時間(固定)
    record_seconds = 10

    p = pyaudio.PyAudio()
    stream = p.open(format=pyaudio.paInt16,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    pitch_file = "temp.pitch"
    mcep_file = "temp.mcep"
    raw_file = "temp.raw"
    output_file = "output.raw"

    # オリジナルの音声を録音してrawファイルとして書き出し
    print "*** Now recording ... (%d sec)" % record_seconds
    record(raw_file, record_seconds)

    # パラメータ抽出
    print "*** extract pitch ..."
    extract_pitch(raw_file, pitch_file)

    print "*** extract mel cepstrum"
    extract_mcep(raw_file, mcep_file)

    # パラメータ変形いろいろ
    print "*** modify parameters ..."

    # どれか一つしか有効にできない
    modify_pitch(0.3, pitch_file, mcep_file, output_file)
#    modify_speed(300, pitch_file, mcep_file, output_file)
#    hoarse_voice(pitch_file, mcep_file, output_file)
#    robot_voice(100, record_seconds, mcep_file, output_file)
#    child_voice(pitch_file, mcep_file, output_file)
    deep_voice(pitch_file, mcep_file, output_file)

    # 変換した音声を再生
    print "*** play!"
    play("output.raw")

試したこと

Pythonのバージョンの問題かと考え、3系から2系に変えたのですがうまくいかず・・・
そもそも録音ができていないのではと思い、何度もサイトを見直したりして勉強したのですが、
ここからエラーがなくならず、どうしてもうまくいきませんでした;;

補足情報(FW/ツールのバージョンなど)

  • Python 2.7.17
  • SPTK 3.5 (ヘルプメッセージまで出力できています)
  • エディタ PyCharm (pyaudio インストール済み)
  • Windows 7

最近Pythonを勉強したばかりの初心者です。助言いただけると本当にうれしいです。
どうぞ宜しくお願い致します。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?