Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.0KB

  1. /*
  2. * Carla JACK API for external applications
  3. * Copyright (C) 2016-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  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.
  9. *
  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.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "libjack.hpp"
  18. CARLA_BACKEND_USE_NAMESPACE
  19. // --------------------------------------------------------------------------------------------------------------------
  20. CARLA_PLUGIN_EXPORT
  21. jack_nframes_t jack_frames_since_cycle_start(const jack_client_t* client)
  22. {
  23. const JackClientState* const jclient = (const JackClientState*)client;
  24. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
  25. // TODO
  26. return 0;
  27. }
  28. CARLA_PLUGIN_EXPORT
  29. jack_nframes_t jack_frame_time(const jack_client_t* client)
  30. {
  31. const JackClientState* const jclient = (const JackClientState*)client;
  32. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
  33. // FIXME this is wrong
  34. return static_cast<jack_nframes_t>(jclient->server.position.usecs);
  35. }
  36. CARLA_PLUGIN_EXPORT
  37. jack_nframes_t jack_last_frame_time(const jack_client_t* client)
  38. {
  39. const JackClientState* const jclient = (const JackClientState*)client;
  40. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
  41. return static_cast<jack_nframes_t>(jclient->server.monotonic_frame);
  42. }
  43. CARLA_PLUGIN_EXPORT
  44. int jack_get_cycle_times(const jack_client_t *client,
  45. jack_nframes_t *current_frames,
  46. jack_time_t *current_usecs,
  47. jack_time_t *next_usecs,
  48. float *period_usecs)
  49. {
  50. const JackClientState* const jclient = (const JackClientState*)client;
  51. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 1);
  52. /* current_frames: the frame time counter at the start of the current cycle, same as jack_last_frame_time().
  53. * current_usecs: the microseconds time at the start of the current cycle.
  54. * next_usecs: the microseconds time of the start of the next next cycle as computed by the DLL.
  55. * period_usecs: the current best estimate of the period time in microseconds.
  56. */
  57. const double _period_usecs = static_cast<double>(jclient->server.bufferSize) / jclient->server.sampleRate;
  58. *current_frames = jclient->server.monotonic_frame;
  59. *current_usecs = jclient->server.position.usecs;
  60. *next_usecs = jclient->server.position.usecs + static_cast<jack_time_t>(_period_usecs + 0.5);
  61. *period_usecs = static_cast<float>(_period_usecs);
  62. return 0;
  63. }
  64. // --------------------------------------------------------------------------------------------------------------------
  65. CARLA_PLUGIN_EXPORT
  66. jack_time_t jack_frames_to_time(const jack_client_t* client, jack_nframes_t frames)
  67. {
  68. const JackClientState* const jclient = (const JackClientState*)client;
  69. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
  70. return static_cast<jack_time_t>(static_cast<double>(frames) / jclient->server.sampleRate * 1000000.0);
  71. }
  72. CARLA_PLUGIN_EXPORT
  73. jack_nframes_t jack_time_to_frames(const jack_client_t* client, jack_time_t time)
  74. {
  75. const JackClientState* const jclient = (const JackClientState*)client;
  76. CARLA_SAFE_ASSERT_RETURN(jclient != nullptr, 0);
  77. return static_cast<jack_nframes_t>(static_cast<double>(time) / 1000000.0 * jclient->server.sampleRate);
  78. }
  79. CARLA_PLUGIN_EXPORT
  80. jack_time_t jack_get_time(void)
  81. {
  82. timespec t;
  83. #ifdef CLOCK_MONOTONIC_RAW
  84. clock_gettime(CLOCK_MONOTONIC_RAW, &t);
  85. #else
  86. clock_gettime(CLOCK_MONOTONIC, &t);
  87. #endif
  88. return static_cast<jack_time_t>(t.tv_sec * 1000000 + t.tv_nsec / 1000);
  89. }
  90. // --------------------------------------------------------------------------------------------------------------------