Skip to content

Commit

Permalink
enunu server
Browse files Browse the repository at this point in the history
  • Loading branch information
stakira committed Apr 25, 2022
1 parent 3ee9817 commit 9132c76
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 31 deletions.
71 changes: 71 additions & 0 deletions enunu_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#! /usr/bin/env python3
# coding: utf-8
# fmt: off
print('Starting enunu server...')
import json
import os
import subprocess
import sys
import traceback

sys.path.append(os.path.dirname(__file__))
import enunu
try:
import zmq
except ModuleNotFoundError:
python_exe = os.path.join('.', 'python-3.8.10-embed-amd64', 'python.exe')
command = [python_exe, '-m', 'pip', 'install', 'pyzmq']
print('command:', command)
subprocess.run(command, check=True)
import zmq
# fmt: on


def timing(path_ust: str):
path_full_timing, path_mono_timing = enunu.run_timing(path_ust)
return {
'path_full_timing': path_full_timing,
'path_mono_timing': path_mono_timing,
}


def acoustic(path_ust: str):
path_acoustic, path_f0, path_spectrogram, \
path_aperiodicity = enunu.run_acoustic(path_ust)
return {
'path_acoustic': path_acoustic,
'path_f0': path_f0,
'path_spectrogram': path_spectrogram,
'path_aperiodicity': path_aperiodicity,
}


def main():
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind('tcp://*:15555')
print('Started enunu server')

while True:
message = socket.recv()
request = json.loads(message)
print('Received request: %s' % request)

response = {}
try:
if request[0] == 'timing':
response['result'] = timing(request[1])
elif request[0] == 'acoustic':
response['result'] = acoustic(request[1])
else:
raise NotImplementedError('unexpected command %s' % request[0])
except Exception as e:
response['error'] = str(e)
traceback.print_exc()

print('Sending response: %s' % response)
socket.send_string(response)


