我正在开发一个小程序,将抖动的传入 MIDI 时钟“转换”为稳定的节拍。抖动的 MIDI 时钟会产生可怕的颤音。
这个想法是“收听”传入的 MIDI 时钟,并在确定速度后,将稳定的 MIDI 时钟发送到虚拟 IAC 设备,这样我就可以将我的 DAW(NI 机器)同步到同一个 IAC 设备。传入的 MIDI 来自 Korg Electribe,所以我被困在 MIDI 电缆上。我正在使用 Komplete Audio 6 接收 MIDI 时钟。
第一部分(听并确定速度)已经介绍过了,但现在我必须为那个速度生成一个稳定的时钟。
我尝试使用高优先级线程来发送 midi 时钟。下面的测试程序给了我 119.8 和 120.2 之间的速度抖动。
我在这个例程中做错了什么还是应该使用其他策略?非常感谢任何帮助。
问候,罗伯
dispatch_source_t CreateDispatchTimer(uint64_t interval,
uint64_t leeway,
dispatch_queue_t queue,
dispatch_block_t block)
{
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, queue);
if (timer)
{
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, interval, leeway);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);
}
return timer;
}
- (void) testTimer{
IAC = MIDIGetDestination(0); // 0 is the MAC IAC device on my system
MIDIPacket pkt;
MIDIPacketList l;
pkt.timeStamp = 0;
pkt.length = 1;
pkt.data[0] = 0xF8;
l.numPackets = 1;
l.packet[0] = pkt;
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
aTimer = CreateDispatchTimer(20833 * NSEC_PER_SEC/1000000, // 20.8333 ms will give me tempo 120
0,
q,
^{
MIDISend(outPort, IAC, &l ); // outport was already created outside this code
});
更新
想出了一个有效的策略。下面的代码在我的系统上给出了完美的结果。我已经在乐队的演出中使用过它,效果很好。
我的解决方案是:
- 发送具有 24 个时钟的数据包列表而不是发送单个时钟
- 仅在第一个时钟中使用当前 machtime 设置时间戳,然后继续使用计算的滴答数增加时间戳。(当packetlist的每个第一个包都设置了当前的machtime时,结果不稳定!)
- 将计算的刻度四舍五入到微秒!这让我感到惊讶,因为您会认为.. 精度越高效果越好.. 但是当我使用纳秒精度时,我的 DAW (NI Maschine) 屏幕上的速度是稳定的,但仍然有“抖动”的声音。不知道这是否与CoreMidi,虚拟IAC设备或NI Machine有关。
速度变化时仍然存在一些问题。速度变化发出的声音并不平滑......但是基本问题(如何使用 CoreMidi 发送稳定的时钟)得到了解决。
dispatch_source_t CreateDispatchTimer(uint64_t interval,
uint64_t leeway,
dispatch_queue_t queue,
dispatch_block_t block)
{
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, queue);
if (timer)
{
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, interval, leeway);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);
}
return timer;
}
- (void) timerTempo:(double) tempo{
if (ignoreTempoChange) return; // ignoreTempoChange is set when a MIDI start is received
_inTempo = tempo;
if (aTimer)
{
nTicks = ticks_per_second / (tempo * 24 / 60); //number of ticks for one beat.
nTicks = nTicks/1000;
nTicks = nTicks*1000;
dispatch_source_set_timer(aTimer, DISPATCH_TIME_NOW, nTicks * 24, 0);
}
}
- (void) startTimer:(double) tempo{
_inTempo = tempo;
mach_timebase_info_data_t mach_timebase_info_data_t;
mach_timebase_info( &mach_timebase_info_data_t ); //denum and numer are always 1 on my system???
ticks_per_second = mach_timebase_info_data_t.denom * NSEC_PER_SEC / mach_timebase_info_data_t.numer;
nTicks = ticks_per_second / (tempo * 24 / 60); //number of ticks for one beat.
nTicks = nTicks/1000;
nTicks = nTicks*1000; // rounding the nTicks to microseconds was THE trick to get a rock solid clock in NI Maschine
clocktTimeStamp = mach_absolute_time();
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
aTimer = CreateDispatchTimer(nTicks * 24,
0,
q,
^{
const int packetListSize = sizeof(uint32)+ (25 *sizeof(MIDIPacket));
MIDIPacketList *packetList= malloc(packetListSize);
MIDIPacket *packet = MIDIPacketListInit( packetList );
Byte clock = 0xF8;
for( int i = 0; i < 24; i++ )
{
packet = MIDIPacketListAdd( packetList, packetListSize, packet, clocktTimeStamp, 1, &clock );
clocktTimeStamp+= nTicks;
}
MIDISend(outPort, IAC, packetList );
free(packetList);
});
timerStarted = true;
}
更新
在对节奏变化的响应方面取得了一些进展。
- 当 MIDITimeStamp 的固定值远超 mach_absolute_time() 时停止发送数据包列表
- 发送只有 8 个时钟而不是 24 个时钟的较小数据包列表
在我的系统上,速度变化是平稳发送的,延迟时间最短,但是在多次改变速度后,发送 MIDI 设备的节拍和监听生成的 MIDIclock 的 DAW 可能会发生小偏移。
在现场表演中,这意味着使用发送 MIDI 设备的“鼓手”必须在他的设备上执行停止和启动,以使声音再次同步。对于我的乐队来说,这不是问题。突然停止和启动效果很棒!
下面是优化的代码。我把它包在一个类中以便于使用。如果您能看到改进,请回复。
//
// MidiClockGenerator.h
// MoxxxClock
//
// Created by Rob Keeris on 17/05/15.
// Copyright (c) 2015 Connector. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreMIDI/CoreMIDI.h>
@interface MidiClockGenerator : NSObject
@property MIDIPortRef outPort;
@property MIDIEndpointRef destination;
@property (nonatomic, setter=setBPM:) float BPM;
@property (readonly) bool started;
@property int listSize;
- (id) initWithBPM:(float)BPM outPort:(MIDIPortRef) outPort destination:(MIDIEndpointRef) destination;
- (void) start;
- (void) stop;
@end
//
// MidiClockGenerator.m
// MoxxxClock
//
// Created by Rob Keeris on 17/05/15.
// Copyright (c) 2015 Connector. All rights reserved.
//
#import "MidiClockGenerator.h"
#import <CoreMIDI/CoreMIDI.h>
@implementation MidiClockGenerator
dispatch_source_t timer;
uint64_t nTicks,bTicks,ticks_per_second;
MIDITimeStamp clockTimeStamp;
bool timerStarted;
dispatch_source_t CreateDispatchTimer(uint64_t interval,
uint64_t leeway,
dispatch_queue_t queue,
dispatch_block_t block)
{
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, queue);
if (timer)
{
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, interval, leeway);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);
}
return timer;
}
- (void) initTemo{
nTicks = ticks_per_second / (_BPM * 24 / 60); // number of ticks between clock's.
nTicks = nTicks/100; // round the nTicks to avoid 'jitter' in the sound
nTicks = nTicks*100;
bTicks = nTicks * _listSize;
}
- (void) setBPM:(float)BPM{
_BPM = BPM;
// calculate new values for nTicks and bTicks
[self initTemo];
// Set the interval of the timer to the new calculated bTicks
if (timer)
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, bTicks, 0);
}
- (void) startTimer{
[self initTemo];
clockTimeStamp = mach_absolute_time();
// default queu is good enough on my iMac.
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
timer = CreateDispatchTimer(bTicks,
0,
q,
^{
// avoid to much blocks send in the future to avoid latency in tempo changes
// just skip on block when the clockTimeStamp is ahead of the mach_absolute_time()
MIDITimeStamp now = mach_absolute_time();
if (clockTimeStamp > now && (clockTimeStamp - now)/(bTicks) > 0) return;
// setup packetlist
Byte clock = 0xF8;
uint32 packetListSize = sizeof(uint32)+ (_listSize *sizeof(MIDIPacket));
MIDIPacketList *packetList= malloc((uint32)packetListSize);
MIDIPacket *packet = MIDIPacketListInit( packetList );
// Set the time stamps
for( int i = 0; i < _listSize; i++ )
{
packet = MIDIPacketListAdd( packetList, packetListSize, packet, clockTimeStamp, 1, &clock );
clockTimeStamp+= nTicks;
}
MIDISend(_outPort, _destination, packetList );
free(packetList);
});
_started = true;
}
- (id) init{
return [self initWithBPM:0 outPort:0 destination:0];
}
- (id) initWithBPM:(float)BPM outPort:(MIDIPortRef) outPort destination:(MIDIEndpointRef) destination{
self = [super init];
if (self) {
_listSize = 4; // nr of clock's send in each packetlist. Should be big enough to deal with instability of the timer
// higher values will slowdown responce to tempochanges
_outPort = outPort;
_destination = destination;
_BPM = BPM;
// find out how many machtime ticks are in one second
mach_timebase_info_data_t mach_timebase_info_data_t;
mach_timebase_info( &mach_timebase_info_data_t ); //denum and numer are always 1 on my system???
ticks_per_second = mach_timebase_info_data_t.denom * NSEC_PER_SEC / mach_timebase_info_data_t.numer;
[self start];
}
return self;
}
- (void) start{
if (_BPM > 0 && _outPort && _destination){
if (!timer) {
[self startTimer];
} else {
if (!_started) {
dispatch_resume(timer);
_started = true;
}
}
}
}
- (void) stop{
if (_started && timer){
dispatch_suspend(timer);
_started = false;
}
}
@end