Permalink
Browse files

Add a Clock for Linux platform

ALSA drivers provide the playback time of one sample using the
REALTIME_CLOCK referencial. So Link should use it as well.
  • Loading branch information...
1 parent 35b403a commit 3113fd732da65d9f8eb34c6465988ea8bfbe4f57 @abique abique committed Feb 2, 2017
Showing with 75 additions and 2 deletions.
  1. +1 −0 include/CMakeLists.txt
  2. +2 −2 include/ableton/platforms/Config.hpp
  3. +72 −0 include/ableton/platforms/linux/Clock.hpp
View
@@ -95,6 +95,7 @@ if(UNIX)
set(link_platform_HEADERS
${link_platform_HEADERS}
${link_platform_DIR}/linux/Linux.hpp
+ ${link_platform_DIR}/linux/Clock.hpp
${link_platform_DIR}/stl/Clock.hpp
)
endif()
@@ -32,8 +32,8 @@
#include <ableton/platforms/posix/ScanIpIfAddrs.hpp>
#elif LINK_PLATFORM_LINUX
#include <ableton/platforms/asio/Context.hpp>
+#include <ableton/platforms/linux/Clock.hpp>
#include <ableton/platforms/posix/ScanIpIfAddrs.hpp>
-#include <ableton/platforms/stl/Clock.hpp>
#endif
namespace ableton
@@ -54,7 +54,7 @@ using IoContext =
platforms::asio::Context<platforms::posix::ScanIpIfAddrs, util::NullLog>;
#elif LINK_PLATFORM_LINUX
-using Clock = platforms::stl::Clock;
+using Clock = platforms::linux::ClockMonotonic;
using IoContext =
platforms::asio::Context<platforms::posix::ScanIpIfAddrs, util::NullLog>;
#endif
@@ -0,0 +1,72 @@
+/* Copyright 2016, Ableton AG, Berlin. All rights reserved.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * If you would like to incorporate Link into a proprietary software application,
+ * please contact <link-devs@ableton.com>.
+ */
+
+#pragma once
+
+#include <chrono>
+#include <cmath>
+#include <ctime>
+
+namespace ableton
+{
+namespace platforms
+{
+
+#ifdef linux
+#undef linux
+#endif
+
+namespace linux
+{
+
+template <clockid_t CLOCK>
+struct Clock
+{
+ using Ticks = std::uint64_t;
+ using Micros = std::chrono::microseconds;
+
+ Micros ticksToMicros(const Ticks ticks) const
+ {
+ return Micros(ticks / 1000ULL);
+ }
+
+ Ticks microsToTicks(const Micros micros) const
+ {
+ return micros.count() * 1000ULL;
+ }
+
+ Ticks ticks() const
+ {
+ ::timespec ts;
+ ::clock_gettime(CLOCK, &ts);
+ return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
+ }
+
+ std::chrono::microseconds micros() const
+ {
+ return ticksToMicros(ticks());
+ }
+};
+
+using ClockRealtime = Clock<CLOCK_REALTIME>;
+using ClockMonotonic = Clock<CLOCK_MONOTONIC>;
+
+} // namespace linux
+} // namespace platforms
+} // namespace ableton

0 comments on commit 3113fd7

Please sign in to comment.