voicevoxcorego

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2023 License: MIT Imports: 5 Imported by: 0

README

voicevoxcore.go

codecov golangci-lint

voicevoxcore.go はvoicevox_coreを Go 言語で使えるラッパーです。 FFI を用いて、voicevox_core の C API を呼んでいます。

以下は本ライブラリを使用して Text to Speech を行う例です。


//go:build ignore

package main

import (
	"fmt"
	"os"

	voicevoxcorego "github.com/sh1ma/voicevoxcore.go"
)

func main() {
	args := os.Args
	if len(args) < 2 {
		fmt.Println("usage:\n\tgo run tts.go [ text ]")
		os.Exit(127)
	}
	text := os.Args[1]

	core := voicevoxcorego.NewVoicevoxCore()
	initializeOptions := voicevoxcorego.NewVoicevoxInitializeOptions(0, 0, false, "./open_jtalk_dic_utf_8-1.11")
	core.Initialize(initializeOptions)

	core.LoadModel(1)

	ttsOptions := voicevoxcorego.NewVoicevoxTtsOptions(false, true)
	result, err := core.Tts(text, 1, ttsOptions)
	if err != nil {
		fmt.Println(err)
	}
	f, _ := os.Create("out.wav")
	_, err = f.Write(result)
	if err != nil {
		fmt.Println(err)
	}
}

おすすめの環境構築方法 (Linux / MacOS)

本ライブラリを使用するには openJTalk の辞書ファイルと voicevox_core の動的ライブラリ、ヘッダファイル、そしてモデルファイルが必要になります。 以下でそれらをダウンロードし、本ライブラリから使えるようにするための簡単なセットアップの手順を説明します。

1. voicevox_core のダウンロード

voicevox_core の releasesから自分の OS、アーキテクチャに合ったダウンローダをダウンロードし、実行してください。実行するとカレントディレクトリにvoicevox_core_*のディレクトリが配置されます。直下には以下のものが入っています

  • openJTalk の辞書ファイルが入ったopen_jtalk_dic_*/ディレクトリ
  • voicevox_core の動的ライブラリ
    • 拡張子は.dll, .dylib, .so のいずれかです
  • 圧縮されたモデルファイルの入ったmodel/ディレクトリ
2. voicevox_core を配置する

voicevox_coreを任意のパスに移動(おすすめは~/.localのなか)します

3. シンボリックリンクを張る

voicevox_core内にある 2 つのファイルのシンボリックリンクを作ります。 以下のようなコマンドを実行します。(プラットフォームによってコマンドが異なる場合があります)。動的ライブラリが.dylibの場合を例に挙げます。

注意: ln に渡すパスは相対パスではなく絶対パスにしてください

# [VOICEVOX_CORE_DIR] を`voicevox_core`の絶対パスにします

# 動的ライブラリのシンボリックリンクを`/usr/local/lib`に配置します
ln -s [VOICEVOX_CORE_DIR]/libvoicevox_core.dylib /usr/local/lib

# 動的ライブラリのシンボリックリンクを`/usr/local/include`に配置します
ln -s [VOICEVOX_CORE_DIR]/voicevox_core.h /usr/local/include

以上の手順で本ライブラリが使えるようになっているはずです。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccentPharase added in v0.0.2

type AccentPharase struct {
	Moras           []Mora `json:"moras"`
	Accent          uint32 `json:"accent"`
	PauseMora       Mora   `json:"pause_mora,omitempty"`
	IsInterrogative bool   `json:"is_interrogative"`
}

アクセント句を表す構造体

`moras`にはモーラの配列を格納する。

`accent`はアクセント位置を表す。

`pause_mora`はポーズのモーラを表す。

`is_interrogative`は疑問文かどうかを表す。

type AudioQuery added in v0.0.2

type AudioQuery struct {
	AccentPharases     []AccentPharase `json:"accent_phrases"`
	SpeedScale         float32         `json:"speed_scale"`
	PitchScale         float32         `json:"pitch_scale"`
	IntonationScale    float32         `json:"intonation_scale"`
	VolumeScale        float32         `json:"volume_scale"`
	PrePhonemeLength   float32         `json:"pre_phoneme_length"`
	PostPhonemeLength  float32         `json:"post_phoneme_length"`
	OutputSamplingRate float32         `json:"output_sampling_rate"`
	OutputStereo       bool            `json:"output_stereo"`
	Kana               string          `json:"kana"`
}

オーディオクエリを表す構造体

`accent_phrases`にはアクセント句の配列を格納する。

`spped_scale`は発話速度の倍率を表す。

`pitch_scale`は音高の倍率を表す。

`intonation_scale`はイントネーションの倍率を表す。

`volume_scale`は音量の倍率を表す。

`pre_phoneme_length`は発声開始前の無音の長さを表す。

`post_phoneme_length`は発声終了後の無音の長さを表す。

