1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
From c1e790e809d38e93dde2d9c788ab29fc8b3b1206 Mon Sep 17 00:00:00 2001
From: Adrian <git@nexadn.de>
Date: Sun, 13 Nov 2022 18:28:07 +0100
Subject: [PATCH 1/4] ci: Install libjack-dev for Linux
The new JACK plugin requires the libjack-dev at compile time.
---
.travis.yml | 1 +
1 file changed, 1 insertion(+)
diff --git a/.travis.yml b/.travis.yml
index ce9e997..212e240 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,6 +10,7 @@ before_install:
install:
- sudo apt-get install build-essential libgl1-mesa-dev
- sudo apt-get install qt514base qt514multimedia qt514quickcontrols2
+ - sudo apt-get install libjack-dev
- source /opt/qt514/bin/qt514-env.sh
--
2.51.0
From cf9dbdc3a8863a767ba0fe29c151063b87dc12e7 Mon Sep 17 00:00:00 2001
From: Adrian <git@nexadn.de>
Date: Sun, 13 Nov 2022 17:41:44 +0100
Subject: [PATCH 2/4] audio: Add JACK plugin
This commit adds a JACK plugin, allowing OpenSoundMeter to run with JACK
or PipeWire as sound server. This can eliminate problems when
OpenSoundMeter is run alongside a sound server which wants to have
exclusive access to the sound card.
This introduces libjack and the JACK headers as a new
dependency on Linux.
The JACK plugin differs from other plugins in the sense that it does not
automatically establish connections to devices. Instead, each opened
stream (be it a measurement or a generator signal) spawns a new JACK
client with each of the inputs/outputs required by OSM present as a JACK
port. The user can then patch these ports freely in JACK, allowing for
freedom in the audio routing. As new JACK clients are spawned upon
request, this plugin does always show only a single JACK device in
device lists as more is not required and avoids additional complexity
when trying to do more elaborate listings.
Closes: https://github.com/psmokotnin/osm/issues/58
---
OpenSoundMeter.pro | 10 +-
src/audio/client.cpp | 2 +
src/audio/plugins/jack.cpp | 462 +++++++++++++++++++++++++++++++++++++
src/audio/plugins/jack.h | 127 ++++++++++
4 files changed, 598 insertions(+), 3 deletions(-)
create mode 100644 src/audio/plugins/jack.cpp
create mode 100644 src/audio/plugins/jack.h
diff --git a/OpenSoundMeter.pro b/OpenSoundMeter.pro
index a52b502..f5b73e3 100644
--- a/OpenSoundMeter.pro
+++ b/OpenSoundMeter.pro
@@ -294,12 +294,16 @@ win32 {
unix:!macx:!ios {
HEADERS += \
- src/audio/plugins/alsa.h
+ src/audio/plugins/alsa.h \
+ src/audio/plugins/jack.h
SOURCES += \
- src/audio/plugins/alsa.cpp
+ src/audio/plugins/alsa.cpp \
+ src/audio/plugins/jack.cpp
- LIBS += -lasound
+ LIBS += \
+ -lasound \
+ -ljack
}
GRAPH = $$(GRAPH_BACKEND)
diff --git a/src/audio/client.cpp b/src/audio/client.cpp
index d319c72..10f611e 100644
--- a/src/audio/client.cpp
+++ b/src/audio/client.cpp
@@ -33,6 +33,7 @@
#ifdef Q_OS_LINUX
#include "plugins/alsa.h"
+#include "plugins/jack.h"
#endif
#ifdef USE_ASIO
@@ -87,6 +88,7 @@ void Client::initPlugins()
#ifdef Q_OS_LINUX
m_plugins.push_back(QSharedPointer<Plugin>(new AlsaPlugin()));
+ m_plugins.push_back(QSharedPointer<Plugin>(new JackPlugin()));
#endif
for (auto &&plugin : m_plugins) {
diff --git a/src/audio/plugins/jack.cpp b/src/audio/plugins/jack.cpp
new file mode 100644
index 0000000..3a87edf
--- /dev/null
+++ b/src/audio/plugins/jack.cpp
@@ -0,0 +1,462 @@
+/**
+ * OSM
+ * Copyright (C) 2022 Adrian Schollmeyer
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "jack.h"
+#include <algorithm>
+#include <cassert>
+
+namespace {
+constexpr unsigned int DEFAULT_SAMPLE_RATE{48000};
+constexpr unsigned int DEFAULT_CHANNEL_COUNT_INPUT{2};
+constexpr unsigned int DEFAULT_CHANNEL_COUNT_OUTPUT{1};
+
+constexpr const char* PLUGIN_NAME{"JACK"};
+constexpr const char* JACK_CLIENT_NAME{"OpenSoundMeter"};
+constexpr const char* DEFAULT_MEASUREMENT_CHANNEL{"measurement"};
+constexpr const char* DEFAULT_REFERENCE_CHANNEL{"reference"};
+constexpr const char* DEFAULT_OUTPUT_CHANNEL{"out"};
+}
+
+namespace audio {
+JackClient::JackClient(const Plugin::Direction &direction, QIODevice& endpoint)
+ : m_client(nullptr, jackClientDeleter()), m_direction(direction), m_endpoint(endpoint)
+{
+ initJackClient();
+ initPorts();
+ initSampleRate();
+ initBufferSize();
+ initProcessing();
+}
+
+JackClient::~JackClient()
+{
+ // Explicitly stop processing first, to prevent us still running the JACK
+ // processor while the rest of the object is being cleaned up
+ if (m_client)
+ close();
+}
+
+jack_nframes_t JackClient::currentSampleRate() const
+{
+ return m_sampleRate;
+}
+
+std::size_t JackClient::currentChannelCount() const
+{
+ return m_ports.size();
+}
+
+Plugin::Direction JackClient::direction() const
+{
+ return m_direction;
+}
+
+void JackClient::close()
+{
+ deactivate();
+ m_client.reset(nullptr);
+}
+
+void JackClient::initJackClient()
+{
+ jack_status_t jackStatus;
+ jack_client_t* jackClient = jack_client_open(
+ JACK_CLIENT_NAME,
+ JackOptions::JackNoStartServer,
+ &jackStatus
+ );
+
+ if (!jackClient)
+ throw JackPluginException{"Failed to open JACK client"};
+
+ m_client.reset(jackClient);
+}
+
+void JackClient::initSampleRate()
+{
+ assert(m_client);
+ m_sampleRate = jack_get_sample_rate(m_client.get());
+ qDebug() << "[JACK] Sample rate set: " << m_sampleRate;
+
+ int res = jack_set_sample_rate_callback(
+ m_client.get(),
+ jackSampleRateCallback,
+ this
+ );
+ if (res != 0)
+ throw JackPluginException{"Failed to register sample rate callback"};
+}
+
+void JackClient::initBufferSize()
+{
+ assert(m_client);
+ assert(!m_ports.empty());
+ m_bufferSize = jack_get_buffer_size(m_client.get());
+ m_sampleBuffer.resize(m_bufferSize * m_ports.size());
+ qDebug() << "[JACK] Buffer size set: " << m_bufferSize;
+
+ int res = jack_set_buffer_size_callback(
+ m_client.get(),
+ jackBufferSizeCallback,
+ this
+ );
+ if (res != 0)
+ throw JackPluginException{"Failed to register buffer size callback"};
+}
+
+void JackClient::initPorts()
+{
+ assert(m_client);
+ switch (m_direction) {
+ case Plugin::Direction::Input:
+ registerInputs();
+ break;
+
+ case Plugin::Direction::Output:
+ registerOutputs();
+ break;
+
+ default:
+ throw JackPluginException{"Unsupported signal direction"};
+ }
+}
+
+void JackClient::initProcessing()
+{
+ assert(m_client);
+ int res = jack_set_process_callback(
+ m_client.get(),
+ jackProcessCallback,
+ this
+ );
+ if (res != 0)
+ throw JackPluginException{"Failed to register process callback"};
+}
+
+void JackClient::activate()
+{
+ assert(m_client);
+ if (m_active)
+ return;
+
+ qDebug() << "[JACK] Activating";
+ int res = jack_activate(m_client.get());
+ if (res != 0)
+ throw JackPluginException{"Failed to activate client"};
+
+ m_active = true;
+}
+
+void JackClient::deactivate()
+{
+ if (!m_active)
+ return;
+
+ m_active = false;
+
+ assert(m_client);
+ int res = jack_deactivate(m_client.get());
+ if (res != 0)
+ throw JackPluginException{"Failed to deactivate client"};
+}
+
+void JackClient::registerInputs()
+{
+ qDebug() << "[JACK] Registering Inputs";
+ jack_port_t *measurementPort = jack_port_register(
+ m_client.get(),
+ DEFAULT_MEASUREMENT_CHANNEL,
+ JACK_DEFAULT_AUDIO_TYPE,
+ JackPortFlags::JackPortIsInput,
+ 0
+ );
+ if (!measurementPort)
+ throw JackPluginException{"Failed to register measurement port"};
+ m_ports.emplace_back(measurementPort, jackPortDeleter(m_client.get()));
+ m_portBuffers.push_back(nullptr);
+
+ jack_port_t *referencePort = jack_port_register(
+ m_client.get(),
+ DEFAULT_REFERENCE_CHANNEL,
+ JACK_DEFAULT_AUDIO_TYPE,
+ JackPortFlags::JackPortIsInput,
+ 0
+ );
+ if (!referencePort)
+ throw JackPluginException{"Failed to register reference port"};
+ m_ports.emplace_back(referencePort, jackPortDeleter(m_client.get()));
+ m_portBuffers.push_back(nullptr);
+}
+
+void JackClient::registerOutputs()
+{
+ qDebug() << "[JACK] Registering Outputs";
+ jack_port_t *outputPort = jack_port_register(
+ m_client.get(),
+ DEFAULT_OUTPUT_CHANNEL,
+ JACK_DEFAULT_AUDIO_TYPE,
+ JackPortFlags::JackPortIsOutput,
+ 0
+ );
+ if (!outputPort)
+ throw JackPluginException{"Failed to register output port"};
+ m_ports.emplace_back(outputPort, jackPortDeleter(m_client.get()));
+ m_portBuffers.push_back(nullptr);
+}
+
+std::function<void(jack_client_t *)> JackClient::jackClientDeleter()
+{
+ return [this](jack_client_t *client) -> void {
+ m_ports.resize(0);
+ jack_client_close(client);
+ };
+}
+
+std::function<void(jack_port_t *)> JackClient::jackPortDeleter(jack_client_t *client)
+{
+ // We can't use the object's client member here as it might already have
+ // changed if this deleter is called in the client object's deleter,
+ // resulting in an assertion failure or segfault.
+ assert(client);
+ return [client](jack_port_t *port) -> void {
+ jack_port_unregister(client, port);
+ };
+}
+
+int JackClient::jackSampleRateCallback(jack_nframes_t newSampleRate, void* obj)
+{
+ assert(obj);
+ return reinterpret_cast<JackClient*>(obj)->jackSampleRateCallbackInt(newSampleRate);
+}
+
+int JackClient::jackSampleRateCallbackInt(jack_nframes_t newSampleRate)
+{
+ m_sampleRate = newSampleRate;
+ qInfo() << "[JACK] Sample rate updated: " << m_sampleRate;
+ emit sampleRateChanged(m_sampleRate);
+ return 0;
+}
+
+int JackClient::jackBufferSizeCallback(jack_nframes_t newBufferSize, void* obj)
+{
+ assert(obj);
+ return reinterpret_cast<JackClient*>(obj)->jackBufferSizeCallbackInt(newBufferSize);
+}
+
+int JackClient::jackBufferSizeCallbackInt(jack_nframes_t newBufferSize)
+{
+ m_bufferSize = newBufferSize;
+ qInfo() << "[JACK] Buffer size updated: " << m_bufferSize;
+ if (newBufferSize > 0)
+ m_sampleBuffer.resize(newBufferSize * m_ports.size());
+
+ return 0;
+}
+
+int JackClient::jackProcessCallback(jack_nframes_t nframes, void* obj)
+{
+ assert(obj);
+ return reinterpret_cast<JackClient*>(obj)->jackProcessCallbackInt(nframes);
+}
+
+int JackClient::jackProcessCallbackInt(jack_nframes_t nframes)
+{
+ // Yes, this can actually happen
+ if (m_bufferSize == 0)
+ return 1;
+
+ if (!m_active)
+ return 0;
+
+ assert(m_ports.size() == m_portBuffers.size());
+ assert(nframes == m_bufferSize);
+ assert(m_endpoint.isOpen());
+
+ if (m_direction == Plugin::Direction::Input) {
+ processInputPorts(nframes);
+ } else {
+ processOutputPorts(nframes);
+ }
+ return 0;
+}
+
+void JackClient::processInputPorts(jack_nframes_t nframes)
+{
+ if (!m_endpoint.isWritable())
+ return;
+
+ for (std::size_t i = 0; i < m_ports.size(); ++i) {
+ m_portBuffers[i] = reinterpret_cast<float*>(
+ jack_port_get_buffer(m_ports[i].get(), nframes));
+ }
+
+ std::size_t sampleBufferPos{0};
+ for (jack_nframes_t i = 0; i < nframes; ++i) {
+ for (const float* portBuf : m_portBuffers) {
+ assert(sampleBufferPos < m_sampleBuffer.size());
+ m_sampleBuffer[sampleBufferPos] = portBuf[i];
+ sampleBufferPos++;
+ }
+ }
+ assert(sampleBufferPos == nframes * m_portBuffers.size());
+ m_endpoint.write(
+ reinterpret_cast<char*>(m_sampleBuffer.data()),
+ sampleBufferPos * sizeof(float)
+ );
+}
+
+void JackClient::processOutputPorts(jack_nframes_t nframes)
+{
+ if (!m_endpoint.isReadable())
+ return;
+
+ for (std::size_t i = 0; i < m_ports.size(); ++i) {
+ m_portBuffers[i] = reinterpret_cast<float*>(
+ jack_port_get_buffer(m_ports[i].get(), nframes));
+ }
+
+ const std::size_t samplesToWrite = std::max(
+ static_cast<std::size_t>(nframes),
+ static_cast<std::size_t>(m_endpoint.bytesAvailable() / sizeof(float))
+ );
+ assert(samplesToWrite % m_ports.size() == 0);
+
+ std::size_t portIdx{0};
+ std::size_t portBufferPos{0};
+ for (std::size_t i = 0; i < samplesToWrite; ++i) {
+ assert(portIdx < m_portBuffers.size());
+ assert(portBufferPos < m_bufferSize);
+ const std::size_t bytesRead = m_endpoint.read(
+ reinterpret_cast<char*>(m_portBuffers[portIdx] + portBufferPos),
+ sizeof(float)
+ );
+ assert(bytesRead == sizeof(float));
+
+ portIdx = (portIdx + 1) % m_portBuffers.size();
+ if (portIdx == 0)
+ portBufferPos++;
+ }
+}
+
+JackPlugin::JackPlugin()
+{
+ DeviceInfo m_defaultDeviceInfo{PLUGIN_NAME, PLUGIN_NAME};
+ m_defaultDeviceInfo.setName(PLUGIN_NAME);
+ m_defaultDeviceInfo.setInputChannels({
+ DEFAULT_MEASUREMENT_CHANNEL,
+ DEFAULT_REFERENCE_CHANNEL
+ });
+ m_defaultDeviceInfo.setOutputChannels({DEFAULT_OUTPUT_CHANNEL});
+ m_defaultDeviceInfo.setDefaultSampleRate(DEFAULT_SAMPLE_RATE);
+ m_list.push_back(m_defaultDeviceInfo);
+}
+
+QString JackPlugin::name() const
+{
+ return PLUGIN_NAME;
+}
+
+DeviceInfo::List JackPlugin::getDeviceInfoList() const
+{
+ return m_list;
+}
+
+DeviceInfo::Id JackPlugin::defaultDeviceId(
+ [[maybe_unused]] const Plugin::Direction &mode) const
+{
+ return DeviceInfo::Id();
+}
+
+Format JackPlugin::deviceFormat(
+ [[maybe_unused]] const DeviceInfo::Id &id,
+ const Plugin::Direction &mode) const
+{
+ const auto client_it = std::find_if(
+ m_clients.begin(),
+ m_clients.end(),
+ [mode](const auto& client) {
+
+ return client.second->direction() == mode;
+ });
+
+ if (client_it == m_clients.end()) {
+ if (mode == Plugin::Direction::Input)
+ return {DEFAULT_SAMPLE_RATE, DEFAULT_CHANNEL_COUNT_INPUT};
+ else
+ return {DEFAULT_SAMPLE_RATE, DEFAULT_CHANNEL_COUNT_OUTPUT};
+ } else {
+ return {
+ static_cast<unsigned int>(client_it->second->currentSampleRate()),
+ static_cast<unsigned int>(client_it->second->currentChannelCount())
+ };
+ }
+}
+
+Stream *JackPlugin::open(
+ [[maybe_unused]] const DeviceInfo::Id &id,
+ const Plugin::Direction &mode,
+ [[maybe_unused]] const Format &format,
+ QIODevice *endpoint)
+{
+ // The sample rate is set by JACK, so we don't mess with it.
+ // The channel count is fixed to the minimum amount of channels necessary
+ // for OSM to work as we can freely patch the cannels in JACK.
+ //
+ // The ID can be ignored as we create individual JACK clients
+ // for each requested Stream.
+ try {
+ std::unique_ptr<JackClient> jack = std::make_unique<JackClient>(mode, *endpoint);
+
+ Format f{
+ jack->currentSampleRate(),
+ static_cast<unsigned int>(jack->currentChannelCount())
+ };
+ Stream *stream = new Stream(f);
+
+ m_clients.emplace(stream, std::move(jack));
+ JackClient& jackClient = *m_clients.at(stream).get();
+
+ connect(stream, &Stream::closeMe, this, [this, stream, endpoint]() {
+ // Order is important here to prevent JACK still writing data to
+ // the endpoint after it has been closed.
+ m_clients.erase(stream);
+
+ if (endpoint->isOpen())
+ endpoint->close();
+
+ stream->deleteLater();
+ });
+
+ connect(
+ &jackClient,
+ &JackClient::sampleRateChanged,
+ this,
+ [stream](jack_nframes_t newRate) {
+ assert(stream);
+ stream->setSampleRate(static_cast<unsigned int>(newRate));
+ });
+
+ endpoint->open(mode == Input ? QIODevice::WriteOnly : QIODevice::ReadOnly);
+ jackClient.activate();
+
+ return stream;
+ } catch (JackPluginException& e) {
+ return nullptr;
+ }
+}
+
+}
diff --git a/src/audio/plugins/jack.h b/src/audio/plugins/jack.h
new file mode 100644
index 0000000..9685f65
--- /dev/null
+++ b/src/audio/plugins/jack.h
@@ -0,0 +1,127 @@
+/**
+ * OSM
+ * Copyright (C) 2022 Adrian Schollmeyer
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef AUDIO_JACKPLUGIN_H
+#define AUDIO_JACKPLUGIN_H
+
+#include "../plugin.h"
+#include <QObject>
+#include <functional>
+#include <jack/jack.h>
+#include <jack/types.h>
+#include <map>
+#include <memory>
+#include <stdexcept>
+#include <vector>
+
+namespace audio {
+class JackClient : public QObject
+{
+ Q_OBJECT
+
+public:
+ constexpr static std::size_t BUFFER_SIZE_MULTIPLIER{4};
+
+ JackClient(const Plugin::Direction &direction, QIODevice& endpoint);
+ JackClient(JackClient&&) = default;
+ JackClient(const JackClient &) = delete;
+ ~JackClient();
+
+ void activate();
+ void deactivate();
+
+ jack_nframes_t currentSampleRate() const;
+ std::size_t currentChannelCount() const;
+
+ Plugin::Direction direction() const;
+
+signals:
+ void sampleRateChanged(jack_nframes_t newSampleRate);
+
+public slots:
+ void close();
+
+private:
+ void initJackClient();
+ void initPorts();
+ void initSampleRate();
+ void initBufferSize();
+ void initProcessing();
+
+ void registerInputs();
+ void registerOutputs();
+
+ std::function<void(jack_client_t *)> jackClientDeleter();
+ static std::function<void(jack_port_t *)> jackPortDeleter(jack_client_t *client);
+
+ static int jackSampleRateCallback(jack_nframes_t newSampleRate, void* obj);
+ int jackSampleRateCallbackInt(jack_nframes_t newSampleRate);
+
+ static int jackBufferSizeCallback(jack_nframes_t newBufferSize, void* obj);
+ int jackBufferSizeCallbackInt(jack_nframes_t newBufferSize);
+
+ static int jackProcessCallback(jack_nframes_t nframes, void* obj);
+ int jackProcessCallbackInt(jack_nframes_t nframes);
+
+ void processInputPorts(jack_nframes_t nframes);
+ void processOutputPorts(jack_nframes_t nframes);
+
+ std::unique_ptr<jack_client_t, std::function<void(jack_client_t *)>> m_client;
+ std::vector<std::unique_ptr<jack_port_t, std::function<void(jack_port_t *)>>> m_ports;
+ std::vector<float*> m_portBuffers;
+ jack_nframes_t m_sampleRate;
+ jack_nframes_t m_bufferSize;
+ const Plugin::Direction m_direction;
+ QIODevice& m_endpoint;
+ std::vector<float> m_sampleBuffer;
+ bool m_active{false};
+};
+
+class JackPlugin : public Plugin
+{
+ Q_OBJECT
+
+public:
+ JackPlugin();
+ JackPlugin(const JackPlugin &) = delete;
+ virtual ~JackPlugin() = default;
+
+ QString name() const override;
+ DeviceInfo::List getDeviceInfoList() const override;
+ DeviceInfo::Id defaultDeviceId(const Direction &mode) const override;
+
+ Format deviceFormat(const DeviceInfo::Id &id, const Direction &mode) const override;
+ Stream *open(const DeviceInfo::Id &id, const Direction &mode, const Format &format, QIODevice *endpoint) override;
+
+private:
+ DeviceInfo::List m_list{};
+ DeviceInfo m_defaultDeviceInfoInput;
+ DeviceInfo m_defaultDeviceInfoOutput;
+ std::map<Stream *, std::unique_ptr<JackClient>> m_clients{};
+};
+
+class JackPluginException : public std::runtime_error
+{
+public:
+ template<typename T>
+ JackPluginException(const T& arg)
+ : std::runtime_error(arg)
+ {}
+};
+}
+
+#endif // AUDIO_JACKPLUGIN_H
--
2.51.0
From 8d163a07db24c17fcd07fa645425ac7bd07f1109 Mon Sep 17 00:00:00 2001
From: Adrian <git@nexadn.de>
Date: Sat, 3 Dec 2022 00:00:09 +0100
Subject: [PATCH 3/4] build: Make JACK support optional
As JACK is not installed on consumer Linux systems, adding unconditional
JACK support introduces an unnecessary dependency for such systems
without bringing any benefit. This commit makes JACK support optional at
configure-time, so users can configure whether they want integrated JACK
support or not.
---
OpenSoundMeter.pro | 22 +++++++++++++++-------
src/audio/client.cpp | 6 ++++++
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/OpenSoundMeter.pro b/OpenSoundMeter.pro
index f5b73e3..f9cef4a 100644
--- a/OpenSoundMeter.pro
+++ b/OpenSoundMeter.pro
@@ -294,16 +294,24 @@ win32 {
unix:!macx:!ios {
HEADERS += \
- src/audio/plugins/alsa.h \
- src/audio/plugins/jack.h
+ src/audio/plugins/alsa.h
SOURCES += \
- src/audio/plugins/alsa.cpp \
- src/audio/plugins/jack.cpp
+ src/audio/plugins/alsa.cpp
- LIBS += \
- -lasound \
- -ljack
+ LIBS += -lasound
+
+ CONFIG(jack) {
+ HEADERS += \
+ src/audio/plugins/jack.h
+
+ SOURCES += \
+ src/audio/plugins/jack.cpp
+
+ LIBS += -ljack
+
+ DEFINES += USE_JACK
+ }
}
GRAPH = $$(GRAPH_BACKEND)
diff --git a/src/audio/client.cpp b/src/audio/client.cpp
index 10f611e..db7c377 100644
--- a/src/audio/client.cpp
+++ b/src/audio/client.cpp
@@ -33,6 +33,9 @@
#ifdef Q_OS_LINUX
#include "plugins/alsa.h"
+#endif
+
+#ifdef USE_JACK
#include "plugins/jack.h"
#endif
@@ -88,6 +91,9 @@ void Client::initPlugins()
#ifdef Q_OS_LINUX
m_plugins.push_back(QSharedPointer<Plugin>(new AlsaPlugin()));
+#endif
+
+#ifdef USE_JACK
m_plugins.push_back(QSharedPointer<Plugin>(new JackPlugin()));
#endif
--
2.51.0
From 5a04a68ec5121e67fa3b140772f114a0e3fd0ed9 Mon Sep 17 00:00:00 2001
From: Adrian Schollmeyer <git@nexadn.de>
Date: Sat, 3 Dec 2022 00:04:08 +0100
Subject: [PATCH 4/4] travis: Enable JACK support for Linux build
Since the JACK integration shall be built in the CI pipeline, this
commit sets the jack config option in the qmake call in the Travis
configuration.
---
.travis.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index 212e240..2b6fbe4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -15,7 +15,7 @@ install:
script:
- - /opt/qt514/bin/qmake PREFIX=/usr
+ - /opt/qt514/bin/qmake PREFIX=/usr CONFIG+=jack
- make
- # Generate AppImage
# - sudo apt-get -y install checkinstall
--
2.51.0
|