Skip to content

Commit acceb0b

Browse files
authored
Merge pull request #9 from Barqawiz/more-audio-languages
Add more audio languages
2 parents 0dec1bf + 302e020 commit acceb0b

File tree

7 files changed

+183
-17
lines changed

7 files changed

+183
-17
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<p align="center">
66

7-
<a href="https://central.sonatype.com/artifact/io.github.barqawiz/intellijava.core/0.8.0" alt="maven build">
7+
<a href="https://central.sonatype.com/artifact/io.github.barqawiz/intellijava.core/0.8.2" alt="maven build">
88
<img src="https://img.shields.io/maven-central/v/io.github.barqawiz/intellijava.core?style=flat-square" />
99
</a>
1010

@@ -40,23 +40,23 @@ Maven:
4040
<dependency>
4141
<groupId>io.github.barqawiz</groupId>
4242
<artifactId>intellijava.core</artifactId>
43-
<version>0.8.0</version>
43+
<version>0.8.2</version>
4444
</dependency>
4545
```
4646

4747
Gradle:
4848

4949
```
50-
implementation 'io.github.barqawiz:intellijava.core:0.8.0'
50+
implementation 'io.github.barqawiz:intellijava.core:0.8.2'
5151
```
5252

5353
Gradle(Kotlin):
5454
```
55-
implementation("io.github.barqawiz:intellijava.core:0.8.0")
55+
implementation("io.github.barqawiz:intellijava.core:0.8.2")
5656
```
5757

5858
Jar download:
59-
[intellijava.jar](https://repo1.maven.org/maven2/io/github/barqawiz/intellijava.core/0.8.0/intellijava.core-0.8.0.jar).
59+
[intellijava.jar](https://repo1.maven.org/maven2/io/github/barqawiz/intellijava.core/0.8.2/intellijava.core-0.8.2.jar).
6060

6161
## Code Example
6262
**Language model code** (2 steps):

core/com.intellijava.core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.barqawiz</groupId>
88
<artifactId>intellijava.core</artifactId>
9-
<version>0.8.0</version>
9+
<version>0.8.2</version>
1010

1111
<name>Intellijava</name>
1212
<description>IntelliJava allows java developers to easily integrate with the latest language models, image generation, and deep learning frameworks.</description>

core/com.intellijava.core/src/main/java/com/intellijava/core/controller/RemoteSpeechModel.java

Lines changed: 147 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ public List<String> getSupportedModels() {
110110
}
111111

112112
/**
113-
* Generates speech from text using the support models.
113+
* Generates speech using advanced audio models.
114114
*
115-
* You can save the returned byte to audio file using FileOutputStream("path/audio.mp3").
115+
* You can save the result byte to audio file using FileOutputStream("path/audio.mp3").
116116
*
117117
* @param input SpeechInput object containing the text and gender to use.
118118
* @return byte array of the decoded audio content.
@@ -121,7 +121,101 @@ public List<String> getSupportedModels() {
121121
public byte[] generateEnglishText(Text2SpeechInput input) throws IOException {
122122

123123
if (this.keyType == SpeechModels.google) {
124-
return this.generateGoogleText(input.getText(), input.getGender(), "en-gb");
124+
return this.generateGoogleText(input.getText(), input.getGender(), "en-GB");
125+
} else {
126+
throw new IllegalArgumentException("the keyType not supported");
127+
}
128+
}
129+
130+
131+
/**
132+
* Generates speech using advanced audio models.
133+
*
134+
* You can save the result byte to audio file using FileOutputStream("path/audio.mp3").
135+
*
136+
* @param input SpeechInput object containing the text and gender to use.
137+
* @return byte array of the decoded audio content.
138+
* @throws IOException in case of communication error.
139+
*/
140+
public byte[] generateMandarinText(Text2SpeechInput input) throws IOException {
141+
142+
if (this.keyType == SpeechModels.google) {
143+
return this.generateGoogleText(input.getText(), input.getGender(), "cmn-CN");
144+
} else {
145+
throw new IllegalArgumentException("the keyType not supported");
146+
}
147+
}
148+
149+
/**
150+
* Generates speech using advanced audio models.
151+
*
152+
* You can save the result byte to audio file using FileOutputStream("path/audio.mp3").
153+
*
154+
* @param input SpeechInput object containing the text and gender to use.
155+
* @return byte array of the decoded audio content.
156+
* @throws IOException in case of communication error.
157+
*/
158+
public byte[] generateArabicText(Text2SpeechInput input) throws IOException {
159+
160+
if (this.keyType == SpeechModels.google) {
161+
return this.generateGoogleText(input.getText(), input.getGender(), "ar-XA");
162+
} else {
163+
throw new IllegalArgumentException("the keyType not supported");
164+
}
165+
}
166+
167+
168+
/**
169+
* Generates speech using advanced audio models.
170+
*
171+
* You can save the result byte to audio file using FileOutputStream("path/audio.mp3").
172+
*
173+
* @param input SpeechInput object containing the text and gender to use.
174+
* @return byte array of the decoded audio content.
175+
* @throws IOException in case of communication error.
176+
*/
177+
public byte[] generateTurkishText(Text2SpeechInput input) throws IOException {
178+
179+
if (this.keyType == SpeechModels.google) {
180+
return this.generateGoogleText(input.getText(), input.getGender(), "tr-TR");
181+
} else {
182+
throw new IllegalArgumentException("the keyType not supported");
183+
}
184+
}
185+
186+
/**
187+
* Generates speech using advanced audio models.
188+
*
189+
* You can save the result byte to audio file using FileOutputStream("path/audio.mp3").
190+
*
191+
* @param input SpeechInput object containing the text and gender to use.
192+
* @param langCode the language code, make sure to use the right code for the model engine.
193+
* @return byte array of the decoded audio content.
194+
* @throws IOException in case of communication error.
195+
*/
196+
public byte[] generateText(Text2SpeechInput input, String langCode) throws IOException {
197+
198+
if (this.keyType == SpeechModels.google) {
199+
return this.generateGoogleText(input.getText(), input.getGender(), langCode);
200+
} else {
201+
throw new IllegalArgumentException("the keyType not supported");
202+
}
203+
}
204+
205+
206+
/**
207+
* Generates speech using advanced audio models.
208+
*
209+
* You can save the result byte to audio file using FileOutputStream("path/audio.mp3").
210+
*
211+
* @param input SpeechInput object containing the text and gender to use.
212+
* @return byte array of the decoded audio content.
213+
* @throws IOException in case of communication error.
214+
*/
215+
public byte[] generateGermanText(Text2SpeechInput input) throws IOException {
216+
217+
if (this.keyType == SpeechModels.google) {
218+
return this.generateGoogleText(input.getText(), input.getGender(), "de-de");
125219
} else {
126220
throw new IllegalArgumentException("the keyType not supported");
127221
}
@@ -143,13 +237,56 @@ private byte[] generateGoogleText(String text, Gender gender, String language) t
143237
params.put("text", text);
144238
params.put("languageCode", language);
145239

146-
if (gender == Gender.FEMALE) {
147-
params.put("name", "en-GB-Standard-A");
148-
params.put("ssmlGender", "FEMALE");
149-
} else {
150-
params.put("name", "en-GB-Standard-B");
151-
params.put("ssmlGender", "MALE");
152-
}
240+
language = language.toLowerCase();
241+
242+
if (language.equals("en-gb")) {
243+
// English
244+
if (gender == Gender.FEMALE) {
245+
params.put("name", "en-GB-Standard-A");
246+
params.put("ssmlGender", "FEMALE");
247+
} else {
248+
params.put("name", "en-GB-Standard-B");
249+
params.put("ssmlGender", "MALE");
250+
}
251+
} else if (language.equals("tr-tr")) {
252+
// Turkish
253+
if (gender == Gender.FEMALE) {
254+
params.put("name", "tr-TR-Standard-A");
255+
params.put("ssmlGender", "FEMALE");
256+
} else {
257+
params.put("name", "tr-TR-Standard-B");
258+
params.put("ssmlGender", "MALE");
259+
}
260+
} else if (language.equals("cmn-cn")) {
261+
// Mandarin Chinese
262+
if (gender == Gender.FEMALE) {
263+
params.put("name", "cmn-CN-Standard-A");
264+
params.put("ssmlGender", "FEMALE");
265+
} else {
266+
params.put("name", "cmn-CN-Standard-B");
267+
params.put("ssmlGender", "MALE");
268+
}
269+
} else if (language.equals("de-de")) {
270+
// German (Germany)
271+
if (gender == Gender.FEMALE) {
272+
params.put("name", "de-DE-Standard-A");
273+
params.put("ssmlGender", "FEMALE");
274+
} else {
275+
params.put("name", "de-DE-Standard-B");
276+
params.put("ssmlGender", "MALE");
277+
}
278+
} else if (language.equals("ar-xa")) {
279+
// Arabic
280+
if (gender == Gender.FEMALE) {
281+
params.put("name", "ar-XA-Wavenet-A");
282+
params.put("ssmlGender", "FEMALE");
283+
} else {
284+
params.put("name", "ar-XA-Standard-B");
285+
params.put("ssmlGender", "MALE");
286+
}
287+
} else {
288+
throw new IllegalArgumentException("Unsupported language code: " + language);
289+
}
153290

154291
AudioResponse resModel = (AudioResponse) wrapper.generateSpeech(params);
155292
decodedAudio = AudioHelper.decode(resModel.getAudioContent());

sample_code/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
</attributes>
99
</classpathentry>
1010
<classpathentry kind="lib" path="jars/gson-2.10.1.jar"/>
11-
<classpathentry kind="lib" path="jars/intellijava.core-0.8.0.jar"/>
11+
<classpathentry kind="lib" path="jars/intellijava.core-0.8.1.jar"/>
1212
<classpathentry kind="output" path="bin"/>
1313
</classpath>
-155 KB
Binary file not shown.
49.9 KB
Binary file not shown.

sample_code/src/com/intelliJava/test/GoogleApp.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static void main(String[] args) {
2525

2626
tryGoogleSpeechModel(apiKey);
2727

28+
System.out.print("Check the temp forlder for the generated audio.");
29+
2830
} catch (IOException e) {
2931
e.printStackTrace();
3032
}
@@ -56,5 +58,32 @@ private static void tryGoogleSpeechModel(String apiKey) throws IOException {
5658
AudioHelper.saveTempAudio(decodedAudio);
5759

5860
}
61+
62+
/**
63+
* Generate speech from text using google API.
64+
*
65+
* To use this model:
66+
* 1- Go to console.cloud.google.com.
67+
* 2- Enable "Cloud Text-to-Speech API" from APIs Services.
68+
* 3- Generate API key from APIs and services Credentials page.
69+
*
70+
* @param apiKey
71+
* @throws IOException
72+
*/
73+
private static void tryGermanSpeechModel(String apiKey) throws IOException {
74+
75+
76+
RemoteSpeechModel model = new RemoteSpeechModel(apiKey, SpeechModels.google);
77+
78+
Text2SpeechInput input = new Text2SpeechInput.Builder("Hallo, ich bin Intelligent Java.").build();
79+
80+
// get the audio bytes
81+
// you can play it using libraries like javafx
82+
byte[] decodedAudio = model.generateGermanText(input);
83+
84+
// save temporary audio file
85+
AudioHelper.saveTempAudio(decodedAudio);
86+
87+
}
5988

6089
}

0 commit comments

Comments
 (0)