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.

232 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. m_peakTimerId = startTimer(refresh > 50 ? refresh : 50);
  113. }
  114. protected:
  115. void timerEvent(QTimerEvent* event)
  116. {
  117. if (x_quitNow)
  118. {
  119. close();
  120. x_quitNow = false;
  121. return;
  122. }
  123. if (event->timerId() == m_peakTimerId)
  124. {
  125. displayMeter(1, x_portValue1);
  126. displayMeter(2, x_portValue2);
  127. x_portValue1 = 0.0;
  128. x_portValue2 = 0.0;
  129. if (x_needReconnect)
  130. reconnect_inputs();
  131. }
  132. QWidget::timerEvent(event);
  133. }
  134. private:
  135. int m_peakTimerId;
  136. };
  137. // -------------------------------
  138. int main(int argc, char* argv[])
  139. {
  140. QApplication app(argc, argv);
  141. app.setApplicationName("JackMeter");
  142. app.setApplicationVersion(VERSION);
  143. app.setOrganizationName("Cadence");
  144. app.setWindowIcon(QIcon(":/scalable/cadence.svg"));
  145. // JACK initialization
  146. jack_status_t jStatus;
  147. #ifdef HAVE_JACKSESSION
  148. jack_options_t jOptions = static_cast<jack_options_t>(JackNoStartServer|JackUseExactName|JackSessionID);
  149. #else
  150. jack_options_t jOptions = static_cast<jack_options_t>(JackNoStartServer|JackUseExactName);
  151. #endif
  152. jClient = jack_client_open("M", jOptions, &jStatus);
  153. if (! jClient)
  154. {
  155. std::string errorString(jack_status_get_error_string(jStatus));
  156. QMessageBox::critical(nullptr, app.translate("MeterW", "Error"), app.translate("MeterW",
  157. "Could not connect to JACK, possible reasons:\n"
  158. "%1").arg(QString::fromStdString(errorString)));
  159. return 1;
  160. }
  161. jPort1 = jack_port_register(jClient, "in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  162. jPort2 = jack_port_register(jClient, "in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  163. jack_set_process_callback(jClient, process_callback, nullptr);
  164. jack_set_port_connect_callback(jClient, port_callback, nullptr);
  165. #ifdef HAVE_JACKSESSION
  166. jack_set_session_callback(jClient, session_callback, argv[0]);
  167. #endif
  168. jack_activate(jClient);
  169. reconnect_inputs();
  170. // Show GUI
  171. MeterW gui;
  172. gui.resize(70, 600);
  173. gui.show();
  174. gui.setAttribute(Qt::WA_QuitOnClose);
  175. // App-Loop
  176. int ret = app.exec();
  177. jack_deactivate(jClient);
  178. jack_client_close(jClient);
  179. return ret;
  180. }