gpt4 book ai didi

java - MIDI Java 生成困惑的声音

转载 作者:行者123 更新时间:2023-11-30 11:14:35 27 4
gpt4 key购买 nike

我一直在一个项目中工作,我需要在 Java 中操作 MIDI 文件中的每个乐器。然后我决定从序列中的每条轨道获取每个 MIDI 事件并将其发送到接收器。之后,线程等待每个滴答持续的时间,然后在下一个滴答中再次执行。问题是:乐器的声音以及它们的顺序变得非常困惑。我也试着单独执行每首轨道,但它仍然一团糟!代码:

    Sequence    sequence        = MidiSystem.getSequence(new File(source));
Synthesizer synth = MidiSystem.getSynthesizer();

//Gets a MidiMessage and send it to Synthesizer
Receiver rcv = synth.getReceiver();

//Contains all tracks and events from MIDI file
Track[] tracks = sequence.getTracks();
synth.open();

//If there are tracks
if(tracks != null)
{
//Verify the division type of the sequence (PPQ, SMPT)
if(sequence.getDivisionType() == Sequence.PPQ)
{
int ppq = sequence.getResolution();
//Do the math to get the time (in miliseconds) each tick takes
long tickTime = TicksToMiliseconds(BPM,ppq);
//Returns the number of ticks from the longest track
int longestTrackTicks = LongestTrackTicks(tracks);

//Each iteration sends a new message to 'receiver'
for(int tick = 0; tick < maiorTick ; tick++)
{
//Iteration of each track
for(int trackNumber = 0; trackNumber < tracks.length; trackNumber++)
{
//If the number of ticks from a track isn't already finished
//continue
if(tick < tracks[trackNumber].size())
{
MidiEvent ev = tracks[trackNumber].get(tick);
rcv.send(ev.getMessage(),-1);
}
}
Thread.sleep(tickTime);
}

}
}
synth.close();

最佳答案

正如 ntabee 所说,Track.get(n) 返回轨道中的第 n 个事件;要按时间获取事件,您必须手动比较事件的时间。

此外,Thread.sleep() 不是很精确,可以等待比预期更长的时间。这些错误会累积起来。


要实时更改 MIDI 消息,请告诉音序器播放您自己的 Receiver,然后对事件做任何您想做的事并将它们传递给“真正的”Receiver.

关于java - MIDI Java 生成困惑的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25859950/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com