Я использую следующий код:
Код: Выделить всё
package org.marytts;
import marytts.LocalMaryInterface;
import java.net.URL;
import java.io.File;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import marytts.exceptions.MaryConfigurationException;
import marytts.exceptions.SynthesisException;
import marytts.util.data.audio.AudioRecorder;
public class MaryTTSWav {
static {
CodeSource codeSource = MaryTTSWav.class.getProtectionDomain().getCodeSource();
URL url = codeSource .getLocation();
File file = new File(url.getFile()).getParentFile();
System.setProperty("mary.base", file.getPath());
}
private LocalMaryInterface maryI;
private float volume = -1f;
private float rate = -1f;
public static void main(String[] args) {
MaryTTSWav wavRecorder = new MaryTTSWav();
wavRecorder.record("this is a test");
}
public MaryTTSWav() {
init();
}
private void init() {
try {
maryI = new LocalMaryInterface();
maryI.setVoice("enst-camille-hsmm");
//setVolume(2);
//setRate(1);
} catch (MaryConfigurationException ex) {
ex.printStackTrace();
}
}
public void record(String sentence) {
try {
AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
AudioInputStream stream = maryI.generateAudio(sentence);
AudioFormat audioFormat = stream.getFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
File dir = new File(System.getProperty("user.dir"));
File targetFile = new File(dir, "toto.wav");
try {
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
AudioRecorder.BufferingRecorder recorder = new AudioRecorder.BufferingRecorder(targetDataLine, targetType, targetFile,(int) 10000);
System.out.println("Recording...");
recorder.start();
try {
recorder.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println("Recording stopped.");
} catch (LineUnavailableException e) {
e.printStackTrace();
}
} catch (SynthesisException ex) {
ex.printStackTrace();
}
}
private void setVolume(float volume) {
StringBuilder buf = new StringBuilder();
buf.append("Volume(amount:").append(volume).append(")");
maryI.setAudioEffects(buf.toString());
}
private void setRate(float rate) {
StringBuilder buf = new StringBuilder();
buf.append("Rate(durScale:").append(rate).append(")");
maryI.setAudioEffects(buf.toString());
}
}
У меня есть результирующий файл .wav, и он кажется действительным (например, я могу без проблем воспроизвести его с помощью VLC), но у него нет звука. Почему?
Подробнее здесь: https://stackoverflow.com/questions/792 ... ated-audio
Мобильная версия