Class: Voicevox

Inherits:
Object
  • Object
show all
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のバージョン。.

Returns:

  • (String)

    voicevox.rbのバージョン。

"0.1.0"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(openjtalk_dict_path, acceleration_mode: :auto, cpu_num_threads: nil, load_all_models: false) ⇒ Voicevox

Voicevoxのコアを初期化します。

Parameters:

  • openjtalk_dict_path (String)

    OpenJTalkの辞書へのパス。

  • acceleration_mode (:cpu, :gpu, :auto) (defaults to: :auto)

    ハードウェアアクセラレーションモード。:autoを指定するとコア側で自動的に決定されます。

  • cpu_num_threads (Integer) (defaults to: nil)

    スレッド数。省略する、または0を渡すとコア側で自動的に決定されます。

  • load_all_models (Boolean) (defaults to: false)

    全てのモデルを読み込むかどうか。省略するとfalseになります。



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
  options = Voicevox::Core.voicevox_make_default_initialize_options
  options[:acceleration_mode] = acceleration_mode_enum
  options[:cpu_num_threads] = @cpu_num_threads
  options[:load_all_models] = @load_all_models
  options[:openjtalk_dict_path] = FFI::MemoryPointer.from_string(
    openjtalk_dict_path
  )

  Voicevox.process_result Voicevox::Core.voicevox_initialize(options)
  @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

.initializedObject 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 ハードウェアアクセラレーションモード。.

Returns:

  • (:cpu, :gpu)

    ハードウェアアクセラレーションモード。



9
10
11
# File 'lib/voicevox/wrapper/manager.rb', line 9

def acceleration_mode
  @acceleration_mode
end

#cpu_num_threadsInteger (readonly)

Returns スレッド数。.

Returns:

  • (Integer)

    スレッド数。



11
12
13
# File 'lib/voicevox/wrapper/manager.rb', line 11

def cpu_num_threads
  @cpu_num_threads
end

#load_all_modelsBoolean (readonly)

Returns 起動時に全てのモデルを読み込むかどうか。.

Returns:

  • (Boolean)

    起動時に全てのモデルを読み込むかどうか。



13
14
15
# File 'lib/voicevox/wrapper/manager.rb', line 13

def load_all_models
  @load_all_models
end

Class Method Details

.charactersArray<CharacterInfo>

キャラクターの一覧を取得します。

Returns:



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.voicevox_get_metas_json)
    .map do |meta|
      CharacterInfo.new(
        **{
          **meta,
          "styles" => meta["styles"].map { |style| StyleInfo.new(**style) }
        }
      )
    end
end

.core_versionString

コアのバージョンを取得します。

Returns:

  • (String)

    コアのバージョン。



109
110
111
# File 'lib/voicevox/wrapper/info.rb', line 109

def core_version
  Voicevox::Core.voicevox_get_version
end

.gpu_supported?Boolean

Note:

CUDA、またはDirectMLが使える場合にtrueを返します。

GPUをサポートしているかを返します。

Returns:

  • (Boolean)

    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_requiredObject

Voicevoxが初期化されていなかったらエラーを出す。

Raises:



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します。

Parameters:

  • result (Symbol)

    voicevox_result_code。



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_devicesVoicevox::SupportedDevices

サポートしているデバイスを取得します。

Returns:



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_pathString?

製品版Voicevoxのパスを返します。

Returns:

  • (String)

    Voicevoxへの絶対パス。

  • (nil)

    Voicevoxが見付からなかった場合。zip版やLinux版ではnilを返します。



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を生成します。

Parameters:

  • text (String)

    生成するAudioQueryのテキスト。

  • speaker (Voicevox::CharacterInfo, Voicevox::StyleInfo, Integer)

    話者、または話者のID。

  • kana (Boolean) (defaults to: false)

    textをAquesTalkライクな記法として解釈するかどうか。デフォルトはfalse。

Returns:

See Also:



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)
  options = Voicevox::Core.voicevox_make_default_audio_query_options
  options[: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,
                            options,
                            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モードで動作しているかどうか。

Returns:

  • (Boolean)

    CPUモードで動作している場合はtrue、そうでない場合はfalse。



29
30
31
# File 'lib/voicevox/wrapper/manager.rb', line 29

def cpu?
  @acceleration_mode == :cpu
end

#finalizeObject

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モードで動作しているかどうか。

Returns:

  • (Boolean)

    GPUモードで動作している場合はtrue、そうでない場合はfalse。



20
21
22
# File 'lib/voicevox/wrapper/manager.rb', line 20

def gpu?
  @acceleration_mode == :gpu
end

#load_model(speaker) ⇒ Object

話者のモデルを読み込みます。

Parameters:



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

モデルが読み込まれているかどうかを返します。

Parameters:

Returns:

  • (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から音声を生成します。

Parameters:

  • query (AudioQuery)

    AudioQuery。

  • speaker (Voicevox::CharacterInfo, Voicevox::StyleInfo, Integer)

    話者、または話者のID。

  • enable_interrogative_upspeak (Boolran) (defaults to: true)

    疑問文の調整を有効にするかどうか。デフォルトはtrue。

Returns:

  • (String)

    生成された音声のwavデータ。



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
  options = Voicevox::Core::VoicevoxSynthesisOptions.new
  options[:enable_interrogative_upspeak] = enable_interrogative_upspeak
  Voicevox.process_result(
    Voicevox::Core.voicevox_synthesis(
      query.to_json,
      id,
      options,
      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を使って音声を生成します。

Parameters:

  • text (String)

    生成する音声のテキスト。

  • speaker (Voicevox::CharacterInfo, Voicevox::StyleInfo, Integer)

    話者、または話者のID。

  • kana (Boolean) (defaults to: false)

    textをAquesTalkライクな記法として解釈するかどうか。デフォルトはfalse。

  • enable_interrogative_upspeak (Boolran) (defaults to: true)

    疑問文の調整を有効にするかどうか。デフォルトはtrue。

Returns:

  • (String)

    生成された音声のwavデータ。



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
  options = Voicevox::Core.voicevox_make_default_tts_options
  options[:kana] = kana
  options[:enable_interrogative_upspeak] = enable_interrogative_upspeak
  Voicevox.process_result(
    Voicevox::Core.voicevox_tts(text, id, options, 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