drumstick  1.1.0
alsaqueue.cpp
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 #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 */