seq_timer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * ALSA sequencer Timer
  3. * Copyright (c) 1998-1999 by Frank van de Pol <fvdpol@coil.demon.nl>
  4. * Jaroslav Kysela <perex@suse.cz>
  5. *
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. *
  21. */
  22. #include <sound/driver.h>
  23. #include <sound/core.h>
  24. #include <linux/slab.h>
  25. #include "seq_timer.h"
  26. #include "seq_queue.h"
  27. #include "seq_info.h"
  28. extern int seq_default_timer_class;
  29. extern int seq_default_timer_sclass;
  30. extern int seq_default_timer_card;
  31. extern int seq_default_timer_device;
  32. extern int seq_default_timer_subdevice;
  33. extern int seq_default_timer_resolution;
  34. #define SKEW_BASE 0x10000 /* 16bit shift */
  35. static void snd_seq_timer_set_tick_resolution(seq_timer_tick_t *tick,
  36. int tempo, int ppq, int nticks)
  37. {
  38. if (tempo < 1000000)
  39. tick->resolution = (tempo * 1000) / ppq;
  40. else {
  41. /* might overflow.. */
  42. unsigned int s;
  43. s = tempo % ppq;
  44. s = (s * 1000) / ppq;
  45. tick->resolution = (tempo / ppq) * 1000;
  46. tick->resolution += s;
  47. }
  48. if (tick->resolution <= 0)
  49. tick->resolution = 1;
  50. tick->resolution *= nticks;
  51. snd_seq_timer_update_tick(tick, 0);
  52. }
  53. /* create new timer (constructor) */
  54. seq_timer_t *snd_seq_timer_new(void)
  55. {
  56. seq_timer_t *tmr;
  57. tmr = kcalloc(1, sizeof(*tmr), GFP_KERNEL);
  58. if (tmr == NULL) {
  59. snd_printd("malloc failed for snd_seq_timer_new() \n");
  60. return NULL;
  61. }
  62. spin_lock_init(&tmr->lock);
  63. /* reset setup to defaults */
  64. snd_seq_timer_defaults(tmr);
  65. /* reset time */
  66. snd_seq_timer_reset(tmr);
  67. return tmr;
  68. }
  69. /* delete timer (destructor) */
  70. void snd_seq_timer_delete(seq_timer_t **tmr)
  71. {
  72. seq_timer_t *t = *tmr;
  73. *tmr = NULL;
  74. if (t == NULL) {
  75. snd_printd("oops: snd_seq_timer_delete() called with NULL timer\n");
  76. return;
  77. }
  78. t->running = 0;
  79. /* reset time */
  80. snd_seq_timer_stop(t);
  81. snd_seq_timer_reset(t);
  82. kfree(t);
  83. }
  84. void snd_seq_timer_defaults(seq_timer_t * tmr)
  85. {
  86. /* setup defaults */
  87. tmr->ppq = 96; /* 96 PPQ */
  88. tmr->tempo = 500000; /* 120 BPM */
  89. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq, 1);
  90. tmr->running = 0;
  91. tmr->type = SNDRV_SEQ_TIMER_ALSA;
  92. tmr->alsa_id.dev_class = seq_default_timer_class;
  93. tmr->alsa_id.dev_sclass = seq_default_timer_sclass;
  94. tmr->alsa_id.card = seq_default_timer_card;
  95. tmr->alsa_id.device = seq_default_timer_device;
  96. tmr->alsa_id.subdevice = seq_default_timer_subdevice;
  97. tmr->preferred_resolution = seq_default_timer_resolution;
  98. tmr->skew = tmr->skew_base = SKEW_BASE;
  99. }
  100. void snd_seq_timer_reset(seq_timer_t * tmr)
  101. {
  102. unsigned long flags;
  103. spin_lock_irqsave(&tmr->lock, flags);
  104. /* reset time & songposition */
  105. tmr->cur_time.tv_sec = 0;
  106. tmr->cur_time.tv_nsec = 0;
  107. tmr->tick.cur_tick = 0;
  108. tmr->tick.fraction = 0;
  109. spin_unlock_irqrestore(&tmr->lock, flags);
  110. }
  111. /* called by timer interrupt routine. the period time since previous invocation is passed */
  112. static void snd_seq_timer_interrupt(snd_timer_instance_t *timeri,
  113. unsigned long resolution,
  114. unsigned long ticks)
  115. {
  116. unsigned long flags;
  117. queue_t *q = (queue_t *)timeri->callback_data;
  118. seq_timer_t *tmr;
  119. if (q == NULL)
  120. return;
  121. tmr = q->timer;
  122. if (tmr == NULL)
  123. return;
  124. if (!tmr->running)
  125. return;
  126. resolution *= ticks;
  127. if (tmr->skew != tmr->skew_base) {
  128. /* FIXME: assuming skew_base = 0x10000 */
  129. resolution = (resolution >> 16) * tmr->skew +
  130. (((resolution & 0xffff) * tmr->skew) >> 16);
  131. }
  132. spin_lock_irqsave(&tmr->lock, flags);
  133. /* update timer */
  134. snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
  135. /* calculate current tick */
  136. snd_seq_timer_update_tick(&tmr->tick, resolution);
  137. /* register actual time of this timer update */
  138. do_gettimeofday(&tmr->last_update);
  139. spin_unlock_irqrestore(&tmr->lock, flags);
  140. /* check queues and dispatch events */
  141. snd_seq_check_queue(q, 1, 0);
  142. }
  143. /* set current tempo */
  144. int snd_seq_timer_set_tempo(seq_timer_t * tmr, int tempo)
  145. {
  146. unsigned long flags;
  147. snd_assert(tmr, return -EINVAL);
  148. if (tempo <= 0)
  149. return -EINVAL;
  150. spin_lock_irqsave(&tmr->lock, flags);
  151. if ((unsigned int)tempo != tmr->tempo) {
  152. tmr->tempo = tempo;
  153. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq, 1);
  154. }
  155. spin_unlock_irqrestore(&tmr->lock, flags);
  156. return 0;
  157. }
  158. /* set current ppq */
  159. int snd_seq_timer_set_ppq(seq_timer_t * tmr, int ppq)
  160. {
  161. unsigned long flags;
  162. snd_assert(tmr, return -EINVAL);
  163. if (ppq <= 0)
  164. return -EINVAL;
  165. spin_lock_irqsave(&tmr->lock, flags);
  166. if (tmr->running && (ppq != tmr->ppq)) {
  167. /* refuse to change ppq on running timers */
  168. /* because it will upset the song position (ticks) */
  169. spin_unlock_irqrestore(&tmr->lock, flags);
  170. snd_printd("seq: cannot change ppq of a running timer\n");
  171. return -EBUSY;
  172. }
  173. tmr->ppq = ppq;
  174. snd_seq_timer_set_tick_resolution(&tmr->tick, tmr->tempo, tmr->ppq, 1);
  175. spin_unlock_irqrestore(&tmr->lock, flags);
  176. return 0;
  177. }
  178. /* set current tick position */
  179. int snd_seq_timer_set_position_tick(seq_timer_t *tmr, snd_seq_tick_time_t position)
  180. {
  181. unsigned long flags;
  182. snd_assert(tmr, return -EINVAL);
  183. spin_lock_irqsave(&tmr->lock, flags);
  184. tmr->tick.cur_tick = position;
  185. tmr->tick.fraction = 0;
  186. spin_unlock_irqrestore(&tmr->lock, flags);
  187. return 0;
  188. }
  189. /* set current real-time position */
  190. int snd_seq_timer_set_position_time(seq_timer_t *tmr, snd_seq_real_time_t position)
  191. {
  192. unsigned long flags;
  193. snd_assert(tmr, return -EINVAL);
  194. snd_seq_sanity_real_time(&position);
  195. spin_lock_irqsave(&tmr->lock, flags);
  196. tmr->cur_time = position;
  197. spin_unlock_irqrestore(&tmr->lock, flags);
  198. return 0;
  199. }
  200. /* set timer skew */
  201. int snd_seq_timer_set_skew(seq_timer_t *tmr, unsigned int skew, unsigned int base)
  202. {
  203. unsigned long flags;
  204. snd_assert(tmr, return -EINVAL);
  205. /* FIXME */
  206. if (base != SKEW_BASE) {
  207. snd_printd("invalid skew base 0x%x\n", base);
  208. return -EINVAL;
  209. }
  210. spin_lock_irqsave(&tmr->lock, flags);
  211. tmr->skew = skew;
  212. spin_unlock_irqrestore(&tmr->lock, flags);
  213. return 0;
  214. }
  215. int snd_seq_timer_open(queue_t *q)
  216. {
  217. snd_timer_instance_t *t;
  218. seq_timer_t *tmr;
  219. char str[32];
  220. int err;
  221. tmr = q->timer;
  222. snd_assert(tmr != NULL, return -EINVAL);
  223. if (tmr->timeri)
  224. return -EBUSY;
  225. sprintf(str, "sequencer queue %i", q->queue);
  226. if (tmr->type != SNDRV_SEQ_TIMER_ALSA) /* standard ALSA timer */
  227. return -EINVAL;
  228. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
  229. tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  230. err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
  231. if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
  232. if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
  233. tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
  234. snd_timer_id_t tid;
  235. memset(&tid, 0, sizeof(tid));
  236. tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
  237. tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
  238. tid.card = -1;
  239. tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
  240. err = snd_timer_open(&t, str, &tid, q->queue);
  241. }
  242. if (err < 0) {
  243. snd_printk(KERN_ERR "seq fatal error: cannot create timer (%i)\n", err);
  244. return err;
  245. }
  246. }
  247. t->callback = snd_seq_timer_interrupt;
  248. t->callback_data = q;
  249. t->flags |= SNDRV_TIMER_IFLG_AUTO;
  250. tmr->timeri = t;
  251. return 0;
  252. }
  253. int snd_seq_timer_close(queue_t *q)
  254. {
  255. seq_timer_t *tmr;
  256. tmr = q->timer;
  257. snd_assert(tmr != NULL, return -EINVAL);
  258. if (tmr->timeri) {
  259. snd_timer_stop(tmr->timeri);
  260. snd_timer_close(tmr->timeri);
  261. tmr->timeri = NULL;
  262. }
  263. return 0;
  264. }
  265. int snd_seq_timer_stop(seq_timer_t * tmr)
  266. {
  267. if (! tmr->timeri)
  268. return -EINVAL;
  269. if (!tmr->running)
  270. return 0;
  271. tmr->running = 0;
  272. snd_timer_pause(tmr->timeri);
  273. return 0;
  274. }
  275. static int initialize_timer(seq_timer_t *tmr)
  276. {
  277. snd_timer_t *t;
  278. t = tmr->timeri->timer;
  279. snd_assert(t, return -EINVAL);
  280. tmr->ticks = 1;
  281. if (tmr->preferred_resolution &&
  282. ! (t->hw.flags & SNDRV_TIMER_HW_SLAVE)) {
  283. unsigned long r = t->hw.resolution;
  284. if (! r && t->hw.c_resolution)
  285. r = t->hw.c_resolution(t);
  286. if (r) {
  287. tmr->ticks = (unsigned int)(1000000000uL / (r * tmr->preferred_resolution));
  288. if (! tmr->ticks)
  289. tmr->ticks = 1;
  290. }
  291. }
  292. tmr->initialized = 1;
  293. return 0;
  294. }
  295. int snd_seq_timer_start(seq_timer_t * tmr)
  296. {
  297. if (! tmr->timeri)
  298. return -EINVAL;
  299. if (tmr->running)
  300. snd_seq_timer_stop(tmr);
  301. snd_seq_timer_reset(tmr);
  302. if (initialize_timer(tmr) < 0)
  303. return -EINVAL;
  304. snd_timer_start(tmr->timeri, tmr->ticks);
  305. tmr->running = 1;
  306. do_gettimeofday(&tmr->last_update);
  307. return 0;
  308. }
  309. int snd_seq_timer_continue(seq_timer_t * tmr)
  310. {
  311. if (! tmr->timeri)
  312. return -EINVAL;
  313. if (tmr->running)
  314. return -EBUSY;
  315. if (! tmr->initialized) {
  316. snd_seq_timer_reset(tmr);
  317. if (initialize_timer(tmr) < 0)
  318. return -EINVAL;
  319. }
  320. snd_timer_start(tmr->timeri, tmr->ticks);
  321. tmr->running = 1;
  322. do_gettimeofday(&tmr->last_update);
  323. return 0;
  324. }
  325. /* return current 'real' time. use timeofday() to get better granularity. */
  326. snd_seq_real_time_t snd_seq_timer_get_cur_time(seq_timer_t *tmr)
  327. {
  328. snd_seq_real_time_t cur_time;
  329. cur_time = tmr->cur_time;
  330. if (tmr->running) {
  331. struct timeval tm;
  332. int usec;
  333. do_gettimeofday(&tm);
  334. usec = (int)(tm.tv_usec - tmr->last_update.tv_usec);
  335. if (usec < 0) {
  336. cur_time.tv_nsec += (1000000 + usec) * 1000;
  337. cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec - 1;
  338. } else {
  339. cur_time.tv_nsec += usec * 1000;
  340. cur_time.tv_sec += tm.tv_sec - tmr->last_update.tv_sec;
  341. }
  342. snd_seq_sanity_real_time(&cur_time);
  343. }
  344. return cur_time;
  345. }
  346. /* TODO: use interpolation on tick queue (will only be useful for very
  347. high PPQ values) */
  348. snd_seq_tick_time_t snd_seq_timer_get_cur_tick(seq_timer_t *tmr)
  349. {
  350. return tmr->tick.cur_tick;
  351. }
  352. /* exported to seq_info.c */
  353. void snd_seq_info_timer_read(snd_info_entry_t *entry, snd_info_buffer_t * buffer)
  354. {
  355. int idx;
  356. queue_t *q;
  357. seq_timer_t *tmr;
  358. snd_timer_instance_t *ti;
  359. unsigned long resolution;
  360. for (idx = 0; idx < SNDRV_SEQ_MAX_QUEUES; idx++) {
  361. q = queueptr(idx);
  362. if (q == NULL)
  363. continue;
  364. if ((tmr = q->timer) == NULL ||
  365. (ti = tmr->timeri) == NULL) {
  366. queuefree(q);
  367. continue;
  368. }
  369. snd_iprintf(buffer, "Timer for queue %i : %s\n", q->queue, ti->timer->name);
  370. resolution = snd_timer_resolution(ti) * tmr->ticks;
  371. snd_iprintf(buffer, " Period time : %lu.%09lu\n", resolution / 1000000000, resolution % 1000000000);
  372. snd_iprintf(buffer, " Skew : %u / %u\n", tmr->skew, tmr->skew_base);
  373. queuefree(q);
  374. }
  375. }