drumstick 2.10.0
C++ MIDI libraries using Qt objects, idioms, and style.
alsaclient.h
Go to the documentation of this file.
1/*
2 MIDI Sequencer C++ library
3 Copyright (C) 2006-2024, Pedro Lopez-Cabanillas <plcl@users.sf.net>
4
5 This library 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 3 of the License, or
8 (at your option) any later version.
9
10 This library 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 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef DRUMSTICK_ALSACLIENT_H
20#define DRUMSTICK_ALSACLIENT_H
21
22#include <QObject>
23#include <QPointer>
24#include <QScopedPointer>
25#include <QThread>
26#include <QReadWriteLock>
27#include "macros.h"
28#include "alsaport.h"
29
34
35#if defined(DRUMSTICK_STATIC)
36#define DRUMSTICK_ALSA_EXPORT
37#else
38#if defined(drumstick_alsa_EXPORTS)
39#define DRUMSTICK_ALSA_EXPORT Q_DECL_EXPORT
40#else
41#define DRUMSTICK_ALSA_EXPORT Q_DECL_IMPORT
42#endif
43#endif
44
48namespace drumstick {
53namespace ALSA {
54
55class MidiQueue;
56class MidiClient;
57class SequencerEvent;
58class RemoveEvents;
59
70class DRUMSTICK_ALSA_EXPORT ClientInfo
71{
72 friend class MidiClient;
73
74public:
75 ClientInfo();
76 ClientInfo(const ClientInfo &other);
77 explicit ClientInfo(snd_seq_client_info_t *other);
78 ClientInfo(MidiClient* seq, int id);
79 virtual ~ClientInfo();
81 ClientInfo& operator=(const ClientInfo &other);
82 int getSizeOfInfo() const;
83
84 int getClientId();
85 snd_seq_client_type_t getClientType();
86 QString getName();
87 bool getBroadcastFilter();
88 bool getErrorBounce();
89 int getNumPorts();
90 int getEventLost();
91 void setClient(int client);
92 void setName(QString name);
93 void setBroadcastFilter(bool val);
94 void setErrorBounce(bool val);
95 PortInfoList getPorts() const;
96
97#if SND_LIB_VERSION > 0x010010
98 void addFilter(int eventType);
99 bool isFiltered(int eventType);
100 void clearFilter();
101 void removeFilter(int eventType);
102#endif
103
104protected:
105 void readPorts(MidiClient* seq);
106 void freePorts();
107
108 Q_DECL_DEPRECATED const unsigned char* getEventFilter();
109 Q_DECL_DEPRECATED void setEventFilter(unsigned char* filter);
110
111private:
112 snd_seq_client_info_t* m_Info;
113 PortInfoList m_Ports;
114};
115
119typedef QList<ClientInfo> ClientInfoList;
120
127class DRUMSTICK_ALSA_EXPORT SystemInfo
128{
129 friend class MidiClient;
130
131public:
132 SystemInfo();
133 SystemInfo(const SystemInfo& other);
134 explicit SystemInfo(snd_seq_system_info_t* other);
135 explicit SystemInfo(MidiClient* seq);
136 virtual ~SystemInfo();
137 SystemInfo* clone();
138 SystemInfo& operator=(const SystemInfo& other);
139 int getSizeOfInfo() const;
140
141 int getMaxClients();
142 int getMaxPorts();
143 int getMaxQueues();
144 int getMaxChannels();
145 int getCurrentQueues();
146 int getCurrentClients();
147
148private:
149 snd_seq_system_info_t* m_Info;
150};
151
158class DRUMSTICK_ALSA_EXPORT PoolInfo
159{
160 friend class MidiClient;
161
162public:
163 PoolInfo();
164 PoolInfo(const PoolInfo& other);
165 explicit PoolInfo(snd_seq_client_pool_t* other);
166 explicit PoolInfo(MidiClient* seq);
167 virtual ~PoolInfo();
168 PoolInfo* clone();
169 PoolInfo& operator=(const PoolInfo& other);
170 int getSizeOfInfo() const;
171
172 int getClientId();
173 int getInputFree();
174 int getInputPool();
175 int getOutputFree();
176 int getOutputPool();
177 int getOutputRoom();
178 void setInputPool(int size);
179 void setOutputPool(int size);
180 void setOutputRoom(int size);
181
182private:
183 snd_seq_client_pool_t* m_Info;
184};
185
195class DRUMSTICK_ALSA_EXPORT SequencerEventHandler
196{
197public:
199 virtual ~SequencerEventHandler() = default;
200
211};
212
218class DRUMSTICK_ALSA_EXPORT MidiClient : public QObject
219{
220 Q_OBJECT
221public:
222 explicit MidiClient( QObject* parent = nullptr );
223 virtual ~MidiClient();
224
225 void open( const QString deviceName = "default",
226 const int openMode = SND_SEQ_OPEN_DUPLEX,
227 const bool blockMode = false );
228 void open( snd_config_t* conf,
229 const QString deviceName = "default",
230 const int openMode = SND_SEQ_OPEN_DUPLEX,
231 const bool blockMode = false );
232 void close();
233 void startSequencerInput();
234 void stopSequencerInput();
237 MidiQueue* createQueue(QString const& name);
239 MidiQueue* useQueue(int queue_id);
240 MidiQueue* useQueue(const QString& name);
242 void portAttach(MidiPort* port);
243 void portDetach(MidiPort* port);
244 void detachAllPorts();
245 void addEventFilter(int evtype);
246 void output(SequencerEvent* ev, bool async = false, int timeout = -1);
247 void outputDirect(SequencerEvent* ev, bool async = false, int timeout = -1);
249 void drainOutput(bool async = false, int timeout = -1);
250 void synchronizeOutput();
251
252 int getClientId();
253 snd_seq_type_t getSequencerType();
254 snd_seq_t* getHandle();
255 bool isOpened();
256
257 size_t getOutputBufferSize();
258 void setOutputBufferSize(size_t newSize);
259 size_t getInputBufferSize();
260 void setInputBufferSize(size_t newSize);
261 QString getDeviceName();
262 int getOpenMode();
263 bool getBlockMode();
264 void setBlockMode(bool newValue);
265 QString getClientName();
266 QString getClientName(const int clientId);
267 void setClientName(QString const& newName);
268 bool getBroadcastFilter();
269 void setBroadcastFilter(bool newValue);
270 bool getErrorBounce();
271 void setErrorBounce(bool newValue);
272
274 void setThisClientInfo(const ClientInfo& val);
280 QList<int> getAvailableQueues();
281
283 void setPoolInfo(const PoolInfo& info);
284 void setPoolInput(int size);
285 void setPoolOutput(int size);
286 void setPoolOutputRoom(int size);
287 void resetPoolInput();
288 void resetPoolOutput();
289 void dropInput();
290 void dropInputBuffer();
291 void dropOutput();
292 void dropOutputBuffer();
293 void removeEvents(const RemoveEvents* spec);
295 int outputPending();
296 int inputPending(bool fetch);
297 int getQueueId(const QString& name);
298
299 void addListener(QObject* listener);
300 void removeListener(QObject* listener);
301 void setEventsEnabled(const bool bEnabled);
302 bool getEventsEnabled() const;
303 void setHandler(SequencerEventHandler* handler);
304 bool parseAddress( const QString& straddr, snd_seq_addr& result );
305 void setRealTimeInput(bool enabled);
307
308Q_SIGNALS:
314
315protected:
316 void doEvents();
317 void applyClientInfo();
318 void readClients();
319 void freeClients();
321 PortInfoList filterPorts(unsigned int filter);
322
323 /* low level public functions */
324 const char * _getDeviceName();
325 int getPollDescriptorsCount(short events);
326 int pollDescriptors(struct pollfd *pfds, unsigned int space, short events);
327 unsigned short pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds);
328
329 /* mid level functions */
330 void _setClientName( const char *name );
331 int createSimplePort( const char *name,
332 unsigned int caps,
333 unsigned int type );
334 void deleteSimplePort( int port );
335 void connectFrom(int myport, int client, int port);
336 void connectTo(int myport, int client, int port);
337 void disconnectFrom(int myport, int client, int port);
338 void disconnectTo(int myport, int client, int port);
339
340private:
342 class MidiClientPrivate;
343 QScopedPointer<MidiClientPrivate> d;
344};
345
346#if SND_LIB_VERSION > 0x010004
347DRUMSTICK_ALSA_EXPORT QString getRuntimeALSALibraryVersion();
348DRUMSTICK_ALSA_EXPORT int getRuntimeALSALibraryNumber();
349#endif
350DRUMSTICK_ALSA_EXPORT QString getRuntimeALSADriverVersion();
351DRUMSTICK_ALSA_EXPORT int getRuntimeALSADriverNumber();
352DRUMSTICK_ALSA_EXPORT QString getCompiledALSALibraryVersion();
353DRUMSTICK_ALSA_EXPORT QString getDrumstickLibraryVersion();
354
356
357}} /* namespace drumstick::ALSA */
358
359#endif // DRUMSTICK_ALSACLIENT_H
Classes managing ALSA Sequencer ports.
The QObject class is the base class of all Qt objects.
Client information.
Definition alsaclient.h:71
This class manages event input from the ALSA sequencer.
Client management.
Definition alsaclient.h:219
void eventReceived(drumstick::ALSA::SequencerEvent *ev)
Signal emitted when an event is received.
Port management.
Definition alsaport.h:125
Queue management.
Definition alsaqueue.h:201
Sequencer Pool information.
Definition alsaclient.h:159
Auxiliary class to remove events from an ALSA queue.
Definition alsaevent.h:752
Sequencer events handler.
Definition alsaclient.h:196
virtual ~SequencerEventHandler()=default
Destructor.
virtual void handleSequencerEvent(SequencerEvent *ev)=0
Callback function to be implemented by the derived class.
Base class for the event's hierarchy.
Definition alsaevent.h:68
System information.
Definition alsaclient.h:128
void outputBuffer(SequencerEvent *ev)
Output an event using the library output buffer, without draining the buffer.
MidiQueue * useQueue(int queue_id)
Create a new MidiQueue instance using a queue already existing in the system, associating it to the c...
void setOutputBufferSize(size_t newSize)
Sets the size of the library output buffer for the ALSA client.
void setPoolInfo(const PoolInfo &info)
Applies (updates) the client's PoolInfo data into the system.
int getInputFree()
Gets the available size on input pool.
void setOutputRoom(int size)
Sets the output room size.
int inputPending(bool fetch)
Gets the size of the events on the input buffer.
void setBroadcastFilter(bool val)
Sets the broadcast filter.
void removeEvents(const RemoveEvents *spec)
Removes events on input/output buffers and pools.
MidiQueue * createQueue()
Create and return a new MidiQueue associated to this client.
void setBroadcastFilter(bool newValue)
Sets the broadcast filter usage of the client.
QString getDeviceName()
Returns the name of the sequencer device.
bool getEventsEnabled() const
Returns true if the events mode of delivery has been enabled.
size_t getOutputBufferSize()
Gets the size of the library output buffer for the ALSA client.
size_t getInputBufferSize()
Gets the size of the library input buffer for the ALSA client.
PoolInfo & operator=(const PoolInfo &other)
Assignment operator.
QString getDrumstickLibraryVersion()
getDrumstickLibraryVersion provides the Drumstick version as an edited QString
int getSizeOfInfo() const
Gets the size of the internal object.
int createSimplePort(const char *name, unsigned int caps, unsigned int type)
Create an ALSA sequencer port, without using MidiPort.
PortInfoList getAvailableOutputs()
Gets the available user output ports in the system.
int getMaxQueues()
Get the system's maximum number of queues.
void startSequencerInput()
Starts reading events from the ALSA sequencer.
bool getErrorBounce()
Gets the client's error bounce.
void readClients()
Reads the ALSA sequencer's clients list.
int getOutputRoom()
Gets the output room size.
void removeListener(QObject *listener)
Removes a QObject listener from the listeners list.
PoolInfo()
Default constructor.
int getCurrentQueues()
Get the system's current number of queues.
int pollDescriptors(struct pollfd *pfds, unsigned int space, short events)
Get poll descriptors.
MidiPortList getMidiPorts() const
Gets the list of MidiPort instances belonging to this client.
int getQueueId(const QString &name)
Gets the queue's numeric identifier corresponding to the provided name.
void _setClientName(const char *name)
Sets the client name.
bool realTimeInputEnabled()
Return the real-time priority setting for the MIDI input thread.
void disconnectTo(int myport, int client, int port)
Unsubscribe one port to another arbitrary sequencer client:port.
snd_seq_type_t getSequencerType()
Returns the type snd_seq_type_t of the given sequencer handle.
PortInfoList filterPorts(unsigned int filter)
Gets a list of the available user ports in the system, filtered by the given bitmap of desired capabi...
void addListener(QObject *listener)
Adds a QObject to the listeners list.
int getMaxPorts()
Get the system's maximum number of ports.
PortInfoList getAvailableInputs()
Gets the available user input ports in the system.
void setBlockMode(bool newValue)
Change the blocking mode of the client.
void applyClientInfo()
This internal method applies the ClientInfo data to the ALSA sequencer client.
void close()
Close the sequencer device.
int getMaxChannels()
Get the system's maximum number of channels.
void readPorts(MidiClient *seq)
Read the client ports.
void setName(QString name)
Sets the client name.
SystemInfo & getSystemInfo()
Gets a SystemInfo instance with the updated state of the system.
Q_DECL_DEPRECATED void setEventFilter(unsigned char *filter)
Sets the event filter.
snd_seq_client_type_t getClientType()
Gets the client's type.
void resetPoolOutput()
Resets the client output pool.
SystemInfo * clone()
Clone the system info object.
int getRuntimeALSADriverNumber()
Gets the runtime ALSA drivers version number.
void setErrorBounce(bool val)
Sets the error bounce.
MidiClient(QObject *parent=nullptr)
Constructor.
MidiQueue * getQueue()
Get the MidiQueue instance associated to this client.
SystemInfo & operator=(const SystemInfo &other)
Assignment operator.
int getInputPool()
Gets the input pool size.
QList< int > getAvailableQueues()
Get a list of the existing queues.
int getPollDescriptorsCount(short events)
Returns the number of poll descriptors.
void setClientName(QString const &newName)
Changes the public name of the ALSA sequencer client.
SequencerEvent * extractOutput()
Extracts (and removes) the first event in the output buffer.
void detachAllPorts()
Detach all the ports belonging to this client.
void setRealTimeInput(bool enabled)
Enables real-time priority for the MIDI input thread.
void resetPoolInput()
Resets the client input pool.
QList< ClientInfo > ClientInfoList
List of sequencer client information.
Definition alsaclient.h:119
void setPoolOutput(int size)
Sets the size of the client's output pool.
void stopSequencerInput()
Stops reading events from the ALSA sequencer.
ClientInfo()
Default constructor.
bool getBroadcastFilter()
Gets the client's broadcast filter.
void deleteSimplePort(int port)
Remove an ALSA sequencer port.
bool parseAddress(const QString &straddr, snd_seq_addr &result)
Parse a text address representation, returning an ALSA address record.
void setInputPool(int size)
Set the input pool size.
int getCurrentClients()
Get the system's current number of clients.
snd_seq_t * getHandle()
Returns the sequencer handler managed by ALSA.
void output(SequencerEvent *ev, bool async=false, int timeout=-1)
Output an event using the library output buffer.
int getOpenMode()
Returns the last open mode used in open()
MidiPort * createPort()
Create and attach a new MidiPort instance to this client.
void portDetach(MidiPort *port)
Detach a MidiPort instance from this client.
int getNumPorts()
Gets the client's port count.
void setClient(int client)
Sets the client identifier number.
ClientInfo & operator=(const ClientInfo &other)
Assignment operator.
const char * _getDeviceName()
Gets the internal sequencer device name.
void setThisClientInfo(const ClientInfo &val)
Sets the data supplied by the ClientInfo object into the ALSA sequencer client.
ClientInfoList getAvailableClients()
Gets the list of clients from the ALSA sequencer.
void setHandler(SequencerEventHandler *handler)
Sets a sequencer event handler enabling the callback delivery mode.
void dropOutput()
Clears the client's output buffer and and remove events in sequencer queue.
void dropInput()
Clears the client's input buffer and and remove events in sequencer queue.
QString getRuntimeALSADriverVersion()
Gets the runtime ALSA drivers version string.
QString getName()
Gets the client's name.
void setPoolInput(int size)
Sets the size of the client's input pool.
int getClientId()
Gets the client's numeric identifier.
void doEvents()
Dispatch the events received from the Sequencer.
void dropInputBuffer()
Remove all events on user-space input buffer.
QString getCompiledALSALibraryVersion()
ALSA library version at build time.
QString getClientName()
Gets the client's public name.
void setPoolOutputRoom(int size)
Sets the room size of the client's output pool.
void open(const QString deviceName="default", const int openMode=SND_SEQ_OPEN_DUPLEX, const bool blockMode=false)
Open the sequencer device.
void dropOutputBuffer()
Removes all events on the library output buffer.
int getMaxClients()
Get the system's maximum number of clients.
void setErrorBounce(bool newValue)
Sets the error-bounce usage of the client.
void disconnectFrom(int myport, int client, int port)
Unsubscribe one port from another arbitrary sequencer client:port.
Q_DECL_DEPRECATED const unsigned char * getEventFilter()
Gets the client's event filter.
void synchronizeOutput()
Wait until all sent events are processed.
int getEventLost()
Gets the number of lost events.
void setInputBufferSize(size_t newSize)
Sets the size of the library input buffer for the ALSA client.
SystemInfo()
Default constructor.
PortInfoList getPorts() const
Gets the ports list.
void updateAvailablePorts()
Update the internal lists of user ports.
void freeClients()
Releases the list of ALSA sequencer's clients.
unsigned short pollDescriptorsRevents(struct pollfd *pfds, unsigned int nfds)
Gets the number of returned events from poll descriptors.
void connectFrom(int myport, int client, int port)
Subscribe one port from another arbitrary sequencer client:port.
int getOutputFree()
Gets the available size on output pool.
int outputPending()
Returns the size of pending events on the output buffer.
PoolInfo * clone()
Clone the pool info obeject.
ClientInfo & getThisClientInfo()
Gets the ClientInfo object holding data about this client.
PoolInfo & getPoolInfo()
Gets a PoolInfo instance with an updated state of the client memory pool.
void freePorts()
Release the ports list.
void portAttach(MidiPort *port)
Attach a MidiPort instance to this client.
void setEventsEnabled(const bool bEnabled)
Enables the notification of received SequencerEvent instances to the listeners registered with addLis...
bool getBlockMode()
Returns the last block mode used in open()
void addEventFilter(int evtype)
Add an event filter to the client.
void setOutputPool(int size)
Sets the output pool size.
void outputDirect(SequencerEvent *ev, bool async=false, int timeout=-1)
Output an event directly to the sequencer.
int getOutputPool()
Gets the output pool size.
void drainOutput(bool async=false, int timeout=-1)
Drain the library output buffer.
bool isOpened()
Returns true if the sequencer is opened.
void connectTo(int myport, int client, int port)
Subscribe one port to another arbitrary sequencer client:port.
ClientInfo * clone()
Clone the client info object.
QList< MidiPort * > MidiPortList
List of Ports instances.
Definition alsaport.h:221
QList< PortInfo > PortInfoList
List of port information objects.
Definition alsaport.h:117
Drumstick ALSA library wrapper.
Drumstick common.