`output_sampling_rate`は出力音声のサンプリングレートを表す。

`output_stereo`は出力音声がステレオかどうかを表す。

`kana`は読み仮名を表す。

func NewAudioQueryFromJson added in v0.0.3

func NewAudioQueryFromJson(queryJson []byte) (*AudioQuery, error)

Jsonのバイト列からAudioQuery構造体を生成する

func (*AudioQuery) ToJson added in v0.0.3

func (q *AudioQuery) ToJson() ([]byte, error)

AudioQuery構造体をJsonのバイト列に変換する

func (*AudioQuery) ToJsonString added in v0.0.3

func (q *AudioQuery) ToJsonString() (string, error)

AudioQuery構造体をJson文字列に変換する

type Mora added in v0.0.2

type Mora struct {
	Text            string  `json:"text"`
	Consonant       string  `json:"consonant,omitempty"`
	ConsonantLength float32 `json:"consonant_length,omitempty"`
	Vowel           string  `json:"vowel"`
	VowelLength     float32 `json:"vowel_length"`
	Pitch           float32 `json:"pitch"`
}

モーラを表す構造体

`text`はモーラの文字列を表す。

`consonant`は子音を表す。

`consonant_length`は子音の長さを表す。

`vowel`は母音を表す。

`vowel_length`は母音の長さを表す。

`pitch`は音高を表す。

type RawVoicevoxCore

type RawVoicevoxCore struct{}

RawVoicevoxCore is a function group that wraps the C API

type VoicevoxAudioQueryOptions

type VoicevoxAudioQueryOptions struct {
	// contains filtered or unexported fields
}

`AudioQuery()`を実行する際のオプションを表す構造体

func NewVoicevoxAudioQueryOptions

func NewVoicevoxAudioQueryOptions(kana bool) *VoicevoxAudioQueryOptions

`AudioQuery()`の初期化オプションを生成する関数

func (*VoicevoxAudioQueryOptions) UpdateKana added in v0.0.4

func (o *VoicevoxAudioQueryOptions) UpdateKana(kana bool)

`AudioQuery()`のオプションの`kana`をアップデートする関数

type VoicevoxCore

type VoicevoxCore struct {
	*RawVoicevoxCore
	// contains filtered or unexported fields
}

VoicevoxCore is top-level API Wrapper instance

func New added in v0.0.5

func New() (core *VoicevoxCore)

VoicevoxCore のコンストラクタ関数

func (*VoicevoxCore) AudioQuery

func (r *VoicevoxCore) AudioQuery(text string, speakerID uint, options *VoicevoxAudioQueryOptions) (*AudioQuery, error)

オーディオクエリを発行する

func (*VoicevoxCore) Decode

func (r *VoicevoxCore) Decode(speakerID uint, phonemeSize int, f0 []float32, phonemeVector []float32) ([]float32, error)

phnemeVectorを元にデコードする

func (*VoicevoxCore) ErrorResultToMessage

func (r *VoicevoxCore) ErrorResultToMessage(resultCode int) string

ErrorResultCode をメッセージに変換する

func (*VoicevoxCore) Finalize

func (r *VoicevoxCore) Finalize()

ファイナライズ

func (*VoicevoxCore) GetCoreVersion

func (r *VoicevoxCore) GetCoreVersion() string

Coreのバージョンを取得する

func (*VoicevoxCore) GetMetasJson

func (r *VoicevoxCore) GetMetasJson() string

メタ情報のjsonを取得する

func (*VoicevoxCore) GetSupportedDevicesJson

func (r *VoicevoxCore) GetSupportedDevicesJson() string

サポートしているデバイス一覧のjsonを取得する

func (*VoicevoxCore) Initialize

func (r *VoicevoxCore) Initialize(options *VoicevoxInitializeOptions) error

C APIを通じてVoicevox_coreを初期化する関数

func (*VoicevoxCore) IsGpuMode

func (r *VoicevoxCore) IsGpuMode() bool

Gpuモードが有効になっているか確認する

func (*VoicevoxCore) IsModelLoaded

func (r *VoicevoxCore) IsModelLoaded(speakerID uint) bool

モデルがロードされているか確認する

func (*VoicevoxCore) LoadModel

func (r *VoicevoxCore) LoadModel(speakerID uint) error

音声合成モデルをロードする関数

func (*VoicevoxCore) MakeDefaultAudioQueryOotions

func (r *VoicevoxCore) MakeDefaultAudioQueryOotions() *VoicevoxAudioQueryOptions

`AudioQuery()` のデフォルトオプションを生成する

func (*VoicevoxCore) MakeDefaultInitializeOptions

func (r *VoicevoxCore) MakeDefaultInitializeOptions() *VoicevoxInitializeOptions

`Initialize()` のデフォルトオプションを生成する

