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

scsynth: use CoreAudio host time to calculate time of day offset #5298

Merged

Conversation

lilyinstarlight
Copy link
Contributor

Purpose and Motivation

See discussion in #5168

In macOS Big Sur, the clock provided by high_resolution_clock (using mach_absolute_time) drifts apart from the host time reported by CoreAudio. This is most apparent after a computer running Big Sur is in sleep mode for a bit, causing sounds from SuperCollider to be played after their scheduled time roughly by the amount of time the computer slept.

This change makes it so that the time of day offset is calculated against the CoreAudio host time (instead of high_resolution_clock time) since that offset is later used against the CoreAudio host time to get OSC time.

This commit effectively reverts the changes in 95a6604.

I read through the wiki page for making pull requests (and didn't know what I could make in terms of a regression test for this change) but let me know if there is anything else I should do!

I know there was still discussion in #5168 on clocks and timing in regards to this change and I will defer to the maintainers on whether this is a proper solution or not.

Types of changes

  • Bug fix

To-do list

  • Code is tested
  • All tests are passing (running on macOS)
  • Updated documentation
  • This PR is ready for review

In macOS Big Sur, the clock provided by `high_resolution_clock` (using
`mach_absolute_time`) drifts apart from the host time reported by
CoreAudio. This is most apparent after a computer running Big Sur is
in sleep mode for a bit, causing sounds from SuperCollider to be played
after their scheduled time roughly by the amount of time the computer
slept.

This change makes it so that the time of day offset is calculated
against the CoreAudio host time (instead of `high_resolution_clock`
time) since that offset is later used against the CoreAudio host
time to get OSC time.

This commit effectively reverts the changes in 95a6604.

See discussion in supercollider#5168.
@lilyinstarlight lilyinstarlight force-pushed the topic/coreaudio-host-time-fix branch from 3f60a8f to 1a74fd5 Compare December 23, 2020 22:07
Copy link
Member

@dyfer dyfer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lilyinstarlight

Aside from fixing the issue on 11.1, I have tested this on macOS 10.13 and could not see any regressions (details below). I think this is the correct solution.
However, I'd prefer if someone else looked this over before merging (@muellmusik / @joshpar / @brianlheim ?)
As @lilyinstarlight mentioned, full discussion is in #5168, some relevant comments: #5168 (comment), #5168 (comment)

To test this, I checked whether bundle time offset is consistent after running the server for some time.
I started two servers, one from a regular develop build, one from this PR, and was scheduling them with short sounds simultaneously, playing to left and right channel, respectively. I've let them run for 2+hrs and they did behave exactly the same after these two hours. (the scheduling is not sample accurate, as the server started at different times and I suspect that their audio callbacks are not aligned; either way the sound result was the same whether I was running two copies of the regular server, or one regular server and one from this PR, and was also the same at the beginning of the test vs 2 hrs later)

~local = Server.default;
~local.boot;
~local.makeGui;
~local.meter;

~other = Server(\testing, NetAddr("localhost", 57112));
Server.program_("/path/to/server/from/this/pr/SuperCollider.app/Contents/Resources/scsynth"); //run this to use a different executable for the second server
~other.boot;
~other.makeGui;
~other.meter;


(
SynthDef(\impulse, {|out=0, amp=1|
	Out.ar(out, WhiteNoise.ar(amp) * Env.perc(0, 0.01).ar(Done.freeSelf))
}).add;
)

//test
~local.sendBundle(0.1, [\s_new, \impulse, -1, 0, 1, \amp, 0.125, \out, 0]); // regular server: left channel
~other.sendBundle(0.1, [\s_new, \impulse, -1, 0, 1, \amp, 0.125, \out, 1]); // PR test server: right channel


(
~routine = Routine.run({
	loop{
		~local.sendBundle(0.1, [\s_new, \impulse, -1, 0, 1, \amp, 0.125, \out, 0]);
		~other.sendBundle(0.1, [\s_new, \impulse, -1, 0, 1, \amp, 0.125, \out, 1]);
		1.wait;
	}
})
)


~local.dumpOSC(1);
~other.dumpOSC(1);

(
~routine.stop;
~local.quit;
~other.quit;
)

Copy link
Contributor

@mossheim mossheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@mossheim mossheim merged commit 6f0b4eb into supercollider:develop Dec 25, 2020
@lilyinstarlight lilyinstarlight deleted the topic/coreaudio-host-time-fix branch December 26, 2020 00:15
This was referenced Nov 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants