home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / bmd-1.0beta.tar.Z / bmd-1.0beta.tar / bmd-1.0beta / app / omtd / event.c < prev    next >
C/C++ Source or Header  |  1991-08-21  |  3KB  |  166 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Lawrence Berkeley Laboratory,
  11.  * Berkeley, CA.  The name of the University may not be used to
  12.  * endorse or promote products derived from this software without
  13.  * specific prior written permission.
  14.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17.  */
  18. #include <sys/types.h>
  19. #include <sundev/midi.h>
  20. #include "readline.h"
  21. #include "event.h"
  22. #include "track.h"
  23.  
  24. struct event *master_list;
  25. struct event *raw_list;
  26. static struct event *efreelist;
  27.  
  28. struct event *
  29. ealloc()
  30. {
  31.     struct event *p;
  32.  
  33.     if (efreelist) {
  34.         p = efreelist;
  35.         efreelist = p->next;
  36.     } else {
  37.         p = (struct event *)malloc(sizeof(*p));
  38.         if (p == 0) {
  39.             error("out of memory");
  40.             exit(1);
  41.         }
  42.     }
  43.     return p;
  44. }
  45.  
  46. void
  47. efree(p)
  48.     struct event *p;
  49. {
  50.     p->next = efreelist;
  51.     efreelist = p;
  52. }
  53.  
  54. void
  55. efreel(p)
  56.     struct event *p;
  57. {
  58.     struct event *next;
  59.  
  60.     for (; p != 0; p = next) {
  61.         next = p->next;
  62.         efree(p);
  63.     }
  64. }
  65.  
  66. void
  67. pq_insert(pp, p)
  68.     struct event **pp, *p;
  69. {
  70.     while (*pp && p->m.mm_time >= (*pp)->m.mm_time)
  71.         pp = &(*pp)->pqnext;
  72.     p->pqnext = *pp;
  73.     *pp = p;
  74. }
  75.  
  76. struct event *
  77. pq_remove_bynote(pp, note)
  78.     struct event **pp;
  79.     int note;
  80. {
  81.     struct event *p;
  82.  
  83.     while (*pp && (*pp)->m.mm_data[1] != note)
  84.         pp = &(*pp)->pqnext;
  85.     if (*pp == 0)
  86.         return 0;
  87.     p = *pp;
  88.     *pp = (*pp)->pqnext;
  89.  
  90.     return p;
  91. }
  92.  
  93. static void
  94. ptrk(p, trk)
  95.     struct event *p;
  96.     struct track *trk;
  97. {
  98.     for (; p != 0; p = p->next) {
  99.         if (trk != 0 && p->trk != trk)
  100.             continue;
  101.         ptime(p->m.mm_time);
  102.         putchar(' ');
  103.         ptime(p->duration);
  104.         printf(" [%d:%d] \"%s\" %x %x %x\n",
  105.                p->m.mm_time, p->duration, 
  106.                p->trk->name,
  107.                p->m.mm_data[0],
  108.                p->m.mm_data[1],
  109.                p->m.mm_data[2]);
  110.     }
  111. }
  112.  
  113. void
  114. dump_command(trk)
  115.     struct track *trk;
  116. {
  117.     ptrk(master_list, trk);
  118. }
  119.  
  120. write_master(fd)
  121.     int fd;
  122. {
  123.     struct event *p;
  124.  
  125.     for (p = master_list; p != 0; p = p->next) {
  126.         write(fd, &p->trk->id, sizeof p->trk->id);
  127.         write(fd, &p->duration, sizeof p->duration);
  128.         write(fd, &p->m, sizeof p->m);
  129.     }
  130. }
  131.  
  132. read_master(fd)
  133.     int fd;
  134. {
  135.     struct event **pp = &master_list, *p;
  136.     int id;
  137.  
  138.     while (1) {
  139.         if (read(fd, &id, sizeof(id)) != sizeof(id))
  140.             break;
  141.         p = ealloc();
  142.         p->trk = id_to_track(id);
  143.         read(fd, &p->duration, sizeof p->duration);
  144.         read(fd, &p->m, sizeof p->m);
  145.         *pp = p;
  146.         pp = &p->next;
  147.     }
  148.     *pp = 0;
  149. }
  150.  
  151. new()
  152. {
  153.     efreel(master_list);
  154.     master_list = 0;
  155.     efreel(raw_list);
  156.     raw_list = 0;
  157.     free_tracks();
  158. }
  159.  
  160. void
  161. new_command()
  162. {
  163.     new();
  164. }
  165.  
  166.