File: seq_timer.c

package info (click to toggle)
alsadriver 0.2.0-pre8-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,808 kB
  • ctags: 6,550
  • sloc: ansic: 43,490; sh: 916; makefile: 759; perl: 54
file content (212 lines) | stat: -rw-r--r-- 5,061 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 *   ALSA sequencer Timer
 *   Copyright (c) 1998 by Frank van de Pol <F.K.W.van.de.Pol@inter.nl.net>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "driver.h"
#include "seq_timer.h"
#include "seq_queue.h"
#include "seq_info.h"

/* static data */
static snd_seq_real_time_t timer_period;

static void snd_seq_timer_reset(timer_t * tmr);


/* create new timer (constructor) */
timer_t *snd_seq_timer_new(void)
{
	timer_t *tmr;
	
	tmr = snd_malloc(sizeof(timer_t));
	if (tmr == NULL) {
		snd_printk("malloc failed for snd_seq_timer_new() \n");
		return NULL;
	}

	/* clear unused variables */
	memset(tmr, 0, sizeof(timer_t));

	/* setup defaults */
	tmr->ppq = 96;		/* 96 PPQ */
	tmr->tempo = 500000;	/* 120 BPM */	
	tmr->running = 0;

	/* reset time */
	snd_seq_timer_reset(tmr);
	
	return tmr;
}

/* delete timer (destructor) */
void snd_seq_timer_delete(timer_t **tmr)
{
	timer_t *t = *tmr;
	*tmr = NULL;

	if (t == NULL) {
		snd_printk("oops: snd_seq_timer_delete() called with NULL timer\n");
		return;
	}
	t->running = 0;

	/* reset time */
	snd_seq_timer_reset(t);

	snd_free(t,sizeof(timer_t));
}


static void snd_seq_timer_reset(timer_t * tmr)
{
	/* reset time & songposition */
	tmr->cur_tick = 0;
	tmr->cur_time.tv_sec = 0;
	tmr->cur_time.tv_nsec = 0;

	tmr->tempo_tick = 0;
	tmr->tempo_time.tv_sec = 0;
	tmr->tempo_time.tv_nsec = 0;

	tmr->sync_tmp = 0;
}


/* called by timer interrupt routine. the period time since previous invocation is passed */
int snd_seq_timer_interrupt(snd_seq_real_time_t * period)
{
	volatile int timers_running = 0;	/* flag if we did increment times */
	int c;
	queue_t *q;
	timer_t *tmr;


	/* FIXME: temporary hack to get timer resolution, should be set by registration call (together with name) */
	memcpy(&timer_period, period, sizeof(snd_seq_real_time_t));


	/* process all timers... */

	for (c = 0; c < SND_SEQ_MAX_QUEUES; c++) {
		q = queueptr(c);
		if (q == NULL)
			continue;

		tmr = q->timer;

		if (tmr != NULL) {
			if (tmr->running) {
				double delta_time;

				/* update timer */
				snd_seq_inc_real_time(&tmr->cur_time, period);
				timers_running = 1;

				/* calculate current tick */
				/* FIXME: use of floating point stuff, illegal in kernel (??) */
				if (tmr->tempo > 0) {
					delta_time = tmr->cur_time.tv_sec - tmr->tempo_time.tv_sec + (1.0E-9 * (double) (tmr->cur_time.tv_nsec - tmr->tempo_time.tv_nsec));
					tmr->cur_tick = tmr->tempo_tick + (tmr->ppq * delta_time * 1.0E6 / tmr->tempo);
				}
			}
		}
	}

	if (timers_running) {

#ifdef 0
		/* sync timer has expired *//* FIXME: hack to send midi sync info */
		if ((tmr->cur_tick > tmr->sync_tmp) && (snd_seq_unused_cells() > 10)) {
			snd_seq_event_t ev;

			ev.type = SND_SEQ_EVENT_CLOCK;
			ev.flags = SND_SEQ_TIME_STAMP_TICK | SND_SEQ_TIME_MODE_ABS;
			ev.time.tick = 0;
			ev.source.queue = 0;
			ev.source.client = 0;
			ev.source.port = 0;
			ev.dest.queue = 1;
			ev.dest.client = 255;
			ev.dest.port = 255;
			if (snd_seq_kernel_client_dispatch(0, &ev)) {
				tmr->sync_tmp += tmr->ppq / 24;
			}
		}
#endif
		/* check queues and dispatch events */
		snd_seq_check_queues();
	}
	return 0;		/* FIXME ??? perhaps return the timers_running flag, so the timer can decrease frequency if there are no active queues */
}


void snd_seq_timer_set_tempo(timer_t * tmr, int tempo)
{
	if (tmr) {
		/* store location of tempo chamge */
		tmr->tempo_tick = tmr->cur_tick;
		tmr->tempo_time.tv_sec = tmr->cur_time.tv_sec;
		tmr->tempo_time.tv_nsec = tmr->cur_time.tv_nsec;

		tmr->tempo = tempo;
	}
}


void snd_seq_timer_set_ppq(timer_t * tmr, int ppq)
{
	if (tmr) {
		if (tmr->running & (ppq != tmr->ppq)) {
			/* refuse to change ppq on running timers, because it wil upset the song position (ticks) */
			snd_printk("seq: cannot change ppq of a running timer\n");
		} else {
			tmr->ppq = ppq;
		}
	}
}


void snd_seq_timer_stop(timer_t * tmr)
{
	tmr->running = 0;
}


void snd_seq_timer_start(timer_t * tmr)
{
	tmr->running = 1;

	/* reset time & songposition */
	snd_seq_timer_reset(tmr);
}


void snd_seq_timer_continue(timer_t * tmr)
{
	tmr->running = 1;
}


/* exported to seq_info.c */
void snd_seq_info_timer_read(snd_info_buffer_t * buffer, void *private_data)
{
	snd_iprintf(buffer, "Timer       : %s\n", "Some unknown Timer...");
	snd_iprintf(buffer, "Period time : %d.%09d\n", timer_period.tv_sec, timer_period.tv_nsec);
}