繁体   English   中英

将带有音频的arraylist转换为.wav文件(Java)

[英]convert an arraylist with audio into a .wav file (Java)

这是我的问题,有人给了我一个功能,如果我对它很了解的话,可以将一些声音样本放到arraylist中。

我想用此音轨创建一个.wav文件,但我真的不知道该怎么做。

这是代码,因为也许我根本不了解它。

public class Track {



private ArrayList<Sample> sounds;
private     AudioFormat audioFormat;
            TargetDataLine targetDataLine;
public Track()
{
    this.sounds = new ArrayList <Sample>();  
}
/*** Sort the sample on the track by ascending start time ***/
public void sortTrack() {
    Collections.sort(sounds);
}

/**
 * Add a sample to the track.
 * @param fic location to the audio file.
 * @param sT set the start time in ms of the sound on the track.
 */
public void addSound(String fic, long sT) {     
    sounds.add(new Sample(fic, sT));
}

/**
 * Delete a sample to the track.
 * @param fic location to the audio file.
 * @param sT set the start time in ms of the sound on the track.
 */
public void deleteSound(String fic, long sT) {      
    int i;
    for ( i = 0; i < sounds.size() &&(
    sounds.get(i).getAudioFile().getName() == fic &&
    sounds.get(i).getStartTime() == sT); ++i) {}
    if (i < sounds.size()) sounds.remove(i);
}

这是示例,在上面的代码中导入。

    public Sample (String fileLocation, long sT) {

try{
    audioFile = new File(fileLocation);
    istream = AudioSystem.getAudioInputStream(audioFile);
    format = istream.getFormat();
    startTime = sT;
    timeLenght = (audioFile.length() / (format.getFrameSize() * format.getFrameRate() )) * 1000;
}
catch (UnsupportedAudioFileException e){
    e.printStackTrace();
       } catch (IOException e) {
    e.printStackTrace();
   }    
    }

在我看来,这不是一个好的Java源代码,原因仅仅是有很多无用的操作可以使用某些特定的Java工具自动进行处理。

显然,您有一个庞大的班级,代表特定专辑的独特曲目; 当然,专辑分为不同的样本。 在对方法进行详细说明后,并附有一些技巧,以改进您的代码:

  • sortTrack()方法用于根据Sample类中定义的特定排序标准对数据进行排序。 在这种情况下,您应该使用一个特定的数据结构,该结构将授予您保留自动排序的所有数据的权限。 我建议使用TreeSet,但是这种数据结构需要其中包含的元素必须实现Comparable接口 这样,在每次插入操作时,您将始终根据定义的标准对数据进行排序(从发布的代码中,每个样本均使用其开始时间进行排序);
  • addSound()方法将指定的Sample实例追加到示例列表中(如果要切换到TreeSet,该方法的实现不会更改);
  • deleteSound()方法尝试从样本列表中删除应该具有特定名称(fic参数)和特定开始时间(sT参数)的样本。 它是如何工作的? 它会一直循环直到找到具有指定样本名称和开始时间的对象,或者到达列表的末尾(注意,for循环为空,因为您需要做的就是继续进行并增加i ,直到条件之一为假)。 但是这种事情确实很糟糕,因为您应该使用提供ArrayList类的remove方法(或者通常是Collection接口的子类的每个类)。在这种情况下,您需要修复equals()方法Sample类,以定义两个Sample何时相等。

暂无
暂无

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

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