Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *  ALSA sequencer Timer
0004  *  Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
0005  */
0006 #ifndef __SND_SEQ_TIMER_H
0007 #define __SND_SEQ_TIMER_H
0008 
0009 #include <sound/timer.h>
0010 #include <sound/seq_kernel.h>
0011 
0012 struct snd_seq_timer_tick {
0013     snd_seq_tick_time_t cur_tick;   /* current tick */
0014     unsigned long       resolution; /* time per tick in nsec */
0015     unsigned long       fraction;   /* current time per tick in nsec */
0016 };
0017 
0018 struct snd_seq_timer {
0019     /* ... tempo / offset / running state */
0020 
0021     unsigned int        running:1,  /* running state of queue */    
0022                 initialized:1;  /* timer is initialized */
0023 
0024     unsigned int        tempo;      /* current tempo, us/tick */
0025     int         ppq;        /* time resolution, ticks/quarter */
0026 
0027     snd_seq_real_time_t cur_time;   /* current time */
0028     struct snd_seq_timer_tick   tick;   /* current tick */
0029     int tick_updated;
0030     
0031     int         type;       /* timer type */
0032     struct snd_timer_id alsa_id;    /* ALSA's timer ID */
0033     struct snd_timer_instance   *timeri;    /* timer instance */
0034     unsigned int        ticks;
0035     unsigned long       preferred_resolution; /* timer resolution, ticks/sec */
0036 
0037     unsigned int skew;
0038     unsigned int skew_base;
0039 
0040     struct timespec64   last_update;     /* time of last clock update, used for interpolation */
0041 
0042     spinlock_t lock;
0043 };
0044 
0045 
0046 /* create new timer (constructor) */
0047 struct snd_seq_timer *snd_seq_timer_new(void);
0048 
0049 /* delete timer (destructor) */
0050 void snd_seq_timer_delete(struct snd_seq_timer **tmr);
0051 
0052 /* */
0053 static inline void snd_seq_timer_update_tick(struct snd_seq_timer_tick *tick,
0054                          unsigned long resolution)
0055 {
0056     if (tick->resolution > 0) {
0057         tick->fraction += resolution;
0058         tick->cur_tick += (unsigned int)(tick->fraction / tick->resolution);
0059         tick->fraction %= tick->resolution;
0060     }
0061 }
0062 
0063 
0064 /* compare timestamp between events */
0065 /* return 1 if a >= b; otherwise return 0 */
0066 static inline int snd_seq_compare_tick_time(snd_seq_tick_time_t *a, snd_seq_tick_time_t *b)
0067 {
0068     /* compare ticks */
0069     return (*a >= *b);
0070 }
0071 
0072 static inline int snd_seq_compare_real_time(snd_seq_real_time_t *a, snd_seq_real_time_t *b)
0073 {
0074     /* compare real time */
0075     if (a->tv_sec > b->tv_sec)
0076         return 1;
0077     if ((a->tv_sec == b->tv_sec) && (a->tv_nsec >= b->tv_nsec))
0078         return 1;
0079     return 0;
0080 }
0081 
0082 
0083 static inline void snd_seq_sanity_real_time(snd_seq_real_time_t *tm)
0084 {
0085     while (tm->tv_nsec >= 1000000000) {
0086         /* roll-over */
0087         tm->tv_nsec -= 1000000000;
0088                 tm->tv_sec++;
0089         }
0090 }
0091 
0092 
0093 /* increment timestamp */
0094 static inline void snd_seq_inc_real_time(snd_seq_real_time_t *tm, snd_seq_real_time_t *inc)
0095 {
0096     tm->tv_sec  += inc->tv_sec;
0097     tm->tv_nsec += inc->tv_nsec;
0098     snd_seq_sanity_real_time(tm);
0099 }
0100 
0101 static inline void snd_seq_inc_time_nsec(snd_seq_real_time_t *tm, unsigned long nsec)
0102 {
0103     tm->tv_nsec  += nsec;
0104     snd_seq_sanity_real_time(tm);
0105 }
0106 
0107 /* called by timer isr */
0108 struct snd_seq_queue;
0109 int snd_seq_timer_open(struct snd_seq_queue *q);
0110 int snd_seq_timer_close(struct snd_seq_queue *q);
0111 int snd_seq_timer_midi_open(struct snd_seq_queue *q);
0112 int snd_seq_timer_midi_close(struct snd_seq_queue *q);
0113 void snd_seq_timer_defaults(struct snd_seq_timer *tmr);
0114 void snd_seq_timer_reset(struct snd_seq_timer *tmr);
0115 int snd_seq_timer_stop(struct snd_seq_timer *tmr);
0116 int snd_seq_timer_start(struct snd_seq_timer *tmr);
0117 int snd_seq_timer_continue(struct snd_seq_timer *tmr);
0118 int snd_seq_timer_set_tempo(struct snd_seq_timer *tmr, int tempo);
0119 int snd_seq_timer_set_tempo_ppq(struct snd_seq_timer *tmr, int tempo, int ppq);
0120 int snd_seq_timer_set_position_tick(struct snd_seq_timer *tmr, snd_seq_tick_time_t position);
0121 int snd_seq_timer_set_position_time(struct snd_seq_timer *tmr, snd_seq_real_time_t position);
0122 int snd_seq_timer_set_skew(struct snd_seq_timer *tmr, unsigned int skew, unsigned int base);
0123 snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr,
0124                            bool adjust_ktime);
0125 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(struct snd_seq_timer *tmr);
0126 
0127 extern int seq_default_timer_class;
0128 extern int seq_default_timer_sclass;
0129 extern int seq_default_timer_card;
0130 extern int seq_default_timer_device;
0131 extern int seq_default_timer_subdevice;
0132 extern int seq_default_timer_resolution;
0133 
0134 #endif