我想抓取一个 MIDI 文件,读取它,然后将数据存储在某种数据结构中。使用这个站点,我找到了一种读取文件的简单方法,它非常有用:
现在我需要找出一种方法来获取并存储该输出。 HashMap 似乎并不理想,因为键需要是唯一的,而对象类型的列表似乎不太好。关于我的最佳选择可能是什么的任何想法。我想我可能会把它输出到文本或 csv 中……有什么想法吗?
更新:关于我已有的更多细节。
这是我得到的输出(通过 System.out.println):
@0 Channel: 1 Note on, E5 key=76 velocity: 127
@192 Channel: 1 Note off, E5 key=76 velocity: 64
@192 Channel: 1 Note on, D#5 key=75 velocity: 127
@384 Channel: 1 Note off, D#5 key=75 velocity: 64
@384 Channel: 1 Note on, E5 key=76 velocity: 127
现在我只需要找到存储这些信息的最佳方法。我可能应该明确说明“为什么”我也想这样做。我正在与另一位开发人员合作,他将获取这些数据并使用 Batik(我对此一无所知)将其显示在屏幕上。
感谢所有回复...今晚我会仔细查看每个回复...
最佳答案
阅读 MIDI 文件规范,我认为您可以开始创建类似的东西
public class MIDIFile {
enum FileFormat {
single_track,
syncronous_multiple_tracks,
assyncronous_multiple_tracks;
}
FileFormat file_format;
short numberOfTracks;
short deltaTimeTicks;
//Create Class for tracks, events, put some collection for storing the tracks,
//some collection for storing the events inside the tracks, etc
//Collection<Integer, MIDITrack> the type of Collection depends on application
}
public class MIDITrack {
int length;
//Collection<MIDIEvent> the type of Collection depends on application
}
public class MIDIEvent {
int delta_time;
int event_type; //Use of enum or final variables is interesting
int key;
int velocity;
}
如果你只想存储 MIDI 消息(而不是 MIDI 文件),你可以为消息做一个类
public class MIDIEvent {
int delta_time;
int channel;
int event_type; //Use of enum or final variables is interesting
//two bytes, interpret according the message type
byte byte0;
byte byte1;
//or more memory consuming
byte key;
byte pressure;
byte controller;
short bend;
}
您用来存储的 Collection 类型将是特定于应用程序的,您希望如何访问列表的元素等等。
如果您只想将 MIDIMessages 插入集合中,然后从头到尾读取,您可以使用 LinkedList(这是 List 的一个实现)。 但是如果你想通过索引修改消息和访问元素,你会想要使用 ArrayList(这也是 List 的一个实现)。
来自 http://faydoc.tripod.com/formats/mid.htm 的 MIDI 文件结构信息
关于java - 将 MIDI 转换为 Java 数据结构(List、Hash Map、???),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18605466/