drumstick  1.1.0
alsaevent.h
Go to the documentation of this file.
1 /*
2  MIDI Sequencer C++ library
3  Copyright (C) 2006-2016, 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 #ifndef DRUMSTICK_ALSAEVENT_H
21 #define DRUMSTICK_ALSAEVENT_H
22 
23 #include "drumstickcommon.h"
24 #include <QEvent>
25 
34 namespace drumstick {
35 
40 const QEvent::Type SequencerEventType = QEvent::Type(QEvent::User + 4154); // :-)
41 
45 #define CLONE_EVENT_DECLARATION(T) virtual T* clone() { return new T(&m_event); }
46 
53 class DRUMSTICK_EXPORT SequencerEvent : public QEvent
54 {
55 public:
57  SequencerEvent(const SequencerEvent& other);
58  SequencerEvent(snd_seq_event_t* event);
60  virtual ~SequencerEvent() {}
61 
62  SequencerEvent& operator=(const SequencerEvent& other);
63  void setSequencerType(const snd_seq_event_type_t eventType);
69  snd_seq_event_type_t getSequencerType() const { return m_event.type; }
70  void setDestination(const unsigned char client, const unsigned char port);
71  void setSource(const unsigned char port);
77  unsigned char getSourceClient() const { return m_event.source.client; }
83  unsigned char getSourcePort() const { return m_event.source.port; }
89  snd_seq_tick_time_t getTick() const { return m_event.time.tick; }
95  unsigned int getRealTimeSecs() const { return m_event.time.time.tv_sec; }
101  unsigned int getRealTimeNanos() const { return m_event.time.time.tv_nsec; }
102  void setSubscribers();
103  void setBroadcast();
104  void setDirect();
105  void scheduleTick(const int queue, const int tick, const bool relative);
106  void scheduleReal(const int queue, const ulong secs, const ulong nanos, const bool relative);
107  void setPriority(const bool high);
113  unsigned char getTag() const { return m_event.tag; }
114  void setTag(const unsigned char aTag);
115  unsigned int getRaw32(const unsigned int n) const;
116  void setRaw32(const unsigned int n, const unsigned int value);
117  unsigned char getRaw8(const unsigned int n) const;
118  void setRaw8(const unsigned int n, const unsigned char value);
123  snd_seq_event_t* getHandle() { return &m_event; }
124  int getEncodedLength();
125 
126  static bool isSubscription(const SequencerEvent* event);
127  static bool isPort(const SequencerEvent* event);
128  static bool isClient(const SequencerEvent* event);
129  static bool isConnectionChange(const SequencerEvent* event);
130  static bool isChannel(const SequencerEvent* event);
131 
134 
135 protected:
136  void free() __attribute__((deprecated));
137 
142  snd_seq_event_t m_event;
143 };
144 
148 class DRUMSTICK_EXPORT ChannelEvent : public SequencerEvent
149 {
150 public:
152  ChannelEvent() : SequencerEvent() {}
154  ChannelEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
160  void setChannel(const MidiByte c) { m_event.data.note.channel = (c & 0xf); }
166  int getChannel() const { return m_event.data.note.channel; }
167 };
168 
172 class DRUMSTICK_EXPORT KeyEvent : public ChannelEvent
173 {
174 public:
178  KeyEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
184  int getKey() const { return m_event.data.note.note; }
190  void setKey(const MidiByte b) { m_event.data.note.note = b; }
196  int getVelocity() const { return m_event.data.note.velocity; }
202  void setVelocity(const MidiByte b) { m_event.data.note.velocity = b; }
203 };
204 
211 class DRUMSTICK_EXPORT NoteEvent : public KeyEvent
212 {
213 public:
215  NoteEvent() : KeyEvent() { m_event.type = SND_SEQ_EVENT_NOTE; }
217  NoteEvent(snd_seq_event_t* event) : KeyEvent(event) {}
218  NoteEvent(const int ch, const int key, const int vel, const int dur);
224  ulong getDuration() const { return m_event.data.note.duration; }
230  void setDuration(const ulong d) { m_event.data.note.duration = d; }
233 };
234 
238 class DRUMSTICK_EXPORT NoteOnEvent : public KeyEvent
239 {
240 public:
242  NoteOnEvent() : KeyEvent() { m_event.type = SND_SEQ_EVENT_NOTEON; }
244  NoteOnEvent(snd_seq_event_t* event) : KeyEvent(event) {}
245  NoteOnEvent(const int ch, const int key, const int vel);
248 };
249 
253 class DRUMSTICK_EXPORT NoteOffEvent : public KeyEvent
254 {
255 public:
257  NoteOffEvent() : KeyEvent() { m_event.type = SND_SEQ_EVENT_NOTEOFF; }
259  NoteOffEvent(snd_seq_event_t* event) : KeyEvent(event) {}
260  NoteOffEvent(const int ch, const int key, const int vel);
263 };
264 
268 class DRUMSTICK_EXPORT KeyPressEvent : public KeyEvent
269 {
270 public:
272  KeyPressEvent() : KeyEvent() { m_event.type = SND_SEQ_EVENT_KEYPRESS; }
274  KeyPressEvent(snd_seq_event_t* event) : KeyEvent(event) {}
275  KeyPressEvent(const int ch, const int key, const int vel);
278 };
279 
283 class DRUMSTICK_EXPORT ControllerEvent : public ChannelEvent
284 {
285 public:
289  ControllerEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
290  ControllerEvent(const int ch, const int cc, const int val);
296  uint getParam() const { return m_event.data.control.param; }
302  void setParam( const uint p ) { m_event.data.control.param = p; }
308  int getValue() const { return m_event.data.control.value; }
314  void setValue( const int v ) { m_event.data.control.value = v; }
317 };
318 
322 class DRUMSTICK_EXPORT ProgramChangeEvent : public ChannelEvent
323 {
324 public:
326  ProgramChangeEvent() : ChannelEvent() { m_event.type = SND_SEQ_EVENT_PGMCHANGE; }
328  ProgramChangeEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
329  ProgramChangeEvent(const int ch, const int val);
331  int getValue() const { return m_event.data.control.value; }
333  void setValue( const int v ) { m_event.data.control.value = v; }
336 };
337 
341 class DRUMSTICK_EXPORT PitchBendEvent : public ChannelEvent
342 {
343 public:
345  PitchBendEvent() : ChannelEvent() { m_event.type = SND_SEQ_EVENT_PITCHBEND; }
347  PitchBendEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
348  PitchBendEvent(const int ch, const int val);
350  int getValue() const { return m_event.data.control.value; }
352  void setValue( const int v ) { m_event.data.control.value = v; }
355 };
356 
360 class DRUMSTICK_EXPORT ChanPressEvent : public ChannelEvent
361 {
362 public:
364  ChanPressEvent() : ChannelEvent() { m_event.type = SND_SEQ_EVENT_CHANPRESS; }
366  ChanPressEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
367  ChanPressEvent( const int ch, const int val);
369  int getValue() const { return m_event.data.control.value; }
371  void setValue( const int v ) { m_event.data.control.value = v; }
374 };
375 
379 class DRUMSTICK_EXPORT VariableEvent : public SequencerEvent
380 {
381 public:
382  VariableEvent();
383  VariableEvent(snd_seq_event_t* event);
384  VariableEvent(const QByteArray& data);
385  VariableEvent(const VariableEvent& other);
386  VariableEvent(const unsigned int datalen, char* dataptr);
387  VariableEvent& operator=(const VariableEvent& other);
389  unsigned int getLength() const { return m_event.data.ext.len; }
391  const char* getData() const { return static_cast<const char*>(m_event.data.ext.ptr); }
394 protected:
395  QByteArray m_data;
396 };
397 
401 class DRUMSTICK_EXPORT SysExEvent : public VariableEvent
402 {
403 public:
404  SysExEvent();
405  SysExEvent(snd_seq_event_t* event);
406  SysExEvent(const QByteArray& data);
407  SysExEvent(const SysExEvent& other);
408  SysExEvent(const unsigned int datalen, char* dataptr);
411 };
412 
419 class DRUMSTICK_EXPORT TextEvent : public VariableEvent
420 {
421 public:
422  TextEvent();
423  TextEvent(snd_seq_event_t* event);
424  explicit TextEvent(const QString& text, const int textType = 1);
425  TextEvent(const TextEvent& other);
426  TextEvent(const unsigned int datalen, char* dataptr);
427  QString getText() const;
428  int getTextType() const;
431 protected:
433 };
434 
438 class DRUMSTICK_EXPORT SystemEvent : public SequencerEvent
439 {
440 public:
444  SystemEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
445  SystemEvent(const snd_seq_event_type_t type);
448 };
449 
455 class DRUMSTICK_EXPORT QueueControlEvent : public SequencerEvent
456 {
457 public:
461  QueueControlEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
462  QueueControlEvent(const snd_seq_event_type_t type, const int queue, const int value);
464  int getQueue() const { return m_event.data.queue.queue; }
466  void setQueue(const uchar q) { m_event.data.queue.queue = q; }
468  int getValue() const { return m_event.data.queue.param.value; }
470  void setValue(const int val) { m_event.data.queue.param.value = val; }
472  uint getPosition() const { return m_event.data.queue.param.position; }
474  void setPosition(const uint pos) { m_event.data.queue.param.position = pos; }
476  snd_seq_tick_time_t getTickTime() const { return m_event.data.queue.param.time.tick; }
478  void setTickTime(const snd_seq_tick_time_t t) { m_event.data.queue.param.time.tick = t; }
480  uint getSkewBase() const { return m_event.data.queue.param.skew.base; }
482  void setSkewBase(const uint base) { m_event.data.queue.param.skew.base = base; }
484  uint getSkewValue() const { return m_event.data.queue.param.skew.value; }
486  void setSkewValue(const uint val) {m_event.data.queue.param.skew.value = val; }
489 };
490 
494 class DRUMSTICK_EXPORT ValueEvent : public SequencerEvent
495 {
496 public:
500  ValueEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
501  ValueEvent(const snd_seq_event_type_t type, const int val);
503  int getValue() const { return m_event.data.control.value; }
505  void setValue( const int v ) { m_event.data.control.value = v; }
508 };
509 
513 class DRUMSTICK_EXPORT TempoEvent : public QueueControlEvent
514 {
515 public:
519  TempoEvent(snd_seq_event_t* event) : QueueControlEvent(event) {}
520  TempoEvent(const int queue, const int tempo);
523 };
524 
528 class DRUMSTICK_EXPORT SubscriptionEvent : public SequencerEvent
529 {
530 public:
534  SubscriptionEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
536  bool subscribed() const { return (m_event.type == SND_SEQ_EVENT_PORT_SUBSCRIBED); }
538  bool unsubscribed() const { return (m_event.type == SND_SEQ_EVENT_PORT_UNSUBSCRIBED); }
540  int getSenderClient() const { return m_event.data.connect.sender.client; }
542  int getSenderPort() const { return m_event.data.connect.sender.port; }
544  int getDestClient() const { return m_event.data.connect.dest.client; }
546  int getDestPort() const { return m_event.data.connect.dest.port; }
549 };
550 
554 class DRUMSTICK_EXPORT ClientEvent : public SequencerEvent
555 {
556 public:
560  ClientEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
561  int getClient() const { return m_event.data.addr.client; }
563  CLONE_EVENT_DECLARATION(ClientEvent)
564 };
565 
569 class DRUMSTICK_EXPORT PortEvent : public ClientEvent
570 {
571 public:
575  PortEvent(snd_seq_event_t* event) : ClientEvent(event) {}
577  int getPort() const { return m_event.data.addr.port; }
580 };
581 
586 class DRUMSTICK_EXPORT RemoveEvents
587 {
588 public:
589  friend class MidiClient;
590 
591 public:
593  RemoveEvents();
594  RemoveEvents(const RemoveEvents& other);
595  RemoveEvents(snd_seq_remove_events_t* other);
596  virtual ~RemoveEvents();
597  RemoveEvents* clone();
598  RemoveEvents& operator=(const RemoveEvents& other);
599  int getSizeOfInfo() const;
600 
601  int getChannel();
602  unsigned int getCondition();
603  const snd_seq_addr_t* getDest();
604  int getEventType();
605  int getQueue();
606  int getTag();
607  const snd_seq_timestamp_t* getTime();
608  void setChannel(int chan);
609  void setCondition(unsigned int cond);
610  void setDest(const snd_seq_addr_t* dest);
611  void setEventType(int type);
612  void setQueue(int queue);
613  void setTag(int tag);
614  void setTime(const snd_seq_timestamp_t* time);
615 
616 private:
617  snd_seq_remove_events_t* m_Info;
618 };
619 
623 class DRUMSTICK_EXPORT MidiCodec : public QObject
624 {
625  Q_OBJECT
626 public:
627  explicit MidiCodec(int bufsize, QObject* parent = 0);
628  ~MidiCodec();
629 
630  void init();
631  long decode(unsigned char *buf,
632  long count,
633  const snd_seq_event_t *ev);
634  long encode(const unsigned char *buf,
635  long count,
636  snd_seq_event_t *ev);
637  long encode(int c,
638  snd_seq_event_t *ev);
639  void enableRunningStatus(bool enable);
640  void resetEncoder();
641  void resetDecoder();
642  void resizeBuffer(int bufsize);
643 private:
644  snd_midi_event_t* m_Info;
645 };
646 
647 } /* namespace drumstick */
648 
651 #endif //DRUMSTICK_ALSAEVENT_H