简体   繁体   English

一种使用Java音频?

[英]One use Java audio?

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. 我有一个存储为.wav文件的音素列表,我使用AudioInputStreams对其进行调用,但不会重复。 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 初始化充满电话的HashMap的代码是

       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. 我认为您无法关闭并重新打开给定的Clip。 But attempts to do that should have generated a LineUnavailable exception, and you say you got no exceptions. 但是尝试这样做应该会产生LineUnavailable异常,并且您说没有异常。

To troubleshoot, I'd first verify that it is possible to play the .wav files prior to placing them in the hash table. 为了进行故障排除,我首先要验证是否可以播放.wav文件,然后再将它们放置在哈希表中。 Sometimes an unexpected format (eg, 24-bit or 32-bit encoding, or big-endian rather than little-endian) can lead to .wav files not playing. 有时,意外的格式(例如24位或32位编码,或大端而不是小端)会导致.wav文件无法播放。

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. 我认为AudioInputStream期望来自.wav文件的一组“标头”数据,但是SequenceInputStream实际上可以发送多个“标头”,每个源文件一个。 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. 您可能需要为每个音素的原始音频创建自己的数据存储,然后从中而不是直接从.wav文件构建组合的音素。 Instead of loading to Clips, load the raw PCM from the AudioInputStream into byte arrays. 而不是加载到Clips,而是将AudioInputStream中的原始PCM加载到字节数组中。 To output the raw audio bytes, you can use a SourceDataLine. 要输出原始音频字节,可以使用SourceDataLine。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM