draft : text to speed , offline code.in java(key) instead of pyton, reason why pyton (not asking subkey), where java asks?
//not testing, for java it is asking subscription key.
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import javax.sound.sampled.*;
import java.io.*;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
public class TextToSpeechEdgeTTS {
static final String INPUT_FILE = "input.txt";
static final String VOICE = "te-IN-ShrutiNeural";
static final int CHUNK_SIZE = 4000;
static final String TEMP_DIR = "temp_chunks";
static final String FINAL_MP3 = "output.mp3";
public static void main(String[] args) throws Exception {
String text = readText(INPUT_FILE);
if (text.isEmpty()) return;
List<String> chunks = chunkText(text, CHUNK_SIZE);
new File(TEMP_DIR).mkdirs();
List<File> chunkFiles = new ArrayList<>();
int idx = 0;
for (String chunk : chunks) {
String rate = mapSpeedToRate(5); // set desired speed 1–7
File chunkFile = new File(TEMP_DIR + "/chunk_" + idx + ".mp3");
generateChunk(chunk, chunkFile, VOICE, rate);
chunkFiles.add(chunkFile);
System.out.println("Generated chunk: " + chunkFile.getName());
idx++;
}
mergeAudioFiles(chunkFiles, FINAL_MP3);
System.out.println("✅ Final audio saved to: " + FINAL_MP3);
cleanup();
}
static String readText(String path) throws IOException {
return new String(Files.readAllBytes(Paths.get(path))).replaceAll("\\s+", " ");
}
static List<String> chunkText(String text, int maxLength) {
List<String> chunks = new ArrayList<>();
while (!text.isEmpty()) {
if (text.length() <= maxLength) {
chunks.add(text);
break;
}
int splitAt = text.lastIndexOf(' ', maxLength);
if (splitAt == -1) splitAt = maxLength;
chunks.add(text.substring(0, splitAt));
text = text.substring(splitAt).trim();
}
return chunks;
}
static String mapSpeedToRate(int value) {
return switch (value) {
case 1 -> "-75%";
case 2 -> "-50%";
case 3 -> "0%";
case 4 -> "+25%";
case 5 -> "+50%";
case 6 -> "+75%";
case 7 -> "+100%";
default -> "0%";
};
}
static void generateChunk(String text, File outputFile, String voice, String rate) throws Exception {
String ssml = "<speak version='1.0' xml:lang='te-IN'>" +
"<prosody rate='" + rate + "'>" + escapeXml(text) + "</prosody></speak>";
String tokenUrl = "https://eastus.tts.speech.microsoft.com/cognitiveservices/v1";
HttpPost post = new HttpPost(tokenUrl);
post.setHeader("Content-Type", "application/ssml+xml");
post.setHeader("X-Microsoft-OutputFormat", "audio-16khz-128kbitrate-mono-mp3");
post.setHeader("User-Agent", "JavaEdgeTTS");
post.setHeader("Ocp-Apim-Subscription-Key", "YOUR_SUBSCRIPTION_KEY"); // 🔑 Replace with valid Azure key
post.setEntity(new StringEntity(ssml));
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpResponse response = client.execute(post);
byte[] audioBytes = EntityUtils.toByteArray(response.getEntity());
Files.write(outputFile.toPath(), audioBytes);
}
}
static String escapeXml(String s) {
return s.replace("&", "&").replace("<", "<")
.replace(">", ">").replace("\"", """).replace("'", "'");
}
static void mergeAudioFiles(List<File> inputFiles, String outputFile) throws IOException {
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
for (File file : inputFiles) {
Files.copy(file.toPath(), fos);
}
}
}
static void cleanup() throws IOException {
for (File file : new File(TEMP_DIR).listFiles()) {
file.delete();
}
new File(TEMP_DIR).delete();
System.out.println("🧹 Temp files cleaned up.");
}
}
🔑 Important Note:
To make the above code work:
You must have a valid Microsoft Azure TTS Subscription Key
You can get it from: https://portal.azure.com > Create a Cognitive Services > Speech
Replace this line with your actual key:
java
Copy
Edit
post.setHeader("Ocp-Apim-Subscription-Key", "YOUR_SUBSCRIPTION_KEY");
Would you like a downloadable ZIP with:
All required .jar files
A working Eclipse project with this code?
Or do you prefer converting this into a Maven-based project for better dependency management?
You said:
httpclient-4.5.13.jar, httpcore-4.4.13.jar download links for these two
ChatGPT said:
You're giving feedback on a new version of ChatGPT.
Which response do you prefer? Responses may take a moment to load.
ChatGPT
Response 1
Here are the official Maven Central download links for the JAR files:
✅ Apache HttpClient 4.5.13
JAR Download:
httpclient-4.5.13.jar
✅ Apache HttpCore 4.4.13
JAR Download:
httpcore-4.4.13.jar
⚠️ Note:
httpclient depends on httpcore, so both are required.
----------
You said:
ChatGPT said:
Also make sure you include commons-logging-1.2.jar if not already added.
Would you like me to prepare a .zip file with all 3 JARs bundled for Eclipse or a Maven project setup?