我正在尝试制作一个能够充当其他设备主时钟的音序器。
我设法让同步计时器启动并运行并用它控制DAW。 但是,节奏非常不稳定,并且大量摇摆+ - 1 BPM。
我最初在我的应用程序中使用了标准的24 PPQ,但很快就将时间质量降低了,所以我尝试了96 PPQ,这样的速度比它应该快了几倍。
我正在使用一个计时器,方法scheduleAtFixedRate将计时时钟消息添加到输出队列,如下所示:
/**
* Used every time the tempo is changed.
*
* Adds a Timer to a list. When a new Timer is added, the previous one is removed.
* Timing clock messages are added to the output queue at a rate set by the current
* Timer in the List.
*/
private void setNewTempoTimer()
{
Log.i(DEBUG_TAG, "set new tempo timer.");
if(!timerList.isEmpty())
{
timerList.getFirst().cancel();
timerList.removeFirst();
}
final Timer timer = new Timer();
timerList.add(timer);
/**
* The shortest time interval in MIDI
*/
long tick = _sixteenthNote / 6;
/**
* Starts adding messages to the output queue
*/
timer.scheduleAtFixedRate(
new TimerTask()
{
@Override
public void run()
{
if(_running)
{
setNewTimingMessage();
}
}
}, 0, tick);
}
速度计算如下:
/**
* Calculates the length of a semiquaver in the sequencer
* @return the time of a semiquaver in milliseconds
*/
private void calculateTempo()
{
Log.i(DEBUG_TAG , "Calculated tempo.");
/**
* Fjärdedelsnot
*/
_quarterNote = Math.round(((60000/_tempo))*100000)/100000;
/**
* Sextondel
*/
_sixteenthNote = (_quarterNote/4);
_sixteenthNote = Math.round(_sixteenthNote*100000)/100000;
}
新的计时消息设置如下:
/**
* Sets new timing message in the midi output queue
*/
private void setNewTimingMessage()
{
try
{
_midiOut.addMessageToQueue(MIDI_TIMING_CLOCK, 0,0);
}
catch (InvalidMidiDataException e)
{
e.printStackTrace();
}
Log.i(DEBUG_TAG , "Set new message.");
}
请为我照亮。
此致 / M