drumstick  0.5.0
alsaqueue.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 "alsaqueue.h"
21 #include "alsaclient.h"
22 #include "alsaevent.h"
23 #include "alsatimer.h"
24 #include <cmath>
25 
31 namespace drumstick {
32 
61 {
62  snd_seq_queue_info_malloc(&m_Info);
63 }
64 
69 QueueInfo::QueueInfo(snd_seq_queue_info_t* other)
70 {
71  snd_seq_queue_info_malloc(&m_Info);
72  snd_seq_queue_info_copy(m_Info, other);
73 }
74 
80 {
81  snd_seq_queue_info_malloc(&m_Info);
82  snd_seq_queue_info_copy(m_Info, other.m_Info);
83 }
84 
89 {
90  snd_seq_queue_info_free(m_Info);
91 }
92 
98 {
99  return new QueueInfo(m_Info);
100 }
101 
108 {
109  snd_seq_queue_info_copy(m_Info, other.m_Info);
110  return *this;
111 }
112 
118 {
119  return snd_seq_queue_info_get_queue(m_Info);
120 }
121 
127 {
128  return QString(snd_seq_queue_info_get_name(m_Info));
129 }
130 
136 {
137  return snd_seq_queue_info_get_owner(m_Info);
138 }
139 
145 {
146  return (snd_seq_queue_info_get_locked(m_Info) != 0);
147 }
148 
153 unsigned int QueueInfo::getFlags()
154 {
155  return snd_seq_queue_info_get_flags(m_Info);
156 }
157 
162 void QueueInfo::setName(QString value)
163 {
164  snd_seq_queue_info_set_name(m_Info, value.toLocal8Bit().data());
165 }
166 
171 void QueueInfo::setOwner(int value)
172 {
173  snd_seq_queue_info_set_owner(m_Info, value);
174 }
175 
180 void QueueInfo::setFlags(unsigned int value)
181 {
182  snd_seq_queue_info_set_flags(m_Info, value);
183 }
184 
189 void QueueInfo::setLocked(bool locked)
190 {
191  snd_seq_queue_info_set_locked(m_Info, locked ? 1 : 0);
192 }
193 
199 {
200  return snd_seq_queue_info_sizeof();
201 }
202 
203 
208 {
209  snd_seq_queue_status_malloc(&m_Info);
210 }
211 
216 QueueStatus::QueueStatus(snd_seq_queue_status_t* other)
217 {
218  snd_seq_queue_status_malloc(&m_Info);
219  snd_seq_queue_status_copy(m_Info, other);
220 }
221 
227 {
228  snd_seq_queue_status_malloc(&m_Info);
229  snd_seq_queue_status_copy(m_Info, other.m_Info);
230 }
231 
236 {
237  snd_seq_queue_status_free(m_Info);
238 }
239 
245 {
246  return new QueueStatus(m_Info);
247 }
248 
255 {
256  snd_seq_queue_status_copy(m_Info, other.m_Info);
257  return *this;
258 }
259 
265 {
266  return snd_seq_queue_status_get_queue(m_Info);
267 }
268 
274 {
275  return snd_seq_queue_status_get_events(m_Info);
276 }
277 
282 const snd_seq_real_time_t* QueueStatus::getRealtime()
283 {
284  return snd_seq_queue_status_get_real_time(m_Info);
285 }
286 
292 {
293  return snd_seq_queue_status_get_status(m_Info);
294 }
295 
300 snd_seq_tick_time_t QueueStatus::getTickTime()
301 {
302  return snd_seq_queue_status_get_tick_time(m_Info);
303 }
304 
310 {
311  return snd_seq_queue_status_sizeof();
312 }
313 
319 {
320  return (snd_seq_queue_status_get_status(m_Info) != 0);
321 }
322 
328 {
329  const snd_seq_real_time_t* time = snd_seq_queue_status_get_real_time(m_Info);
330  return (time->tv_sec * 1.0) + (time->tv_nsec * 1.0e-9);
331 }
332 
337 {
338  snd_seq_queue_tempo_malloc(&m_Info);
339 }
340 
345 QueueTempo::QueueTempo(snd_seq_queue_tempo_t* other)
346 {
347  snd_seq_queue_tempo_malloc(&m_Info);
348  snd_seq_queue_tempo_copy(m_Info, other);
349 }
350 
356 {
357  snd_seq_queue_tempo_malloc(&m_Info);
358  snd_seq_queue_tempo_copy(m_Info, other.m_Info);
359 }
360 
365 {
366  snd_seq_queue_tempo_free(m_Info);
367 }
368 
374 {
375  return new QueueTempo(m_Info);
376 }
377 
384 {
385  snd_seq_queue_tempo_copy(m_Info, other.m_Info);
386  return *this;
387 }
388 
394 {
395  return snd_seq_queue_tempo_get_queue(m_Info);
396 }
397 
403 {
404  return snd_seq_queue_tempo_get_ppq(m_Info);
405 }
406 
414 {
415  return snd_seq_queue_tempo_get_skew(m_Info);
416 }
417 
425 {
426  return snd_seq_queue_tempo_get_skew_base(m_Info);
427 }
428 
433 unsigned int QueueTempo::getTempo()
434 {
435  return snd_seq_queue_tempo_get_tempo(m_Info);
436 }
437 
442 void QueueTempo::setPPQ(int value)
443 {
444  snd_seq_queue_tempo_set_ppq(m_Info, value);
445 }
446 
453 void QueueTempo::setSkewValue(unsigned int value)
454 {
455  snd_seq_queue_tempo_set_skew(m_Info, value);
456 }
457 
465 void QueueTempo::setSkewBase(unsigned int value)
466 {
467  snd_seq_queue_tempo_set_skew_base(m_Info, value);
468 }
469 
474 void QueueTempo::setTempo(unsigned int value)
475 {
476  snd_seq_queue_tempo_set_tempo(m_Info, value);
477 }
478 
484 {
485  int itempo = getTempo();
486  if (itempo != 0)
487  return 6.0e7f / itempo;
488  return 0.0f;
489 }
490 
497 {
498  float tempo = getNominalBPM();
499  return tempo * getSkewValue() / SKEW_BASE;
500 }
501 
506 void QueueTempo::setTempoFactor(float value)
507 {
508  setSkewValue(floor(SKEW_BASE * value));
510 }
511 
516 void QueueTempo::setNominalBPM(float value)
517 {
518  setTempo(floor(6.0e7f / value));
519 }
520 
526 {
527  return snd_seq_queue_tempo_sizeof();
528 }
529 
534 {
535  snd_seq_queue_timer_malloc(&m_Info);
536 }
537 
542 QueueTimer::QueueTimer(snd_seq_queue_timer_t* other)
543 {
544  snd_seq_queue_timer_malloc(&m_Info);
545  snd_seq_queue_timer_copy(m_Info, other);
546 }
547 
553 {
554  snd_seq_queue_timer_malloc(&m_Info);
555  snd_seq_queue_timer_copy(m_Info, other.m_Info);
556 }
557 
562 {
563  snd_seq_queue_timer_free(m_Info);
564 }
565 
571 {
572  return new QueueTimer(m_Info);
573 }
574 
581 {
582  snd_seq_queue_timer_copy(m_Info, other.m_Info);
583  return *this;
584 }
585 
591 {
592  return snd_seq_queue_timer_get_queue(m_Info);
593 }
594 
607 snd_seq_queue_timer_type_t QueueTimer::getType()
608 {
609  return snd_seq_queue_timer_get_type(m_Info);
610 }
611 
616 const snd_timer_id_t* QueueTimer::getId()
617 {
618  return snd_seq_queue_timer_get_id(m_Info);
619 }
620 
626 {
627  return snd_seq_queue_timer_get_resolution(m_Info);
628 }
629 
641 void QueueTimer::setType(snd_seq_queue_timer_type_t value)
642 {
643  snd_seq_queue_timer_set_type(m_Info, value);
644 }
645 
650 void QueueTimer::setId(snd_timer_id_t* value)
651 {
652  snd_seq_queue_timer_set_id(m_Info, value);
653 }
654 
660 void QueueTimer::setId(const TimerId& id)
661 {
662  setId(id.m_Info);
663 }
664 
669 void QueueTimer::setResolution(unsigned int value)
670 {
671  snd_seq_queue_timer_set_resolution(m_Info, value);
672 }
673 
679 {
680  return snd_seq_queue_timer_sizeof();
681 }
682 
689  : QObject(parent)
690 {
691  m_MidiClient = seq;
692  m_Id = CHECK_ERROR(snd_seq_alloc_queue(m_MidiClient->getHandle()));
693  m_allocated = !(m_Id < 0);
694 }
695 
703  : QObject(parent)
704 {
705  m_MidiClient = seq;
706  m_Info = info;
707  m_Id = CHECK_ERROR(snd_seq_create_queue(m_MidiClient->getHandle(), m_Info.m_Info));
708  m_allocated = !(m_Id < 0);
709 }
710 
717 MidiQueue::MidiQueue(MidiClient* seq, const QString name, QObject* parent)
718  : QObject(parent)
719 {
720  m_MidiClient = seq;
721  m_Id = CHECK_ERROR(snd_seq_alloc_named_queue(m_MidiClient->getHandle(), name.toLocal8Bit().data()));
722  m_allocated = !(m_Id < 0);
723 }
724 
733 MidiQueue::MidiQueue(MidiClient* seq, const int queue_id, QObject* parent)
734  : QObject(parent)
735 {
736  m_MidiClient = seq;
737  m_Id = queue_id;
738  m_allocated = false;
739 }
740 
745 {
746  if ( m_allocated && (m_MidiClient->getHandle() != NULL) )
747  {
748  CHECK_ERROR(snd_seq_free_queue(m_MidiClient->getHandle(), m_Id));
749  }
750 }
751 
757 {
758  CHECK_WARNING(snd_seq_get_queue_info(m_MidiClient->getHandle(), m_Id, m_Info.m_Info));
759  return m_Info;
760 }
761 
767 {
768  CHECK_WARNING(snd_seq_get_queue_status(m_MidiClient->getHandle(), m_Id, m_Status.m_Info));
769  return m_Status;
770 }
771 
777 {
778  CHECK_WARNING(snd_seq_get_queue_tempo(m_MidiClient->getHandle(), m_Id, m_Tempo.m_Info));
779  return m_Tempo;
780 }
781 
787 {
788  CHECK_WARNING(snd_seq_get_queue_timer(m_MidiClient->getHandle(), m_Id, m_Timer.m_Info));
789  return m_Timer;
790 }
791 
796 void MidiQueue::setInfo(const QueueInfo& value)
797 {
798  m_Info = value;
799  CHECK_WARNING(snd_seq_set_queue_info(m_MidiClient->getHandle(), m_Id, m_Info.m_Info));
800 }
801 
806 void MidiQueue::setTempo(const QueueTempo& value)
807 {
808  m_Tempo = value;
809  CHECK_WARNING(snd_seq_set_queue_tempo(m_MidiClient->getHandle(), m_Id, m_Tempo.m_Info));
810 }
811 
816 void MidiQueue::setTimer(const QueueTimer& value)
817 {
818  m_Timer = value;
819  CHECK_WARNING(snd_seq_set_queue_timer(m_MidiClient->getHandle(), m_Id, m_Timer.m_Info));
820 }
821 
828 {
829  return CHECK_WARNING(snd_seq_get_queue_usage(m_MidiClient->getHandle(), m_Id));
830 }
831 
837 void MidiQueue::setUsage(int used)
838 {
839  CHECK_WARNING(snd_seq_set_queue_usage(m_MidiClient->getHandle(), m_Id, used));
840 }
841 
848 {
849  CHECK_WARNING(snd_seq_start_queue(m_MidiClient->getHandle(), m_Id, NULL));
850  CHECK_WARNING(snd_seq_drain_output(m_MidiClient->getHandle()));
851 }
852 
859 {
860  if (m_MidiClient != NULL && m_MidiClient->getHandle() != NULL) {
861  CHECK_WARNING(snd_seq_stop_queue(m_MidiClient->getHandle(), m_Id, NULL));
862  CHECK_WARNING(snd_seq_drain_output(m_MidiClient->getHandle()));
863  }
864 }
865 
872 {
873  CHECK_WARNING(snd_seq_continue_queue(m_MidiClient->getHandle(), m_Id, NULL));
874  CHECK_WARNING(snd_seq_drain_output(m_MidiClient->getHandle()));
875 }
876 
881 {
882  if (m_MidiClient != NULL && m_MidiClient->getHandle() != NULL)
883  snd_seq_drop_output(m_MidiClient->getHandle());
884 }
885 
890 void MidiQueue::setTickPosition(snd_seq_tick_time_t pos)
891 {
892  SystemEvent event(SND_SEQ_EVENT_SETPOS_TICK);
893  snd_seq_ev_set_queue_pos_tick(event.getHandle(), m_Id, pos);
894  event.setDirect();
895  m_MidiClient->outputDirect(&event);
896 }
897 
902 void MidiQueue::setRealTimePosition(snd_seq_real_time_t* pos)
903 {
904  SystemEvent event(SND_SEQ_EVENT_SETPOS_TIME);
905  snd_seq_ev_set_queue_pos_real(event.getHandle(), m_Id, pos);
906  event.setDirect();
907  m_MidiClient->outputDirect(&event);
908 }
909 
910 } /* namespace drumstick */
Queue tempo container.
Definition: alsaqueue.h:117
void setTimer(const QueueTimer &value)
Applies q QueueTimer object to the queue.
Definition: alsaqueue.cpp:816
snd_seq_event_t * getHandle()
Gets the handle of the event.
Definition: alsaevent.h:123
virtual ~QueueTempo()
Destructor.
Definition: alsaqueue.cpp:364
void setPPQ(int value)
Sets the queue resolution in parts per quarter note.
Definition: alsaqueue.cpp:442
void setTickPosition(snd_seq_tick_time_t pos)
Sets the queue position in musical time (ticks).
Definition: alsaqueue.cpp:890
virtual ~QueueStatus()
Destructor.
Definition: alsaqueue.cpp:235
int getInfoSize() const
Gets the size of the ALSA queue timer object.
Definition: alsaqueue.cpp:678
#define SKEW_BASE
This is the value for the base skew used in ALSA.
Definition: alsaqueue.h:37
Classes managing ALSA Sequencer clients.
void continueRunning()
Start the queue without resetting the last position.
Definition: alsaqueue.cpp:871
void setLocked(bool locked)
Sets the locked status of the queue.
Definition: alsaqueue.cpp:189
float getRealBPM()
Gets the queue's real BPM tempo in beats per minute.
Definition: alsaqueue.cpp:496
void setTempo(const QueueTempo &value)
Applies a QueueTempo object to the queue.
Definition: alsaqueue.cpp:806
void clear()
Clear the queue, dropping any scheduled events.
Definition: alsaqueue.cpp:880
int getId()
Gets the queue's numeric identifier.
Definition: alsaqueue.cpp:117
bool isRunning()
Gets the queue's running state.
Definition: alsaqueue.cpp:318
QueueInfo()
Default constructor.
Definition: alsaqueue.cpp:60
void setFlags(unsigned int value)
Sets the bit flags of the queue.
Definition: alsaqueue.cpp:180
void start()
Start the queue.
Definition: alsaqueue.cpp:847
void setNominalBPM(float value)
Sets the queue's nominal tempo in BPM (beats per minute).
Definition: alsaqueue.cpp:516
void setTempoFactor(float value)
Sets the queue's tempo skew factor.
Definition: alsaqueue.cpp:506
int getPPQ()
Gets the PPQ (parts per quarter note) resolution of the queue.
Definition: alsaqueue.cpp:402
snd_seq_tick_time_t getTickTime()
Gets the musical time (ticks) of the queue.
Definition: alsaqueue.cpp:300
void stop()
Stop the queue.
Definition: alsaqueue.cpp:858
unsigned int getStatusBits()
Gets the running status bits.
Definition: alsaqueue.cpp:291
void setRealTimePosition(snd_seq_real_time_t *pos)
Sets the queue position in real time (clock) units: seconds and nanoseconds.
Definition: alsaqueue.cpp:902
Generic event.
Definition: alsaevent.h:438
int getUsage()
Gets the queue usage flag.
Definition: alsaqueue.cpp:827
void setName(QString value)
Sets the queue name.
Definition: alsaqueue.cpp:162
float getNominalBPM()
Gets the queue's nominal BPM tempo (in beats per minute)
Definition: alsaqueue.cpp:483
QueueStatus & getStatus()
Gets a QueueStatus object reference.
Definition: alsaqueue.cpp:766
The QObject class is the base class of all Qt objects.
QueueTimer & getTimer()
Gets a QueueTimer object reference.
Definition: alsaqueue.cpp:786
Classes managing ALSA Timers.
double getClockTime()
Gets the clock time in seconds of the queue.
Definition: alsaqueue.cpp:327
int getOwner()
Gets the owner's client id of the queue.
Definition: alsaqueue.cpp:135
int getEvents()
Gets the number of queued events.
Definition: alsaqueue.cpp:273
MidiQueue(MidiClient *seq, QObject *parent=0)
Constructor.
Definition: alsaqueue.cpp:688
Queue timer container.
Definition: alsaqueue.h:157
QueueTempo()
Default constructor.
Definition: alsaqueue.cpp:336
Client management.
Definition: alsaclient.h:198
snd_seq_t * getHandle()
Returns the sequencer handler managed by ALSA.
Definition: alsaclient.h:235
unsigned int getSkewValue()
Gets the tempo skew numerator.
Definition: alsaqueue.cpp:413
QueueTempo & operator=(const QueueTempo &other)
Assignment operator.
Definition: alsaqueue.cpp:383
QueueTempo & getTempo()
Gets a QueueTempo object reference.
Definition: alsaqueue.cpp:776
void setSkewBase(unsigned int value)
Sets the tempo skew base.
Definition: alsaqueue.cpp:465
unsigned int getTempo()
Gets the queue's tempo in microseconds per beat.
Definition: alsaqueue.cpp:433
void setSkewValue(unsigned int value)
Sets the tempo skew numerator.
Definition: alsaqueue.cpp:453
QueueTimer * clone()
Copy the current object and return the copy.
Definition: alsaqueue.cpp:570
void setType(snd_seq_queue_timer_type_t value)
Sets the timer type.
Definition: alsaqueue.cpp:641
void setOwner(int value)
Sets the client ID of the owner.
Definition: alsaqueue.cpp:171
QueueTempo * clone()
Copy the current object returning the copied object.
Definition: alsaqueue.cpp:373
QString getName()
Gets the queue name.
Definition: alsaqueue.cpp:126
virtual ~QueueTimer()
Destructor.
Definition: alsaqueue.cpp:561
void setId(snd_timer_id_t *value)
Sets the timer identifier record.
Definition: alsaqueue.cpp:650
unsigned int getResolution()
Gets the timer resolution.
Definition: alsaqueue.cpp:625
const snd_timer_id_t * getId()
Gets the timer identifier record.
Definition: alsaqueue.cpp:616
QueueTimer & operator=(const QueueTimer &other)
Assignment operator.
Definition: alsaqueue.cpp:580
virtual ~QueueInfo()
Destructor.
Definition: alsaqueue.cpp:88
Queue information container.
Definition: alsaqueue.h:47
void setResolution(unsigned int value)
Sets the timer resolution.
Definition: alsaqueue.cpp:669
QueueTimer()
Default constructor.
Definition: alsaqueue.cpp:533
int getQueueId()
The queue's numeric identifier.
Definition: alsaqueue.cpp:590
QueueInfo & operator=(const QueueInfo &other)
Assignment operator.
Definition: alsaqueue.cpp:107
int getInfoSize() const
Gets the size of the ALSA queue info object.
Definition: alsaqueue.cpp:198
unsigned int getSkewBase()
Gets the tempo skew base.
Definition: alsaqueue.cpp:424
virtual ~MidiQueue()
Destructor.
Definition: alsaqueue.cpp:744
Queue status container.
Definition: alsaqueue.h:80
QueueInfo * clone()
Copy the current object and return the copy.
Definition: alsaqueue.cpp:97
bool isLocked()
Returns the locking status of the queue.
Definition: alsaqueue.cpp:144
QueueStatus & operator=(const QueueStatus &other)
Assignment operator.
Definition: alsaqueue.cpp:254
void setTempo(unsigned int value)
Sets the queue tempo in microseconds per beat.
Definition: alsaqueue.cpp:474
void outputDirect(SequencerEvent *ev, bool async=false, int timeout=-1)
Output an event directly to the sequencer.
snd_seq_queue_timer_type_t getType()
Gets the timer type.
Definition: alsaqueue.cpp:607
QueueStatus * clone()
Copy the current object and return the copy.
Definition: alsaqueue.cpp:244
int getInfoSize() const
Gets the size of the ALSA status object.
Definition: alsaqueue.cpp:309
#define CHECK_ERROR(x)
This macro calls the check error function.
void setUsage(int used)
Sets the queue usage flag.
Definition: alsaqueue.cpp:837
void setInfo(const QueueInfo &value)
Applies a QueueInfo object to the queue.
Definition: alsaqueue.cpp:796
unsigned int getFlags()
Gets the flags of the queue.
Definition: alsaqueue.cpp:153
#define CHECK_WARNING(x)
This macro calls the check warning function.
Classes managing ALSA Sequencer queues.
QueueInfo & getInfo()
Gets a QueueInfo object reference.
Definition: alsaqueue.cpp:756
const snd_seq_real_time_t * getRealtime()
Gets the real time (secods and nanoseconds) of the queue.
Definition: alsaqueue.cpp:282
Classes managing ALSA Sequencer events.
int getInfoSize() const
Gets the size of the ALSA queue tempo object.
Definition: alsaqueue.cpp:525
int getId()
Gets the queue's numeric identifier.
Definition: alsaqueue.cpp:393
QueueStatus()
Default constructor.
Definition: alsaqueue.cpp:207
ALSA Timer identifier container.
Definition: alsatimer.h:79
int getId()
Gets the queue's numeric identifier.
Definition: alsaqueue.cpp:264