Last active
June 24, 2026 07:29
-
-
Save z80oolong/f95f5e98cd4874bd6f7173f03cdf6fce to your computer and use it in GitHub Desktop.
VOICEVOX_CORE 関連の短いプログラム若しくはファイル等
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| CLI より VOICEVOX CORE の C API 経由でテキスト音声合成を行い、 | |
| 音声ファイルを出力する。簡単なテスト目的のプログラムである。 | |
| Copyright (C) 2026 z80oolong (zool@zool.jpn.org) | |
| Permission is hereby granted, free of charge, to any person obtaining | |
| a copy of this software and associated documentation files (the | |
| "Software"), to deal in the Software without restriction, including | |
| without limitation the rights to use, copy, modify, merge, publish, | |
| distribute, sublicense, and/or sell copies of the Software, and to | |
| permit persons to whom the Software is furnished to do so, subject to | |
| the following conditions: | |
| The above copyright notice and this permission notice shall be | |
| included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <stdbool.h> | |
| #include "voicevox_core.h" | |
| #define HOMEBREW_PREFIX "/home/linuxbrew/.linuxbrew" | |
| #ifndef VOICEVOX_CORE_PREFIX | |
| #define VOICEVOX_CORE_PREFIX HOMEBREW_PREFIX "/opt/voicevox-core" | |
| #endif | |
| #ifndef DEFAULT_ONNXRUNTIME_PATH | |
| #define DEFAULT_ONNXRUNTIME_PATH VOICEVOX_CORE_PREFIX "/lib/voicevox/libonnxruntime.so" | |
| #endif | |
| #ifndef DEFAULT_OPENJTALK_DIC_DIR | |
| #define DEFAULT_OPENJTALK_DIC_DIR VOICEVOX_CORE_PREFIX "/share/voicevox/dic" | |
| #endif | |
| #ifndef DEFAULT_VVM_MODEL_PATH | |
| #define DEFAULT_VVM_MODEL_PATH VOICEVOX_CORE_PREFIX "/share/voicevox/models/sample.vvm" | |
| #endif | |
| #define DEFAULT_SPEAKER_ID 0 | |
| #define DEFAULT_OUTPUT_FILE "./audio.wav" | |
| static void print_usage(const char *progname) { | |
| fprintf(stderr, "Usage: %s [-x libonnxruntime_so] [-d open_jtalk_dic_dir] [-v vvm_model] [-i speaker_id] [-o output_wav] TEXT\n", progname); | |
| fprintf(stderr, " -x : ONNX Runtime .so ファイル (default: %s)\n", DEFAULT_ONNXRUNTIME_PATH); | |
| fprintf(stderr, " -d : OpenJTalk辞書ディレクトリ (default: %s)\n", DEFAULT_OPENJTALK_DIC_DIR); | |
| fprintf(stderr, " -v : VVMモデルファイル (.vvm) (default: %s)\n", DEFAULT_VVM_MODEL_PATH); | |
| fprintf(stderr, " -i : 話者ID (default: %d)\n", DEFAULT_SPEAKER_ID); | |
| fprintf(stderr, " -o : 出力WAVファイル (default: %s)\n", DEFAULT_OUTPUT_FILE); | |
| fprintf(stderr, " TEXT: 合成するテキスト\n"); | |
| } | |
| int main(int argc, char *argv[]) { | |
| const char *onnx_path = DEFAULT_ONNXRUNTIME_PATH; | |
| const char *dic_dir = DEFAULT_OPENJTALK_DIC_DIR; | |
| const char *vvm_path = DEFAULT_VVM_MODEL_PATH; | |
| VoicevoxStyleId speaker_id = DEFAULT_SPEAKER_ID; | |
| const char *output_file = DEFAULT_OUTPUT_FILE; | |
| char *text = NULL; | |
| int opt; | |
| while ((opt = getopt(argc, argv, "x:d:v:i:o:h")) != -1) { | |
| switch (opt) { | |
| case 'x': | |
| onnx_path = optarg; | |
| break; | |
| case 'd': | |
| dic_dir = optarg; | |
| break; | |
| case 'v': | |
| vvm_path = optarg; | |
| break; | |
| case 'i': | |
| speaker_id = (VoicevoxStyleId)atoi(optarg); | |
| break; | |
| case 'o': | |
| output_file = optarg; | |
| break; | |
| case 'h': | |
| print_usage(argv[0]); | |
| return 0; | |
| default: | |
| print_usage(argv[0]); | |
| return 1; | |
| } | |
| } | |
| if (optind >= argc) { | |
| fprintf(stderr, "Error: TEXT が指定されていません。\n"); | |
| print_usage(argv[0]); | |
| return 1; | |
| } | |
| text = argv[optind]; | |
| VoicevoxResultCode result = VOICEVOX_RESULT_OK; | |
| const VoicevoxOnnxruntime *onnxruntime = NULL; | |
| OpenJtalkRc *open_jtalk = NULL; | |
| VoicevoxSynthesizer *synthesizer = NULL; | |
| VoicevoxVoiceModelFile *model = NULL; | |
| uint8_t *wav = NULL; | |
| size_t wav_size = 0; | |
| /* ONNX Runtime ロード */ | |
| VoicevoxLoadOnnxruntimeOptions ort_opts = voicevox_make_default_load_onnxruntime_options(); | |
| ort_opts.filename = onnx_path; | |
| result = voicevox_onnxruntime_load_once(ort_opts, &onnxruntime); | |
| if (result != VOICEVOX_RESULT_OK) { | |
| fprintf(stderr, "ONNX Runtime ロード失敗: %s\n", voicevox_error_result_to_message(result)); | |
| goto cleanup; | |
| } | |
| /* OpenJTalk */ | |
| result = voicevox_open_jtalk_rc_new(dic_dir, &open_jtalk); | |
| if (result != VOICEVOX_RESULT_OK) { | |
| fprintf(stderr, "OpenJTalk 初期化失敗: %s\n", voicevox_error_result_to_message(result)); | |
| goto cleanup; | |
| } | |
| /* Synthesizer */ | |
| VoicevoxInitializeOptions init_opts = voicevox_make_default_initialize_options(); | |
| result = voicevox_synthesizer_new(onnxruntime, open_jtalk, init_opts, &synthesizer); | |
| if (result != VOICEVOX_RESULT_OK) { | |
| fprintf(stderr, "Synthesizer 作成失敗: %s\n", voicevox_error_result_to_message(result)); | |
| goto cleanup; | |
| } | |
| /* VVMモデルロード */ | |
| result = voicevox_voice_model_file_open(vvm_path, &model); | |
| if (result != VOICEVOX_RESULT_OK) { | |
| fprintf(stderr, "VVMモデルロード失敗 (%s): %s\n", vvm_path, voicevox_error_result_to_message(result)); | |
| goto cleanup; | |
| } | |
| /* VVMモデル適用 */ | |
| result = voicevox_synthesizer_load_voice_model(synthesizer, model); | |
| if (result != VOICEVOX_RESULT_OK) { | |
| fprintf(stderr, "モデル適用失敗: %s\n", voicevox_error_result_to_message(result)); | |
| goto cleanup; | |
| } | |
| /* 音声合成 */ | |
| VoicevoxTtsOptions tts_opts = voicevox_make_default_tts_options(); | |
| result = voicevox_synthesizer_tts(synthesizer, text, speaker_id, tts_opts, &wav_size, &wav); | |
| if (result != VOICEVOX_RESULT_OK) { | |
| fprintf(stderr, "音声合成失敗: %s\n", voicevox_error_result_to_message(result)); | |
| goto cleanup; | |
| } | |
| /* WAVファイル出力 */ | |
| FILE *fp = fopen(output_file, "wb"); | |
| if (fp == NULL) { | |
| fprintf(stderr, "出力ファイルを開けません: %s\n", output_file); | |
| goto cleanup; | |
| } | |
| if (fwrite(wav, 1, wav_size, fp) != wav_size) { | |
| fprintf(stderr, "ファイル書き込み中にエラーが発生しました: %s\n", output_file); | |
| fclose(fp); | |
| goto cleanup; | |
| } | |
| fclose(fp); | |
| printf("音声合成完了: %s (%zu bytes)\n", output_file, wav_size); | |
| cleanup: | |
| if (wav) voicevox_wav_free(wav); | |
| if (model) voicevox_voice_model_file_delete(model); | |
| if (synthesizer) voicevox_synthesizer_delete(synthesizer); | |
| if (open_jtalk) voicevox_open_jtalk_rc_delete(open_jtalk); | |
| return (result == VOICEVOX_RESULT_OK) ? 0 : 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| CLI より VOICEVOX CORE の Python API 経由でテキスト音声合成を行い、 | |
| 音声ファイルを出力する。簡単なテスト目的のプログラムである。 | |
| なお、本プログラムは C API 用のプログラムの移植版である。 | |
| Copyright (C) 2026 z80oolong (zool@zool.jpn.org) | |
| Permission is hereby granted, free of charge, to any person obtaining | |
| a copy of this software and associated documentation files (the | |
| "Software"), to deal in the Software without restriction, including | |
| without limitation the rights to use, copy, modify, merge, publish, | |
| distribute, sublicense, and/or sell copies of the Software, and to | |
| permit persons to whom the Software is furnished to do so, subject to | |
| the following conditions: | |
| The above copyright notice and this permission notice shall be | |
| included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| """ | |
| import argparse | |
| import sys | |
| from pathlib import Path | |
| from voicevox_core.blocking import ( | |
| Onnxruntime, | |
| OpenJtalk, | |
| Synthesizer, | |
| VoiceModelFile, | |
| ) | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description="VOICEVOX CORE Python API でテキストを音声合成してWAV出力" | |
| ) | |
| parser.add_argument("text", nargs="?", help="合成するテキスト") | |
| parser.add_argument("-x", "--onnxruntime", default=None, | |
| help="ONNX Runtime .so ファイルのパス") | |
| parser.add_argument("-d", "--dict-dir", default=None, | |
| help="OpenJTalk辞書ディレクトリ") | |
| parser.add_argument("-v", "--vvm", default=None, | |
| help="VVMモデルファイル (.vvm)") | |
| parser.add_argument("-i", "--speaker-id", type=int, default=0, | |
| help="話者ID (style_id)") | |
| parser.add_argument("-o", "--output", default="audio.wav", | |
| help="出力WAVファイルパス") | |
| args = parser.parse_args() | |
| if not args.text: | |
| print("Error: TEXT が指定されていません。", file=sys.stderr) | |
| parser.print_usage() | |
| sys.exit(1) | |
| # パス設定(Homebrew prefixに合わせる) | |
| HOMEBREW_PREFIX = Path("/home/linuxbrew/.linuxbrew") | |
| VOICEVOX_CORE_PREFIX = HOMEBREW_PREFIX / "opt/voicevox-core" | |
| onnx_path = args.onnxruntime or str(VOICEVOX_CORE_PREFIX / "lib/voicevox/libonnxruntime.so") | |
| dict_dir = args.dict_dir or str(VOICEVOX_CORE_PREFIX / "share/voicevox/dic") | |
| vvm_path = args.vvm or str(VOICEVOX_CORE_PREFIX / "share/voicevox/models/sample.vvm") | |
| output_path = Path(args.output) | |
| print(f"ONNX Runtime: {onnx_path}") | |
| print(f"OpenJTalk辞書: {dict_dir}") | |
| print(f"VVMモデル: {vvm_path}") | |
| print(f"話者ID: {args.speaker_id}") | |
| print(f"出力ファイル: {output_path}") | |
| print(f"テキスト: {args.text}") | |
| try: | |
| # ONNX Runtime ロード | |
| ort = Onnxruntime.load_once(filename=onnx_path) | |
| # OpenJTalk | |
| open_jtalk = OpenJtalk(dict_dir) | |
| # Synthesizer作成 | |
| synthesizer = Synthesizer(ort, open_jtalk) | |
| # VVMモデルロード & 適用 | |
| with VoiceModelFile.open(vvm_path) as model: | |
| synthesizer.load_voice_model(model) | |
| # 音声合成 (TTS) | |
| wav_bytes = synthesizer.tts( | |
| text=args.text, | |
| style_id=args.speaker_id, | |
| # tts_options=... で各種オプションも指定可能 | |
| ) | |
| # WAV保存 | |
| output_path.write_bytes(wav_bytes) | |
| print(f"[OK] 音声合成完了: {output_path} ({len(wav_bytes):,} bytes)") | |
| except Exception as e: | |
| print(f"[ERR] エラー: {e}", file=sys.stderr) | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| prefix=@prefix@ | |
| exec_prefix=${prefix} | |
| libdir=${prefix}/lib | |
| includedir=${prefix}/include | |
| Name: voicevox_core | |
| Description: VOICEVOX CORE Library | |
| Version: 0.16.4 | |
| URL: https://voicevox.hiroshiba.jp/ | |
| Libs: -L${libdir} -lvoicevox_core -ldl | |
| Cflags: -I${includedir} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment