Skip to content

Commit

Permalink
Add: Java APIのサンプルを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi committed Nov 3, 2023
1 parent a6fb4ae commit 300dd30
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 0 deletions.
9 changes: 9 additions & 0 deletions example/java/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

6 changes: 6 additions & 0 deletions example/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
output.wav
59 changes: 59 additions & 0 deletions example/java/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.2.1/userguide/building_java_projects.html in the Gradle documentation.
*/

plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
id 'org.jetbrains.kotlin.jvm' version '1.8.20'

// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// Use the Kotlin JUnit 5 integration.
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'

// Use the JUnit 5 integration.
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.2'

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

implementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.6")

// TODO: ちゃんと配布したらそれに置き換える
implementation files("../../../crates/voicevox_core_java_api/lib/build/libs/lib-0.0.0.jar")
// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation group: 'com.google.code.gson', name: 'gson', version: "2.10.1"
// https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api
implementation group: 'jakarta.validation', name: 'jakarta.validation-api', version: "3.0.2"
// https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api
implementation group: 'jakarta.annotation', name: 'jakarta.annotation-api', version: "2.1.1"
implementation group: 'com.microsoft.onnxruntime', name: 'onnxruntime', version: "1.14.0"
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}

application {
// Define the main class for the application.
mainClass = 'jp.hiroshiba.voicevoxcoreexample.AppKt'
tasks.run.workingDir = rootProject.projectDir
}

tasks.named('test') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package jp.hiroshiba.voicevoxcoreexample

import java.io.File
import jp.hiroshiba.voicevoxcore.*
import kotlinx.cli.*

enum class Mode {
AUTO,
CPU,
GPU
}

fun main(args: Array<String>) {
val parser = ArgParser("example")
val mode by parser.option(ArgType.Choice<Mode>(), description = "モード").default(Mode.AUTO)
val vvmPath by
parser.option(ArgType.String, fullName = "vvm", description = "vvmファイルへのパス").required()
val dictDir by
parser
.option(ArgType.String, description = "Open JTalkの辞書ディレクトリ")
.default("./open_jtalk_dic_utf_8-1.11")
val text by
parser
.option(ArgType.String, description = "読み上げさせたい文章")
.default("この音声は、ボイスボックスを使用して、出力されています。")
val out by parser.option(ArgType.String, description = "出力wavファイルのパス").default("./output.wav")
val styleId by parser.option(ArgType.Int, description = "話者IDを指定").default(0)

parser.parse(args)

println("Inititalizing: ${mode}, ${dictDir}")
val openJtalk = OpenJtalk(dictDir)
val synthesizer =
Synthesizer.builder(openJtalk)
.accelerationMode(
when (mode) {
Mode.AUTO -> Synthesizer.AccelerationMode.AUTO
Mode.CPU -> Synthesizer.AccelerationMode.CPU
Mode.GPU -> Synthesizer.AccelerationMode.GPU
}
)
.build()

println("Loading: ${vvmPath}")
val vvm = VoiceModel(vvmPath)
synthesizer.loadVoiceModel(vvm)

println("Creating an AudioQuery from the text: ${text}")
val audioQuery = synthesizer.createAudioQuery(text, styleId)

println("Synthesizing... ${audioQuery}")
val audio = synthesizer.synthesis(audioQuery, styleId).execute()

println("Saving the audio to ${out}")
File(out).writeBytes(audio)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package jp.hiroshiba.voicevoxcoreexample

import kotlin.test.Test
import kotlin.test.assertNotNull

class AppTest {
@Test fun appHasAGreeting() {
val classUnderTest = App()
assertNotNull(classUnderTest.greeting, "app should have a greeting")
}
}
Binary file added example/java/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions example/java/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 300dd30

Please sign in to comment.