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.

233 lines
6.3KB

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