Skip to content

Commit

Permalink
Merge pull request #14 from umjammer/0.0.13
Browse files Browse the repository at this point in the history
0.0.13
  • Loading branch information
umjammer committed Mar 5, 2024
2 parents e88906e + 7c7e647 commit 1376acd
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ tmp/
lib/
local.properties
src/test/resources/nicotalk/
sudachi
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>vavi</groupId>
<artifactId>vavi-speech2</artifactId>
<version>0.0.12</version>
<version>0.0.13</version>

<name>vavi-speech2</name>
<description/>
Expand Down Expand Up @@ -219,7 +219,7 @@
<dependency>
<groupId>com.github.umjammer</groupId> <!-- vavi / com.github.umjammer -->
<artifactId>vavi-speech</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
<exclusions>
<exclusion>
<groupId>javax.speech</groupId>
Expand Down Expand Up @@ -269,9 +269,9 @@
</dependency>

<dependency>
<groupId>com.github.umjammer</groupId>
<artifactId>Gyutan</artifactId>
<version>0.0.2</version>
<groupId>com.github.umjammer</groupId> <!-- icn-lab / com.github.umjammer -->
<artifactId>Gyutan</artifactId> <!-- gyutan / Gyutan -->
<version>0.0.3</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
Expand All @@ -25,7 +24,6 @@
import org.jvoicexml.jsapi2.BaseAudioSegment;
import org.jvoicexml.jsapi2.BaseEngineProperties;
import org.jvoicexml.jsapi2.synthesis.BaseSynthesizer;

