Discussion:
[Interest] Ultra precise timing in Qt
Nuno Santos
2016-04-23 17:06:38 UTC
Permalink
Hi,

I have an audio app can sends midi to machines. Midi clock will set the machine tempo. When I have an arpeggiator running in the synth I want the machine to have exactly the same tempo. If the tempo is not the same, both will start to get out of sync. In music that is simply not acceptable.

So far I was using QElapsedTimer to measure this. One particular behaviour I observed was, once I started moving the virtual knobs and faders of the synth, it would start to get out of sync with the machine. My conclusion is that somehow, QElapsedTimer is tied to the event loop thus making it slower when other interactions are happening. Does this make sense? I haven’t looked to QElapsedTimer source code, i’m just guessing. Maybe I’m saying silly things.

I search for options and started using mach_absolute_time. The problem seems to be solved now. I have the soft synth running with the arpeggiator enabled sending midi clock to the machine, and they keep their tempo constant, even when the controls are being used or the device is under stress.

Does Qt provide any kind of cpu absolute clock for every platform? I couldn’t find it so far.

Does anyone know the equivalent to mach_absolute_time in Windows and Android (probably the same as linux).

Regards,

Nuno
Thiago Macieira
2016-04-23 17:20:21 UTC
Permalink
Post by Nuno Santos
So far I was using QElapsedTimer to measure this. One particular behaviour I
observed was, once I started moving the virtual knobs and faders of the
synth, it would start to get out of sync with the machine. My conclusion is
that somehow, QElapsedTimer is tied to the event loop thus making it slower
when other interactions are happening. Does this make sense? I haven’t
looked to QElapsedTimer source code, i’m just guessing. Maybe I’m saying
silly things.
Yeah, kinda... Your description above summarises to "the more code I add, the
more work needs to be done".

The use of QElapsedTimer does not have any further impact on the event loop
besides executing more code and is not any different from any other further
addition of code that causes a system call. On some OSes, getting the system
time is a very fast operation (Linux); on others, it might be slow.
Post by Nuno Santos
I search for options and started using mach_absolute_time. The problem seems
to be solved now. I have the soft synth running with the arpeggiator
enabled sending midi clock to the machine, and they keep their tempo
constant, even when the controls are being used or the device is under
stress.
Does Qt provide any kind of cpu absolute clock for every platform? I
couldn’t find it so far.
Yes, it's called QElapsedTimer.
Post by Nuno Santos
Does anyone know the equivalent to mach_absolute_time in Windows and Android
(probably the same as linux).
On Windows, you use GetTickCount64 or QueryPerformanceCounter. On Android and
Linux, use clock_gettime for the CLOCK_MONOTONIC or CLOCK_MONOTONIC_COARSE.

Do you know how I know this? I looked into qelapsedtimer_win.cpp and
qelapsedtimer_unix.cpp.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Nuno Santos
2016-04-23 17:46:14 UTC
Permalink
Alright! Even that I feel completely dumb right now, I’m happy that I don’t need to reinvent the wheel!!! :)

In the mean time, with all of this refactoring I have changes that could probably lead to better timing mechanism.

With this in mind I will do further refactor.

Thanks for your prompt reply.

Regards

Nuno
Post by Thiago Macieira
Post by Nuno Santos
So far I was using QElapsedTimer to measure this. One particular behaviour I
observed was, once I started moving the virtual knobs and faders of the
synth, it would start to get out of sync with the machine. My conclusion is
that somehow, QElapsedTimer is tied to the event loop thus making it slower
when other interactions are happening. Does this make sense? I haven’t
looked to QElapsedTimer source code, i’m just guessing. Maybe I’m saying
silly things.
Yeah, kinda... Your description above summarises to "the more code I add, the
more work needs to be done".
The use of QElapsedTimer does not have any further impact on the event loop
besides executing more code and is not any different from any other further
addition of code that causes a system call. On some OSes, getting the system
time is a very fast operation (Linux); on others, it might be slow.
Post by Nuno Santos
I search for options and started using mach_absolute_time. The problem seems
to be solved now. I have the soft synth running with the arpeggiator
enabled sending midi clock to the machine, and they keep their tempo
constant, even when the controls are being used or the device is under
stress.
Does Qt provide any kind of cpu absolute clock for every platform? I
couldn’t find it so far.
Yes, it's called QElapsedTimer.
Post by Nuno Santos
Does anyone know the equivalent to mach_absolute_time in Windows and Android
(probably the same as linux).
On Windows, you use GetTickCount64 or QueryPerformanceCounter. On Android and
Linux, use clock_gettime for the CLOCK_MONOTONIC or CLOCK_MONOTONIC_COARSE.
Do you know how I know this? I looked into qelapsedtimer_win.cpp and
qelapsedtimer_unix.cpp.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Loading...