Using MBROLA voices in a Java program with FreeTTS...
I am working on a simple text-to-speech program in Java. I have decided to use FreeTTS, but the voices are not really what I was thinking, and I was looking to use a female voice anyway. So I started looking around, and decided that I would use MBROLA to change the voice of my text-to-speech program.
I read that "FreeTTS can use MBROLA voices", but I searched everywhere and couldn't find a clear guide how to set MBROLA up, and what files are needed to do so. There are many forums on MBROLA working alongside FreeTTS, however it also seems that none of the people have any idea what they are doing.
So the questions:
What files need to be downloaded?
Steps to include these into my program?
Simple FreeTTS example using MBROLA voices?
Answers to the questions above:
1. What files need to be downloaded?
FreeTTS with all the libraries (freeTTS 1.2.2-bin) - download here
MBROLA zip folder mbr301d.zip
Voices which can be found on the MBROLA website
1.1 The FreeTTS libraries (found in freetts-1.2.2-bin/freetts-1.2/lib):
cmu_time_awb.jar
cmu_us_kal.jar
cmudict04.jar
cmulex.jar
cmutimelex.jar
en_us.jar
freetts.jar
freetts-jsapi10.jar
mbrola.jar
1.2 The MBROLA zip folder will include:
mbrola.exe
mbr302a (folder)
readme.txt
1.3 The Voices are zipped folders that include a single folder named 'us1' or 'af1' etc.
2. Steps to include these into my program?
NOTE: I had the MBROLA Tooklit installed on my computer too, however I am unsure whether it has an impact on the program, but I suspect that it doesn't. EDIT: I have tested to see whether the MBROLA toolkit is needed to run MBROLA alongside FreeTTS, and it turns out that it is not needed.
Extract freetts-1.2.2-bin
Copy the libraries to your project and include in build path
Unzip the mbr301d.zip folder
Rename 'mbr301d' to 'mbrola'
Unzip the voices to the folder you named 'mbrola'
After this is done, your mbrola folder should look like this:
[mbr302a] - folder
[us1] - folder (name depends on the language you downloaded)
mbrola.exe - file
readme.txt - file
You can place all your languages in this folder, and they will just be called from your Java program.
3. Simple FreeTTS example using MBROLA voices?
I've seen many people get this error:
System property "mbrola.base" is undefined. Will not use MBROLA voices.
The mbrola.base refers to where your mbrola files are located on your computer, and without the property being pointed to the correct location, you will recieve this error.
To NON-MBROLA users who get this error: Simply remove the mbrola.jar from your referenced libraries if you're only using FreeTTS
To set the mbrola.base property, use:
System.setProperty("mbrola.base", "C:/Path/to/your/mbrola")
Below is a simple Example to use the MBROLA voices in your FreeTTS program. Note that the steps above must be done before this will work. Simply changing the name of the voice to "mbrola_us1" will not work if the base isn't set!
package com.madmob.test;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
public class TestTTS {
VoiceManager freettsVM;
Voice freettsVoice;
public TestTTS(String words) {
// Most important part!
System.setProperty("mbrola.base", "C:/mbrola");
freettsVM = VoiceManager.getInstance();
// Simply change to MBROLA voice
freettsVoice = freettsVM.getVoice("mbrola_us1");
// Allocate your chosen voice
freettsVoice.allocate();
sayWords(words);
}
public void sayWords(String words) {
// Make her speak!
freettsVoice.speak(words);
}
public static void main(String [] args) {
new TestTTS("Hello there! Now M BROLA and Free T T S work together!");
}
}
MBROLA and FreeTTS should now be working together! This code was copied right from my computer and has been tested before putting it down here.
Thanks to responses on this forum, I was finally able to make it working.
On windows 10; I carried out following steps to make it working:
Download freeTTS libraries and include them in my Java project in eclipse.
Download mbr301d.zip, extract it in a folder named mbrola within my project
Download mbrola database for us1, us2, us3 and en1 from http://www.tcts.fpms.ac.be/synthesis/mbrola/mbrcopybin.html
extract the voice DB zips downloaded in previous step directly in mbrola folder - don't change names of the folders.
include following code pieces to use it:
System.setProperty("mbrola.base", "ABSOLUTE_PATH_TO_mbrola_directory_ending_with_/");
voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice("mbrola_us1");
Note: if your voice DB folder name is us1; then you should add it above as "mbrola_us1"; if it is en1, then it should be "mbrola_en1". This actually has done the trick for me.
Please find working example from here:
https://github.com/sunrise-projects/sphinx4/tree/glass
package com.sunriseprojects.freetts.demo;
import java.beans.PropertyVetoException;
import java.util.Locale;
import javax.speech.AudioException;
import javax.speech.Central;
import javax.speech.EngineException;
import javax.speech.EngineStateError;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Voice;
public class SpeechUtils {
SynthesizerModeDesc desc;
Synthesizer synthesizer;
Voice voice;
public void init(String voiceName) throws EngineException, AudioException,
EngineStateError, PropertyVetoException {
if (desc == null) {
//default
// System.setProperty("freetts.voices",
// "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
//have to be setup
System.setProperty("freetts.voices",
"de.dfki.lt.freetts.en.us.MbrolaVoiceDirectory");
desc = new SynthesizerModeDesc(Locale.US);
Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
synthesizer = Central.createSynthesizer(desc);
synthesizer.allocate();
synthesizer.resume();
SynthesizerModeDesc smd = (SynthesizerModeDesc) synthesizer
.getEngineModeDesc();
Voice[] voices = smd.getVoices();
Voice voice = null;
for (int i = 0; i < voices.length; i++) {
if (voices[i].getName().equals(voiceName)) {
voice = voices[i];
break;
}
}
synthesizer.getSynthesizerProperties().setVoice(voice);
}
}
public void terminate() throws EngineException, EngineStateError {
synthesizer.deallocate();
}
public void doSpeak(String speakText) throws EngineException,
AudioException, IllegalArgumentException, InterruptedException {
synthesizer.speakPlainText(speakText, null);
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
}
public static void main(String[] args) throws Exception {
System.setProperty("mbrola.base", "C:\\lnx1\\home\\ggon\\git-projects\\mbrola");
SpeechUtils su = new SpeechUtils();
//have to be setup on your env
su.init("mbrola_us1");
//default
//su.init("kevin16");
//su.init("kevin");
//su.doSpeak("Hello world!");
su.doSpeak(SAMPLE);
su.terminate();
}
final static String SAMPLE = "Wiki said, Floyd Mayweather, Jr. is an American professional boxer. He is currently undefeated as a professional and is a five-division world champion, having won ten world titles and the lineal championship in four different weight classes";
}
Related
I am using freetts jar file for text to speech.
But I need much more voices like Indian English.
There my code is:
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
class s
{
VoiceManager freettsVM;
Voice freettsVoice;
private Voice voice;
public s(String voiceName)
{
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice(voiceName);
}
public void speak(String msg) {
freettsVoice.allocate();
freettsVoice.speak(msg);
freettsVoice.deallocate();
}
}
public class Main {
public static void main(String[] args) {
String speekstring = txtmassage.getText();
s t = new s("kevin16");
t.speak(speekstring);}
}
It’s work correctly, but I need more voices.
now i changed my code into
then there error is
Could not validate any MBROLA voices at
C:/Users/anand/Documents/NetBeansProjects/mbrola
Make sure you FULLY specify the path to
the MBROLA directory using the mbrola.base
system property.
Exception in thread "main" java.lang.NullPointerException
at newpro.TestTTS.<init>(TestTTS.java:20)
at newpro.TestTTS.main(TestTTS.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
You could use the MBROLA - Project. It's free and offers
several languages.
You will need to download the project-package itself and the voices.
For a step-by-step guide on how to install and use MBROLA you can go to here:
https://stackoverflow.com/a/26236563/1368690
Or go to the project page itself:
http://tcts.fpms.ac.be/synthesis/mbrola.html
When you scroll down a bit, there a lots of voice you can test and see, if they will be usefull for you.
I've looked around on stack overflow, and a few other sites, but I can't really find anything helpful. I need to have code that can play an audio file that is inside the same as my Class.java file. In other words, I need it to play a file without typing in the exact location of the file, llike if I was sending it to a friend. Here is what I have:
import java.applet.*;
import java.net.*;
public class MainClass extends Applet {
public void init() {
try {
AudioClip clip = Applet.newAudioClip(
new URL(“file://C:/sound.wav”));
clip.play();
} catch (MalformedURLException murle) {
murle.printStackTrace();
}
}
But I can't get it to play from just anywhere, only that specific folder. Is there a way to do this without typing "URL" before the file location?
Change your URL declaration , change "file://C:/sound.wav" to "file:C:/sound.wav"
import java.applet.*;
import java.net.*;
public class MainClass {
public static void main(String[] args) {
try {
AudioClip clip = Applet.newAudioClip(
new URL("file:C:/sound.wav"));
clip.play();
} catch (MalformedURLException murle) {
murle.printStackTrace();
}}}
*I had tested it and working great under NetBeans IDE
I believe Applet.audioClip() is intended for use inside Applets, rather than a desktop app. One of the limitations is that you can only use a URL to locate the sound resource. On the other hand the Java Sound API is more versatile. It allows you to locate the sound resource with a File object as well as many other options.
You also need to figure out how to refer to your file. In particular, if you want to use a relative path, you need to figure out what base path your environment will start from. Embedding resources (images, sound bits, etc) into a Java project then use those resources will give you more details about how to resolve this issue.
I am trying to print out all of the capture devices that are supported using the #getDeviceList() method in the CaptureDeviceManager class and the returned Vector has a size of 0.
Why is that? I have a webcam that works - so there should be at least one. I am running Mac OS X Lion - using JMF 2.1.1e.
Thanks!
CaptureDeviceManager.getDeviceList(Format format) does not detect devices. Instead it reads from the JMF registry which is the jmf.properties file. It searches for the jmf.properties file in the classpath.
If your JMF install has succeeded, then the classpath would have been configured to include all the relevant JMF jars and directories. The JMF install comes with a jmf.properties file included in the 'lib' folder under the JMF installation directory. This means the jmf.properties would be located by JMStudio and you would usually see the JMStudio application executing correctly. (If your JMF install is under 'C:\Program Files', then run as administrator to get around UAC)
When you create your own application to detect the devices, the problem you described above might occur. I have seen a few questions related to the same problem. This is because your application's classpath might be different and might not include the environment classpath. Check out your IDE's properties here. The problem is that CaptureDeviceManager cannot find the jmf.properties file because it is not there.
As you have found out correctly, you can copy the jmf.properties file from the JMF installation folder. It would contain the correct device list since JMF detects it during the install (Check it out just to make sure anyway).
If you want do device detection yourself, then create an empty jmf.properties file and put it somewhere in your classpath (it might throw a java.io.EOFException initially during execution but that's properly handled by the JMF classes). Then use the following code for detecting webcams...
import javax.media.*;
import java.util.*;
public static void main(String[] args) {
VFWAuto vfwObj = new VFWAuto();
Vector devices = CaptureDeviceManager.getDeviceList(null);
Enumeration deviceEnum = devices.elements();
System.out.println("Device count : " + devices.size());
while (deviceEnum.hasMoreElements()) {
CaptureDeviceInfo cdi = (CaptureDeviceInfo) deviceEnum.nextElement();
System.out.println("Device : " + cdi.getName());
}
}
The code for the VFWAuto class is given below. This is part of the JMStudio source code. You can get a good idea on how the devices are detected and recorded in the registry. Put both classes in the same package when you test. Disregard the main method in the VFWAuto class.
import com.sun.media.protocol.vfw.VFWCapture;
import java.util.*;
import javax.media.*;
public class VFWAuto {
public VFWAuto() {
Vector devices = (Vector) CaptureDeviceManager.getDeviceList(null).clone();
Enumeration enum = devices.elements();
while (enum.hasMoreElements()) {
CaptureDeviceInfo cdi = (CaptureDeviceInfo) enum.nextElement();
String name = cdi.getName();
if (name.startsWith("vfw:"))
CaptureDeviceManager.removeDevice(cdi);
}
int nDevices = 0;
for (int i = 0; i < 10; i++) {
String name = VFWCapture.capGetDriverDescriptionName(i);
if (name != null && name.length() > 1) {
System.err.println("Found device " + name);
System.err.println("Querying device. Please wait...");
com.sun.media.protocol.vfw.VFWSourceStream.autoDetect(i);
nDevices++;
}
}
}
public static void main(String [] args) {
VFWAuto a = new VFWAuto();
System.exit(0);
}
}
Assuming you are on a Windows platform and you have a working web-cam, then this code should detect the device and populate the jmf.properties file. On the next run you can also comment out the VFWAuto section and it's object references and you can see that CaptureDeviceManager reads from the jmf.properties file.
The VFWAuto class is part of jmf.jar. You can also see the DirectSoundAuto and JavaSoundAuto classes for detecting audio devices in the JMStudio sample source code. Try it out the same way as you did for VFWAuto.
My configuration was Windows 7 64 bit + JMF 2.1.1e windows performance pack + a web-cam.
I had the same issue and I solved by invoking flush() on my ObjectInputStream object.
According to the API documentation for ObjectInputStream's constructor:
The stream header containing the magic number and version number are read from the stream and verified. This method will block until the corresponding ObjectOutputStream has written and flushed the header.
This is a very important point to be aware of when trying to send objects in both directions over a socket because opening the streams in the wrong order will cause deadlock.
Consider for example what would happen if both client and server tried to construct an ObjectInputStream from a socket's input stream, prior to either constructing the corresponding ObjectOutputStream. The ObjectInputStream constructor on the client would block, waiting for the magic number and version number to arrive over the connection, while at the same time the ObjectInputStream constructor on the server side would also block for the same reason. Hence, deadlock.
Because of this, you should always make it a practice in your code to open the ObjectOutputStream and flush it first, before you open the ObjectInputStream. The ObjectOutputStream constructor will not block, and invoking flush() will force the magic number and version number to travel over the wire. If you follow this practice in both your client and server, you shouldn't have a problem with deadlock.
Credit goes to Tim Rohaly and his explanation here.
Before calling CaptureDeviceManager.getDeviceList(), the available devices must be loaded into the memory first.
You can do it manually by running JMFRegistry after installing JMF.
or do it programmatically with the help of the extension library FMJ (Free Media in Java). Here is the code:
import java.lang.reflect.Field;
import java.util.Vector;
import javax.media.*;
import javax.media.format.RGBFormat;
import net.sf.fmj.media.cdp.GlobalCaptureDevicePlugger;
public class FMJSandbox {
static {
System.setProperty("java.library.path", "D:/fmj-sf/native/win32-x86/");
try {
final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
GlobalCaptureDevicePlugger.addCaptureDevices();
Vector deviceInfo = CaptureDeviceManager.getDeviceList(new RGBFormat());
System.out.println(deviceInfo.size());
for (Object obj : deviceInfo ) {
System.out.println(obj);
}
}
}
Here is the output:
USB2.0 Camera : civil:\\?\usb#vid_5986&pid_02d3&mi_00#7&584a19f&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global
RGB, -1-bit, Masks=-1:-1:-1, PixelStride=-1, LineStride=-1
I'm using JACOB to do COM calls to PowerPoint and other Office applications from Java. On a particular Windows 7 box I'm getting the following message quite often, but not always:
Source: Microsoft Office PowerPoint 2007
Description: PowerPoint could not open the file.
From excel I get:
ERROR - Invoke of: Open
Source: Microsoft Office Excel
Description: Microsoft Office Excel cannot access the file 'c:\marchena\marchena10\work\marchena\batch_58288\input\content_1.xlsx'. There are several possible reasons:
? The file name or path does not exist.
? The file is being used by another program.
? The workbook you are trying to save has the same name as a currently open workbook.
The Word error is just:
VariantChangeType failed
The following is what I'm running, the error comes from the last line.
ComThread.InitSTA();
slideApp = new ActiveXComponent("PowerPoint.Application");
Dispatch presentations = slideApp.getProperty("Presentations").toDispatch();
Dispatch presentation = Dispatch.call(presentations, "Open", inputFile.getAbsolutePath(),
MsoTriState.msoTrue.getInteger(), // ReadOnly
MsoTriState.msoFalse.getInteger(), // Untitled The Untitled parameter is used to create a copy of the presentation.
MsoTriState.msoFalse.getInteger() // WithWindow
).toDispatch();
I've tried putting a breakpoint just before doing the Open call and the file is there, and I can actually open it with PowerPoint in the GUI but when I step the exception is thrown.
The annoying thing about this issue is that it seems to happen continuously to begin with, but after poking at it for a while (rerunning the same code), it eventually completes successfully, and after that never reoccurs.
Further research I've found this only happens with to .ppt, .doc and .xls files, not .pptx, .docx and .xlsx. And as far as I can tell it's not file system related (I've swapped out the mechanism that copies the files and tried putting the files on a different file system).
I've just noticed that this only happens when the Java application is running as a service, not when I run catalina.bat start from command line.
I had the same problem (jacob in service not working), and this helpful posting (changing the dcomcnfg) did the trick:
http://bytes.com/topic/c-sharp/answers/819740-c-service-excel-application-workbooks-open-fails-when-called-service#post3466746
Does this work for you?
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class PPT {
private static final String inputFile = "c:\\learning.ppt";
public static void main(String[] args) {
ActiveXComponent slideApp = new ActiveXComponent("PowerPoint.Application");
slideApp.setProperty("Visible", new Variant(true));
ActiveXComponent presentations = slideApp.getPropertyAsComponent("Presentations");
ActiveXComponent presentation = presentations.invokeGetComponent("Open",new Variant(inputFile), new Variant(true));
ComThread.Release();
}
}
Update: If this is working the client and it's just automation that is causing the issues, you can view http://support.microsoft.com/kb/257757 to look at possible issues. There error codes are obviously different, but it may help you troubleshoot all the same.
So my 2009 new years resolution is to learn Java. I recently acquired "Java for Dummies" and have been following along with the demo code in the book by re-writing it using Eclipse. Anyway, every example in the book that uses a relative path does not seem to read the .txt file it's supposed to read from.
Here is the sample code:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.GridLayout;
class TeamFrame extends JFrame {
public TeamFrame() throws IOException {
PlayerPlus player;
Scanner myScanner = new Scanner(new File("Hankees.txt"));
for (int num = 1; num <= 9; num++) {
player = new PlayerPlus(myScanner.nextLine(), myScanner.nextDouble());
myScanner.nextLine();
addPlayerInfo(player);
}
add(new JLabel());
add(new JLabel(" ------"));
add(new JLabel("Team Batting Aberage:"));
add(new JLabel(PlayerPlus.findTeamAverageString()));
setTitle("The Hankees");
setLayout(new GridLayout(11,2));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
void addPlayerInfo(PlayerPlus player) {
add(new JLabel(player.getName()));
add(new JLabel(player.getAverageString()));
}
}
And you can see in the below screen shot I have included this file.
image no longer available
Also, I have verified that when I build the application that a copy of Hankees.txt is placed in the bin folder with the compiled .class files.
Lastly, if I change line 12 to the following and place Hankees.txt in the root of my C:\ drive the program compiles and runs fine.
Scanner myScanner = new Scanner(new File("C:\\Hankees.txt"));
So basically, my question is what am I doing wrong? Or is Eclipse responsible for this in some way?
Thanks for any and all help!
You need "src/Hankees.txt"
Your file is in the source folder which is not counted as the working directory.\
Or you can move the file up to the root directory of your project and just use "Hankees.txt"
A project's build path defines which resources from your source folders are copied to your output folders. Usually this is set to Include all files.
New run configurations default to using the project directory for the working directory, though this can also be changed.
This code shows the difference between the working directory, and the location of where the class was loaded from:
public class TellMeMyWorkingDirectory {
public static void main(String[] args) {
System.out.println(new java.io.File("").getAbsolutePath());
System.out.println(TellMeMyWorkingDirectory.class.getClassLoader().getResource("").getPath());
}
}
The output is likely to be something like:
C:\your\project\directory
/C:/your/project/directory/bin/
This is really similar to another question.
How should I load files into my Java application?
How should I load my files into my Java Application?
You do not want to load your files in by:
C:\your\project\file.txt
this is bad!
You should use getResourceAsStream.
InputStream inputStream = YourClass.class.getResourceAsStream(“file.txt”);
And also you should use File.separator; which is the system-dependent name-separator character, represented as a string for convenience.
Yeah, eclipse sees the top directory as the working/root directory, for the purposes of paths.
...just thought I'd add some extra info. I'm new here! I'd like to help.
You can always get your runtime path by using:
String path = new File(".").getCanonicalPath();
This provides valuable information about where to put files and resources.
Paraphrasing from http://java.sun.com/javase/6/docs/api/java/io/File.html:
The classes under java.io resolve relative pathnames against the current user directory, which is typically the directory in which the virtual machine was started.
Eclipse sets the working directory to the top-level project folder.