Lyraは、Googleが開発した低ビットレートでも高品質な音声コーデックである。
Lyraよりも高性能なニューラルコーデックには、VALL-Eでも使用されているEncodecがあるが、商用利用は禁止されている。
Lyraは、Apache-2.0 ライセンスで、商用でも利用できる。
LyraはC++で実装されており、コマンドラインツールと、C/C++用のライブラリ(.so)がビルドできる。
Python用のインターフェースは用意されていないため、Pythonから呼び出せるモジュールをビルドできるようにした。
以下、ビルド方法について説明する。
実行環境には、DockerのUbuntu 22.04のコンテナを使用している。
ビルド環境構築
npmインストール
apt install nodejs npm npm install -g n n stable apt purge nodejs npm
再ログインする。
bazeliskインストール
npm install -g @bazel/bazelisk
Numpyインストール
Numpyをインストールする。
apt install python3-numpy -y
ソース修正
WORKSPACE編集
WORKSPACEの末尾に以下の内容を追記する。
http_archive(
name = "pybind11_bazel",
strip_prefix = "pybind11_bazel-master",
urls = ["https://github.com/pybind/pybind11_bazel/archive/master.zip"],
)
# We still require the pybind library.
http_archive(
name = "pybind11",
build_file = "@pybind11_bazel//:pybind11.BUILD",
strip_prefix = "pybind11-2.10.4",
urls = ["https://github.com/pybind/pybind11/archive/v2.10.4.tar.gz"],
)
load("@pybind11_bazel//:python_configure.bzl", "python_configure")
python_configure(name = "local_config_python", python_version = "3")
BUILDの編集
lyra/cli_example/BUILDの末尾に以下の内容を追記する。
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
pybind_extension(
name = "lyra",
srcs = [
"python_lib.cc",
],
deps = [
":encoder_main_lib",
":decoder_main_lib",
"//lyra:lyra_config",
"//lyra:lyra_encoder",
"//lyra:no_op_preprocessor",
"//lyra:wav_utils",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
"@com_google_absl//absl/types:span",
"@com_google_glog//:glog",
"@gulrak_filesystem//:filesystem",
],
)
ビルド
以下のコマンドでビルドを実行する。
bazelisk build -c opt lyra/cli_example:lyra.so
テスト
bazel-bin/lyra/cli_exampleに作業ディレクトリを移動し、Pythonインタプリタを起動する。
インタプリタで以下を実行する。
>>> import lyra >>> from scipy.io.wavfile import read, write >>> fs, wave_data = read('/work/lyra/lyra/testdata/sample1_16kHz.wav') >>> fs 16000 >>> len(wave_data) 55177 >>> encoded_features = lyra.encode_wav(wave_data, 1, 16000, 3200, False, False, '/work/lyra/lyra/model_coeffs') INFO: Created TensorFlow Lite XNNPACK delegate for CPU. WARNING: Logging before InitGoogleLogging() is written to STDERR I20230420 23:16:08.065335 33860 encoder_main_lib.cc:91] Elapsed seconds : 0 I20230420 23:16:08.065402 33860 encoder_main_lib.cc:92] Samples per second : 835735 >>> len(encoded_features) 1376 >>> decoded_wav = lyra.decode_features(encoded_features, 16000, 3200, False, 0, 1, '/work/lyra/lyra/model_coeffs') I20230420 23:16:34.768642 33860 decoder_main_lib.cc:138] Elapsed seconds : 0 I20230420 23:16:34.768692 33860 decoder_main_lib.cc:139] Samples per second : 719900 >>> len(decoded_wav) 55040 >>> write('/work/decoded.wav', rate=16000, data=decoded_wav)
ログが出力されているが、ログを抑止したい場合は、
lyra.set_loglevel(2) # ERRORレベル以上
で抑止できる。