• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert a text into an audio file?

 
Sabastein Mittal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some text that i want to convert into an audio file like .wav.

So far, I can produce the speech from the text using FreeTTS and can create an audio file but couldn't put the converted audio out of text into the created file.

Code that converts text into speech using FreeTTS:
-------------------------------------------------


String voiceClassName = (args.length > 0) ? args[1] :
"com.sun.speech.freetts.en.us.CMUDiphoneVoice";
Class voiceClass = Class.forName(voiceClassName);


// instantiate the Voice

Voice helloVoice = (Voice) voiceClass.newInstance();

if (helloVoice instanceof MbrolaVoice) {
try {
(new MbrolaVoiceValidator((MbrolaVoice) helloVoice)).
validate();
} catch (ValidationException ve) {
System.err.println(ve.getMessage());
throw new IllegalStateException
("Problem starting MBROLA voice");
}
}

// sets the lexicon to CMU lexicon

helloVoice.setLexicon(new CMULexicon());


// sets the AudioPlayer to the Java clip player

helloVoice.setAudioPlayer(new JavaClipAudioPlayer());


// loads the Voice, which mainly is loading the lexicon
//Voice.allocate();
//helloVoice.allocate();
//helloVoice.loader();
helloVoice.load();

// does the text-to-speech
System.out.println("before speak");
helloVoice.speak("She sells seashells by the seashore");
System.out.println("after speak");




Code for creating a file:
------------------------


AudioFormat audioFormat;
TargetDataLine targetDataLine;
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;

int channels = 1;

boolean signed = true;
boolean bigEndian = false;
audioFormat = new AudioFormat(sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);

DataLine.Info dataLineInfo =
new DataLine.Info(TargetDataLine.class,audioFormat);

if (AudioSystem.isLineSupported(dataLineInfo) ==
false) {
System.out.println("Line matching " + dataLineInfo +
" not supported.");
return;
}

targetDataLine = (TargetDataLine)AudioSystem.getLine(dataLineInfo);

AudioFileFormat.Type fileType = null;
File audioFile = null;
System.out.println("Creating file");
fileType = AudioFileFormat.Type.AU;
audioFile = new File("junk.au");

targetDataLine.open(audioFormat);
targetDataLine.start();
System.out.println("Writing into file");
AudioSystem.write(
new AudioInputStream(targetDataLine),
fileType,
audioFile);
System.out.println("Writing Complete");


---------------------------------------------------------------------------

Please suggest..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!