Collection of tools useful for audio production
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.

252 lines
7.1KB

  1. /*
  2. * Simple JACK Audio Meter
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #include <QtCore/Qt>
  18. #ifndef Q_COMPILER_LAMBDA
  19. # define nullptr (0)
  20. #endif
  21. #define VERSION "0.8.1"
  22. #include "../jack_utils.hpp"
  23. #include "../widgets/digitalpeakmeter.hpp"
  24. #include <cmath>
  25. #include <QtGui/QApplication>
  26. #include <QtGui/QIcon>
  27. #include <QtGui/QMessageBox>
  28. // -------------------------------
  29. volatile double x_portValue1 = 0.0;
  30. volatile double x_portValue2 = 0.0;
  31. volatile bool x_needReconnect = false;
  32. volatile bool x_quitNow = false;
  33. jack_client_t* jClient = nullptr;
  34. jack_port_t* jPort1 = nullptr;
  35. jack_port_t* jPort2 = nullptr;
  36. bool gIsOutput = true;
  37. // -------------------------------
  38. // JACK callbacks
  39. int process_callback(const jack_nframes_t nframes, void*)
  40. {
  41. float* const jOut1 = (float*)jackbridge_port_get_buffer(jPort1, nframes);
  42. float* const jOut2 = (float*)jackbridge_port_get_buffer(jPort2, nframes);
  43. for (jack_nframes_t i = 0; i < nframes; i++)
  44. {
  45. if (std::abs(jOut1[i]) > x_portValue1)
  46. x_portValue1 = std::abs(jOut1[i]);
  47. if (std::abs(jOut2[i]) > x_portValue2)
  48. x_portValue2 = std::abs(jOut2[i]);
  49. }
  50. return 0;
  51. }
  52. void port_callback(jack_port_id_t, jack_port_id_t, int, void*)
  53. {
  54. x_needReconnect = true;
  55. }
  56. #ifdef HAVE_JACKSESSION
  57. void session_callback(jack_session_event_t* const event, void* const arg)
  58. {
  59. #ifdef Q_OS_LINUX
  60. QString filepath("cadence-jackmeter");
  61. Q_UNUSED(arg);
  62. #else
  63. QString filepath((char*)arg);
  64. #endif
  65. event->command_line = strdup(filepath.toUtf8().constData());
  66. jackbridge_session_reply(jClient, event);
  67. if (event->type == JackSessionSaveAndQuit)
  68. x_quitNow = true;
  69. jackbridge_session_event_free(event);
  70. }
  71. #endif
  72. // -------------------------------
  73. // helpers
  74. void reconnect_ports()
  75. {
  76. x_needReconnect = false;
  77. jack_port_t* const jPlayPort1 = jackbridge_port_by_name(jClient, gIsOutput ? "system:playback_1" : "system:capture_1");
  78. jack_port_t* const jPlayPort2 = jackbridge_port_by_name(jClient, gIsOutput ? "system:playback_2" : "system:capture_2");
  79. std::vector<char*> jPortList1(jackbridge_port_get_all_connections_as_vector(jClient, jPlayPort1));
  80. std::vector<char*> jPortList2(jackbridge_port_get_all_connections_as_vector(jClient, jPlayPort2));
  81. foreach (char* const& thisPortName, jPortList1)
  82. {
  83. jack_port_t* const thisPort = jackbridge_port_by_name(jClient, thisPortName);
  84. if (! (jackbridge_port_is_mine(jClient, thisPort) || jackbridge_port_connected_to(jPort1, thisPortName)))
  85. {
  86. if (gIsOutput)
  87. jackbridge_connect(jClient, thisPortName, "M:in1");
  88. else
  89. jackbridge_connect(jClient, "Mi:in1", thisPortName);
  90. }
  91. free(thisPortName);
  92. }
  93. foreach (char* const& thisPortName, jPortList2)
  94. {
  95. jack_port_t* const thisPort = jackbridge_port_by_name(jClient, thisPortName);
  96. if (! (jackbridge_port_is_mine(jClient, thisPort) || jackbridge_port_connected_to(jPort2, thisPortName)))
  97. {
  98. if (gIsOutput)
  99. jackbridge_connect(jClient, thisPortName, "M:in2");
  100. else
  101. jackbridge_connect(jClient, "Mi:in2", thisPortName);
  102. }
  103. free(thisPortName);
  104. }
  105. jPortList1.clear();
  106. jPortList2.clear();
  107. }
  108. // -------------------------------
  109. // Meter class
  110. class MeterW : public DigitalPeakMeter
  111. {
  112. public:
  113. MeterW() : DigitalPeakMeter(nullptr)
  114. {
  115. setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
  116. setWindowTitle(gIsOutput ? "M" : "Mi");
  117. if (gIsOutput)
  118. setColor(Color::GREEN);
  119. else
  120. setColor(Color::BLUE);
  121. setChannels(2);
  122. setOrientation(VERTICAL);
  123. setSmoothRelease(1);
  124. displayMeter(1, 0.0f);
  125. displayMeter(2, 0.0f);
  126. int refresh = float(jackbridge_get_buffer_size(jClient)) / jackbridge_get_sample_rate(jClient) * 1000;
  127. m_peakTimerId = startTimer(refresh > 50 ? refresh : 50);
  128. }
  129. protected:
  130. void timerEvent(QTimerEvent* event)
  131. {
  132. if (x_quitNow)
  133. {
  134. close();
  135. x_quitNow = false;
  136. return;
  137. }
  138. if (event->timerId() == m_peakTimerId)
  139. {
  140. displayMeter(1, x_portValue1);
  141. displayMeter(2, x_portValue2);
  142. x_portValue1 = 0.0;
  143. x_portValue2 = 0.0;
  144. if (x_needReconnect)
  145. reconnect_ports();
  146. }
  147. QWidget::timerEvent(event);
  148. }
  149. private:
  150. int m_peakTimerId;
  151. };
  152. // -------------------------------
  153. int main(int argc, char* argv[])
  154. {
  155. QApplication app(argc, argv);
  156. app.setApplicationName("JackMeter");
  157. app.setApplicationVersion(VERSION);
  158. app.setOrganizationName("Cadence");
  159. app.setWindowIcon(QIcon(":/scalable/cadence.svg"));
  160. if (app.arguments().contains("-in"))
  161. gIsOutput = false;
  162. // JACK initialization
  163. jack_status_t jStatus;
  164. #ifdef HAVE_JACKSESSION
  165. jack_options_t jOptions = static_cast<jack_options_t>(JackNoStartServer|JackUseExactName|JackSessionID);
  166. #else
  167. jack_options_t jOptions = static_cast<jack_options_t>(JackNoStartServer|JackUseExactName);
  168. #endif
  169. jClient = jackbridge_client_open(gIsOutput ? "M" : "Mi", jOptions, &jStatus);
  170. if (! jClient)
  171. {
  172. std::string errorString(jackbridge_status_get_error_string(jStatus));
  173. QMessageBox::critical(nullptr, app.translate("MeterW", "Error"), app.translate("MeterW",
  174. "Could not connect to JACK, possible reasons:\n"
  175. "%1").arg(QString::fromStdString(errorString)));
  176. return 1;
  177. }
  178. jPort1 = jackbridge_port_register(jClient, "in1", JACK_DEFAULT_AUDIO_TYPE, gIsOutput ? JackPortIsInput : JackPortIsOutput, 0);
  179. jPort2 = jackbridge_port_register(jClient, "in2", JACK_DEFAULT_AUDIO_TYPE, gIsOutput ? JackPortIsInput : JackPortIsOutput, 0);
  180. jackbridge_set_process_callback(jClient, process_callback, nullptr);
  181. jackbridge_set_port_connect_callback(jClient, port_callback, nullptr);
  182. #ifdef HAVE_JACKSESSION
  183. jackbridge_set_session_callback(jClient, session_callback, argv[0]);
  184. #endif
  185. jackbridge_activate(jClient);
  186. reconnect_ports();
  187. // Show GUI
  188. MeterW gui;
  189. gui.resize(70, 600);
  190. gui.show();
  191. gui.setAttribute(Qt::WA_QuitOnClose);
  192. // App-Loop
  193. int ret = app.exec();
  194. jackbridge_deactivate(jClient);
  195. jackbridge_client_close(jClient);
  196. return ret;
  197. }