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.

236 lines
6.4KB

  1. /*
  2. * Simple JACK Audio Meter
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.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. #include "../jack_utils.h"
  22. #include "../widgets/digitalpeakmeter.h"
  23. #include <QtGui/QApplication>
  24. #include <QtGui/QMessageBox>
  25. // -------------------------------
  26. volatile double x_portValue1 = 0.0;
  27. volatile double x_portValue2 = 0.0;
  28. volatile bool x_needReconnect = false;
  29. volatile bool x_quitNow = false;
  30. jack_client_t* jClient = nullptr;
  31. jack_port_t* jPort1 = nullptr;
  32. jack_port_t* jPort2 = nullptr;
  33. // -------------------------------
  34. float abs_f(const float value)
  35. {
  36. return (value < 1.0f) ? -value : value;
  37. }
  38. // -------------------------------
  39. // JACK callbacks
  40. int process_callback(const jack_nframes_t nframes, void*)
  41. {
  42. float* const jOut1 = (float*)jack_port_get_buffer(jPort1, nframes);
  43. float* const jOut2 = (float*)jack_port_get_buffer(jPort2, nframes);
  44. for (jack_nframes_t i = 0; i < nframes; i++)
  45. {
  46. if (abs_f(jOut1[i]) > x_portValue1)
  47. x_portValue1 = abs_f(jOut1[i]);
  48. if (abs_f(jOut2[i]) > x_portValue2)
  49. x_portValue2 = abs_f(jOut2[i]);
  50. }
  51. return 0;
  52. }
  53. void port_callback(jack_port_id_t, jack_port_id_t, int, void*)
  54. {
  55. x_needReconnect = true;
  56. }
  57. #ifdef HAVE_JACKSESSION
  58. void session_callback(jack_session_event_t* const event, void* const arg)
  59. {
  60. #ifdef Q_OS_LINUX
  61. QString filepath("cadence_jackmeter");
  62. Q_UNUSED(arg);
  63. #else
  64. QString filepath((char*)arg);
  65. #endif
  66. event->command_line = strdup(filepath.toUtf8().constData());
  67. jack_session_reply(jClient, event);
  68. if (event->type == JackSessionSaveAndQuit)
  69. x_quitNow = true;
  70. jack_session_event_free(event);
  71. }
  72. #endif
  73. // -------------------------------
  74. // helpers
  75. void reconnect_inputs()
  76. {
  77. x_needReconnect = false;
  78. jack_port_t* const jPlayPort1 = jack_port_by_name(jClient, "system:playback_1");
  79. jack_port_t* const jPlayPort2 = jack_port_by_name(jClient, "system:playback_2");
  80. std::vector<char*> jPortList1(jack_port_get_all_connections_as_vector(jClient, jPlayPort1));
  81. std::vector<char*> jPortList2(jack_port_get_all_connections_as_vector(jClient, jPlayPort2));
  82. foreach (char* const& thisPortName, jPortList1)
  83. {
  84. jack_port_t* const thisPort = jack_port_by_name(jClient, thisPortName);
  85. if (! (jack_port_is_mine(jClient, thisPort) || jack_port_connected_to(jPort1, thisPortName)))
  86. jack_connect(jClient, thisPortName, "M:in1");
  87. free(thisPortName);
  88. }
  89. foreach (char* const& thisPortName, jPortList2)
  90. {
  91. jack_port_t* const thisPort = jack_port_by_name(jClient, thisPortName);
  92. if (! (jack_port_is_mine(jClient, thisPort) || jack_port_connected_to(jPort2, thisPortName)))
  93. jack_connect(jClient, thisPortName, "M:in2");
  94. free(thisPortName);
  95. }
  96. jPortList1.clear();
  97. jPortList2.clear();
  98. }
  99. // -------------------------------
  100. // Meter class
  101. class MeterW : public DigitalPeakMeter
  102. {
  103. public:
  104. MeterW() : DigitalPeakMeter(nullptr)
  105. {
  106. setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
  107. setWindowTitle("M");
  108. setChannels(2);
  109. setOrientation(VERTICAL);
  110. setSmoothRelease(1);
  111. displayMeter(1, 0.0);
  112. displayMeter(2, 0.0);
  113. int refresh = float(jack_get_buffer_size(jClient)) / jack_get_sample_rate(jClient) * 1000;
  114. setRefreshRate(refresh > 25 ? refresh : 25);
  115. m_peakTimerId = startTimer(refresh > 50 ? refresh : 50);
  116. }
  117. protected:
  118. void timerEvent(QTimerEvent* event)
  119. {
  120. if (x_quitNow)
  121. {
  122. close();
  123. x_quitNow = false;
  124. return;
  125. }
  126. if (event->timerId() == m_peakTimerId)
  127. {
  128. displayMeter(1, x_portValue1);
  129. displayMeter(2, x_portValue2);
  130. x_portValue1 = 0.0;
  131. x_portValue2 = 0.0;
  132. if (x_needReconnect)
  133. reconnect_inputs();
  134. }
  135. QWidget::timerEvent(event);
  136. }
  137. private:
  138. int m_peakTimerId;
  139. };
  140. // -------------------------------
  141. int main(int argc, char* argv[])
  142. {
  143. QApplication app(argc, argv);
  144. app.setApplicationName("JackMeter");
  145. //app.setApplicationVersion(VERSION);
  146. app.setOrganizationName("Cadence");
  147. //app.setWindowIcon(QIcon(":/scalable/meter.svg"));
  148. // JACK initialization
  149. jack_status_t jStatus;
  150. #ifdef HAVE_JACKSESSION
  151. jack_options_t jOptions = static_cast<JackOptions>(JackNoStartServer|JackUseExactName|JackSessionID);
  152. #else
  153. jack_options_t jOptions = static_cast<JackOptions>(JackNoStartServer|JackUseExactName);
  154. #endif
  155. jClient = jack_client_open("M", jOptions, &jStatus);
  156. if (! jClient)
  157. {
  158. std::string errorString(jack_status_get_error_string(jStatus));
  159. QMessageBox::critical(nullptr, app.translate("MeterW", "Error"), app.translate("MeterW",
  160. "Could not connect to JACK, possible reasons:\n"
  161. "%1").arg(QString::fromStdString(errorString)));
  162. return 1;
  163. }
  164. jPort1 = jack_port_register(jClient, "in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  165. jPort2 = jack_port_register(jClient, "in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  166. jack_set_process_callback(jClient, process_callback, nullptr);
  167. jack_set_port_connect_callback(jClient, port_callback, nullptr);
  168. #ifdef HAVE_JACKSESSION
  169. jack_set_session_callback(jClient, session_callback, argv[0]);
  170. #endif
  171. jack_activate(jClient);
  172. reconnect_inputs();
  173. // Show GUI
  174. MeterW gui;
  175. gui.resize(70, 600);
  176. gui.show();
  177. gui.setAttribute(Qt::WA_QuitOnClose);
  178. // App-Loop
  179. int ret = app.exec();
  180. jack_deactivate(jClient);
  181. jack_client_close(jClient);
  182. return ret;
  183. }