Fix crash with clap plugins without MIDI input
[carla.git] / source / libjack / libjack_time.cpp
blobc5172721ec49fa9db2764f09ec2d87b7044fdf97
1 /*
2 * Carla JACK API for external applications
3 * Copyright (C) 2016-2022 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "libjack.hpp"
20 CARLA_BACKEND_USE_NAMESPACE
22 // --------------------------------------------------------------------------------------------------------------------
24 CARLA_PLUGIN_EXPORT
25 jack_nframes_t jack_frames_since_cycle_start(const jack_client_t* client)
27 const JackClientState* const jclient = (const JackClientState*)client;
28 CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
30 // TODO
31 return 0;
34 CARLA_PLUGIN_EXPORT
35 jack_nframes_t jack_frame_time(const jack_client_t* client)
37 const JackClientState* const jclient = (const JackClientState*)client;
38 CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
40 // FIXME this is wrong
41 return static_cast<jack_nframes_t>(jclient->server.position.usecs);
44 CARLA_PLUGIN_EXPORT
45 jack_nframes_t jack_last_frame_time(const jack_client_t* client)
47 const JackClientState* const jclient = (const JackClientState*)client;
48 CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
50 return static_cast<jack_nframes_t>(jclient->server.monotonic_frame);
53 CARLA_PLUGIN_EXPORT
54 int jack_get_cycle_times(const jack_client_t *client,
55 jack_nframes_t *current_frames,
56 jack_time_t *current_usecs,
57 jack_time_t *next_usecs,
58 float *period_usecs)
60 const JackClientState* const jclient = (const JackClientState*)client;
61 CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 1);
63 /* current_frames: the frame time counter at the start of the current cycle, same as jack_last_frame_time().
64 * current_usecs: the microseconds time at the start of the current cycle.
65 * next_usecs: the microseconds time of the start of the next next cycle as computed by the DLL.
66 * period_usecs: the current best estimate of the period time in microseconds.
68 const double _period_usecs = static_cast<double>(jclient->server.bufferSize) / jclient->server.sampleRate;
70 *current_frames = jclient->server.monotonic_frame;
71 *current_usecs = jclient->server.position.usecs;
72 *next_usecs = jclient->server.position.usecs + static_cast<jack_time_t>(_period_usecs + 0.5);
73 *period_usecs = static_cast<float>(_period_usecs);
74 return 0;
77 // --------------------------------------------------------------------------------------------------------------------
79 CARLA_PLUGIN_EXPORT
80 jack_time_t jack_frames_to_time(const jack_client_t* client, jack_nframes_t frames)
82 const JackClientState* const jclient = (const JackClientState*)client;
83 CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
85 return static_cast<jack_time_t>(static_cast<double>(frames) / jclient->server.sampleRate * 1000000.0);
88 CARLA_PLUGIN_EXPORT
89 jack_nframes_t jack_time_to_frames(const jack_client_t* client, jack_time_t time)
91 const JackClientState* const jclient = (const JackClientState*)client;
92 CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
94 return static_cast<jack_nframes_t>(static_cast<double>(time) / 1000000.0 * jclient->server.sampleRate);
97 CARLA_PLUGIN_EXPORT
98 jack_time_t jack_get_time(void)
100 timespec t;
101 #ifdef CLOCK_MONOTONIC_RAW
102 clock_gettime(CLOCK_MONOTONIC_RAW, &t);
103 #else
104 clock_gettime(CLOCK_MONOTONIC, &t);
105 #endif
106 return static_cast<jack_time_t>(t.tv_sec * 1000000 + t.tv_nsec / 1000);
109 // --------------------------------------------------------------------------------------------------------------------