drumstick  1.1.2
alsaevent.cpp
Go to the documentation of this file.
1 /*
2  MIDI Sequencer C++ library
3  Copyright (C) 2006-2018, 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 2 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 #include "alsaevent.h"
20 
32 namespace drumstick {
33 
100 {
101  snd_seq_ev_clear( &m_event );
102 }
103 
109 {
110  snd_seq_ev_clear( &m_event );
111  m_event = *event;
112 }
113 
119 {
120  snd_seq_ev_clear( &m_event );
121  m_event = other.m_event;
122 }
123 
131 {
132  m_event = other.m_event;
133  return *this;
134 }
135 
141 bool
143 {
144  snd_seq_event_type_t te = event->getSequencerType();
145  return ( te == SND_SEQ_EVENT_PORT_SUBSCRIBED ||
146  te == SND_SEQ_EVENT_PORT_UNSUBSCRIBED );
147 }
148 
154 bool
156 {
157  snd_seq_event_type_t te = event->getSequencerType();
158  return ( te == SND_SEQ_EVENT_PORT_START ||
159  te == SND_SEQ_EVENT_PORT_EXIT ||
160  te == SND_SEQ_EVENT_PORT_CHANGE );
161 }
162 
168 bool
170 {
171  snd_seq_event_type_t te = event->getSequencerType();
172  return ( te == SND_SEQ_EVENT_CLIENT_START ||
173  te == SND_SEQ_EVENT_CLIENT_EXIT ||
174  te == SND_SEQ_EVENT_CLIENT_CHANGE );
175 }
176 
182 bool
184 {
185  snd_seq_event_type_t te = event->getSequencerType();
186  return ( te == SND_SEQ_EVENT_PORT_START ||
187  te == SND_SEQ_EVENT_PORT_EXIT ||
188  te == SND_SEQ_EVENT_PORT_CHANGE ||
189  te == SND_SEQ_EVENT_CLIENT_START ||
190  te == SND_SEQ_EVENT_CLIENT_EXIT ||
191  te == SND_SEQ_EVENT_CLIENT_CHANGE ||
192  te == SND_SEQ_EVENT_PORT_SUBSCRIBED ||
193  te == SND_SEQ_EVENT_PORT_UNSUBSCRIBED );
194 }
195 
202 bool
204 {
205  snd_seq_event_type_t te = event->getSequencerType();
206  return ( te == SND_SEQ_EVENT_NOTEOFF ||
207  te == SND_SEQ_EVENT_NOTEON ||
208  te == SND_SEQ_EVENT_NOTE ||
209  te == SND_SEQ_EVENT_KEYPRESS ||
210  te == SND_SEQ_EVENT_CONTROLLER ||
211  te == SND_SEQ_EVENT_CONTROL14 ||
212  te == SND_SEQ_EVENT_PGMCHANGE ||
213  te == SND_SEQ_EVENT_CHANPRESS ||
214  te == SND_SEQ_EVENT_PITCHBEND );
215 }
216 
221 void SequencerEvent::setSequencerType(const snd_seq_event_type_t eventType)
222 {
223  m_event.type = eventType;
224 }
225 
232 void SequencerEvent::setDestination(const unsigned char client, const unsigned char port)
233 {
234  snd_seq_ev_set_dest(&m_event, client, port);
235 }
236 
242 void SequencerEvent::setSource(const unsigned char port)
243 {
244  snd_seq_ev_set_source(&m_event, port);
245 }
246 
251 {
252  snd_seq_ev_set_subs(&m_event);
253 }
254 
259 {
260  snd_seq_ev_set_broadcast(&m_event);
261 }
262 
268 {
269  snd_seq_ev_set_direct(&m_event);
270 }
271 
278 void SequencerEvent::scheduleTick(int queue, int tick, bool relative)
279 {
280  snd_seq_ev_schedule_tick(&m_event, queue, relative, tick);
281 }
282 
290 void SequencerEvent::scheduleReal(int queue, ulong secs, ulong nanos, bool relative)
291 {
292  snd_seq_real_time_t rtime;
293  rtime.tv_sec = secs;
294  rtime.tv_nsec = nanos;
295  snd_seq_ev_schedule_real(&m_event, queue, relative, &rtime);
296 }
297 
304 void SequencerEvent::setPriority(const bool high)
305 {
306  snd_seq_ev_set_priority(&m_event, high);
307 }
308 
314 void SequencerEvent::setTag(const unsigned char aTag)
315 {
316 #if SND_LIB_VERSION > 0x010008
317  snd_seq_ev_set_tag(&m_event, aTag);
318 #else
319  m_event.tag = aTag;
320 #endif
321 }
322 
329 unsigned int SequencerEvent::getRaw32(const unsigned int n) const
330 {
331  if (n < 3) return m_event.data.raw32.d[n];
332  return 0;
333 }
334 
340 void SequencerEvent::setRaw32(const unsigned int n, const unsigned int value)
341 {
342  if (n < 3) m_event.data.raw32.d[n] = value;
343 }
344 
351 unsigned char SequencerEvent::getRaw8(const unsigned int n) const
352 {
353  if (n < 12) return m_event.data.raw8.d[n];
354  return 0;
355 }
356 
362 void SequencerEvent::setRaw8(const unsigned int n, const unsigned char value)
363 {
364  if (n < 12) m_event.data.raw8.d[n] = value;
365 }
366 
372 {
373  snd_seq_free_event(&m_event);
374 }
375 
381 {
382  return snd_seq_event_length(&m_event);
383 }
384 
392 NoteEvent::NoteEvent(int ch, int key, int vel, int dur) : KeyEvent()
393 {
394  snd_seq_ev_set_note(&m_event, ch, key, vel, dur);
395 }
396 
403 NoteOnEvent::NoteOnEvent(int ch, int key, int vel) : KeyEvent()
404 {
405  snd_seq_ev_set_noteon(&m_event, ch, key, vel);
406 }
407 
414 NoteOffEvent::NoteOffEvent(int ch, int key, int vel) : KeyEvent()
415 {
416  snd_seq_ev_set_noteoff(&m_event, ch, key, vel);
417 }
418 
425 KeyPressEvent::KeyPressEvent(int ch, int key, int vel) : KeyEvent()
426 {
427  snd_seq_ev_set_keypress(&m_event, ch, key, vel);
428 }
429 
436 ControllerEvent::ControllerEvent(int ch, int cc, int val) : ChannelEvent()
437 {
438  snd_seq_ev_set_controller(&m_event, ch, cc, val);
439 }
440 
447 {
448  snd_seq_ev_set_pgmchange(&m_event, ch, val);
449 }
450 
457 {
458  snd_seq_ev_set_pitchbend(&m_event, ch, val);
459 }
460 
467 {
468  snd_seq_ev_set_chanpress(&m_event, ch, val);
469 }
470 
475  : SequencerEvent()
476 {
477  m_data.clear();
478  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
479 }
480 
485 VariableEvent::VariableEvent(snd_seq_event_t* event)
486  : SequencerEvent(event)
487 {
488  m_data = QByteArray((char *) event->data.ext.ptr,
489  event->data.ext.len);
490  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
491 }
492 
497 VariableEvent::VariableEvent(const QByteArray& data)
498  : SequencerEvent()
499 {
500  m_data = data;
501  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
502 }
503 
509  : SequencerEvent()
510 {
511  m_data = other.m_data;
512  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
513 }
514 
520 VariableEvent::VariableEvent(const unsigned int datalen, char* dataptr)
521  : SequencerEvent()
522 {
523  m_data = QByteArray(dataptr, datalen);
524  snd_seq_ev_set_variable( &m_event, m_data.size(), m_data.data() );
525 }
526 
533 {
534  m_event = other.m_event;
535  m_data = other.m_data;
536  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
537  return *this;
538 }
539 
544  : VariableEvent()
545 {
546  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
547 }
548 
553 SysExEvent::SysExEvent(snd_seq_event_t* event)
554  : VariableEvent(event)
555 {
556  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
557 }
558 
563 SysExEvent::SysExEvent(const QByteArray& data)
564  : VariableEvent(data)
565 {
566  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
567 }
568 
574  : VariableEvent(other)
575 {
576  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
577 }
578 
584 SysExEvent::SysExEvent(const unsigned int datalen, char* dataptr)
585  : VariableEvent( datalen, dataptr )
586 {
587  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
588 }
589 
594  : VariableEvent(), m_textType(1)
595 {
596  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
597 }
598 
603 TextEvent::TextEvent(snd_seq_event_t* event)
604  : VariableEvent(event), m_textType(1)
605 {
606  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
607 }
608 
614 TextEvent::TextEvent(const QString& text, const int textType)
615  : VariableEvent(text.toUtf8()), m_textType(textType)
616 {
617  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
618 }
619 
625  : VariableEvent(other)
626 {
627  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
628  m_textType = other.getTextType();
629 }
630 
636 TextEvent::TextEvent(const unsigned int datalen, char* dataptr)
637  : VariableEvent(datalen, dataptr), m_textType(1)
638 {
639  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
640 }
641 
646 QString TextEvent::getText() const
647 {
648  return QString::fromUtf8(m_data.data(), m_data.size());
649 }
650 
656 {
657  return m_textType;
658 }
659 
664 SystemEvent::SystemEvent(const snd_seq_event_type_t type) : SequencerEvent()
665 {
666  snd_seq_ev_set_fixed(&m_event);
667  setSequencerType(type);
668 }
669 
676 QueueControlEvent::QueueControlEvent(snd_seq_event_type_t type, int queue, int value)
677  : SequencerEvent()
678 {
679  snd_seq_ev_set_queue_control(&m_event, type, queue, value);
680 }
681 
687 ValueEvent::ValueEvent(const snd_seq_event_type_t type, int val) : SequencerEvent()
688 {
689  snd_seq_ev_set_fixed(&m_event);
690  setSequencerType(type);
691  setValue(val);
692 }
693 
699 TempoEvent::TempoEvent(int queue, int tempo) : QueueControlEvent()
700 {
701  snd_seq_ev_set_queue_tempo(&m_event, queue, tempo);
702 }
703 
708 {
709  snd_seq_remove_events_malloc(&m_Info);
710 }
711 
717 {
718  snd_seq_remove_events_malloc(&m_Info);
719  snd_seq_remove_events_copy(m_Info, other.m_Info);
720 }
721 
726 RemoveEvents::RemoveEvents(snd_seq_remove_events_t* other)
727 {
728  snd_seq_remove_events_malloc(&m_Info);
729  snd_seq_remove_events_copy(m_Info, other);
730 }
731 
736 {
737  snd_seq_remove_events_free(m_Info);
738 }
739 
746 {
747  return new RemoveEvents(m_Info);
748 }
749 
757 {
758  snd_seq_remove_events_copy(m_Info, other.m_Info);
759  return *this;
760 }
761 
766 int
768 {
769  return snd_seq_remove_events_sizeof();
770 }
771 
777 int
779 {
780  return snd_seq_remove_events_get_channel(m_Info);
781 }
782 
788 unsigned int
790 {
791  return snd_seq_remove_events_get_condition(m_Info);
792 }
793 
799 const snd_seq_addr_t*
801 {
802  return snd_seq_remove_events_get_dest(m_Info);
803 }
804 
810 int
812 {
813  return snd_seq_remove_events_get_event_type(m_Info);
814 }
815 
821 int
823 {
824  return snd_seq_remove_events_get_queue(m_Info);
825 }
826 
832 int
834 {
835  return snd_seq_remove_events_get_tag(m_Info);
836 }
837 
843 const snd_seq_timestamp_t*
845 {
846  return snd_seq_remove_events_get_time(m_Info);
847 }
848 
854 void
856 {
857  snd_seq_remove_events_set_channel(m_Info, chan);
858 }
859 
878 void
879 RemoveEvents::setCondition(unsigned int cond)
880 {
881  snd_seq_remove_events_set_condition(m_Info, cond);
882 }
883 
889 void
890 RemoveEvents::setDest(const snd_seq_addr_t* dest)
891 {
892  snd_seq_remove_events_set_dest(m_Info, dest);
893 }
894 
900 void
902 {
903  snd_seq_remove_events_set_event_type(m_Info, type);
904 }
905 
911 void
913 {
914  snd_seq_remove_events_set_queue(m_Info, queue);
915 }
916 
922 void
924 {
925  snd_seq_remove_events_set_tag(m_Info, tag);
926 }
927 
933 void
934 RemoveEvents::setTime(const snd_seq_timestamp_t* time)
935 {
936  snd_seq_remove_events_set_time(m_Info, time);
937 }
938 
944 MidiCodec::MidiCodec( int bufsize, QObject* parent ) : QObject(parent)
945 {
946  CHECK_ERROR(snd_midi_event_new(bufsize, &m_Info));
947 }
948 
953 {
954  snd_midi_event_free(m_Info);
955 }
956 
960 void
962 {
963  snd_midi_event_init(m_Info);
964 }
965 
973 long
974 MidiCodec::decode(unsigned char *buf,
975  long count,
976  const snd_seq_event_t *ev)
977 {
978  return CHECK_WARNING(snd_midi_event_decode(m_Info, buf, count, ev));
979 }
980 
988 long
989 MidiCodec::encode(const unsigned char *buf,
990  long count,
991  snd_seq_event_t *ev)
992 {
993  return CHECK_WARNING(snd_midi_event_encode(m_Info, buf, count, ev));
994 }
995 
1002 long
1004  snd_seq_event_t *ev)
1005 {
1006  return CHECK_WARNING(snd_midi_event_encode_byte(m_Info, c, ev));
1007 }
1008 
1013 void
1015 {
1016  snd_midi_event_no_status(m_Info, enable ? 0 : 1);
1017 }
1018 
1022 void
1024 {
1025  snd_midi_event_reset_decode(m_Info);
1026 }
1027 
1031 void
1033 {
1034  snd_midi_event_reset_encode(m_Info);
1035 }
1036 
1041 void
1043 {
1044  CHECK_WARNING(snd_midi_event_resize_buffer(m_Info, bufsize));
1045 }
1046 
1047 } /* namespace drumstick */
QString getText() const
Gets the event's text content.
Definition: alsaevent.cpp:646
void setSequencerType(const snd_seq_event_type_t eventType)
Sets the event's ALSA sequencer type.
Definition: alsaevent.cpp:221
ALSA Event representing a queue control command.
Definition: alsaevent.h:454
void resetDecoder()
Reset MIDI decode parser.
Definition: alsaevent.cpp:1023
void setChannel(int chan)
Gets the MIDI channel.
Definition: alsaevent.cpp:855
void init()
CODEC initialization.
Definition: alsaevent.cpp:961
Base class for the events having Key and Velocity properties.
Definition: alsaevent.h:171
void setDirect()
Sets the event to be immediately delivered, not queued/scheduled.
Definition: alsaevent.cpp:267
void setTime(const snd_seq_timestamp_t *time)
Sets the timestamp.
Definition: alsaevent.cpp:934
void setEventType(int type)
Sets the event type.
Definition: alsaevent.cpp:901
ChanPressEvent()
Default constructor.
Definition: alsaevent.h:363
void setQueue(int queue)
Sets the queue number.
Definition: alsaevent.cpp:912
const QEvent::Type SequencerEventType
Constant SequencerEventType is the QEvent::type() of any SequencerEvent object to be used to check th...
Definition: alsaevent.h:39
QByteArray m_data
Clone this object returning a pointer to the new object.
Definition: alsaevent.h:394
const snd_seq_addr_t * getDest()
Gets the destination.
Definition: alsaevent.cpp:800
static bool isPort(const SequencerEvent *event)
Checks if the event's type is of type port.
Definition: alsaevent.cpp:155
void free() __attribute__((deprecated))
Releases the event record.
Definition: alsaevent.cpp:371
Base class for variable length events.
Definition: alsaevent.h:378
static bool isConnectionChange(const SequencerEvent *event)
Checks if the event's type is of type connection change.
Definition: alsaevent.cpp:183
RemoveEvents * clone()
Create a new object copied from this object and return a pointer to the copy.
Definition: alsaevent.cpp:745
The QObject class is the base class of all Qt objects.
Base class for the event's hierarchy.
Definition: alsaevent.h:52
Event representing a MIDI system exclusive event.
Definition: alsaevent.h:400
VariableEvent()
Default constructor.
Definition: alsaevent.cpp:474
long encode(const unsigned char *buf, long count, snd_seq_event_t *ev)
Encode from byte stream.
Definition: alsaevent.cpp:989
unsigned int getCondition()
Gets the condition.
Definition: alsaevent.cpp:789
VariableEvent & operator=(const VariableEvent &other)
Assignment operator.
Definition: alsaevent.cpp:532
void setTag(int tag)
Sets the numeric tag.
Definition: alsaevent.cpp:923
NoteOnEvent()
Default constructor.
Definition: alsaevent.h:241
NoteEvent()
Default constructor.
Definition: alsaevent.h:214
void setRaw8(const unsigned int n, const unsigned char value)
Sets an event's raw 8 bits parameter.
Definition: alsaevent.cpp:362
~MidiCodec()
Destructor.
Definition: alsaevent.cpp:952
virtual ~RemoveEvents()
Destructor.
Definition: alsaevent.cpp:735
int getEventType()
Gets the event type.
Definition: alsaevent.cpp:811
Event representing a SMF text event.
Definition: alsaevent.h:418
SequencerEvent & operator=(const SequencerEvent &other)
Assignment operator.
Definition: alsaevent.cpp:130
void enableRunningStatus(bool enable)
Enable MIDI running status (command merge)
Definition: alsaevent.cpp:1014
void scheduleTick(const int queue, const int tick, const bool relative)
Sets the event to be scheduled in musical time (ticks) units.
Definition: alsaevent.cpp:278
PitchBendEvent()
Default constructor.
Definition: alsaevent.h:344
void resizeBuffer(int bufsize)
Resize the CODEC buffer.
Definition: alsaevent.cpp:1042
Base class for the events having a Channel property.
Definition: alsaevent.h:147
void setSource(const unsigned char port)
Sets the event's source port ID.
Definition: alsaevent.cpp:242
int getTag()
Gets the numeric tag.
Definition: alsaevent.cpp:833
int getChannel()
Gets the MIDI channel.
Definition: alsaevent.cpp:778
void setPriority(const bool high)
Sets the priority of the event.
Definition: alsaevent.cpp:304
void setValue(const int v)
Sets the event's value.
Definition: alsaevent.h:504
ControllerEvent()
Default constructor.
Definition: alsaevent.h:286
Auxiliary class to remove events from an ALSA queue.
Definition: alsaevent.h:585
const snd_seq_timestamp_t * getTime()
Gets the timestamp.
Definition: alsaevent.cpp:844
void resetEncoder()
Reset MIDI encode parser.
Definition: alsaevent.cpp:1032
void setRaw32(const unsigned int n, const unsigned int value)
Sets an event's raw 32 bits parameter.
Definition: alsaevent.cpp:340
int m_textType
Clone this object returning a pointer to the new object.
Definition: alsaevent.h:431
static bool isSubscription(const SequencerEvent *event)
Checks if the event's type is a subscription.
Definition: alsaevent.cpp:142
unsigned int getRaw32(const unsigned int n) const
Gets an event's raw 32 bits parameter.
Definition: alsaevent.cpp:329
QueueControlEvent()
Default constructor.
Definition: alsaevent.h:458
ValueEvent()
Default constructor.
Definition: alsaevent.h:497
static bool isChannel(const SequencerEvent *event)
Checks if the event's type is a Channel Voice message.
Definition: alsaevent.cpp:203
MidiCodec(int bufsize, QObject *parent=0)
MidiCodec constructor.
Definition: alsaevent.cpp:944
void setTag(const unsigned char aTag)
Sets the event's tag.
Definition: alsaevent.cpp:314
SysExEvent()
Default constructor.
Definition: alsaevent.cpp:543
void setBroadcast()
Sets the event's destination to be all queues/clients/ports/channels.
Definition: alsaevent.cpp:258
void setCondition(unsigned int cond)
Sets the flags of the conditional event's removal.
Definition: alsaevent.cpp:879
RemoveEvents()
Default constructor.
Definition: alsaevent.cpp:707
int getTextType() const
Gets the event's SMF text type.
Definition: alsaevent.cpp:655
ProgramChangeEvent()
Default constructor.
Definition: alsaevent.h:325
NoteOffEvent()
Default constructor.
Definition: alsaevent.h:256
unsigned char getRaw8(const unsigned int n) const
Gets an event's raw 8 bits parameter.
Definition: alsaevent.cpp:351
long decode(unsigned char *buf, long count, const snd_seq_event_t *ev)
Decode from event to bytes.
Definition: alsaevent.cpp:974
TempoEvent()
Default constructor.
Definition: alsaevent.h:516
void setSubscribers()
Sets the event's destination to be all the subscribers of the source port.
Definition: alsaevent.cpp:250
snd_seq_event_t m_event
ALSA sequencer event record.
Definition: alsaevent.h:141
void setDest(const snd_seq_addr_t *dest)
Set the destination address.
Definition: alsaevent.cpp:890
#define CHECK_ERROR(x)
This macro calls the check error function.
#define CHECK_WARNING(x)
This macro calls the check warning function.
static bool isClient(const SequencerEvent *event)
Checks if the event's type is of type client.
Definition: alsaevent.cpp:169
The QEvent class is the base class of all event classes.
int getQueue()
Gets the queue number.
Definition: alsaevent.cpp:822
TextEvent()
Default constructor.
Definition: alsaevent.cpp:593
int getEncodedLength()
Gets the encoded length of the event record.
Definition: alsaevent.cpp:380
Classes managing ALSA Sequencer events.
KeyPressEvent()
Default constructor.
Definition: alsaevent.h:271
SequencerEvent()
Default constructor.
Definition: alsaevent.cpp:99
void scheduleReal(const int queue, const ulong secs, const ulong nanos, const bool relative)
Sets the event to be scheduled in real (clock) time units.
Definition: alsaevent.cpp:290
SystemEvent()
Default constructor.
Definition: alsaevent.h:441
int getSizeOfInfo() const
Gets the allocated size of the ALSA remove events object.
Definition: alsaevent.cpp:767
RemoveEvents & operator=(const RemoveEvents &other)
Assignment operator.
Definition: alsaevent.cpp:756
void setDestination(const unsigned char client, const unsigned char port)
Sets the client:port destination of the event.
Definition: alsaevent.cpp:232