seq_timer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * ALSA sequencer Timer
  3. * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #ifndef __SND_SEQ_TIMER_H
  22. #define __SND_SEQ_TIMER_H
  23. #include <sound/timer.h>
  24. #include <sound/seq_kernel.h>
  25. struct snd_seq_timer_tick {
  26. snd_seq_tick_time_t cur_tick; /* current tick */
  27. unsigned long resolution; /* time per tick in nsec */
  28. unsigned long fraction; /* current time per tick in nsec */
  29. };
  30. struct snd_seq_timer {
  31. /* ... tempo / offset / running state */
  32. unsigned int running:1, /* running state of queue */
  33. initialized:1; /* timer is initialized */
  34. unsigned int tempo; /* current tempo, us/tick */
  35. int ppq; /* time resolution, ticks/quarter */
  36. snd_seq_real_time_t cur_time; /* current time */
  37. struct snd_seq_timer_tick tick; /* current tick */
  38. int tick_updated;
  39. int type; /* timer type */
  40. struct snd_timer_id alsa_id; /* ALSA's timer ID */
  41. struct snd_timer_instance *timeri; /* timer instance */
  42. unsigned int ticks;
  43. unsigned long preferred_resolution; /* timer resolution, ticks/sec */
  44. unsigned int skew;
  45. unsigned int skew_base;
  46. struct timeval last_update; /* time of last clock update, used for interpolation */
  47. spinlock_t lock;
  48. };
  49. /* create new timer (constructor) */
  50. struct snd_seq_timer *snd_seq_timer_new(void);
  51. /* delete timer (destructor) */
  52. void snd_seq_timer_delete(struct snd_seq_timer **tmr);
  53. /* */
  54. static inline void snd_seq_timer_update_tick(struct snd_seq_timer_tick *tick,
  55. unsigned long resolution)
  56. {
  57. if (tick->resolution > 0) {
  58. tick->fraction += resolution;
  59. tick->cur_tick += (unsigned int)(tick->fraction / tick->resolution);
  60. tick->fraction %= tick->resolution;
  61. }
  62. }
  63. /* compare timestamp between events */
  64. /* return 1 if a >= b; otherwise return 0 */
  65. static inline int snd_seq_compare_tick_time(snd_seq_tick_time_t *a, snd_seq_tick_time_t *b)
  66. {
  67. /* compare ticks */
  68. return (*a >= *b);
  69. }
  70. static inline int snd_seq_compare_real_time(snd_seq_real_time_t *a, snd_seq_real_time_t *b)
  71. {
  72. /* compare real time */
  73. if (a->tv_sec > b->tv_sec)
  74. return 1;
  75. if ((a->tv_sec == b->tv_sec) && (a->tv_nsec >= b->tv_nsec))
  76. return 1;
  77. return 0;
  78. }
  79. static inline void snd_seq_sanity_real_time(snd_seq_real_time_t *tm)
  80. {
  81. while (tm->tv_nsec >= 1000000000) {
  82. /* roll-over */
  83. tm->tv_nsec -= 1000000000;
  84. tm->tv_sec++;
  85. }
  86. }
  87. /* increment timestamp */
  88. static inline void snd_seq_inc_real_time(snd_seq_real_time_t *tm, snd_seq_real_time_t *inc)
  89. {
  90. tm->tv_sec += inc->tv_sec;
  91. tm->tv_nsec += inc->tv_nsec;
  92. snd_seq_sanity_real_time(tm);
  93. }
  94. static inline void snd_seq_inc_time_nsec(snd_seq_real_time_t *tm, unsigned long nsec)
  95. {
  96. tm->tv_nsec += nsec;
  97. snd_seq_sanity_real_time(tm);
  98. }
  99. /* called by timer isr */
  100. struct snd_seq_queue;
  101. int snd_seq_timer_open(struct snd_seq_queue *q);
  102. int snd_seq_timer_close(struct snd_seq_queue *q);
  103. int snd_seq_timer_midi_open(struct snd_seq_queue *q);
  104. int snd_seq_timer_midi_close(struct snd_seq_queue *q);
  105. void snd_seq_timer_defaults(struct snd_seq_timer *tmr);
  106. void snd_seq_timer_reset(struct snd_seq_timer *tmr);
  107. int snd_seq_timer_stop(struct snd_seq_timer *tmr);
  108. int snd_seq_timer_start(struct snd_seq_timer *tmr);
  109. int snd_seq_timer_continue(struct snd_seq_timer *tmr);
  110. int snd_seq_timer_set_tempo(struct snd_seq_timer *tmr, int tempo);
  111. int snd_seq_timer_set_ppq(struct snd_seq_timer *tmr, int ppq);
  112. int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, snd_seq_tick_time_t position);
  113. int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, snd_seq_real_time_t position);
  114. int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, unsigned int base);
  115. snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr);
  116. snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr);
  117. extern int seq_default_timer_class;
  118. extern int seq_default_timer_sclass;
  119. extern int seq_default_timer_card;
  120. extern int seq_default_timer_device;
  121. extern int seq_default_timer_subdevice;
  122. extern int seq_default_timer_resolution;
  123. #endif