seq_timer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. typedef struct {
  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. } seq_timer_tick_t;
  30. typedef struct {
  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. seq_timer_tick_t tick; /* current tick */
  38. int tick_updated;
  39. int type; /* timer type */
  40. snd_timer_id_t alsa_id; /* ALSA's timer ID */
  41. snd_timer_instance_t *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. } seq_timer_t;
  49. /* create new timer (constructor) */
  50. extern seq_timer_t *snd_seq_timer_new(void);
  51. /* delete timer (destructor) */
  52. extern void snd_seq_timer_delete(seq_timer_t **tmr);
  53. /* */
  54. static inline void snd_seq_timer_update_tick(seq_timer_tick_t *tick, unsigned long resolution)
  55. {
  56. if (tick->resolution > 0) {
  57. tick->fraction += resolution;
  58. tick->cur_tick += (unsigned int)(tick->fraction / tick->resolution);
  59. tick->fraction %= tick->resolution;
  60. }
  61. }
  62. /* compare timestamp between events */
  63. /* return 1 if a >= b; otherwise return 0 */
  64. static inline int snd_seq_compare_tick_time(snd_seq_tick_time_t *a, snd_seq_tick_time_t *b)
  65. {
  66. /* compare ticks */
  67. return (*a >= *b);
  68. }
  69. static inline int snd_seq_compare_real_time(snd_seq_real_time_t *a, snd_seq_real_time_t *b)
  70. {
  71. /* compare real time */
  72. if (a->tv_sec > b->tv_sec)
  73. return 1;
  74. if ((a->tv_sec == b->tv_sec) && (a->tv_nsec >= b->tv_nsec))
  75. return 1;
  76. return 0;
  77. }
  78. static inline void snd_seq_sanity_real_time(snd_seq_real_time_t *tm)
  79. {
  80. while (tm->tv_nsec >= 1000000000) {
  81. /* roll-over */
  82. tm->tv_nsec -= 1000000000;
  83. tm->tv_sec++;
  84. }
  85. }
  86. /* increment timestamp */
  87. static inline void snd_seq_inc_real_time(snd_seq_real_time_t *tm, snd_seq_real_time_t *inc)
  88. {
  89. tm->tv_sec += inc->tv_sec;
  90. tm->tv_nsec += inc->tv_nsec;
  91. snd_seq_sanity_real_time(tm);
  92. }
  93. static inline void snd_seq_inc_time_nsec(snd_seq_real_time_t *tm, unsigned long nsec)
  94. {
  95. tm->tv_nsec += nsec;
  96. snd_seq_sanity_real_time(tm);
  97. }
  98. /* called by timer isr */
  99. int snd_seq_timer_open(queue_t *q);
  100. int snd_seq_timer_close(queue_t *q);
  101. int snd_seq_timer_midi_open(queue_t *q);
  102. int snd_seq_timer_midi_close(queue_t *q);
  103. void snd_seq_timer_defaults(seq_timer_t *tmr);
  104. void snd_seq_timer_reset(seq_timer_t *tmr);
  105. int snd_seq_timer_stop(seq_timer_t *tmr);
  106. int snd_seq_timer_start(seq_timer_t *tmr);
  107. int snd_seq_timer_continue(seq_timer_t *tmr);
  108. int snd_seq_timer_set_tempo(seq_timer_t *tmr, int tempo);
  109. int snd_seq_timer_set_ppq(seq_timer_t *tmr, int ppq);
  110. int snd_seq_timer_set_position_tick(seq_timer_t *tmr, snd_seq_tick_time_t position);
  111. int snd_seq_timer_set_position_time(seq_timer_t *tmr, snd_seq_real_time_t position);
  112. int snd_seq_timer_set_skew(seq_timer_t *tmr, unsigned int skew, unsigned int base);
  113. snd_seq_real_time_t snd_seq_timer_get_cur_time(seq_timer_t *tmr);
  114. snd_seq_tick_time_t snd_seq_timer_get_cur_tick(seq_timer_t *tmr);
  115. #endif