简体   繁体   English

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

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

here is my problem, someone gave me a function, which, if I understand it well, put some sound samples into an arraylist. 这是我的问题,有人给了我一个功能,如果我对它很了解的话,可以将一些声音样本放到arraylist中。

I'd like to create a .wav file with this audio track, and I really don't know how to do so. 我想用此音轨创建一个.wav文件,但我真的不知道该怎么做。

Here is the code, because maybe I just don't understand it at all... 这是代码,因为也许我根本不了解它。

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);
}

Here is sample, imported in the code above. 这是示例,在上面的代码中导入。

    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();
   }    
    }

In my opinion, this is not a good Java source code simply because there are a lot of useless operation that could be automatized using some specific java tools. 在我看来,这不是一个好的Java源代码,原因仅仅是有很多无用的操作可以使用某些特定的Java工具自动进行处理。

Apparently, you have a huge class which represents a unique track of a specific album; 显然,您有一个庞大的班级,代表特定专辑的独特曲目; the album is, off course, subdivided in distinct samples. 当然,专辑分为不同的样本。 Follows a detailed description of the methods with some tips in order to improve your code: 在对方法进行详细说明后,并附有一些技巧,以改进您的代码:

  • sortTrack() method is used in order to sort your data according a specific ordering criteria defined in the Sample class. sortTrack()方法用于根据Sample类中定义的特定排序标准对数据进行排序。 In this case you should use a specific data structure that grants to you to mantain all the data automatically sorted. 在这种情况下,您应该使用一个特定的数据结构,该结构将授予您保留自动排序的所有数据的权限。 I would suggest the TreeSet but this kind of data structure needs that the elements, which are contained in it, must implement the Comparable interface . 我建议使用TreeSet,但是这种数据结构需要其中包含的元素必须实现Comparable接口 In this way, at each insert operation, you will always have your data sorted according to the criteria that you have defined (from the code that you've posted, each sample is ordered using its start time); 这样,在每次插入操作时,您将始终根据定义的标准对数据进行排序(从发布的代码中,每个样本均使用其开始时间进行排序);
  • addSound() method appends the specified Sample instance in your sample's list (if you want to switch to the TreeSet, the method's implementation doesn't change); addSound()方法将指定的Sample实例追加到示例列表中(如果要切换到TreeSet,该方法的实现不会更改);
  • deleteSound() method tries to remove from your sample's list a sample which should have a specific name (fic parameter) and a specific start time (sT parameter). deleteSound()方法尝试从样本列表中删除应该具有特定名称(fic参数)和特定开始时间(sT参数)的样本。 How does it work? 它是如何工作的? It loops for until an object with the specified sample's name and start time is found or if you arrive to the end of the list (pay attention, the for loop is empty because all you need to do is to go ahead and increment the i , until one of the condition is false). 它会一直循环直到找到具有指定样本名称和开始时间的对象,或者到达列表的末尾(注意,for循环为空,因为您需要做的就是继续进行并增加i ,直到条件之一为假)。 But this kind of stuff is really awful because you should use, instead, the remove method which provides the ArrayList class (or in general each class which is subclass of the Collection interface . In that case you need to fix the equals() method in the Sample class in order to define when two Sample are equal. 但是这种事情确实很糟糕,因为您应该使用提供ArrayList类的remove方法(或者通常是Collection接口的子类的每个类)。在这种情况下,您需要修复equals()方法Sample类,以定义两个Sample何时相等。

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

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