Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in MIKMIDIClock::MIKMIDIClockSecondsPerMIDITimeStamp() - Fix inside #199

Closed
rrc2soft opened this issue Jun 8, 2017 · 2 comments
Closed
Assignees
Labels
Milestone

Comments

@rrc2soft
Copy link

rrc2soft commented Jun 8, 2017

Hi everyone,

I found an issue in MIKMIDIClockSecondsPerMIDITimeStamp: in a real iOS environment (iPad mini 4, iOS 10.3), secondsPerMIDITimeStamp was not accurate, and the clock would drift ~0.60s every 60s (in a 120BPM sequence). After some debugging, and thanks to this entry in StackOverflow (https://stackoverflow.com/questions/675626/coreaudio-audiotimestamp-mhosttime-clock-frequency), I found the issue: timeBaseInfoData.numer is a Float32, but the result is a double. So, the division by 1.0e9 would be inaccurate, and the clock would drift. Yet the solution is, as Robert pointed out:

'By casting the first number to double, GCC automatically casts the second number to double as well and the result will also be double'

The fixed code is below:

Float64 MIKMIDIClockSecondsPerMIDITimeStamp()
{
	static Float64 secondsPerMIDITimeStamp;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		mach_timebase_info_data_t timeBaseInfoData;
		mach_timebase_info(&timeBaseInfoData);
		secondsPerMIDITimeStamp = ((double)timeBaseInfoData.numer / timeBaseInfoData.denom) / 1.0e9;
	});
	return secondsPerMIDITimeStamp;
}
@rrc2soft
Copy link
Author

rrc2soft commented Jun 8, 2017

I realize now that probably that (double) conversion should be (Float64) :-)

@armadsen armadsen self-assigned this Aug 22, 2017
@armadsen armadsen added the bug label Aug 22, 2017
@armadsen armadsen added this to the 1.6.2 milestone Aug 22, 2017
armadsen added a commit that referenced this issue Aug 27, 2017
@armadsen
Copy link
Member

Thanks for reporting this. It doesn't show up on x86_64 (ie. macOS and the iOS simulator) where .numer and .denom are both 1, and integer division is fine. On arm64, they're 125, and 3, respectively, so integer division indeed produces an imprecise/incorrect result. I've pushed a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants