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
*tmr
)
38 if (tmr
->tempo
< 1000000)
39 tmr
->tick
.resolution
= (tmr
->tempo
* 1000) / tmr
->ppq
;
41 /* might overflow.. */
43 s
= tmr
->tempo
% tmr
->ppq
;
44 s
= (s
* 1000) / tmr
->ppq
;
45 tmr
->tick
.resolution
= (tmr
->tempo
/ tmr
->ppq
) * 1000;
46 tmr
->tick
.resolution
+= s
;
48 if (tmr
->tick
.resolution
<= 0)
49 tmr
->tick
.resolution
= 1;
50 snd_seq_timer_update_tick(&tmr
->tick
, 0);
53 /* create new timer (constructor) */
54 struct snd_seq_timer
*snd_seq_timer_new(void)
56 struct snd_seq_timer
*tmr
;
58 tmr
= kzalloc(sizeof(*tmr
), GFP_KERNEL
);
60 snd_printd("malloc failed for snd_seq_timer_new() \n");
63 spin_lock_init(&tmr
->lock
);
65 /* reset setup to defaults */
66 snd_seq_timer_defaults(tmr
);
69 snd_seq_timer_reset(tmr
);
74 /* delete timer (destructor) */
75 void snd_seq_timer_delete(struct snd_seq_timer
**tmr
)
77 struct snd_seq_timer
*t
= *tmr
;
81 snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
87 snd_seq_timer_stop(t
);
88 snd_seq_timer_reset(t
);
93 void snd_seq_timer_defaults(struct snd_seq_timer
* tmr
)
96 tmr
->ppq
= 96; /* 96 PPQ */
97 tmr
->tempo
= 500000; /* 120 BPM */
98 snd_seq_timer_set_tick_resolution(tmr
);
101 tmr
->type
= SNDRV_SEQ_TIMER_ALSA
;
102 tmr
->alsa_id
.dev_class
= seq_default_timer_class
;
103 tmr
->alsa_id
.dev_sclass
= seq_default_timer_sclass
;
104 tmr
->alsa_id
.card
= seq_default_timer_card
;
105 tmr
->alsa_id
.device
= seq_default_timer_device
;
106 tmr
->alsa_id
.subdevice
= seq_default_timer_subdevice
;
107 tmr
->preferred_resolution
= seq_default_timer_resolution
;
109 tmr
->skew
= tmr
->skew_base
= SKEW_BASE
;
112 void snd_seq_timer_reset(struct snd_seq_timer
* tmr
)
116 spin_lock_irqsave(&tmr
->lock
, flags
);
118 /* reset time & songposition */
119 tmr
->cur_time
.tv_sec
= 0;
120 tmr
->cur_time
.tv_nsec
= 0;
122 tmr
->tick
.cur_tick
= 0;
123 tmr
->tick
.fraction
= 0;
125 spin_unlock_irqrestore(&tmr
->lock
, flags
);
129 /* called by timer interrupt routine. the period time since previous invocation is passed */
130 static void snd_seq_timer_interrupt(struct snd_timer_instance
*timeri
,
131 unsigned long resolution
,
135 struct snd_seq_queue
*q
= timeri
->callback_data
;
136 struct snd_seq_timer
*tmr
;
147 if (tmr
->skew
!= tmr
->skew_base
) {
148 /* FIXME: assuming skew_base = 0x10000 */
149 resolution
= (resolution
>> 16) * tmr
->skew
+
150 (((resolution
& 0xffff) * tmr
->skew
) >> 16);
153 spin_lock_irqsave(&tmr
->lock
, flags
);
156 snd_seq_inc_time_nsec(&tmr
->cur_time
, resolution
);
158 /* calculate current tick */
159 snd_seq_timer_update_tick(&tmr
->tick
, resolution
);
161 /* register actual time of this timer update */
162 do_gettimeofday(&tmr
->last_update
);
164 spin_unlock_irqrestore(&tmr
->lock
, flags
);
166 /* check queues and dispatch events */
167 snd_seq_check_queue(q
, 1, 0);
170 /* set current tempo */
171 int snd_seq_timer_set_tempo(struct snd_seq_timer
* tmr
, int tempo
)
175 if (snd_BUG_ON(!tmr
))
179 spin_lock_irqsave(&tmr
->lock
, flags
);
180 if ((unsigned int)tempo
!= tmr
->tempo
) {
182 snd_seq_timer_set_tick_resolution(tmr
);
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 if (snd_BUG_ON(!tmr
))
197 spin_lock_irqsave(&tmr
->lock
, flags
);
198 if (tmr
->running
&& (ppq
!= tmr
->ppq
)) {
199 /* refuse to change ppq on running timers */
200 /* because it will upset the song position (ticks) */
201 spin_unlock_irqrestore(&tmr
->lock
, flags
);
202 snd_printd("seq: cannot change ppq of a running timer\n");
207 snd_seq_timer_set_tick_resolution(tmr
);
208 spin_unlock_irqrestore(&tmr
->lock
, flags
);
212 /* set current tick position */
213 int snd_seq_timer_set_position_tick(struct snd_seq_timer
*tmr
,
214 snd_seq_tick_time_t position
)
218 if (snd_BUG_ON(!tmr
))
221 spin_lock_irqsave(&tmr
->lock
, flags
);
222 tmr
->tick
.cur_tick
= position
;
223 tmr
->tick
.fraction
= 0;
224 spin_unlock_irqrestore(&tmr
->lock
, flags
);
228 /* set current real-time position */
229 int snd_seq_timer_set_position_time(struct snd_seq_timer
*tmr
,
230 snd_seq_real_time_t position
)
234 if (snd_BUG_ON(!tmr
))
237 snd_seq_sanity_real_time(&position
);
238 spin_lock_irqsave(&tmr
->lock
, flags
);
239 tmr
->cur_time
= position
;
240 spin_unlock_irqrestore(&tmr
->lock
, flags
);
245 int snd_seq_timer_set_skew(struct snd_seq_timer
*tmr
, unsigned int skew
,
250 if (snd_BUG_ON(!tmr
))
254 if (base
!= SKEW_BASE
) {
255 snd_printd("invalid skew base 0x%x\n", base
);
258 spin_lock_irqsave(&tmr
->lock
, flags
);
260 spin_unlock_irqrestore(&tmr
->lock
, flags
);
264 int snd_seq_timer_open(struct snd_seq_queue
*q
)
266 struct snd_timer_instance
*t
;
267 struct snd_seq_timer
*tmr
;
272 if (snd_BUG_ON(!tmr
))
276 sprintf(str
, "sequencer queue %i", q
->queue
);
277 if (tmr
->type
!= SNDRV_SEQ_TIMER_ALSA
) /* standard ALSA timer */
279 if (tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
)
280 tmr
->alsa_id
.dev_sclass
= SNDRV_TIMER_SCLASS_SEQUENCER
;
281 err
= snd_timer_open(&t
, str
, &tmr
->alsa_id
, q
->queue
);
282 if (err
< 0 && tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_SLAVE
) {
283 if (tmr
->alsa_id
.dev_class
!= SNDRV_TIMER_CLASS_GLOBAL
||
284 tmr
->alsa_id
.device
!= SNDRV_TIMER_GLOBAL_SYSTEM
) {
285 struct snd_timer_id tid
;
286 memset(&tid
, 0, sizeof(tid
));
287 tid
.dev_class
= SNDRV_TIMER_CLASS_GLOBAL
;
288 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_SEQUENCER
;
290 tid
.device
= SNDRV_TIMER_GLOBAL_SYSTEM
;
291 err
= snd_timer_open(&t
, str
, &tid
, q
->queue
);
294 snd_printk(KERN_ERR
"seq fatal error: cannot create timer (%i)\n", err
);
298 t
->callback
= snd_seq_timer_interrupt
;
299 t
->callback_data
= q
;
300 t
->flags
|= SNDRV_TIMER_IFLG_AUTO
;
305 int snd_seq_timer_close(struct snd_seq_queue
*q
)
307 struct snd_seq_timer
*tmr
;
310 if (snd_BUG_ON(!tmr
))
313 snd_timer_stop(tmr
->timeri
);
314 snd_timer_close(tmr
->timeri
);
320 int snd_seq_timer_stop(struct snd_seq_timer
* tmr
)
327 snd_timer_pause(tmr
->timeri
);
331 static int initialize_timer(struct snd_seq_timer
*tmr
)
336 t
= tmr
->timeri
->timer
;
340 freq
= tmr
->preferred_resolution
;
342 freq
= DEFAULT_FREQUENCY
;
343 else if (freq
< MIN_FREQUENCY
)
344 freq
= MIN_FREQUENCY
;
345 else if (freq
> MAX_FREQUENCY
)
346 freq
= MAX_FREQUENCY
;
349 if (!(t
->hw
.flags
& SNDRV_TIMER_HW_SLAVE
)) {
350 unsigned long r
= t
->hw
.resolution
;
351 if (! r
&& t
->hw
.c_resolution
)
352 r
= t
->hw
.c_resolution(t
);
354 tmr
->ticks
= (unsigned int)(1000000000uL / (r
* freq
));
359 tmr
->initialized
= 1;
363 int snd_seq_timer_start(struct snd_seq_timer
* tmr
)
368 snd_seq_timer_stop(tmr
);
369 snd_seq_timer_reset(tmr
);
370 if (initialize_timer(tmr
) < 0)
372 snd_timer_start(tmr
->timeri
, tmr
->ticks
);
374 do_gettimeofday(&tmr
->last_update
);
378 int snd_seq_timer_continue(struct snd_seq_timer
* tmr
)
384 if (! tmr
->initialized
) {
385 snd_seq_timer_reset(tmr
);
386 if (initialize_timer(tmr
) < 0)
389 snd_timer_start(tmr
->timeri
, tmr
->ticks
);
391 do_gettimeofday(&tmr
->last_update
);
395 /* return current 'real' time. use timeofday() to get better granularity. */
396 snd_seq_real_time_t
snd_seq_timer_get_cur_time(struct snd_seq_timer
*tmr
)
398 snd_seq_real_time_t cur_time
;
400 cur_time
= tmr
->cur_time
;
404 do_gettimeofday(&tm
);
405 usec
= (int)(tm
.tv_usec
- tmr
->last_update
.tv_usec
);
407 cur_time
.tv_nsec
+= (1000000 + usec
) * 1000;
408 cur_time
.tv_sec
+= tm
.tv_sec
- tmr
->last_update
.tv_sec
- 1;
410 cur_time
.tv_nsec
+= usec
* 1000;
411 cur_time
.tv_sec
+= tm
.tv_sec
- tmr
->last_update
.tv_sec
;
413 snd_seq_sanity_real_time(&cur_time
);
419 /* TODO: use interpolation on tick queue (will only be useful for very
421 snd_seq_tick_time_t
snd_seq_timer_get_cur_tick(struct snd_seq_timer
*tmr
)
423 return tmr
->tick
.cur_tick
;
427 #ifdef CONFIG_PROC_FS
428 /* exported to seq_info.c */
429 void snd_seq_info_timer_read(struct snd_info_entry
*entry
,
430 struct snd_info_buffer
*buffer
)
433 struct snd_seq_queue
*q
;
434 struct snd_seq_timer
*tmr
;
435 struct snd_timer_instance
*ti
;
436 unsigned long resolution
;
438 for (idx
= 0; idx
< SNDRV_SEQ_MAX_QUEUES
; idx
++) {
442 if ((tmr
= q
->timer
) == NULL
||
443 (ti
= tmr
->timeri
) == NULL
) {
447 snd_iprintf(buffer
, "Timer for queue %i : %s\n", q
->queue
, ti
->timer
->name
);
448 resolution
= snd_timer_resolution(ti
) * tmr
->ticks
;
449 snd_iprintf(buffer
, " Period time : %lu.%09lu\n", resolution
/ 1000000000, resolution
% 1000000000);
450 snd_iprintf(buffer
, " Skew : %u / %u\n", tmr
->skew
, tmr
->skew_base
);
454 #endif /* CONFIG_PROC_FS */