func (*VoicevoxCore) MakeDefaultSynthesisOotions

func (r *VoicevoxCore) MakeDefaultSynthesisOotions() *VoicevoxSynthesisOptions

`Synthesis()` のデフォルトオプションを生成する

func (*VoicevoxCore) MakeDefaultTtsOotions

func (r *VoicevoxCore) MakeDefaultTtsOotions() *VoicevoxTtsOptions

`Tts()` のデフォルトオプションを生成する

func (*VoicevoxCore) PredictDuration

func (r *VoicevoxCore) PredictDuration(speakerID int, phonemeVector []int64) ([]float32, error)

音素長を取得

func (*VoicevoxCore) PredictIntonation

func (r *VoicevoxCore) PredictIntonation(
	speakerID int,
	vowelPhonemeVector, consonantPhonemeVector []int64,
	startAccentVector, endAccentVector []int64,
	startAccentPhraseVector, endAccentPhraseVector []int64,
) ([]float32, error)

モーラごとのF0を推論する

func (*VoicevoxCore) Synthesis

func (r *VoicevoxCore) Synthesis(
	audioQuery *AudioQuery,
	speakerID int,
	options *VoicevoxSynthesisOptions,
) ([]byte, error)

Audio Queryを基に音声合成を実行する関数。実行結果はwavファイルフォーマットのバイト列。

Sample: https://github.com/sh1ma/sample-synthesis

func (*VoicevoxCore) Tts

func (r *VoicevoxCore) Tts(text string, speakerID int, options *VoicevoxTtsOptions) ([]byte, error)

Text to Speechを実行する関数。実行結果はwavファイルフォーマットのバイト列。

Sample: https://github.com/sh1ma/sample-tts

type VoicevoxInitializeOptions

type VoicevoxInitializeOptions struct {
	// contains filtered or unexported fields
}

`VoicevoxCore`を初期化する際のオプションを表す構造体

func NewVoicevoxInitializeOptions

func NewVoicevoxInitializeOptions(accelerationMode int, cpuNumThreads int, loadAllModels bool, openJtalkDictDir string) *VoicevoxInitializeOptions

`VoiceVoxCore`の初期化オプションを生成する関数

func (*VoicevoxInitializeOptions) UpdateAccelerationMode added in v0.0.4

func (o *VoicevoxInitializeOptions) UpdateAccelerationMode(accelerationMode int)

初期化オプションの`accelerationMode`をアップデートする関数

func (*VoicevoxInitializeOptions) UpdateCpuNumThreads added in v0.0.4

func (o *VoicevoxInitializeOptions) UpdateCpuNumThreads(cpuNumThreads int)

初期化オプションの`cpuNumThreads`をアップデートする関数

func (*VoicevoxInitializeOptions) UpdateLoadAllModels added in v0.0.4

func (o *VoicevoxInitializeOptions) UpdateLoadAllModels(loadAllModels bool)

初期化オプションの`loadAllModels`をアップデートする関数

func (*VoicevoxInitializeOptions) UpdateOpenJtalkDictDir added in v0.0.4

func (o *VoicevoxInitializeOptions) UpdateOpenJtalkDictDir(openJtalkDictDir string)

初期化オプションの`openJtalkDictDir`をアップデートする関数

type VoicevoxSynthesisOptions

type VoicevoxSynthesisOptions struct {
	// contains filtered or unexported fields
}

`Synthesis()`を実行する際のオプションを表す構造体

func NewVoicevoxSynthesisOptions

func NewVoicevoxSynthesisOptions(enableInterrogativeUpspeak bool) *VoicevoxSynthesisOptions

`Synthesis()`の初期化オプションを生成する関数

func (*VoicevoxSynthesisOptions) UpdateInterrogativeUpspeak added in v0.0.4

func (o *VoicevoxSynthesisOptions) UpdateInterrogativeUpspeak(kana bool)

`AudioQuery()`のオプションの`kana`をアップデートする関数

type VoicevoxTtsOptions

type VoicevoxTtsOptions struct {
	// contains filtered or unexported fields
}

`Tts()`を実行する際のオプションを表す構造体

func NewVoicevoxTtsOptions

func NewVoicevoxTtsOptions(kana bool, enableInterrogativeUpspeak bool) *VoicevoxTtsOptions

`Tts()`の初期化オプションを生成する関数

func (*VoicevoxTtsOptions) UpdateInterrogativeUpspeak added in v0.0.4

func (o *VoicevoxTtsOptions) UpdateInterrogativeUpspeak(kana bool)

`Tts()`のオプションの`kana`をアップデートする関数

func (*VoicevoxTtsOptions) UpdateKana added in v0.0.4

func (o *VoicevoxTtsOptions) UpdateKana(kana bool)

`Tts()`のオプションの`kana`をアップデートする関数

Directories

Path Synopsis
examples
scripts

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL