drumstick  1.0.2
alsaevent.h
Go to the documentation of this file.
00001 /*
00002     MIDI Sequencer C++ library
00003     Copyright (C) 2006-2015, Pedro Lopez-Cabanillas <plcl@users.sf.net>
00004 
00005     This library is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License along
00016     with this program; if not, write to the Free Software Foundation, Inc.,
00017     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 */
00019 
00020 #ifndef DRUMSTICK_ALSAEVENT_H
00021 #define DRUMSTICK_ALSAEVENT_H
00022 
00023 #include "drumstickcommon.h"
00024 #include <QEvent>
00025 
00034 namespace drumstick {
00035 
00040 const QEvent::Type SequencerEventType = QEvent::Type(QEvent::User + 4154); // :-)
00041 
00045 #define CLONE_EVENT_DECLARATION(T) virtual T* clone() { return new T(&m_event); }
00046 
00053 class DRUMSTICK_EXPORT SequencerEvent : public QEvent
00054 {
00055 public:
00056     SequencerEvent();
00057     SequencerEvent(const SequencerEvent& other);
00058     SequencerEvent(snd_seq_event_t* event);
00060     virtual ~SequencerEvent() {}
00061 
00062     SequencerEvent& operator=(const SequencerEvent& other);
00063     void setSequencerType(const snd_seq_event_type_t eventType);
00069     snd_seq_event_type_t getSequencerType() const { return m_event.type; }
00070     void setDestination(const unsigned char client, const unsigned char port);
00071     void setSource(const unsigned char port);
00077     unsigned char getSourceClient() const { return m_event.source.client; }
00083     unsigned char getSourcePort() const { return m_event.source.port; }
00089     snd_seq_tick_time_t getTick() const { return m_event.time.tick; }
00095     unsigned int getRealTimeSecs() const { return m_event.time.time.tv_sec; }
00101     unsigned int getRealTimeNanos() const { return m_event.time.time.tv_nsec; }
00102     void setSubscribers();
00103     void setBroadcast();
00104     void setDirect();
00105     void scheduleTick(const int queue, const int tick, const bool relative);
00106     void scheduleReal(const int queue, const ulong secs, const ulong nanos, const bool relative);
00107     void setPriority(const bool high);
00113     unsigned char getTag() const { return m_event.tag; }
00114     void setTag(const unsigned char aTag);
00115     unsigned int getRaw32(const unsigned int n) const;
00116     void setRaw32(const unsigned int n, const unsigned int value);
00117     unsigned char getRaw8(const unsigned int n) const;
00118     void setRaw8(const unsigned int n, const unsigned char value);
00123     snd_seq_event_t* getHandle() { return &m_event; }
00124     int getEncodedLength();
00125 
00126     static bool isSubscription(const SequencerEvent* event);
00127     static bool isPort(const SequencerEvent* event);
00128     static bool isClient(const SequencerEvent* event);
00129     static bool isConnectionChange(const SequencerEvent* event);
00130     static bool isChannel(const SequencerEvent* event);
00131 
00133     CLONE_EVENT_DECLARATION(SequencerEvent);
00134 
00135 protected:
00136     void free() __attribute__((deprecated));
00137 
00142     snd_seq_event_t m_event;
00143 };
00144 
00148 class DRUMSTICK_EXPORT ChannelEvent : public SequencerEvent
00149 {
00150 public:
00152     ChannelEvent() : SequencerEvent() {}
00154     ChannelEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
00160     void setChannel(const MidiByte c) { m_event.data.note.channel = (c & 0xf); }
00166     int getChannel() const { return m_event.data.note.channel; }
00167 };
00168 
00172 class DRUMSTICK_EXPORT KeyEvent : public ChannelEvent
00173 {
00174 public:
00176     KeyEvent() : ChannelEvent() {}
00178     KeyEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
00184     int getKey() const { return m_event.data.note.note; }
00190     void setKey(const MidiByte b) { m_event.data.note.note = b; }
00196     int getVelocity() const { return m_event.data.note.velocity; }
00202     void setVelocity(const MidiByte b) { m_event.data.note.velocity = b; }
00203 };
00204 
00211 class DRUMSTICK_EXPORT NoteEvent : public KeyEvent
00212 {
00213 public:
00215     NoteEvent() : KeyEvent() { m_event.type = SND_SEQ_EVENT_NOTE; }
00217     NoteEvent(snd_seq_event_t* event) : KeyEvent(event) {}
00218     NoteEvent(const int ch, const int key, const int vel, const int dur);
00224     ulong getDuration() const { return m_event.data.note.duration; }
00230     void setDuration(const ulong d) { m_event.data.note.duration = d; }
00232     CLONE_EVENT_DECLARATION(NoteEvent)
00233 };
00234 
00238 class DRUMSTICK_EXPORT NoteOnEvent : public KeyEvent
00239 {
00240 public:
00242     NoteOnEvent() : KeyEvent() { m_event.type = SND_SEQ_EVENT_NOTEON; }
00244     NoteOnEvent(snd_seq_event_t* event) : KeyEvent(event) {}
00245     NoteOnEvent(const int ch, const int key, const int vel);
00247     CLONE_EVENT_DECLARATION(NoteOnEvent)
00248 };
00249 
00253 class DRUMSTICK_EXPORT NoteOffEvent : public KeyEvent
00254 {
00255 public:
00257     NoteOffEvent() : KeyEvent() { m_event.type = SND_SEQ_EVENT_NOTEOFF; }
00259     NoteOffEvent(snd_seq_event_t* event) : KeyEvent(event) {}
00260     NoteOffEvent(const int ch, const int key, const int vel);
00262     CLONE_EVENT_DECLARATION(NoteOffEvent)
00263 };
00264 
00268 class DRUMSTICK_EXPORT KeyPressEvent : public KeyEvent
00269 {
00270 public:
00272     KeyPressEvent() : KeyEvent() { m_event.type = SND_SEQ_EVENT_KEYPRESS; }
00274     KeyPressEvent(snd_seq_event_t* event) : KeyEvent(event) {}
00275     KeyPressEvent(const int ch, const int key, const int vel);
00277     CLONE_EVENT_DECLARATION(KeyPressEvent)
00278 };
00279 
00283 class DRUMSTICK_EXPORT ControllerEvent : public ChannelEvent
00284 {
00285 public:
00287     ControllerEvent() : ChannelEvent() {}
00289     ControllerEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
00290     ControllerEvent(const int ch, const int cc, const int val);
00296     uint getParam() const { return m_event.data.control.param; }
00302     void setParam( const uint p ) { m_event.data.control.param = p; }
00308     int getValue() const { return m_event.data.control.value; }
00314     void setValue( const int v ) { m_event.data.control.value = v; }
00316     CLONE_EVENT_DECLARATION(ControllerEvent)
00317 };
00318 
00322 class DRUMSTICK_EXPORT ProgramChangeEvent : public ChannelEvent
00323 {
00324 public:
00326     ProgramChangeEvent() : ChannelEvent() { m_event.type = SND_SEQ_EVENT_PGMCHANGE; }
00328     ProgramChangeEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
00329     ProgramChangeEvent(const int ch, const int val);
00331     int getValue() const { return m_event.data.control.value; }
00333     void setValue( const int v ) { m_event.data.control.value = v; }
00335     CLONE_EVENT_DECLARATION(ProgramChangeEvent)
00336 };
00337 
00341 class DRUMSTICK_EXPORT PitchBendEvent : public ChannelEvent
00342 {
00343 public:
00345     PitchBendEvent() : ChannelEvent() { m_event.type = SND_SEQ_EVENT_PITCHBEND; }
00347     PitchBendEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
00348     PitchBendEvent(const int ch, const int val);
00350     int getValue() const { return m_event.data.control.value; }
00352     void setValue( const int v ) { m_event.data.control.value = v; }
00354     CLONE_EVENT_DECLARATION(PitchBendEvent)
00355 };
00356 
00360 class DRUMSTICK_EXPORT ChanPressEvent : public ChannelEvent
00361 {
00362 public:
00364     ChanPressEvent() : ChannelEvent() { m_event.type = SND_SEQ_EVENT_CHANPRESS; }
00366     ChanPressEvent(snd_seq_event_t* event) : ChannelEvent(event) {}
00367     ChanPressEvent( const int ch, const int val);
00369     int getValue() const { return m_event.data.control.value; }
00371     void setValue( const int v ) { m_event.data.control.value = v; }
00373     CLONE_EVENT_DECLARATION(ChanPressEvent)
00374 };
00375 
00379 class DRUMSTICK_EXPORT VariableEvent : public SequencerEvent
00380 {
00381 public:
00382     VariableEvent();
00383     VariableEvent(snd_seq_event_t* event);
00384     VariableEvent(const QByteArray& data);
00385     VariableEvent(const VariableEvent& other);
00386     VariableEvent(const unsigned int datalen, char* dataptr);
00387     VariableEvent& operator=(const VariableEvent& other);
00389     unsigned int getLength() const { return m_event.data.ext.len; }
00391     const char* getData() const { return static_cast<const char*>(m_event.data.ext.ptr); }
00393     CLONE_EVENT_DECLARATION(VariableEvent)
00394 protected:
00395     QByteArray m_data;
00396 };
00397 
00401 class DRUMSTICK_EXPORT SysExEvent : public VariableEvent
00402 {
00403 public:
00404     SysExEvent();
00405     SysExEvent(snd_seq_event_t* event);
00406     SysExEvent(const QByteArray& data);
00407     SysExEvent(const SysExEvent& other);
00408     SysExEvent(const unsigned int datalen, char* dataptr);
00410     CLONE_EVENT_DECLARATION(SysExEvent)
00411 };
00412 
00419 class DRUMSTICK_EXPORT TextEvent : public VariableEvent
00420 {
00421 public:
00422     TextEvent();
00423     TextEvent(snd_seq_event_t* event);
00424     explicit TextEvent(const QString& text, const int textType = 1);
00425     TextEvent(const TextEvent& other);
00426     TextEvent(const unsigned int datalen, char* dataptr);
00427     QString getText() const;
00428     int getTextType() const;
00430     CLONE_EVENT_DECLARATION(TextEvent)
00431 protected:
00432     int m_textType;
00433 };
00434 
00438 class DRUMSTICK_EXPORT SystemEvent : public SequencerEvent
00439 {
00440 public:
00442     SystemEvent() : SequencerEvent() {}
00444     SystemEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
00445     SystemEvent(const snd_seq_event_type_t type);
00447     CLONE_EVENT_DECLARATION(SystemEvent)
00448 };
00449 
00455 class DRUMSTICK_EXPORT QueueControlEvent : public SequencerEvent
00456 {
00457 public:
00459     QueueControlEvent() : SequencerEvent() {}
00461     QueueControlEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
00462     QueueControlEvent(const snd_seq_event_type_t type, const int queue, const int value);
00464     int getQueue() const { return m_event.data.queue.queue; }
00466     void setQueue(const uchar q) { m_event.data.queue.queue = q; }
00468     int getValue() const { return m_event.data.queue.param.value; }
00470     void setValue(const int val) { m_event.data.queue.param.value = val; }
00472     uint getPosition() const { return m_event.data.queue.param.position; }
00474     void setPosition(const uint pos) { m_event.data.queue.param.position = pos; }
00476     snd_seq_tick_time_t getTickTime() const { return m_event.data.queue.param.time.tick; }
00478     void setTickTime(const snd_seq_tick_time_t t) { m_event.data.queue.param.time.tick = t; }
00480     uint getSkewBase() const { return m_event.data.queue.param.skew.base;  }
00482     void setSkewBase(const uint base) { m_event.data.queue.param.skew.base = base; }
00484     uint getSkewValue() const { return m_event.data.queue.param.skew.value;  }
00486     void setSkewValue(const uint val) {m_event.data.queue.param.skew.value = val; }
00488     CLONE_EVENT_DECLARATION(QueueControlEvent)
00489 };
00490 
00494 class DRUMSTICK_EXPORT ValueEvent : public SequencerEvent
00495 {
00496 public:
00498     ValueEvent() : SequencerEvent() {}
00500     ValueEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
00501     ValueEvent(const snd_seq_event_type_t type, const int val);
00503     int getValue() const { return m_event.data.control.value; }
00505     void setValue( const int v ) { m_event.data.control.value = v; }
00507     CLONE_EVENT_DECLARATION(ValueEvent)
00508 };
00509 
00513 class DRUMSTICK_EXPORT TempoEvent : public QueueControlEvent
00514 {
00515 public:
00517     TempoEvent() : QueueControlEvent() {}
00519     TempoEvent(snd_seq_event_t* event) : QueueControlEvent(event) {}
00520     TempoEvent(const int queue, const int tempo);
00522     CLONE_EVENT_DECLARATION(TempoEvent)
00523 };
00524 
00528 class DRUMSTICK_EXPORT SubscriptionEvent : public SequencerEvent
00529 {
00530 public:
00532     SubscriptionEvent() : SequencerEvent() {}
00534     SubscriptionEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
00536     bool subscribed() const { return (m_event.type == SND_SEQ_EVENT_PORT_SUBSCRIBED); }
00538     bool unsubscribed() const { return (m_event.type == SND_SEQ_EVENT_PORT_UNSUBSCRIBED); }
00540     int getSenderClient() const { return m_event.data.connect.sender.client; }
00542     int getSenderPort() const { return m_event.data.connect.sender.port; }
00544     int getDestClient() const { return m_event.data.connect.dest.client; }
00546     int getDestPort() const { return m_event.data.connect.dest.port; }
00548     CLONE_EVENT_DECLARATION(SubscriptionEvent)
00549 };
00550 
00554 class DRUMSTICK_EXPORT ClientEvent : public SequencerEvent
00555 {
00556 public:
00558     ClientEvent() : SequencerEvent() {}
00560     ClientEvent(snd_seq_event_t* event) : SequencerEvent(event) {}
00561     int getClient() const { return m_event.data.addr.client; }
00563     CLONE_EVENT_DECLARATION(ClientEvent)
00564 };
00565 
00569 class DRUMSTICK_EXPORT PortEvent : public ClientEvent
00570 {
00571 public:
00573     PortEvent() : ClientEvent() {}
00575     PortEvent(snd_seq_event_t* event) : ClientEvent(event) {}
00577     int getPort() const { return m_event.data.addr.port; }
00579     CLONE_EVENT_DECLARATION(PortEvent)
00580 };
00581 
00586 class DRUMSTICK_EXPORT RemoveEvents
00587 {
00588 public:
00589     friend class MidiClient;
00590 
00591 public:
00593     RemoveEvents();
00594     RemoveEvents(const RemoveEvents& other);
00595     RemoveEvents(snd_seq_remove_events_t* other);
00596     virtual ~RemoveEvents();
00597     RemoveEvents* clone();
00598     RemoveEvents& operator=(const RemoveEvents& other);
00599     int getSizeOfInfo() const;
00600 
00601     int getChannel();
00602     unsigned int getCondition();
00603     const snd_seq_addr_t* getDest();
00604     int getEventType();
00605     int getQueue();
00606     int getTag();
00607     const snd_seq_timestamp_t* getTime();
00608     void setChannel(int chan);
00609     void setCondition(unsigned int cond);
00610     void setDest(const snd_seq_addr_t* dest);
00611     void setEventType(int type);
00612     void setQueue(int queue);
00613     void setTag(int tag);
00614     void setTime(const snd_seq_timestamp_t* time);
00615 
00616 private:
00617     snd_seq_remove_events_t* m_Info;
00618 };
00619 
00623 class DRUMSTICK_EXPORT MidiCodec : public QObject
00624 {
00625     Q_OBJECT
00626 public:
00627     explicit MidiCodec(int bufsize, QObject* parent = 0);
00628     ~MidiCodec();
00629 
00630     void init();
00631     long decode(unsigned char *buf,
00632                 long count,
00633                 const snd_seq_event_t *ev);
00634     long encode(const unsigned char *buf,
00635                 long count,
00636                 snd_seq_event_t *ev);
00637     long encode(int c,
00638                 snd_seq_event_t *ev);
00639     void enableRunningStatus(bool enable);
00640     void resetEncoder();
00641     void resetDecoder();
00642     void resizeBuffer(int bufsize);
00643 private:
00644     snd_midi_event_t* m_Info;
00645 };
00646 
00647 } /* namespace drumstick */
00648 
00651 #endif //DRUMSTICK_ALSAEVENT_H