drumstick  0.5.0
alsaevent.cpp
Go to the documentation of this file.
1 /*
2  MIDI Sequencer C++ library
3  Copyright (C) 2006-2010, 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 along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #include "alsaevent.h"
21 
33 namespace drumstick {
34 
101 {
102  snd_seq_ev_clear( &m_event );
103 }
104 
110 {
111  snd_seq_ev_clear( &m_event );
112  m_event = *event;
113 }
114 
120 {
121  snd_seq_ev_clear( &m_event );
122  m_event = other.m_event;
123 }
124 
132 {
133  m_event = other.m_event;
134  return *this;
135 }
136 
142 bool
144 {
145  snd_seq_event_type_t te = event->getSequencerType();
146  return ( te == SND_SEQ_EVENT_PORT_SUBSCRIBED ||
147  te == SND_SEQ_EVENT_PORT_UNSUBSCRIBED );
148 }
149 
155 bool
157 {
158  snd_seq_event_type_t te = event->getSequencerType();
159  return ( te == SND_SEQ_EVENT_PORT_START ||
160  te == SND_SEQ_EVENT_PORT_EXIT ||
161  te == SND_SEQ_EVENT_PORT_CHANGE );
162 }
163 
169 bool
171 {
172  snd_seq_event_type_t te = event->getSequencerType();
173  return ( te == SND_SEQ_EVENT_CLIENT_START ||
174  te == SND_SEQ_EVENT_CLIENT_EXIT ||
175  te == SND_SEQ_EVENT_CLIENT_CHANGE );
176 }
177 
183 bool
185 {
186  snd_seq_event_type_t te = event->getSequencerType();
187  return ( te == SND_SEQ_EVENT_PORT_START ||
188  te == SND_SEQ_EVENT_PORT_EXIT ||
189  te == SND_SEQ_EVENT_PORT_CHANGE ||
190  te == SND_SEQ_EVENT_CLIENT_START ||
191  te == SND_SEQ_EVENT_CLIENT_EXIT ||
192  te == SND_SEQ_EVENT_CLIENT_CHANGE ||
193  te == SND_SEQ_EVENT_PORT_SUBSCRIBED ||
194  te == SND_SEQ_EVENT_PORT_UNSUBSCRIBED );
195 }
196 
203 bool
205 {
206  snd_seq_event_type_t te = event->getSequencerType();
207  return ( te == SND_SEQ_EVENT_NOTEOFF ||
208  te == SND_SEQ_EVENT_NOTEON ||
209  te == SND_SEQ_EVENT_NOTE ||
210  te == SND_SEQ_EVENT_KEYPRESS ||
211  te == SND_SEQ_EVENT_CONTROLLER ||
212  te == SND_SEQ_EVENT_CONTROL14 ||
213  te == SND_SEQ_EVENT_PGMCHANGE ||
214  te == SND_SEQ_EVENT_CHANPRESS ||
215  te == SND_SEQ_EVENT_PITCHBEND );
216 }
217 
222 void SequencerEvent::setSequencerType(const snd_seq_event_type_t eventType)
223 {
224  m_event.type = eventType;
225 }
226 
233 void SequencerEvent::setDestination(const unsigned char client, const unsigned char port)
234 {
235  snd_seq_ev_set_dest(&m_event, client, port);
236 }
237 
243 void SequencerEvent::setSource(const unsigned char port)
244 {
245  snd_seq_ev_set_source(&m_event, port);
246 }
247 
252 {
253  snd_seq_ev_set_subs(&m_event);
254 }
255 
260 {
261  snd_seq_ev_set_broadcast(&m_event);
262 }
263 
269 {
270  snd_seq_ev_set_direct(&m_event);
271 }
272 
279 void SequencerEvent::scheduleTick(int queue, int tick, bool relative)
280 {
281  snd_seq_ev_schedule_tick(&m_event, queue, relative, tick);
282 }
283 
291 void SequencerEvent::scheduleReal(int queue, ulong secs, ulong nanos, bool relative)
292 {
293  snd_seq_real_time_t rtime;
294  rtime.tv_sec = secs;
295  rtime.tv_nsec = nanos;
296  snd_seq_ev_schedule_real(&m_event, queue, relative, &rtime);
297 }
298 
305 void SequencerEvent::setPriority(const bool high)
306 {
307  snd_seq_ev_set_priority(&m_event, high);
308 }
309 
315 void SequencerEvent::setTag(const unsigned char aTag)
316 {
317 #if SND_LIB_VERSION > 0x010008
318  snd_seq_ev_set_tag(&m_event, aTag);
319 #else
320  m_event.tag = aTag;
321 #endif
322 }
323 
330 unsigned int SequencerEvent::getRaw32(const unsigned int n) const
331 {
332  if (n < 3) return m_event.data.raw32.d[n];
333  return 0;
334 }
335 
341 void SequencerEvent::setRaw32(const unsigned int n, const unsigned int value)
342 {
343  if (n < 3) m_event.data.raw32.d[n] = value;
344 }
345 
352 unsigned char SequencerEvent::getRaw8(const unsigned int n) const
353 {
354  if (n < 12) return m_event.data.raw8.d[n];
355  return 0;
356 }
357 
363 void SequencerEvent::setRaw8(const unsigned int n, const unsigned char value)
364 {
365  if (n < 12) m_event.data.raw8.d[n] = value;
366 }
367 
373 {
374  snd_seq_free_event(&m_event);
375 }
376 
382 {
383  return snd_seq_event_length(&m_event);
384 }
385 
393 NoteEvent::NoteEvent(int ch, int key, int vel, int dur) : KeyEvent()
394 {
395  snd_seq_ev_set_note(&m_event, ch, key, vel, dur);
396 }
397 
404 NoteOnEvent::NoteOnEvent(int ch, int key, int vel) : KeyEvent()
405 {
406  snd_seq_ev_set_noteon(&m_event, ch, key, vel);
407 }
408 
415 NoteOffEvent::NoteOffEvent(int ch, int key, int vel) : KeyEvent()
416 {
417  snd_seq_ev_set_noteoff(&m_event, ch, key, vel);
418 }
419 
426 KeyPressEvent::KeyPressEvent(int ch, int key, int vel) : KeyEvent()
427 {
428  snd_seq_ev_set_keypress(&m_event, ch, key, vel);
429 }
430 
437 ControllerEvent::ControllerEvent(int ch, int cc, int val) : ChannelEvent()
438 {
439  snd_seq_ev_set_controller(&m_event, ch, cc, val);
440 }
441 
448 {
449  snd_seq_ev_set_pgmchange(&m_event, ch, val);
450 }
451 
458 {
459  snd_seq_ev_set_pitchbend(&m_event, ch, val);
460 }
461 
468 {
469  snd_seq_ev_set_chanpress(&m_event, ch, val);
470 }
471 
476  : SequencerEvent()
477 {
478  m_data.clear();
479  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
480 }
481 
486 VariableEvent::VariableEvent(snd_seq_event_t* event)
487  : SequencerEvent(event)
488 {
489  m_data = QByteArray((char *) event->data.ext.ptr,
490  event->data.ext.len);
491  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
492 }
493 
498 VariableEvent::VariableEvent(const QByteArray& data)
499  : SequencerEvent()
500 {
501  m_data = data;
502  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
503 }
504 
510  : SequencerEvent()
511 {
512  m_data = other.m_data;
513  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
514 }
515 
521 VariableEvent::VariableEvent(const unsigned int datalen, char* dataptr)
522  : SequencerEvent()
523 {
524  m_data = QByteArray(dataptr, datalen);
525  snd_seq_ev_set_variable( &m_event, m_data.size(), m_data.data() );
526 }
527 
534 {
535  m_event = other.m_event;
536  m_data = other.m_data;
537  snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
538  return *this;
539 }
540 
545  : VariableEvent()
546 {
547  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
548 }
549 
554 SysExEvent::SysExEvent(snd_seq_event_t* event)
555  : VariableEvent(event)
556 {
557  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
558 }
559 
564 SysExEvent::SysExEvent(const QByteArray& data)
565  : VariableEvent(data)
566 {
567  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
568 }
569 
575  : VariableEvent(other)
576 {
577  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
578 }
579 
585 SysExEvent::SysExEvent(const unsigned int datalen, char* dataptr)
586  : VariableEvent( datalen, dataptr )
587 {
588  snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
589 }
590 
595  : VariableEvent(), m_textType(1)
596 {
597  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
598 }
599 
604 TextEvent::TextEvent(snd_seq_event_t* event)
605  : VariableEvent(event), m_textType(1)
606 {
607  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
608 }
609 
615 TextEvent::TextEvent(const QString& text, const int textType)
616  : VariableEvent(text.toUtf8()), m_textType(textType)
617 {
618  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
619 }
620 
626  : VariableEvent(other)
627 {
628  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
629  m_textType = other.getTextType();
630 }
631 
637 TextEvent::TextEvent(const unsigned int datalen, char* dataptr)
638  : VariableEvent(datalen, dataptr), m_textType(1)
639 {
640  setSequencerType(SND_SEQ_EVENT_USR_VAR0);
641 }
642 
647 QString TextEvent::getText() const
648 {
649  return QString::fromUtf8(m_data.data(), m_data.size());
650 }
651 
657 {
658  return m_textType;
659 }
660 
665 SystemEvent::SystemEvent(const snd_seq_event_type_t type) : SequencerEvent()
666 {
667  snd_seq_ev_set_fixed(&m_event);
668  setSequencerType(type);
669 }
670 
677 QueueControlEvent::QueueControlEvent(snd_seq_event_type_t type, int queue, int value)
678  : SequencerEvent()
679 {
680  snd_seq_ev_set_queue_control(&m_event, type, queue, value);
681 }
682 
688 ValueEvent::ValueEvent(const snd_seq_event_type_t type, int val) : SequencerEvent()
689 {
690  snd_seq_ev_set_fixed(&m_event);
691  setSequencerType(type);
692  setValue(val);
693 }
694 
700 TempoEvent::TempoEvent(int queue, int tempo) : QueueControlEvent()
701 {
702  snd_seq_ev_set_queue_tempo(&m_event, queue, tempo);
703 }
704 
709 {
710  snd_seq_remove_events_malloc(&m_Info);
711 }
712 
718 {
719  snd_seq_remove_events_malloc(&m_Info);
720  snd_seq_remove_events_copy(m_Info, other.m_Info);
721 }
722 
727 RemoveEvents::RemoveEvents(snd_seq_remove_events_t* other)
728 {
729  snd_seq_remove_events_malloc(&m_Info);
730  snd_seq_remove_events_copy(m_Info, other);
731 }
732 
737 {
738  snd_seq_remove_events_free(m_Info);
739 }
740 
747 {
748  return new RemoveEvents(m_Info);
749 }
750 
758 {
759  snd_seq_remove_events_copy(m_Info, other.m_Info);
760  return *this;
761 }
762 
767 int
769 {
770  return snd_seq_remove_events_sizeof();
771 }
772 
778 int
780 {
781  return snd_seq_remove_events_get_channel(m_Info);
782 }
783 
789 unsigned int
791 {
792  return snd_seq_remove_events_get_condition(m_Info);
793 }
794 
800 const snd_seq_addr_t*
802 {
803  return snd_seq_remove_events_get_dest(m_Info);
804 }
805 
811 int
813 {
814  return snd_seq_remove_events_get_event_type(m_Info);
815 }
816 
822 int
824 {
825  return snd_seq_remove_events_get_queue(m_Info);
826 }
827 
833 int
835 {
836  return snd_seq_remove_events_get_tag(m_Info);
837 }
838 
844 const snd_seq_timestamp_t*
846 {
847  return snd_seq_remove_events_get_time(m_Info);
848 }
849 
855 void
857 {
858  snd_seq_remove_events_set_channel(m_Info, chan);
859 }
860 
879 void
880 RemoveEvents::setCondition(unsigned int cond)
881 {
882  snd_seq_remove_events_set_condition(m_Info, cond);
883 }
884 
890 void
891 RemoveEvents::setDest(const snd_seq_addr_t* dest)
892 {
893  snd_seq_remove_events_set_dest(m_Info, dest);
894 }
895 
901 void
903 {
904  snd_seq_remove_events_set_event_type(m_Info, type);
905 }
906 
912 void
914 {
915  snd_seq_remove_events_set_queue(m_Info, queue);
916 }
917 
923 void
925 {
926  snd_seq_remove_events_set_tag(m_Info, tag);
927 }
928 
934 void
935 RemoveEvents::setTime(const snd_seq_timestamp_t* time)
936 {
937  snd_seq_remove_events_set_time(m_Info, time);
938 }
939 
945 MidiCodec::MidiCodec( int bufsize, QObject* parent ) : QObject(parent)
946 {
947  CHECK_ERROR(snd_midi_event_new(bufsize, &m_Info));
948 }
949 
954 {
955  snd_midi_event_free(m_Info);
956 }
957 
961 void
963 {
964  snd_midi_event_init(m_Info);
965 }
966 
974 long
975 MidiCodec::decode(unsigned char *buf,
976  long count,
977  const snd_seq_event_t *ev)
978 {
979  return CHECK_WARNING(snd_midi_event_decode(m_Info, buf, count, ev));
980 }
981 
989 long
990 MidiCodec::encode(const unsigned char *buf,
991  long count,
992  snd_seq_event_t *ev)
993 {
994  return CHECK_WARNING(snd_midi_event_encode(m_Info, buf, count, ev));
995 }
996 
1003 long
1005  snd_seq_event_t *ev)
1006 {
1007  return CHECK_WARNING(snd_midi_event_encode_byte(m_Info, c, ev));
1008 }
1009 
1014 void
1016 {
1017  snd_midi_event_no_status(m_Info, enable ? 0 : 1);
1018 }
1019 
1023 void
1025 {
1026  snd_midi_event_reset_decode(m_Info);
1027 }
1028 
1032 void
1034 {
1035  snd_midi_event_reset_encode(m_Info);
1036 }
1037 
1042 void
1044 {
1045  CHECK_WARNING(snd_midi_event_resize_buffer(m_Info, bufsize));
1046 }
1047 
1048 } /* namespace drumstick */
QString getText() const
Gets the event&#39;s text content.
Definition: alsaevent.cpp:647
void setSequencerType(const snd_seq_event_type_t eventType)
Sets the event&#39;s ALSA sequencer type.
Definition: alsaevent.cpp:222
ALSA Event representing a queue control command.
Definition: alsaevent.h:455
void resetDecoder()
Reset MIDI decode parser.
Definition: alsaevent.cpp:1024
void setChannel(int chan)
Gets the MIDI channel.
Definition: alsaevent.cpp:856
void init()
CODEC initialization.
Definition: alsaevent.cpp:962
Base class for the events having Key and Velocity properties.
Definition: alsaevent.h:172
void setDirect()
Sets the event to be immediately delivered, not queued/scheduled.
Definition: alsaevent.cpp:268
void setTime(const snd_seq_timestamp_t *time)
Sets the timestamp.
Definition: alsaevent.cpp:935
void setEventType(int type)
Sets the event type.
Definition: alsaevent.cpp:902
ChanPressEvent()
Default constructor.
Definition: alsaevent.h:364
void setQueue(int queue)
Sets the queue number.
Definition: alsaevent.cpp:913
const QEvent::Type SequencerEventType
Constant SequencerEventType is the QEvent::type() of any SequencerEvent object to be used to check th...
Definition: alsaevent.h:40
QByteArray m_data
Clone this object returning a pointer to the new object.
Definition: alsaevent.h:395
const snd_seq_addr_t * getDest()
Gets the destination.
Definition: alsaevent.cpp:801
static bool isPort(const SequencerEvent *event)
Checks if the event&#39;s type is of type port.
Definition: alsaevent.cpp:156
void free() __attribute__((deprecated))
Releases the event record.
Definition: alsaevent.cpp:372
Base class for variable length events.
Definition: alsaevent.h:379
static bool isConnectionChange(const SequencerEvent *event)
Checks if the event&#39;s type is of type connection change.
Definition: alsaevent.cpp:184
RemoveEvents * clone()
Create a new object copied from this object and return a pointer to the copy.
Definition: alsaevent.cpp:746
The QObject class is the base class of all Qt objects.
Base class for the event&#39;s hierarchy.
Definition: alsaevent.h:53
Event representing a MIDI system exclusive event.
Definition: alsaevent.h:401
VariableEvent()
Default constructor.
Definition: alsaevent.cpp:475
long encode(const unsigned char *buf, long count, snd_seq_event_t *ev)
Encode from byte stream.
Definition: alsaevent.cpp:990
unsigned int getCondition()
Gets the condition.
Definition: alsaevent.cpp:790
VariableEvent & operator=(const VariableEvent &other)
Assignment operator.
Definition: alsaevent.cpp:533
void setTag(int tag)
Sets the numeric tag.
Definition: alsaevent.cpp:924
NoteOnEvent()
Default constructor.
Definition: alsaevent.h:242
NoteEvent()
Default constructor.
Definition: alsaevent.h:215
void setRaw8(const unsigned int n, const unsigned char value)
Sets an event&#39;s raw 8 bits parameter.
Definition: alsaevent.cpp:363
~MidiCodec()
Destructor.
Definition: alsaevent.cpp:953
virtual ~RemoveEvents()
Destructor.
Definition: alsaevent.cpp:736
int getEventType()
Gets the event type.
Definition: alsaevent.cpp:812
Event representing a SMF text event.
Definition: alsaevent.h:419
SequencerEvent & operator=(const SequencerEvent &other)
Assignment operator.
Definition: alsaevent.cpp:131
void enableRunningStatus(bool enable)
Enable MIDI running status (command merge)
Definition: alsaevent.cpp:1015
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:279
PitchBendEvent()
Default constructor.
Definition: alsaevent.h:345
void resizeBuffer(int bufsize)
Resize the CODEC buffer.
Definition: alsaevent.cpp:1043
Base class for the events having a Channel property.
Definition: alsaevent.h:148
void setSource(const unsigned char port)
Sets the event&#39;s source port ID.
Definition: alsaevent.cpp:243
int getTag()
Gets the numeric tag.
Definition: alsaevent.cpp:834
int getChannel()
Gets the MIDI channel.
Definition: alsaevent.cpp:779
void setPriority(const bool high)
Sets the priority of the event.
Definition: alsaevent.cpp:305
void setValue(const int v)
Sets the event&#39;s value.
Definition: alsaevent.h:505
ControllerEvent()
Default constructor.
Definition: alsaevent.h:287
Auxiliary class to remove events from an ALSA queue.
Definition: alsaevent.h:586
const snd_seq_timestamp_t * getTime()
Gets the timestamp.
Definition: alsaevent.cpp:845
void resetEncoder()
Reset MIDI encode parser.
Definition: alsaevent.cpp:1033
void setRaw32(const unsigned int n, const unsigned int value)
Sets an event&#39;s raw 32 bits parameter.
Definition: alsaevent.cpp:341
int m_textType
Clone this object returning a pointer to the new object.
Definition: alsaevent.h:432
static bool isSubscription(const SequencerEvent *event)
Checks if the event&#39;s type is a subscription.
Definition: alsaevent.cpp:143
unsigned int getRaw32(const unsigned int n) const
Gets an event&#39;s raw 32 bits parameter.
Definition: alsaevent.cpp:330
QueueControlEvent()
Default constructor.
Definition: alsaevent.h:459
ValueEvent()
Default constructor.
Definition: alsaevent.h:498
static bool isChannel(const SequencerEvent *event)
Checks if the event&#39;s type is a Channel Voice message.
Definition: alsaevent.cpp:204
MidiCodec(int bufsize, QObject *parent=0)
MidiCodec constructor.
Definition: alsaevent.cpp:945
void setTag(const unsigned char aTag)
Sets the event&#39;s tag.
Definition: alsaevent.cpp:315
SysExEvent()
Default constructor.
Definition: alsaevent.cpp:544
void setBroadcast()
Sets the event&#39;s destination to be all queues/clients/ports/channels.
Definition: alsaevent.cpp:259
void setCondition(unsigned int cond)
Sets the flags of the conditional event&#39;s removal.
Definition: alsaevent.cpp:880
RemoveEvents()
Default constructor.
Definition: alsaevent.cpp:708
int getTextType() const
Gets the event&#39;s SMF text type.
Definition: alsaevent.cpp:656
ProgramChangeEvent()
Default constructor.
Definition: alsaevent.h:326
NoteOffEvent()
Default constructor.
Definition: alsaevent.h:257
unsigned char getRaw8(const unsigned int n) const
Gets an event&#39;s raw 8 bits parameter.
Definition: alsaevent.cpp:352
long decode(unsigned char *buf, long count, const snd_seq_event_t *ev)
Decode from event to bytes.
Definition: alsaevent.cpp:975
TempoEvent()
Default constructor.
Definition: alsaevent.h:517
void setSubscribers()
Sets the event&#39;s destination to be all the subscribers of the source port.
Definition: alsaevent.cpp:251
snd_seq_event_t m_event
ALSA sequencer event record.
Definition: alsaevent.h:142
void setDest(const snd_seq_addr_t *dest)
Set the destination address.
Definition: alsaevent.cpp:891
#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&#39;s type is of type client.
Definition: alsaevent.cpp:170
The QEvent class is the base class of all event classes.
int getQueue()
Gets the queue number.
Definition: alsaevent.cpp:823
TextEvent()
Default constructor.
Definition: alsaevent.cpp:594
int getEncodedLength()
Gets the encoded length of the event record.
Definition: alsaevent.cpp:381
Classes managing ALSA Sequencer events.
KeyPressEvent()
Default constructor.
Definition: alsaevent.h:272
SequencerEvent()
Default constructor.
Definition: alsaevent.cpp:100
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:291
SystemEvent()
Default constructor.
Definition: alsaevent.h:442
int getSizeOfInfo() const
Gets the allocated size of the ALSA remove events object.
Definition: alsaevent.cpp:768
RemoveEvents & operator=(const RemoveEvents &other)
Assignment operator.
Definition: alsaevent.cpp:757
void setDestination(const unsigned char client, const unsigned char port)
Sets the client:port destination of the event.
Definition: alsaevent.cpp:233