Class: Voicevox
- Inherits:
-
Object
- Object
- Voicevox
- Defined in:
- lib/voicevox.rb,
lib/voicevox/core.rb,
lib/voicevox/error.rb,
lib/voicevox/version.rb,
lib/voicevox/wrapper/info.rb,
lib/voicevox/wrapper/utils.rb,
lib/voicevox/wrapper/manager.rb,
lib/voicevox/wrapper/audio_query.rb
Overview
voicevox_coreのラッパー。
Defined Under Namespace
Modules: Core Classes: AccentPhrase, AudioQuery, CharacterInfo, CoreError, Error, StyleInfo, SupportedDevices
Constant Summary collapse
- VERSION =
Returns voicevox.rbのバージョン。.
"0.1.0"
Class Attribute Summary collapse
-
.initialized ⇒ Object
(also: initialized?)
Returns the value of attribute initialized.
Instance Attribute Summary collapse
-
#acceleration_mode ⇒ :cpu, :gpu
readonly
ハードウェアアクセラレーションモード。.
-
#cpu_num_threads ⇒ Integer
readonly
スレッド数。.
-
#load_all_models ⇒ Boolean
readonly
起動時に全てのモデルを読み込むかどうか。.
Class Method Summary collapse
-
.characters ⇒ Array<CharacterInfo>
キャラクターの一覧を取得します。.
-
.core_version ⇒ String
コアのバージョンを取得します。.
-
.gpu_supported? ⇒ Boolean
GPUをサポートしているかを返します。.
-
.initialize_required ⇒ Object
Voicevoxが初期化されていなかったらエラーを出す。.
-
.process_result(result) ⇒ Object
voicevox_result_codeに対応するエラーをraiseします。.
-
.supported_devices ⇒ Voicevox::SupportedDevices
サポートしているデバイスを取得します。.
-
.voicevox_path ⇒ String?
製品版Voicevoxのパスを返します。.
Instance Method Summary collapse
-
#audio_query(text, speaker, kana: false) ⇒ Voicevox::AudioQuery
テキストからAudioQueryを生成します。.
-
#cpu? ⇒ Boolean
CPUモードで動作しているかどうか。.
-
#finalize ⇒ Object
Voicevoxのコアをファイナライズします。.
-
#gpu? ⇒ Boolean
GPUモードで動作しているかどうか。.
-
#initialize(openjtalk_dict_path, acceleration_mode: :auto, cpu_num_threads: nil, load_all_models: false) ⇒ Voicevox
constructor
Voicevoxのコアを初期化します。.
-
#load_model(speaker) ⇒ Object
話者のモデルを読み込みます。.
-
#model_loaded?(speaker) ⇒ Boolean
モデルが読み込まれているかどうかを返します。.
-
#synthesis(query, speaker, enable_interrogative_upspeak: true) ⇒ String
AudioQueryから音声を生成します。.
-
#tts(text, speaker, kana: false, enable_interrogative_upspeak: true) ⇒ String
voicevox_ttsを使って音声を生成します。.
Constructor Details
#initialize(openjtalk_dict_path, acceleration_mode: :auto, cpu_num_threads: nil, load_all_models: false) ⇒ Voicevox
Voicevoxのコアを初期化します。
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/voicevox/wrapper/manager.rb', line 41 def initialize( openjtalk_dict_path, acceleration_mode: :auto, cpu_num_threads: nil, load_all_models: false ) acceleration_mode_enum = { auto: :voicevox_acceleration_mode_auto, gpu: :voicevox_acceleration_mode_gpu, cpu: :voicevox_acceleration_mode_cpu }.fetch(acceleration_mode) do raise ArgumentError, "無効なacceleration_mode: #{acceleration_mode}" end @cpu_num_threads = cpu_num_threads || 0 @load_all_models = load_all_models @openjtalk_dict_path = openjtalk_dict_path = Voicevox::Core. [:acceleration_mode] = acceleration_mode_enum [:cpu_num_threads] = @cpu_num_threads [:load_all_models] = @load_all_models [:openjtalk_dict_path] = FFI::MemoryPointer.from_string( openjtalk_dict_path ) Voicevox.process_result Voicevox::Core.voicevox_initialize() @acceleration_mode = Voicevox::Core.voicevox_is_gpu_mode ? :gpu : :cpu at_exit { Voicevox::Core.voicevox_finalize } unless self.class.initialized self.class.initialized = true end |
Class Attribute Details
.initialized ⇒ Object Also known as: initialized?
Returns the value of attribute initialized.
133 134 135 |
# File 'lib/voicevox/wrapper/manager.rb', line 133 def initialized @initialized end |
Instance Attribute Details
#acceleration_mode ⇒ :cpu, :gpu (readonly)
Returns ハードウェアアクセラレーションモード。.
9 10 11 |
# File 'lib/voicevox/wrapper/manager.rb', line 9 def acceleration_mode @acceleration_mode end |
#cpu_num_threads ⇒ Integer (readonly)
Returns スレッド数。.
11 12 13 |
# File 'lib/voicevox/wrapper/manager.rb', line 11 def cpu_num_threads @cpu_num_threads end |
#load_all_models ⇒ Boolean (readonly)
Returns 起動時に全てのモデルを読み込むかどうか。.
13 14 15 |
# File 'lib/voicevox/wrapper/manager.rb', line 13 def load_all_models @load_all_models end |
Class Method Details
.characters ⇒ Array<CharacterInfo>
キャラクターの一覧を取得します。
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/voicevox/wrapper/info.rb', line 80 def characters JSON .parse(Voicevox::Core.) .map do || CharacterInfo.new( **{ **, "styles" => ["styles"].map { |style| StyleInfo.new(**style) } } ) end end |
.core_version ⇒ String
コアのバージョンを取得します。
109 110 111 |
# File 'lib/voicevox/wrapper/info.rb', line 109 def core_version Voicevox::Core.voicevox_get_version end |
.gpu_supported? ⇒ Boolean
CUDA、またはDirectMLが使える場合にtrueを返します。
GPUをサポートしているかを返します。
100 101 102 |
# File 'lib/voicevox/wrapper/info.rb', line 100 def gpu_supported? Voicevox.supported_devices.cuda || Voicevox.supported_devices.dml end |
.initialize_required ⇒ Object
Voicevoxが初期化されていなかったらエラーを出す。
10 11 12 |
# File 'lib/voicevox/wrapper/utils.rb', line 10 def initialize_required raise Voicevox::Error, "Voicevoxが初期化されていません" unless Voicevox.initialized? end |
.process_result(result) ⇒ Object
voicevox_result_codeに対応するエラーをraiseします。
19 20 21 22 23 24 |
# File 'lib/voicevox/wrapper/utils.rb', line 19 def process_result(result) return if result == :voicevox_result_succeed raise "#{result}はSymbolではありません" unless result.is_a?(Symbol) raise Voicevox::CoreError.from_code(result) end |
.supported_devices ⇒ Voicevox::SupportedDevices
サポートしているデバイスを取得します。
69 70 71 72 73 |
# File 'lib/voicevox/wrapper/info.rb', line 69 def supported_devices SupportedDevices.new( **JSON.parse(Voicevox::Core.voicevox_get_supported_devices_json) ) end |
.voicevox_path ⇒ String?
製品版Voicevoxのパスを返します。
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/voicevox/wrapper/utils.rb', line 32 def voicevox_path paths = if Gem.win_platform? [File.join(ENV.fetch("LOCALAPPDATA", ""), "Programs", "VOICEVOX")] else [ "/Applications/VOICEVOX", "/Users/#{Etc.getlogin}/Library/Application Support/VOICEVOX" ] end paths.find { |path| Dir.exist?(path) } end |
Instance Method Details
#audio_query(text, speaker, kana: false) ⇒ Voicevox::AudioQuery
テキストからAudioQueryを生成します。
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/voicevox/wrapper/audio_query.rb', line 16 def audio_query(text, speaker, kana: false) = Voicevox::Core. [:kana] = kana speaker_id = speaker.is_a?(Integer) ? speaker : speaker.id load_model speaker_id return_ptr = FFI::MemoryPointer.new(:pointer) Voicevox.process_result Voicevox::Core.voicevox_audio_query( text, speaker_id, , return_ptr ) return_str_ptr = return_ptr.read_pointer json = return_str_ptr.read_string Voicevox::Core.voicevox_audio_query_json_free return_str_ptr AudioQuery.new JSON.parse(json, symbolize_names: true) end |
#cpu? ⇒ Boolean
CPUモードで動作しているかどうか。
29 30 31 |
# File 'lib/voicevox/wrapper/manager.rb', line 29 def cpu? @acceleration_mode == :cpu end |
#finalize ⇒ Object
Voicevoxのコアをファイナライズします。
75 76 77 78 |
# File 'lib/voicevox/wrapper/manager.rb', line 75 def finalize Voicevox::Core.voicevox_finalize self.class.initialized = false end |
#gpu? ⇒ Boolean
GPUモードで動作しているかどうか。
20 21 22 |
# File 'lib/voicevox/wrapper/manager.rb', line 20 def gpu? @acceleration_mode == :gpu end |
#load_model(speaker) ⇒ Object
話者のモデルを読み込みます。
85 86 87 88 89 |
# File 'lib/voicevox/wrapper/manager.rb', line 85 def load_model(speaker) id = speaker.is_a?(Integer) ? speaker : speaker.id Voicevox.process_result Voicevox::Core.voicevox_load_model(id) end |
#model_loaded?(speaker) ⇒ Boolean
モデルが読み込まれているかどうかを返します。
98 99 100 101 102 |
# File 'lib/voicevox/wrapper/manager.rb', line 98 def model_loaded?(speaker) id = speaker.is_a?(Integer) ? speaker : speaker.id Voicevox::Core.voicevox_is_model_loaded(id) end |
#synthesis(query, speaker, enable_interrogative_upspeak: true) ⇒ String
AudioQueryから音声を生成します。
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/voicevox/wrapper/audio_query.rb', line 44 def synthesis(query, speaker, enable_interrogative_upspeak: true) size_ptr = FFI::MemoryPointer.new(:int) return_ptr = FFI::MemoryPointer.new(:pointer) id = speaker.is_a?(Integer) ? speaker : speaker.id load_model id = Voicevox::Core::VoicevoxSynthesisOptions.new [:enable_interrogative_upspeak] = enable_interrogative_upspeak Voicevox.process_result( Voicevox::Core.voicevox_synthesis( query.to_json, id, , size_ptr, return_ptr ) ) data_ptr = return_ptr.read_pointer size_ptr.free data = data_ptr.read_string(size_ptr.read_int) Voicevox::Core.voicevox_wav_free(data_ptr) data end |
#tts(text, speaker, kana: false, enable_interrogative_upspeak: true) ⇒ String
voicevox_ttsを使って音声を生成します。
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/voicevox/wrapper/manager.rb', line 114 def tts(text, speaker, kana: false, enable_interrogative_upspeak: true) size_ptr = FFI::MemoryPointer.new(:int) return_ptr = FFI::MemoryPointer.new(:pointer) id = speaker.is_a?(Integer) ? speaker : speaker.id load_model id = Voicevox::Core. [:kana] = kana [:enable_interrogative_upspeak] = enable_interrogative_upspeak Voicevox.process_result( Voicevox::Core.voicevox_tts(text, id, , size_ptr, return_ptr) ) data_ptr = return_ptr.read_pointer data = data_ptr.read_string(size_ptr.read_int) size_ptr.free Voicevox::Core.voicevox_wav_free(data_ptr) data end |