hydrogen 1.2.3
JackAudioDriver.h
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#ifndef H2_JACK_OUTPUT_H
24#define H2_JACK_OUTPUT_H
25
26#include <core/IO/AudioOutput.h>
27#include <core/IO/NullDriver.h>
28
29// check if jack support is disabled
30#if defined(H2CORE_HAVE_JACK) || _DOXYGEN_
31// JACK support is enabled.
32
33#include <map>
34#include <memory>
35#include <pthread.h>
36#include <jack/jack.h>
37#include <jack/transport.h>
38
39#include <core/Globals.h>
40
41
42
43namespace H2Core
44{
45
46class Song;
47class Instrument;
48class InstrumentComponent;
49
94class JackAudioDriver : public Object<JackAudioDriver>, public AudioOutput
95{
97public:
101 enum class Timebase {
103 Master = 1,
107 Slave = 0,
109 None = -1
110 };
111
116 jack_client_t* m_pClient;
123 JackAudioDriver( JackProcessCallback m_processCallback );
130
144 virtual int connect() override;
149 virtual void disconnect() override;
154 void deactivate();
156 virtual unsigned getBufferSize() override;
158 virtual unsigned getSampleRate() override;
159
160 virtual int getXRuns() const override;
161
168 void clearPerTrackAudioBuffers( uint32_t nFrames );
169
173 void makeTrackOutputs( std::shared_ptr<Song> pSong );
174
176 void setConnectDefaults( bool flag ) {
177 m_bConnectDefaults = flag;
178 }
181 return m_bConnectDefaults;
182 }
183
190 virtual float* getOut_L() override;
197 virtual float* getOut_R() override;
207 float* getTrackOut_L( unsigned nTrack );
217 float* getTrackOut_R( unsigned nTrack );
232 float* getTrackOut_L( std::shared_ptr<Instrument> instr, std::shared_ptr<InstrumentComponent> pCompo );
247 float* getTrackOut_R( std::shared_ptr<Instrument> instr, std::shared_ptr<InstrumentComponent> pCompo );
248
263 virtual int init( unsigned bufferSize ) override;
264
268 void startTransport();
272 void stopTransport();
282 void locateTransport( long long nFrame );
290
294 void initTimebaseMaster();
303
308
312 static unsigned long jackServerSampleRate;
316 static jack_nframes_t jackServerBufferSize;
322 static int jackServerXRuns;
323
333 static int jackDriverSampleRate( jack_nframes_t nframes, void* param );
334
344 static int jackDriverBufferSize( jack_nframes_t nframes, void* arg );
346 static int jackXRunCallback( void* arg );
347
350 float getMasterBpm() const;
351
359 void relocateUsingBBT();
360
372 static bool checkSupport();
373
374private:
375
383 bool compareAdjacentBBT() const;
384
416 static void JackTimebaseCallback( jack_transport_state_t state,
417 jack_nframes_t nFrames,
418 jack_position_t* pJackPosition,
419 int new_pos,
420 void* arg );
427 static void jackDriverShutdown( void* arg );
428
429 static void printJackTransportPos( const jack_position_t* pPos );
430
432 void printState() const;
433
459 void setTrackOutput( int n, std::shared_ptr<Instrument> instr, std::shared_ptr<InstrumentComponent> pCompo, std::shared_ptr<Song> pSong );
461 JackProcessCallback m_processCallback;
465 jack_port_t* m_pOutputPort1;
469 jack_port_t* m_pOutputPort2;
504
522 jack_transport_state_t m_JackTransportState;
545 jack_position_t m_JackTransportPos;
551
585
590
598 static QString checkExecutable( const QString& sExecutable, const QString& sOption );
599};
600
601}; // H2Core namespace
602
603
604#else // H2CORE_HAVE_JACK
605// JACK is disabled
606
607namespace H2Core {
609class JackAudioDriver : public NullDriver {
611public:
615 enum class Timebase {
617 Master = 1,
621 Slave = 0,
623 None = -1
624 };
632
633 // Required since these functions are a friend of AudioEngine which
634 // need to be build even if no JACK support is desired.
636 void relocateUsingBBT() {}
637};
638
639}; // H2Core namespace
640
641
642#endif // H2CORE_HAVE_JACK
643
644#endif
645
#define H2_OBJECT(name)
Definition Object.h:224
Base abstract class for audio output classes.
Definition AudioOutput.h:39
JACK (Jack Audio Connection Kit) server driver.
virtual void disconnect() override
Disconnects the JACK client of the Hydrogen from the JACK server.
int m_nTrackPortCount
Total number of output ports currently in use.
static QString checkExecutable(const QString &sExecutable, const QString &sOption)
Calls sExecutable in a subprocess using the sOption CLI option and reports the results.
jack_port_t * m_pTrackOutputPortsL[MAX_INSTRUMENTS]
Vector of all left audio output ports currently used by the local JACK client.
bool compareAdjacentBBT() const
Compares the BBT information stored in m_JackTransportPos and m_previousJackTransportPos with respect...
virtual float * getOut_L() override
Get content in the left stereo output port.
Timebase m_timebaseState
More user-friendly version of m_nTimebaseTracking.
virtual int init(unsigned bufferSize) override
Initializes the JACK audio driver.
JackAudioDriver(JackProcessCallback m_processCallback)
Constructor of the JACK server driver.
bool m_bConnectDefaults
Specifies whether the default left and right (master) audio JACK ports will be automatically connecte...
static unsigned long jackServerSampleRate
Sample rate of the JACK audio server.
int m_nTimebaseTracking
Whether Hydrogen or another program is Jack timebase master.
static JackAudioDriver * pJackDriverInstance
Instance of the JackAudioDriver.
QString m_sOutputPortName2
Destination of the right source port m_pOutputPort2, for which a connection will be established in co...
QString m_sOutputPortName1
Destination of the left source port m_pOutputPort1, for which a connection will be established in con...
float * getTrackOut_R(unsigned nTrack)
Get content of right output port of a specific track.
static int jackDriverSampleRate(jack_nframes_t nframes, void *param)
Callback function for the JACK audio server to set the sample rate jackServerSampleRate.
void clearPerTrackAudioBuffers(uint32_t nFrames)
Resets the buffers contained in m_pTrackOutputPortsL and m_pTrackOutputPortsR.
jack_port_t * m_pOutputPort2
Right source port.
int m_trackMap[MAX_INSTRUMENTS][MAX_COMPONENTS]
Matrix containing the track number of each component of all instruments.
void startTransport()
Tells the JACK server to start transport.
virtual float * getOut_R() override
Get content in the right stereo output port.
static int jackServerXRuns
Number of XRuns since the driver started.
virtual unsigned getBufferSize() override
void setTrackOutput(int n, std::shared_ptr< Instrument > instr, std::shared_ptr< InstrumentComponent > pCompo, std::shared_ptr< Song > pSong)
Renames the n 'th port of JACK client and creates it if it's not already present.
Timebase
Whether Hydrogen or another program is Jack timebase master.
@ None
Only normal clients registered.
@ Master
Hydrogen itself is timebase master.
@ Slave
An external program is timebase master and Hydrogen will disregard all tempo markers on the Timeline ...
jack_client_t * m_pClient
Object holding the external client session with the JACK server.
static void printJackTransportPos(const jack_position_t *pPos)
void deactivate()
Deactivates the JACK client of Hydrogen and disconnects all ports belonging to it.
void setConnectDefaults(bool flag)
virtual int connect() override
Connects to output ports via the JACK server.
Timebase getTimebaseState() const
jack_position_t m_JackTransportPos
Current transport position obtained using jack_transport_query() (jack/transport.h).
jack_position_t m_previousJackTransportPos
Used for detecting changes in the BBT transport information with external timebase master application...
jack_port_t * m_pOutputPort1
Left source port.
static int jackDriverBufferSize(jack_nframes_t nframes, void *arg)
Callback function for the JACK audio server to set the buffer size jackServerBufferSize.
static jack_nframes_t jackServerBufferSize
Buffer size of the JACK audio server.
void stopTransport()
Tells the JACK server to stop transport.
void updateTransportPosition()
The function queries the transport position and additional information from the JACK server,...
static void jackDriverShutdown(void *arg)
Callback function for the JACK audio server to shutting down the JACK driver.
static int jackXRunCallback(void *arg)
Report an XRun event to the GUI.
void initTimebaseMaster()
Registers Hydrogen as JACK timebase master.
static bool checkSupport()
Attempts to call several JACK executables in order to check for existing JACK support.
void makeTrackOutputs(std::shared_ptr< Song > pSong)
Creates per component output ports for each instrument.
virtual unsigned getSampleRate() override
static void JackTimebaseCallback(jack_transport_state_t state, jack_nframes_t nFrames, jack_position_t *pJackPosition, int new_pos, void *arg)
Callback function for the JACK server to supply additional timebase information.
JackProcessCallback m_processCallback
Main process callback.
~JackAudioDriver()
Destructor of the JACK server driver.
void printState() const
Show debugging information.
float * getTrackOut_L(unsigned nTrack)
Get content of left output port of a specific track.
jack_transport_state_t m_JackTransportState
Current transport state returned by jack_transport_query() (jack/transport.h).
void releaseTimebaseMaster()
Release Hydrogen from the JACK timebase master responsibilities.
void relocateUsingBBT()
Uses the bar-beat-tick information to relocate the transport position.
jack_port_t * m_pTrackOutputPortsR[MAX_INSTRUMENTS]
Vector of all right audio output ports currently used by the local JACK client.
void locateTransport(long long nFrame)
Re-positions the transport position to nFrame.
virtual int getXRuns() const override
Get the number of XRuns that occurred since the audio driver has started.
#define MAX_INSTRUMENTS
Maximum number of instruments allowed in Hydrogen.
Definition config.dox:70
#define MAX_COMPONENTS
Maximum number of components each Instrument is allowed to have.
Definition config.dox:75
int(* audioProcessCallback)(uint32_t, void *)
Definition AudioOutput.h:32