3 * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@perex.cz>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sound/core.h>
24 #include <linux/slab.h>
25 #include "seq_timer.h"
26 #include "seq_queue.h"
29 /* allowed sequencer timer frequencies, in Hz */
30 #define MIN_FREQUENCY 10
31 #define MAX_FREQUENCY 6250
32 #define DEFAULT_FREQUENCY 1000
34 #define SKEW_BASE 0x10000 /* 16bit shift */
36 static void snd_seq_timer_set_tick_resolution(struct snd_seq_timer_tick
*tick
,
40 tick
->resolution
= (tempo
* 1000) / ppq
;
42 /* might overflow.. */
46 tick
->resolution
= (tempo
/ ppq
) * 1000;
47 tick
->resolution
+= s
;
49 if (tick
->resolution
<= 0)
51 snd_seq_timer_update_tick(tick
, 0);
54 /* create new timer (constructor) */
55 struct snd_seq_timer
*snd_seq_timer_new(void)
57 struct snd_seq_timer
*tmr
;
59 tmr
= kzalloc(sizeof(*tmr
), GFP_KERNEL
);
61 snd_printd("malloc failed for snd_seq_timer_new() \n");
64 spin_lock_init(&tmr
->lock
);
66 /* reset setup to defaults */
67 snd_seq_timer_defaults(tmr
);
70 snd_seq_timer_reset(tmr
);
75 /* delete timer (destructor) */
76 void snd_seq_timer_delete(struct snd_seq_timer
**tmr
)
78 struct snd_seq_timer
*t
= *tmr
;
82 snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
88 snd_seq_timer_stop(t
);
89 snd_seq_timer_reset(t
);
94 void snd_seq_timer_defaults(struct snd_seq_timer
* tmr
)
97 tmr
->ppq
= 96; /* 96 PPQ */
98 tmr
->tempo
= 500000; /* 120 BPM */
99 snd_seq_timer_set_tick_resolution(&tmr
->tick
, tmr
->tempo
, tmr
->ppq
);
102 tmr
->type
= SNDRV_SEQ_TIMER_ALSA
;
103 tmr
->alsa_id
.dev_class
= seq_default_timer_class
;
104 tmr
->alsa_id
.dev_sclass
= seq_default_timer_sclass
;
105 tmr
->alsa_id
.card
= seq_default_timer_card
;
106 tmr
->alsa_id
.device
= seq_default_timer_device
;
107 tmr
->alsa_id
.subdevice
= seq_default_timer_subdevice
;
108 tmr
->preferred_resolution
= seq_default_timer_resolution
;
110 tmr
->skew
= tmr
->skew_base
= SKEW_BASE
;
113 void snd_seq_timer_reset(struct snd_seq_timer
* tmr
)
117 spin_lock_irqsave(&tmr
->lock
, flags
);
119 /* reset time & songposition */
120 tmr
->cur_time
.tv_sec
= 0;
121 tmr
->cur_time
.tv_nsec
= 0;
123 tmr
->tick
.cur_tick
= 0;
124 tmr
->tick
.fraction
= 0;
126 spin_unlock_irqrestore(&tmr
->lock
, flags
);
130 /* called by timer interrupt routine. the period time since previous invocation is passed */
131 static void snd_seq_timer_interrupt(struct snd_timer_instance
*timeri
,
132 unsigned long resolution
,
136 struct snd_seq_queue
*q
= timeri
->callback_data
;
137 struct snd_seq_timer
*tmr
;
148 if (tmr
->skew
!= tmr
->skew_base
) {
149 /* FIXME: assuming skew_base = 0x10000 */
150 resolution
= (resolution
>> 16) * tmr
->skew
+
151 (((resolution
& 0xffff) * tmr
->skew
) >> 16);
154 spin_lock_irqsave(&tmr
->lock
, flags
);
157 snd_seq_inc_time_nsec(&tmr
->cur_time
, resolution
);
159 /* calculate current tick */
160 snd_seq_timer_update_tick(&tmr
->tick
, resolution
);
162 /* register actual time of this timer update */
163 do_gettimeofday(&tmr
->last_update
);
165 spin_unlock_irqrestore(&tmr
->lock
, flags
);
167 /* check queues and dispatch events */
168 snd_seq_check_queue(q
, 1, 0);
171 /* set current tempo */
172 int snd_seq_timer_set_tempo(struct snd_seq_timer
* tmr
, int tempo
)
176 snd_assert(tmr
, return -EINVAL
);
179 spin_lock_irqsave(&tmr
->lock
, flags
);
180 if ((unsigned int)tempo
!= tmr
->tempo
) {
182 snd_seq_timer_set_tick_resolution(&tmr
->tick
, tmr
->tempo
, tmr
->ppq
);
184 spin_unlock_irqrestore(&tmr
->lock
, flags
);
188 /* set current ppq */
189 int snd_seq_timer_set_ppq(struct snd_seq_timer
* tmr
, int ppq
)
193 snd_assert(tmr
, return -EINVAL
);
196 spin_lock_irqsave(&tmr
->lock
, flags
);
197 if (tmr
->running
&& (ppq
!= tmr
->ppq
)) {
198 /* refuse to change ppq on running timers */
199 /* because it will upset the song position (ticks) */
200 spin_unlock_irqrestore(&tmr
->lock
, flags
);
201 snd_printd("seq: cannot change ppq of a running timer\n");
206 snd_seq_timer_set_tick_resolution(&tmr
->tick
, tmr
->tempo
, tmr
->ppq
);
207 spin_unlock_irqrestore(&tmr
->lock
, flags
);
211 /* set current tick position */
212 int snd_seq_timer_set_position_tick(struct snd_seq_timer
*tmr
,
213 snd_seq_tick_time_t position
)
217 snd_assert(tmr
, return -EINVAL
);
219 spin_lock_irqsave(&tmr
->lock
, flags
);
220 tmr
->tick
.cur_tick
= position
;
221 tmr
->tick
.fraction
= 0;
222 spin_unlock_irqrestore(&tmr
->lock
, flags
);
226 /* set current real-time position */
227 int snd_seq_timer_set_position_time(struct snd_seq_timer
*tmr
,
228 snd_seq_real_time_t position
)
232 snd_assert(tmr
, return -EINVAL
);
234 snd_seq_sanity_real_time(&position
);
235 spin_lock_irqsave(&tmr
->lock
, flags
);
236 tmr
->cur_time
= position
;
237 spin_unlock_irqrestore(&tmr
->lock
, flags
);
242 int snd_seq_timer_set_skew(struct snd_seq_timer
*tmr
, unsigned int skew
,
247 snd_assert(tmr
, return -EINVAL
);
250 if (base
!= SKEW_BASE
) {
251 snd_printd("invalid skew base 0x%x\n", base
);
254 spin_lock_irqsave(&tmr
->lock
, flags
);
256 spin_unlock_irqrestore(&tmr
->lock
, flags
);
260 int snd_seq_timer_open(struct snd_seq_queue
*q
)
262 struct snd_timer_instance
*t
;
263 struct snd_seq_timer
*tmr
;
268 snd_assert(tmr
!= NULL
, return -EINVAL
);
271 sprintf(str
, "sequencer queue %i", q
->queue
);
272 if (tmr
->type
!= SNDRV_SEQ_TIMER_ALSA
) /* standard ALSA timer */
274 if (tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
275 tmr
->alsa_id
.dev_sclass
= SNDRV_TIMER_SCLASS_SEQUENCER
;
276 err
= snd_timer_open(&t
, str
, &tmr
->alsa_id
, q
->queue
);
277 if (err
< 0 && tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
) {
278 if (tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_GLOBAL
||
279 tmr
->alsa_id
.device
!= SNDRV_TIMER_GLOBAL_SYSTEM
) {
280 struct snd_timer_id tid
;
281 memset(&tid
, 0, sizeof(tid
));
282 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
283 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_SEQUENCER
;
285 tid
.device
= SNDRV_TIMER_GLOBAL_SYSTEM
;
286 err
= snd_timer_open(&t
, str
, &tid
, q
->queue
);
289 snd_printk(KERN_ERR
"seq fatal error: cannot create timer (%i)\n", err
);
293 t
->callback
= snd_seq_timer_interrupt
;
294 t
->callback_data
= q
;
295 t
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
300 int snd_seq_timer_close(struct snd_seq_queue
*q
)
302 struct snd_seq_timer
*tmr
;
305 snd_assert(tmr
!= NULL
, return -EINVAL
);
307 snd_timer_stop(tmr
->timeri
);
308 snd_timer_close(tmr
->timeri
);
314 int snd_seq_timer_stop(struct snd_seq_timer
* tmr
)
321 snd_timer_pause(tmr
->timeri
);
325 static int initialize_timer(struct snd_seq_timer
*tmr
)
330 t
= tmr
->timeri
->timer
;
331 snd_assert(t
, return -EINVAL
);
333 freq
= tmr
->preferred_resolution
;
335 freq
= DEFAULT_FREQUENCY
;
336 else if (freq
< MIN_FREQUENCY
)
337 freq
= MIN_FREQUENCY
;
338 else if (freq
> MAX_FREQUENCY
)
339 freq
= MAX_FREQUENCY
;
342 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)) {
343 unsigned long r
= t
->hw
.resolution
;
344 if (! r
&& t
->hw
.c_resolution
)
345 r
= t
->hw
.c_resolution(t
);
347 tmr
->ticks
= (unsigned int)(1000000000uL / (r
* freq
));
352 tmr
->initialized
= 1;
356 int snd_seq_timer_start(struct snd_seq_timer
* tmr
)
361 snd_seq_timer_stop(tmr
);
362 snd_seq_timer_reset(tmr
);
363 if (initialize_timer(tmr
) < 0)
365 snd_timer_start(tmr
->timeri
, tmr
->ticks
);
367 do_gettimeofday(&tmr
->last_update
);
371 int snd_seq_timer_continue(struct snd_seq_timer
* tmr
)
377 if (! tmr
->initialized
) {
378 snd_seq_timer_reset(tmr
);
379 if (initialize_timer(tmr
) < 0)
382 snd_timer_start(tmr
->timeri
, tmr
->ticks
);
384 do_gettimeofday(&tmr
->last_update
);
388 /* return current 'real' time. use timeofday() to get better granularity. */
389 snd_seq_real_time_t
snd_seq_timer_get_cur_time(struct snd_seq_timer
*tmr
)
391 snd_seq_real_time_t cur_time
;
393 cur_time
= tmr
->cur_time
;
397 do_gettimeofday(&tm
);
398 usec
= (int)(tm
.tv_usec
- tmr
->last_update
.tv_usec
);
400 cur_time
.tv_nsec
+= (1000000 + usec
) * 1000;
401 cur_time
.tv_sec
+= tm
.tv_sec
- tmr
->last_update
.tv_sec
- 1;
403 cur_time
.tv_nsec
+= usec
* 1000;
404 cur_time
.tv_sec
+= tm
.tv_sec
- tmr
->last_update
.tv_sec
;
406 snd_seq_sanity_real_time(&cur_time
);
412 /* TODO: use interpolation on tick queue (will only be useful for very
414 snd_seq_tick_time_t
snd_seq_timer_get_cur_tick(struct snd_seq_timer
*tmr
)
416 return tmr
->tick
.cur_tick
;
420 #ifdef CONFIG_PROC_FS
421 /* exported to seq_info.c */
422 void snd_seq_info_timer_read(struct snd_info_entry
*entry
,
423 struct snd_info_buffer
*buffer
)
426 struct snd_seq_queue
*q
;
427 struct snd_seq_timer
*tmr
;
428 struct snd_timer_instance
*ti
;
429 unsigned long resolution
;
431 for (idx
= 0; idx
< SNDRV_SEQ_MAX_QUEUES
; idx
++) {
435 if ((tmr
= q
->timer
) == NULL
||
436 (ti
= tmr
->timeri
) == NULL
) {
440 snd_iprintf(buffer
, "Timer for queue %i : %s\n", q
->queue
, ti
->timer
->name
);
441 resolution
= snd_timer_resolution(ti
) * tmr
->ticks
;
442 snd_iprintf(buffer
, " Period time : %lu.%09lu\n", resolution
/ 1000000000, resolution
% 1000000000);
443 snd_iprintf(buffer
, " Skew : %u / %u\n", tmr
->skew
, tmr
->skew_base
);
447 #endif /* CONFIG_PROC_FS */