xref: /linux/sound/core/seq/seq_timer.h (revision d67b569f5f620c0fb95d5212642746b7ba9d29e4)
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 
24 #include <sound/timer.h>
25 #include <sound/seq_kernel.h>
26 
27 typedef struct {
28 	snd_seq_tick_time_t	cur_tick;	/* current tick */
29 	unsigned long		resolution;	/* time per tick in nsec */
30 	unsigned long		fraction;	/* current time per tick in nsec */
31 } seq_timer_tick_t;
32 
33 typedef struct {
34 	/* ... tempo / offset / running state */
35 
36 	unsigned int		running:1,	/* running state of queue */
37 				initialized:1;	/* timer is initialized */
38 
39 	unsigned int		tempo;		/* current tempo, us/tick */
40 	int			ppq;		/* time resolution, ticks/quarter */
41 
42 	snd_seq_real_time_t	cur_time;	/* current time */
43 	seq_timer_tick_t	tick;		/* current tick */
44 	int tick_updated;
45 
46 	int			type;		/* timer type */
47 	snd_timer_id_t		alsa_id;	/* ALSA's timer ID */
48 	snd_timer_instance_t	*timeri;	/* timer instance */
49 	unsigned int		ticks;
50 	unsigned long		preferred_resolution; /* timer resolution, ticks/sec */
51 
52 	unsigned int skew;
53 	unsigned int skew_base;
54 
55 	struct timeval 		last_update;	 /* time of last clock update, used for interpolation */
56 
57 	spinlock_t lock;
58 } seq_timer_t;
59 
60 
61 /* create new timer (constructor) */
62 extern seq_timer_t *snd_seq_timer_new(void);
63 
64 /* delete timer (destructor) */
65 extern void snd_seq_timer_delete(seq_timer_t **tmr);
66 
67 /* */
68 static inline void snd_seq_timer_update_tick(seq_timer_tick_t *tick, unsigned long resolution)
69 {
70 	if (tick->resolution > 0) {
71 		tick->fraction += resolution;
72 		tick->cur_tick += (unsigned int)(tick->fraction / tick->resolution);
73 		tick->fraction %= tick->resolution;
74 	}
75 }
76 
77 
78 /* compare timestamp between events */
79 /* return 1 if a >= b; otherwise return 0 */
80 static inline int snd_seq_compare_tick_time(snd_seq_tick_time_t *a, snd_seq_tick_time_t *b)
81 {
82 	/* compare ticks */
83 	return (*a >= *b);
84 }
85 
86 static inline int snd_seq_compare_real_time(snd_seq_real_time_t *a, snd_seq_real_time_t *b)
87 {
88 	/* compare real time */
89 	if (a->tv_sec > b->tv_sec)
90 		return 1;
91 	if ((a->tv_sec == b->tv_sec) && (a->tv_nsec >= b->tv_nsec))
92 		return 1;
93 	return 0;
94 }
95 
96 
97 static inline void snd_seq_sanity_real_time(snd_seq_real_time_t *tm)
98 {
99 	while (tm->tv_nsec >= 1000000000) {
100 		/* roll-over */
101 		tm->tv_nsec -= 1000000000;
102                 tm->tv_sec++;
103         }
104 }
105 
106 
107 /* increment timestamp */
108 static inline void snd_seq_inc_real_time(snd_seq_real_time_t *tm, snd_seq_real_time_t *inc)
109 {
110 	tm->tv_sec  += inc->tv_sec;
111 	tm->tv_nsec += inc->tv_nsec;
112 	snd_seq_sanity_real_time(tm);
113 }
114 
115 static inline void snd_seq_inc_time_nsec(snd_seq_real_time_t *tm, unsigned long nsec)
116 {
117 	tm->tv_nsec  += nsec;
118 	snd_seq_sanity_real_time(tm);
119 }
120 
121 /* called by timer isr */
122 int snd_seq_timer_open(queue_t *q);
123 int snd_seq_timer_close(queue_t *q);
124 int snd_seq_timer_midi_open(queue_t *q);
125 int snd_seq_timer_midi_close(queue_t *q);
126 void snd_seq_timer_defaults(seq_timer_t *tmr);
127 void snd_seq_timer_reset(seq_timer_t *tmr);
128 int snd_seq_timer_stop(seq_timer_t *tmr);
129 int snd_seq_timer_start(seq_timer_t *tmr);
130 int snd_seq_timer_continue(seq_timer_t *tmr);
131 int snd_seq_timer_set_tempo(seq_timer_t *tmr, int tempo);
132 int snd_seq_timer_set_ppq(seq_timer_t *tmr, int ppq);
133 int snd_seq_timer_set_position_tick(seq_timer_t *tmr, snd_seq_tick_time_t position);
134 int snd_seq_timer_set_position_time(seq_timer_t *tmr, snd_seq_real_time_t position);
135 int snd_seq_timer_set_skew(seq_timer_t *tmr, unsigned int skew, unsigned int base);
136 snd_seq_real_time_t snd_seq_timer_get_cur_time(seq_timer_t *tmr);
137 snd_seq_tick_time_t snd_seq_timer_get_cur_tick(seq_timer_t *tmr);
138 
139 #endif
140