drumstick  0.5.0
alsaevent.cpp
Go to the documentation of this file.
00001 /*
00002     MIDI Sequencer C++ library
00003     Copyright (C) 2006-2010, 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 #include "alsaevent.h"
00021 
00033 namespace drumstick {
00034 
00100 SequencerEvent::SequencerEvent() : QEvent(SequencerEventType)
00101 {
00102     snd_seq_ev_clear( &m_event );
00103 }
00104 
00109 SequencerEvent::SequencerEvent(snd_seq_event_t* event) : QEvent(SequencerEventType)
00110 {
00111     snd_seq_ev_clear( &m_event );
00112     m_event = *event;
00113 }
00114 
00119 SequencerEvent::SequencerEvent(const SequencerEvent& other) : QEvent(SequencerEventType)
00120 {
00121     snd_seq_ev_clear( &m_event );
00122     m_event = other.m_event;
00123 }
00124 
00130 SequencerEvent&
00131 SequencerEvent::operator=(const SequencerEvent& other)
00132 {
00133     m_event = other.m_event;
00134     return *this;
00135 }
00136 
00142 bool
00143 SequencerEvent::isSubscription(const SequencerEvent* event)
00144 {
00145     snd_seq_event_type_t te = event->getSequencerType();
00146     return ( te == SND_SEQ_EVENT_PORT_SUBSCRIBED ||
00147              te == SND_SEQ_EVENT_PORT_UNSUBSCRIBED );
00148 }
00149 
00155 bool
00156 SequencerEvent::isPort(const SequencerEvent* event)
00157 {
00158     snd_seq_event_type_t te = event->getSequencerType();
00159     return ( te == SND_SEQ_EVENT_PORT_START ||
00160              te == SND_SEQ_EVENT_PORT_EXIT ||
00161              te == SND_SEQ_EVENT_PORT_CHANGE );
00162 }
00163 
00169 bool
00170 SequencerEvent::isClient(const SequencerEvent* event)
00171 {
00172     snd_seq_event_type_t te = event->getSequencerType();
00173     return ( te == SND_SEQ_EVENT_CLIENT_START ||
00174              te == SND_SEQ_EVENT_CLIENT_EXIT ||
00175              te == SND_SEQ_EVENT_CLIENT_CHANGE );
00176 }
00177 
00183 bool
00184 SequencerEvent::isConnectionChange(const SequencerEvent* event)
00185 {
00186     snd_seq_event_type_t te = event->getSequencerType();
00187     return ( te == SND_SEQ_EVENT_PORT_START ||
00188              te == SND_SEQ_EVENT_PORT_EXIT ||
00189              te == SND_SEQ_EVENT_PORT_CHANGE ||
00190              te == SND_SEQ_EVENT_CLIENT_START ||
00191              te == SND_SEQ_EVENT_CLIENT_EXIT ||
00192              te == SND_SEQ_EVENT_CLIENT_CHANGE ||
00193              te == SND_SEQ_EVENT_PORT_SUBSCRIBED ||
00194              te == SND_SEQ_EVENT_PORT_UNSUBSCRIBED );
00195 }
00196 
00203 bool
00204 SequencerEvent::isChannel(const SequencerEvent* event)
00205 {
00206     snd_seq_event_type_t te = event->getSequencerType();
00207     return ( te == SND_SEQ_EVENT_NOTEOFF ||
00208              te == SND_SEQ_EVENT_NOTEON ||
00209              te == SND_SEQ_EVENT_NOTE ||
00210              te == SND_SEQ_EVENT_KEYPRESS ||
00211              te == SND_SEQ_EVENT_CONTROLLER ||
00212              te == SND_SEQ_EVENT_CONTROL14 ||
00213              te == SND_SEQ_EVENT_PGMCHANGE ||
00214              te == SND_SEQ_EVENT_CHANPRESS ||
00215              te == SND_SEQ_EVENT_PITCHBEND );
00216 }
00217 
00222 void SequencerEvent::setSequencerType(const snd_seq_event_type_t eventType)
00223 {
00224     m_event.type = eventType;
00225 }
00226 
00233 void SequencerEvent::setDestination(const unsigned char client, const unsigned char port)
00234 {
00235     snd_seq_ev_set_dest(&m_event, client, port);
00236 }
00237 
00243 void SequencerEvent::setSource(const unsigned char port)
00244 {
00245     snd_seq_ev_set_source(&m_event, port);
00246 }
00247 
00251 void SequencerEvent::setSubscribers()
00252 {
00253     snd_seq_ev_set_subs(&m_event);
00254 }
00255 
00259 void SequencerEvent::setBroadcast()
00260 {
00261     snd_seq_ev_set_broadcast(&m_event);
00262 }
00263 
00268 void SequencerEvent::setDirect()
00269 {
00270     snd_seq_ev_set_direct(&m_event);
00271 }
00272 
00279 void SequencerEvent::scheduleTick(int queue, int tick, bool relative)
00280 {
00281     snd_seq_ev_schedule_tick(&m_event, queue, relative, tick);
00282 }
00283 
00291 void SequencerEvent::scheduleReal(int queue, ulong secs, ulong nanos, bool relative)
00292 {
00293     snd_seq_real_time_t rtime;
00294     rtime.tv_sec = secs;
00295     rtime.tv_nsec = nanos;
00296     snd_seq_ev_schedule_real(&m_event, queue, relative, &rtime);
00297 }
00298 
00305 void SequencerEvent::setPriority(const bool high)
00306 {
00307     snd_seq_ev_set_priority(&m_event, high);
00308 }
00309 
00315 void SequencerEvent::setTag(const unsigned char aTag)
00316 {
00317 #if SND_LIB_VERSION > 0x010008
00318     snd_seq_ev_set_tag(&m_event, aTag);
00319 #else
00320     m_event.tag = aTag;
00321 #endif
00322 }
00323 
00330 unsigned int SequencerEvent::getRaw32(const unsigned int n) const
00331 {
00332     if (n < 3) return m_event.data.raw32.d[n];
00333     return 0;
00334 }
00335 
00341 void SequencerEvent::setRaw32(const unsigned int n, const unsigned int value)
00342 {
00343     if (n < 3) m_event.data.raw32.d[n] = value;
00344 }
00345 
00352 unsigned char SequencerEvent::getRaw8(const unsigned int n) const
00353 {
00354     if (n < 12) return m_event.data.raw8.d[n];
00355     return 0;
00356 }
00357 
00363 void SequencerEvent::setRaw8(const unsigned int n, const unsigned char value)
00364 {
00365     if (n < 12) m_event.data.raw8.d[n] = value;
00366 }
00367 
00372 void SequencerEvent::free()
00373 {
00374     snd_seq_free_event(&m_event);
00375 }
00376 
00381 int SequencerEvent::getEncodedLength()
00382 {
00383     return snd_seq_event_length(&m_event);
00384 }
00385 
00393 NoteEvent::NoteEvent(int ch, int key, int vel, int dur) : KeyEvent()
00394 {
00395     snd_seq_ev_set_note(&m_event, ch, key, vel, dur);
00396 }
00397 
00404 NoteOnEvent::NoteOnEvent(int ch, int key, int vel) : KeyEvent()
00405 {
00406     snd_seq_ev_set_noteon(&m_event, ch, key, vel);
00407 }
00408 
00415 NoteOffEvent::NoteOffEvent(int ch, int key, int vel) : KeyEvent()
00416 {
00417     snd_seq_ev_set_noteoff(&m_event, ch, key, vel);
00418 }
00419 
00426 KeyPressEvent::KeyPressEvent(int ch, int key, int vel) : KeyEvent()
00427 {
00428     snd_seq_ev_set_keypress(&m_event, ch, key, vel);
00429 }
00430 
00437 ControllerEvent::ControllerEvent(int ch, int cc, int val) : ChannelEvent()
00438 {
00439     snd_seq_ev_set_controller(&m_event, ch, cc, val);
00440 }
00441 
00447 ProgramChangeEvent::ProgramChangeEvent(int ch, int val) : ChannelEvent()
00448 {
00449     snd_seq_ev_set_pgmchange(&m_event, ch, val);
00450 }
00451 
00457 PitchBendEvent::PitchBendEvent(int ch, int val) : ChannelEvent()
00458 {
00459     snd_seq_ev_set_pitchbend(&m_event, ch, val);
00460 }
00461 
00467 ChanPressEvent::ChanPressEvent(int ch, int val) : ChannelEvent()
00468 {
00469     snd_seq_ev_set_chanpress(&m_event, ch, val);
00470 }
00471 
00475 VariableEvent::VariableEvent()
00476     : SequencerEvent()
00477 {
00478     m_data.clear();
00479     snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
00480 }
00481 
00486 VariableEvent::VariableEvent(snd_seq_event_t* event)
00487     : SequencerEvent(event)
00488 {
00489     m_data = QByteArray((char *) event->data.ext.ptr,
00490                         event->data.ext.len);
00491     snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
00492 }
00493 
00498 VariableEvent::VariableEvent(const QByteArray& data)
00499     : SequencerEvent()
00500 {
00501     m_data = data;
00502     snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
00503 }
00504 
00509 VariableEvent::VariableEvent(const VariableEvent& other)
00510     : SequencerEvent()
00511 {
00512     m_data = other.m_data;
00513     snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
00514 }
00515 
00521 VariableEvent::VariableEvent(const unsigned int datalen, char* dataptr)
00522     : SequencerEvent()
00523 {
00524     m_data = QByteArray(dataptr, datalen);
00525     snd_seq_ev_set_variable( &m_event, m_data.size(), m_data.data() );
00526 }
00527 
00533 VariableEvent& VariableEvent::operator=(const VariableEvent& other)
00534 {
00535     m_event = other.m_event;
00536     m_data = other.m_data;
00537     snd_seq_ev_set_variable ( &m_event, m_data.size(), m_data.data() );
00538     return *this;
00539 }
00540 
00544 SysExEvent::SysExEvent()
00545     : VariableEvent()
00546 {
00547     snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
00548 }
00549 
00554 SysExEvent::SysExEvent(snd_seq_event_t* event)
00555     : VariableEvent(event)
00556 {
00557     snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
00558 }
00559 
00564 SysExEvent::SysExEvent(const QByteArray& data)
00565     : VariableEvent(data)
00566 {
00567     snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
00568 }
00569 
00574 SysExEvent::SysExEvent(const SysExEvent& other)
00575     : VariableEvent(other)
00576 {
00577     snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
00578 }
00579 
00585 SysExEvent::SysExEvent(const unsigned int datalen, char* dataptr)
00586     : VariableEvent( datalen, dataptr )
00587 {
00588     snd_seq_ev_set_sysex( &m_event, m_data.size(), m_data.data() );
00589 }
00590 
00594 TextEvent::TextEvent()
00595     : VariableEvent(), m_textType(1)
00596 {
00597     setSequencerType(SND_SEQ_EVENT_USR_VAR0);
00598 }
00599 
00604 TextEvent::TextEvent(snd_seq_event_t* event)
00605     : VariableEvent(event), m_textType(1)
00606 {
00607     setSequencerType(SND_SEQ_EVENT_USR_VAR0);
00608 }
00609 
00615 TextEvent::TextEvent(const QString& text, const int textType)
00616     : VariableEvent(text.toUtf8()), m_textType(textType)
00617 {
00618     setSequencerType(SND_SEQ_EVENT_USR_VAR0);
00619 }
00620 
00625 TextEvent::TextEvent(const TextEvent& other)
00626     : VariableEvent(other)
00627 {
00628     setSequencerType(SND_SEQ_EVENT_USR_VAR0);
00629     m_textType = other.getTextType();
00630 }
00631 
00637 TextEvent::TextEvent(const unsigned int datalen, char* dataptr)
00638     : VariableEvent(datalen, dataptr), m_textType(1)
00639 {
00640     setSequencerType(SND_SEQ_EVENT_USR_VAR0);
00641 }
00642 
00647 QString TextEvent::getText() const
00648 {
00649     return QString::fromUtf8(m_data.data(), m_data.size());
00650 }
00651 
00656 int TextEvent::getTextType() const
00657 {
00658     return m_textType;
00659 }
00660 
00665 SystemEvent::SystemEvent(const snd_seq_event_type_t type) : SequencerEvent()
00666 {
00667     snd_seq_ev_set_fixed(&m_event);
00668     setSequencerType(type);
00669 }
00670 
00677 QueueControlEvent::QueueControlEvent(snd_seq_event_type_t type, int queue, int value)
00678     : SequencerEvent()
00679 {
00680     snd_seq_ev_set_queue_control(&m_event, type, queue, value);
00681 }
00682 
00688 ValueEvent::ValueEvent(const snd_seq_event_type_t type, int val) : SequencerEvent()
00689 {
00690     snd_seq_ev_set_fixed(&m_event);
00691     setSequencerType(type);
00692     setValue(val);
00693 }
00694 
00700 TempoEvent::TempoEvent(int queue, int tempo) : QueueControlEvent()
00701 {
00702     snd_seq_ev_set_queue_tempo(&m_event, queue, tempo);
00703 }
00704 
00708 RemoveEvents::RemoveEvents()
00709 {
00710     snd_seq_remove_events_malloc(&m_Info);
00711 }
00712 
00717 RemoveEvents::RemoveEvents(const RemoveEvents& other)
00718 {
00719     snd_seq_remove_events_malloc(&m_Info);
00720     snd_seq_remove_events_copy(m_Info, other.m_Info);
00721 }
00722 
00727 RemoveEvents::RemoveEvents(snd_seq_remove_events_t* other)
00728 {
00729     snd_seq_remove_events_malloc(&m_Info);
00730     snd_seq_remove_events_copy(m_Info, other);
00731 }
00732 
00736 RemoveEvents::~RemoveEvents()
00737 {
00738     snd_seq_remove_events_free(m_Info);
00739 }
00740 
00745 RemoveEvents*
00746 RemoveEvents::clone()
00747 {
00748     return new RemoveEvents(m_Info);
00749 }
00750 
00756 RemoveEvents&
00757 RemoveEvents::operator=(const RemoveEvents& other)
00758 {
00759     snd_seq_remove_events_copy(m_Info, other.m_Info);
00760     return *this;
00761 }
00762 
00767 int
00768 RemoveEvents::getSizeOfInfo() const
00769 {
00770     return snd_seq_remove_events_sizeof();
00771 }
00772 
00778 int
00779 RemoveEvents::getChannel()
00780 {
00781     return snd_seq_remove_events_get_channel(m_Info);
00782 }
00783 
00789 unsigned int
00790 RemoveEvents::getCondition()
00791 {
00792     return snd_seq_remove_events_get_condition(m_Info);
00793 }
00794 
00800 const snd_seq_addr_t*
00801 RemoveEvents::getDest()
00802 {
00803     return snd_seq_remove_events_get_dest(m_Info);
00804 }
00805 
00811 int
00812 RemoveEvents::getEventType()
00813 {
00814     return snd_seq_remove_events_get_event_type(m_Info);
00815 }
00816 
00822 int
00823 RemoveEvents::getQueue()
00824 {
00825     return snd_seq_remove_events_get_queue(m_Info);
00826 }
00827 
00833 int
00834 RemoveEvents::getTag()
00835 {
00836     return snd_seq_remove_events_get_tag(m_Info);
00837 }
00838 
00844 const snd_seq_timestamp_t*
00845 RemoveEvents::getTime()
00846 {
00847     return snd_seq_remove_events_get_time(m_Info);
00848 }
00849 
00855 void
00856 RemoveEvents::setChannel(int chan)
00857 {
00858     snd_seq_remove_events_set_channel(m_Info, chan);
00859 }
00860 
00879 void
00880 RemoveEvents::setCondition(unsigned int cond)
00881 {
00882     snd_seq_remove_events_set_condition(m_Info, cond);
00883 }
00884 
00890 void
00891 RemoveEvents::setDest(const snd_seq_addr_t* dest)
00892 {
00893     snd_seq_remove_events_set_dest(m_Info, dest);
00894 }
00895 
00901 void
00902 RemoveEvents::setEventType(int type)
00903 {
00904     snd_seq_remove_events_set_event_type(m_Info, type);
00905 }
00906 
00912 void
00913 RemoveEvents::setQueue(int queue)
00914 {
00915     snd_seq_remove_events_set_queue(m_Info, queue);
00916 }
00917 
00923 void
00924 RemoveEvents::setTag(int tag)
00925 {
00926     snd_seq_remove_events_set_tag(m_Info, tag);
00927 }
00928 
00934 void
00935 RemoveEvents::setTime(const snd_seq_timestamp_t* time)
00936 {
00937     snd_seq_remove_events_set_time(m_Info, time);
00938 }
00939 
00945 MidiCodec::MidiCodec( int bufsize, QObject* parent ) : QObject(parent)
00946 {
00947     CHECK_ERROR(snd_midi_event_new(bufsize, &m_Info));
00948 }
00949 
00953 MidiCodec::~MidiCodec()
00954 {
00955     snd_midi_event_free(m_Info);
00956 }
00957 
00961 void
00962 MidiCodec::init()
00963 {
00964     snd_midi_event_init(m_Info);
00965 }
00966 
00974 long
00975 MidiCodec::decode(unsigned char *buf,
00976                   long count,
00977                   const snd_seq_event_t *ev)
00978 {
00979     return CHECK_WARNING(snd_midi_event_decode(m_Info, buf, count, ev));
00980 }
00981 
00989 long
00990 MidiCodec::encode(const unsigned char *buf,
00991                   long count,
00992                   snd_seq_event_t *ev)
00993 {
00994     return CHECK_WARNING(snd_midi_event_encode(m_Info, buf, count, ev));
00995 }
00996 
01003 long
01004 MidiCodec::encode(int c,
01005                   snd_seq_event_t *ev)
01006 {
01007     return CHECK_WARNING(snd_midi_event_encode_byte(m_Info, c, ev));
01008 }
01009 
01014 void
01015 MidiCodec::enableRunningStatus(bool enable)
01016 {
01017     snd_midi_event_no_status(m_Info, enable ? 0 : 1);
01018 }
01019 
01023 void
01024 MidiCodec::resetDecoder()
01025 {
01026     snd_midi_event_reset_decode(m_Info);
01027 }
01028 
01032 void
01033 MidiCodec::resetEncoder()
01034 {
01035     snd_midi_event_reset_encode(m_Info);
01036 }
01037 
01042 void
01043 MidiCodec::resizeBuffer(int bufsize)
01044 {
01045     CHECK_WARNING(snd_midi_event_resize_buffer(m_Info, bufsize));
01046 }
01047 
01048 } /* namespace drumstick */