if __name__ == '__main__':
main()
88 changes: 57 additions & 31 deletions synthesis/enunu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- キャッシュフォルダでいいと思う。
3. LABファイル→WAVファイル
"""
# fmt: off
import sys
import warnings
from datetime import datetime
Expand Down Expand Up @@ -40,6 +41,7 @@
print('インストール成功しました。歌声合成を始めます。')
print('----------------------------------------------------------\n')
import enulib
# fmt: on


def get_standard_function_config(config, key) -> Union[None, str]:
Expand Down Expand Up @@ -87,10 +89,20 @@ def get_project_path(path_utauplugin):
return path_ust, voice_dir, cache_dir


def main_as_plugin(path_plugin: str, path_wav: Union[str, None]) -> str:
def main_as_plugin(path_plugin: str) -> str:
"""
UtauPluginオブジェクトから音声ファイルを作る
"""
run_timing(path_plugin)
run_acoustic(path_plugin)
path_wav = run_synthesizer(path_plugin)

# 音声を再生する。
if exists(path_wav):
startfile(path_wav)


def setup(path_plugin: str):
# UTAUの一時ファイルに書いてある設定を読み取る
print(f'{datetime.now()} : reading settings in TMP')
path_ust, voice_dir, _ = get_project_path(path_plugin)
Expand All @@ -112,29 +124,19 @@ def main_as_plugin(path_plugin: str, path_wav: Union[str, None]) -> str:
# 日付時刻を取得
str_now = datetime.now().strftime('%Y%m%d_%H%M%S')

# wav出力パスが指定されていない(プラグインとして実行している)場合
if path_wav is None:
# 入出力パスを設定する
if path_ust is not None:
songname = splitext(basename(path_ust))[0]
out_dir = dirname(path_ust)
temp_dir = join(out_dir, f'{songname}_enutemp')
path_wav = abspath(join(out_dir, f'{songname}__{str_now}.wav'))
# WAV出力パス指定なしかつUST未保存の場合
else:
print('USTが保存されていないので一時フォルダにWAV出力します。')
songname = f'temp__{str_now}'
out_dir = mkdtemp(prefix='enunu-')
temp_dir = join(out_dir, f'{songname}_enutemp')
path_wav = abspath(join(out_dir, f'{songname}__{str_now}.wav'))
play_after_synth = True
# WAV出力パスが指定されている場合
# 入出力パスを設定する
if path_ust is not None:
songname = splitext(basename(path_ust))[0]
out_dir = dirname(path_ust)
temp_dir = join(out_dir, f'{songname}_enutemp')
path_wav = abspath(join(out_dir, f'{songname}__{str_now}.wav'))
# WAV出力パス指定なしかつUST未保存の場合
else:
songname = splitext(basename(path_wav))[0]
out_dir = dirname(path_wav)
print('USTが保存されていないので一時フォルダにWAV出力します。')
songname = f'temp__{str_now}'
out_dir = mkdtemp(prefix='enunu-')
temp_dir = join(out_dir, f'{songname}_enutemp')
path_wav = abspath(path_wav)
play_after_synth = False
path_wav = abspath(join(out_dir, f'{songname}__{str_now}.wav'))

# 一時出力フォルダがなければつくる
makedirs(temp_dir, exist_ok=True)
Expand All @@ -157,6 +159,18 @@ def main_as_plugin(path_plugin: str, path_wav: Union[str, None]) -> str:
print(f'{datetime.now()} : copying Table')
copy(config.table_path, path_temp_table)

return config, path_temp_ust, path_temp_table, \
path_full_score, path_mono_score, \
path_full_timing, path_mono_timing, path_acoustic, \
path_f0, path_spectrogram, path_aperiodicity, path_wav


def run_timing(path_plugin: str):
config, path_temp_ust, path_temp_table, path_full_score, \
path_mono_score, path_full_timing, path_mono_timing, \
path_acoustic, path_f0, path_spectrogram, path_aperiodicity, \
path_wav = setup(path_plugin)

# USTを事前加工------------------------------------------------------------------
extension_list = get_extension_path_list(config, 'ust_editor')
if extension_list is not None:
Expand Down Expand Up @@ -298,6 +312,15 @@ def main_as_plugin(path_plugin: str, path_wav: Union[str, None]) -> str:
enulib.extensions.merge_full_time_change_to_mono(
path_full_timing, path_mono_timing)

return path_full_timing, path_mono_timing


def run_acoustic(path_plugin: str):
config, path_temp_ust, path_temp_table, path_full_score, \
path_mono_score, path_full_timing, path_mono_timing, \
path_acoustic, path_f0, path_spectrogram, path_aperiodicity, \
path_wav = setup(path_plugin)

# 音響パラメータを推定 timing.full -> acoustic---------------------------
calculator = get_standard_function_config(config, 'acoustic_calculator')
# 計算をしない場合
Expand Down Expand Up @@ -354,6 +377,15 @@ def main_as_plugin(path_plugin: str, path_wav: Union[str, None]) -> str:
aperiodicity=path_aperiodicity
)

return path_acoustic, path_f0, path_spectrogram, path_aperiodicity


def run_synthesizer(path_plugin: str):
config, path_temp_ust, path_temp_table, path_full_score, \
path_mono_score, path_full_timing, path_mono_timing, \
path_acoustic, path_f0, path_spectrogram, path_aperiodicity, \
path_wav = setup(path_plugin)

# WORLDを使って音声ファイルを生成: acoustic.csv -> <songname>.wav--------------
synthesizer = get_standard_function_config(config, 'wav_synthesizer')

Expand Down Expand Up @@ -412,10 +444,6 @@ def main_as_plugin(path_plugin: str, path_wav: Union[str, None]) -> str:
# print(f'{datetime.now()} : converting LAB to JSON')
# hts2json(path_full_score, path_json)

# 音声を再生する。
if play_after_synth and exists(path_wav):
startfile(path_wav)

return path_wav


Expand All @@ -425,18 +453,16 @@ def main(path_plugin: str, path_wav_out: Union[str, None]):
"""
# logging.basicConfig(level=logging.INFO)
if path_plugin.endswith('.tmp'):
main_as_plugin(path_plugin, path_wav_out)
main_as_plugin(path_plugin)
else:
raise ValueError('Input file must be TMP(plugin).')


if __name__ == '__main__':
print('_____ξ ・ヮ・)ξ < ENUNU v0.4.0 ________')
print(f'argv: {argv}')
if len(argv) == 3:
main(argv[1], argv[2])
elif len(argv) == 2:
main(argv[1], None)
if len(argv) == 2:
main(argv[1])
elif len(argv) == 1:
main(input('Input file path of TMP(plugin)\n>>> ').strip('"'), None)
else:
Expand Down

0 comments on commit 9132c76

Please sign in to comment.