seq_timer.c 11 KB

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