Rob*_*ris 5 macos midi-interface coremidi
我正在开发一个小程序,用于将抖动的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
});
Run Code Online (Sandbox Code Playgroud)
更新
找出可行的策略。下面的代码在我的系统上给出了完美的结果。我已经在乐队的演出中使用过,效果很好。
我的解决方案是:
速度变化时还存在一些问题。速度变化不能顺利发出...但基本问题(如何使用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;
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
在对速度变化的响应上取得了一些进展。
在我的系统上,速度变化可以平稳地发送,并且延迟最小,但是在多次改变速度后,发送MIDI设备的节拍和DAW监听生成的MIDIclock时可能会出现很小的偏移。
在现场表演中,这意味着使用发送Midi设备的“鼓手”必须在其to设备上执行停止和启动操作才能再次使声音同步。对于我的乐队来说,这不是问题。突然停止和开始效果很好!
下面是优化的代码。我将其包装在一个类中,以方便使用。如果可以看到改进,请回复。
//
// 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
Run Code Online (Sandbox Code Playgroud)
//
// 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
891 次 |
| 最近记录: |