Вопрос

I am building a speech synthesizer, and everything works except the audio. I have a list of phonemes that are stored as .wav files, and I am calling them with AudioInputStreams, but they won't repeat. I have no idea what could be the issue, so any help would be appreciated. The code that initializes a HashMap full of phones is

       for(File phone : listOfFiles){
            String path = phone.getPath();
            if(path.startsWith(".")){continue;}
            path = path.replace(".wav", "").replace("phones/", "");
            AudioInputStream clip1 = AudioSystem.getAudioInputStream(phone);
            phonemes.put(path,clip1);
        }

and the code that combines and outputs the sound is

public void speak(String[] input){
    AudioInputStream phrase = phonemes.get(input[0]);
    AudioInputStream phone;
    int x = input.length;
    for(int i=1; i<input.length; i++){
        phone = phonemes.get(input[i]);
        phrase = new AudioInputStream(new SequenceInputStream(phrase, phone), phrase.getFormat(), phrase.getFrameLength() + phone.getFrameLength());
    }
    try {
        Clip clip = AudioSystem.getClip();
        clip.open(phrase);
        clip.start();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Это было полезно?

Решение

To replay a Clip, you have to stop it and reposition it, then start it. I don't think you can close and reopen a given Clip. But attempts to do that should have generated a LineUnavailable exception, and you say you got no exceptions.

To troubleshoot, I'd first verify that it is possible to play the .wav files prior to placing them in the hash table. Sometimes an unexpected format (e.g., 24-bit or 32-bit encoding, or big-endian rather than little-endian) can lead to .wav files not playing.

If you are trying to concatenate a series of clips or audio data into a single clip, that could also be problematic. I think that AudioInputStream expects a single set of "header" data from the .wav file, but the SequenceInputStream could in effect be sending multiple "headers", one for each source file. I've never seen concatenation attempted like that before.

You might need to make your own data storage for the raw audio for each phoneme, and then build your combined phonemes from that rather than directly from .wav files. Instead of loading to Clips, load the raw PCM from the AudioInputStream into byte arrays. To output the raw audio bytes, you can use a SourceDataLine.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top