import vavi.beans.InstanciationBinder;
import vavi.speech.Phonemizer;
import vavi.speech.aquestalk10.jna.AquesTalk10.AQTK_VOICE;
Expand Down Expand Up @@ -142,7 +140,7 @@ public boolean handleResume() {
public AudioSegment handleSpeak(int id, String item) {
try {
aquesTalk10.setVoice(toNativeVoice(getSynthesizerProperties().getVoice()));
byte[] bytes = aquesTalk10.synthe(phonemizer.phoneme(item));
byte[] bytes = aquesTalk10.synthesize(phonemizer.phoneme(item));
AudioManager manager = getAudioManager();
String locator = manager.getMediaLocator();
// you should pass bytes to BaseAudioSegment as AudioInputStream or causes crackling!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public boolean handleResume() {
@Override
public AudioSegment handleSpeak(int id, String item) {
try {
byte[] bytes = synthesis(item);
byte[] bytes = synthesize(item);
AudioManager manager = getAudioManager();
String locator = manager.getMediaLocator();
// you should pass bytes to BaseAudioSegment as AudioInputStream or causes crackling!
Expand All @@ -157,7 +157,7 @@ public AudioSegment handleSpeak(int id, String item) {
}

/** */
private byte[] synthesis(String text) {
private byte[] synthesize(String text) {
SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();

VoiceSelectionParams voice = VoiceSelectionParams.newBuilder()
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/vavi/speech/gyutan/jsapi2/GyutanSynthesizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
Expand All @@ -31,6 +32,7 @@
import org.jvoicexml.jsapi2.BaseAudioSegment;
import org.jvoicexml.jsapi2.BaseEngineProperties;
import org.jvoicexml.jsapi2.synthesis.BaseSynthesizer;
import vavi.util.Debug;


/**
Expand All @@ -44,9 +46,6 @@ public final class GyutanSynthesizer extends BaseSynthesizer {
/** Logger for this class. */
private static final Logger logger = Logger.getLogger(GyutanSynthesizer.class.getName());

/** */
private final Gyutan gyutan = new Gyutan();

/**
* Constructs a new synthesizer object.
*
Expand Down Expand Up @@ -115,7 +114,7 @@ public void handleDeallocate() {
// Leave some time to let all resources detach
try {
Thread.sleep(500);
} catch (InterruptedException e) {
} catch (InterruptedException ignored) {
}

//
Expand Down Expand Up @@ -151,14 +150,11 @@ public AudioSegment handleSpeak(int id, String item) {
/** */
private AudioInputStream synthe(String text) {
try {
//System.err.println("vioce: " + getSynthesizerProperties().getVoice());
Debug.println(Level.FINER, "vioce: " + getSynthesizerProperties().getVoice());
Path wave = Files.createTempFile(getClass().getName(), ".wav");
Path voice = toNativeVoice(getSynthesizerProperties().getVoice());
boolean flag = gyutan.initialize(System.getProperty("sen.home"), voice.toString());
if (!flag) {
throw new IOException("initialize");
}
gyutan.synthesis(text, new FileOutputStream(wave.toFile()), null);
Gyutan gyutan = new Gyutan(System.getProperty("sen.home"), voice.toString());
gyutan.synthesize(text, new FileOutputStream(wave.toFile()), null);
byte[] wav = Files.readAllBytes(wave);
ByteArrayInputStream bais = new ByteArrayInputStream(wav);
// you should pass bytes to BaseAudioSegment as AudioInputStream or causes crackling!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public AudioSegment handleSpeak(int id, String item) {
AudioManager manager = getAudioManager();
String locator = manager.getMediaLocator();
// you should pass bytes to BaseAudioSegment as AudioInputStream or causes crackling!
InputStream in = synthesis(item);
InputStream in = synthesize(item);
AudioSegment segment;
if (locator == null) {
segment = new BaseAudioSegment(item, in);
Expand All @@ -141,7 +141,7 @@ public AudioSegment handleSpeak(int id, String item) {
}

/** */
private AudioInputStream synthesis(String text) {
private AudioInputStream synthesize(String text) {
try {
//logger.log(Level.DEBUG, "vioce: " + getSynthesizerProperties().getVoice());
openJTalk.setVoice(toNativeVoice(getSynthesizerProperties().getVoice()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public AudioSegment handleSpeak(int id, String item) {
if (false) {
AudioManager manager = getAudioManager();
String locator = manager.getMediaLocator();
InputStream in = synthesis(item);
InputStream in = synthesize(item);
AudioSegment segment;
if (locator == null) {
segment = new BaseAudioSegment(item, in);
Expand All @@ -184,7 +184,7 @@ public AudioSegment handleSpeak(int id, String item) {
}

/** */
private AudioInputStream synthesis(String text) {
private AudioInputStream synthesize(String text) {
try {
//Debug.println(Level.FINER, "voice: " + getSynthesizerProperties().getVoice());
AVSpeechUtterance utterance = AVSpeechUtterance.of(text);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vavi/speech/voicevox/VoiceVox.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public AudioQuery getQuery(String text, int speakerId) {
}

/** */
public InputStream synthesis(AudioQuery audioQuery, int speakerId) {
public InputStream synthesize(AudioQuery audioQuery, int speakerId) {
Entity<String> entity = Entity.entity(gson.toJson(audioQuery), MediaType.APPLICATION_JSON);
return target.path("synthesis")
.queryParam("speaker", speakerId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public AudioSegment handleSpeak(int id, String item) {
// audioQuery.setSpeed(props.getSpeakingRate() / 200f);
// audioQuery.setPitch(props.getPitch() / 300f);
// audioQuery.setIntonation(props.getPitchRange());
InputStream wave = client.synthesis(audioQuery, voiceId);
InputStream wave = client.synthesize(audioQuery, voiceId);
AudioManager manager = getAudioManager();
String locator = manager.getMediaLocator();
// you should pass bytes to BaseAudioSegment as AudioInputStream or causes crackling!
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/Jsapi2Test_aquestalk10.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
import javax.speech.synthesis.SynthesizerMode;
import javax.speech.synthesis.Voice;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import vavi.util.Debug;
import vavi.util.properties.annotation.Property;
import vavi.util.properties.annotation.PropsEntity;


/**
Expand All @@ -25,10 +30,25 @@
* @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (umjammer)
* @version 0.00 2019/09/21 umjammer initial version <br>
*/
@PropsEntity(url = "file:local.properties")
@EnabledIfSystemProperty(named = "os.arch", matches = "x86_64")
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
class Jsapi2Test_aquestalk10 {

static boolean localPropertiesExists() {
return Files.exists(Paths.get("local.properties"));
}

@Property(name = "text")
String text = "src/test/resources/test.txt";

@BeforeEach
void setup() throws Exception {
if (localPropertiesExists()) {
PropsEntity.Util.bind(this);
}
}

/**
* @param args 0: text
*/
Expand All @@ -47,8 +67,9 @@ void test01() throws Exception {
}

@Test
@EnabledIf("localPropertiesExists")
void test02() throws Exception {
Path path = Paths.get("tmp/repezen.txt");
Path path = Paths.get(text);
String text = String.join("\n", Files.readAllLines(path));
speak(text);
}
Expand All @@ -69,6 +90,7 @@ void speak(String text) throws Exception {
synthesizer.getSynthesizerProperties().setVoice(voice);
synthesizer.getSynthesizerProperties().setVolume(2);

Debug.println("split " + text.split("[。\n]").length);
for (String line : text.split("[。\n]")) {
System.out.println(line);
synthesizer.speak(line + "。", System.err::println);
Expand Down
150 changes: 150 additions & 0 deletions src/test/java/vavi/speech/voicevox/CoeiroInkTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2023 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/

package vavi.speech.voicevox;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.speech.synthesis.Voice;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import vavi.speech.voicevox.VoiceVox.AudioQuery;
import vavi.speech.voicevox.VoiceVox.Speaker;
import vavi.speech.voicevox.VoiceVox.SpeakerInfo;
import vavi.util.Debug;

import static org.junit.jupiter.api.Assertions.assertEquals;


/**
* CoeiroInkTest.
*
* @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (nsano)
* @version 0.00 2023-04-22 nsano initial version <br>
*/
class CoeiroInkTest {

private WebTarget target;

@BeforeEach
public void setUp() throws Exception {
Client c = ClientBuilder.newClient();
target = c.target("http://localhost:50032/");
}

@Test
@DisplayName("raw rest api")
void test1() throws Exception {
String text = "これはテストです。";

int speakerId = 1; // つくよみちゃん(れいせい)

String query = target
.path("audio_query")
.queryParam("text", text)
.queryParam("speaker", 1)
.request()
.post(null, String.class);
Debug.println(query);

Entity<String> entity = Entity.entity(query, MediaType.APPLICATION_JSON);
InputStream wav = target
.path("synthesis")
.queryParam("speaker", 1)
.request()
.post(entity, InputStream.class);
speak(wav);
}

/** */
static void speak(InputStream is) throws Exception {
AudioInputStream ais = AudioSystem.getAudioInputStream(new BufferedInputStream(is));
DataLine.Info line = new DataLine.Info(Clip.class, ais.getFormat());
Clip clip = (Clip) AudioSystem.getLine(line);
CountDownLatch cdl = new CountDownLatch(1);
clip.addLineListener(e -> { if (e.getType() == LineEvent.Type.STOP) cdl.countDown(); });
clip.open(ais);
clip.start();
cdl.await();
clip.stop();
clip.close();
}

static Gson gson = new GsonBuilder().setPrettyPrinting().create();

@Test
void test2() throws Exception {
//
String version = target
.path("version")
.request()
.get(String.class);
Debug.println("version: " + version);

//
String presets = target
.path("presets")
.request()
.get(String.class);
Debug.println("presets: " + presets);

//
String speakersJson = target
.path("speakers")
.request()
.get(String.class);
//Debug.println("speakers: " + speakers);

Speaker[] speakers = gson.fromJson(speakersJson, Speaker[].class);
Arrays.stream(speakers).forEach(System.err::println);

Arrays.stream(speakers).forEach(s -> {
String speakerInfoJson = target
.path("speaker_info")
.queryParam("speaker_uuid", s.speaker_uuid)
.request()
.get(String.class);
SpeakerInfo speakerInfo = gson.fromJson(speakerInfoJson, SpeakerInfo.class);
Debug.println("SpeakerInfo: " + speakerInfo);
});
}

@Test
@DisplayName("use wrapped api")
void test3() throws Exception {
int speakerId = 3; // ずんだもん(ノーマル)
VoiceVox voiceVox = new VoiceVox();
AudioQuery audioQuery = voiceVox.getQuery("ひざまずくが良いのだ、この愚かな地球人共よ", speakerId);
audioQuery.setSpeed(1.1f);
audioQuery.setVolume(0.2f);
speak(voiceVox.synthesize(audioQuery, speakerId));
}

@Test
@DisplayName("wrapped api list voices")
void test4() throws Exception {
VoiceVox voiceVox = new VoiceVox();
Voice[] voices = voiceVox.getAllVoices();
Arrays.stream(voices).forEach(System.err::println);
assertEquals(22, voiceVox.getId(voices[10]));
}
}

0 comments on commit 1376acd

Please sign in to comment.