seq_timer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * ALSA sequencer Timer
  4. * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
  5. */
  6. #ifndef __SND_SEQ_TIMER_H
  7. #define __SND_SEQ_TIMER_H
  8. #include <sound/timer.h>
  9. #include <sound/seq_kernel.h>
  10. struct snd_seq_timer_tick {
  11. snd_seq_tick_time_t cur_tick; /* current tick */
  12. unsigned long resolution; /* time per tick in nsec */
  13. unsigned long fraction; /* current time per tick in nsec */
  14. };
  15. struct snd_seq_timer {
  16. /* ... tempo / offset / running state */
  17. unsigned int running:1, /* running state of queue */
  18. initialized:1; /* timer is initialized */
  19. unsigned int tempo; /* current tempo, us/tick */
  20. int ppq; /* time resolution, ticks/quarter */
  21. snd_seq_real_time_t cur_time; /* current time */
  22. struct snd_seq_timer_tick tick; /* current tick */
  23. int tick_updated;
  24. int type; /* timer type */
  25. struct snd_timer_id alsa_id; /* ALSA's timer ID */
  26. struct snd_timer_instance *timeri; /* timer instance */
  27. unsigned int ticks;
  28. unsigned long preferred_resolution; /* timer resolution, ticks/sec */
  29. unsigned int skew;
  30. unsigned int skew_base;
  31. unsigned int tempo_base;
  32. struct timespec64 last_update; /* time of last clock update, used for interpolation */
  33. spinlock_t lock;
  34. };
  35. /* create new timer (constructor) */
  36. struct snd_seq_timer *snd_seq_timer_new(void);
  37. /* delete timer (destructor) */
  38. void snd_seq_timer_delete(struct snd_seq_timer **tmr);
  39. /* */
  40. static inline void snd_seq_timer_update_tick(struct snd_seq_timer_tick *tick,
  41. unsigned long resolution)
  42. {
  43. if (tick->resolution > 0) {
  44. tick->fraction += resolution;
  45. tick->cur_tick += (unsigned int)(tick->fraction / tick->resolution);
  46. tick->fraction %= tick->resolution;
  47. }
  48. }
  49. /* compare timestamp between events */
  50. /* return 1 if a >= b; otherwise return 0 */
  51. static inline int snd_seq_compare_tick_time(snd_seq_tick_time_t *a, snd_seq_tick_time_t *b)
  52. {
  53. /* compare ticks */
  54. return (*a >= *b);
  55. }
  56. static inline int snd_seq_compare_real_time(snd_seq_real_time_t *a, snd_seq_real_time_t *b)
  57. {
  58. /* compare real time */
  59. if (a->tv_sec > b->tv_sec)
  60. return 1;
  61. if ((a->tv_sec == b->tv_sec) && (a->tv_nsec >= b->tv_nsec))
  62. return 1;
  63. return 0;
  64. }
  65. static inline void snd_seq_sanity_real_time(snd_seq_real_time_t *tm)
  66. {
  67. while (tm->tv_nsec >= 1000000000) {
  68. /* roll-over */
  69. tm->tv_nsec -= 1000000000;
  70. tm->tv_sec++;
  71. }
  72. }
  73. /* increment timestamp */
  74. static inline void snd_seq_inc_real_time(snd_seq_real_time_t *tm, snd_seq_real_time_t *inc)
  75. {
  76. tm->tv_sec += inc->tv_sec;
  77. tm->tv_nsec += inc->tv_nsec;
  78. snd_seq_sanity_real_time(tm);
  79. }
  80. static inline void snd_seq_inc_time_nsec(snd_seq_real_time_t *tm, unsigned long nsec)
  81. {
  82. tm->tv_nsec += nsec;
  83. snd_seq_sanity_real_time(tm);
  84. }
  85. /* called by timer isr */
  86. struct snd_seq_queue;
  87. int snd_seq_timer_open(struct snd_seq_queue *q);
  88. int snd_seq_timer_close(struct snd_seq_queue *q);
  89. void snd_seq_timer_defaults(struct snd_seq_timer *tmr);
  90. void snd_seq_timer_reset(struct snd_seq_timer *tmr);
  91. int snd_seq_timer_stop(struct snd_seq_timer *tmr);
  92. int snd_seq_timer_start(struct snd_seq_timer *tmr);
  93. int snd_seq_timer_continue(struct snd_seq_timer *tmr);
  94. int snd_seq_timer_set_tempo(struct snd_seq_timer *tmr, int tempo);
  95. int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq,
  96. unsigned int tempo_base);
  97. int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, snd_seq_tick_time_t position);
  98. int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, snd_seq_real_time_t position);
  99. int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, unsigned int base);
  100. snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
  101. bool adjust_ktime);
  102. snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr);
  103. extern int seq_default_timer_class;
  104. extern int seq_default_timer_sclass;
  105. extern int seq_default_timer_card;
  106. extern int seq_default_timer_device;
  107. extern int seq_default_timer_subdevice;
  108. extern int seq_default_timer_resolution;
  109. #endif