home *** CD-ROM | disk | FTP | other *** search
/ Chestnut's Multimedia Mania / MM_MANIA.ISO / midi / cmtcmu / noteoff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-28  |  4.4 KB  |  159 lines

  1. /* noteoff.c -- this module keeps track of pending note offs for adagio */
  2.  
  3. /*****************************************************************************
  4. *        Change Log
  5. *  Date        | Change
  6. *-----------+-----------------------------------------------------------------
  7. * 31-Dec-85 | Created changelog
  8. * 31-Dec-85 | Add c:\ to include directives
  9. *  1-Jan-86 | Declare malloc char * for lint consistency
  10. * 21-Jan-86 | note_offs can now turn off more than one note per call
  11. *****************************************************************************/
  12.  
  13. #include "cext.h"
  14. #include "stdio.h"
  15. #include "ctype.h"
  16. #include "malloc.h"
  17. #include "adagio.h"
  18.  
  19. /* off_type is a structure containing note-off information */
  20.  
  21. typedef struct off_struct {
  22.     long when;
  23.     int voice;
  24.     int pitch;
  25.     struct off_struct *next;
  26. } *off_type;
  27.  
  28. private off_type free_off;        /* free list of off_type structures */
  29. private off_type off_events = NULL;    /* active list */
  30. extern void *malloc();
  31.  
  32. /****************************************************************************
  33. *    Routines declared in this module
  34. ****************************************************************************/
  35.  
  36.     boolean        note_offs();
  37. private off_type    off_alloc();
  38. private void        off_free();
  39.     void        off_init();
  40.     void        off_schedule();
  41.  
  42. /****************************************************************************
  43. *                note_offs
  44. * Inputs:
  45. *    long time: the current time
  46. * Outputs:
  47. *    return true if off list has more notes 
  48. * Effect: turn off notes if it is time 
  49. * Assumes:
  50. * Implementation:
  51. *    Find scheduled note off events in off_events, compare with time
  52. ****************************************************************************/
  53.  
  54. boolean note_offs(time)
  55. long time;
  56. {
  57.     off_type temp;
  58.     while (off_events != NULL && off_events->when <= time) {
  59.     midi_note((off_events->voice) + 1, off_events->pitch, 0);
  60.     temp = off_events;
  61.     off_events = off_events->next;
  62.     off_free(temp);
  63.     }
  64.     return (off_events != NULL);
  65. }
  66.  
  67. /****************************************************************************
  68. *                off_alloc
  69. * Outputs:
  70. *    returns off_type: an allocated note off structure
  71. * Effect:
  72. *    allocates a structure using malloc
  73. ****************************************************************************/
  74.  
  75. private off_type off_alloc()
  76. {
  77.     return (off_type) malloc(sizeof(struct off_struct));
  78. }
  79.  
  80. /****************************************************************************
  81. *                off_free
  82. * Inputs:
  83. *    off_type off: a structure to deallocate
  84. * Effect: 
  85. *    returns off to freelist
  86. ****************************************************************************/
  87.  
  88. private void off_free(off)
  89.     off_type off;
  90. {
  91.     off->next = free_off;
  92.     free_off = off;
  93. }
  94.  
  95. /****************************************************************************
  96. *                off_init
  97. * Effect: initialize this module
  98. * Assumes:
  99. *    only called once, otherwise storage is leaked
  100. ****************************************************************************/
  101.  
  102. void off_init()
  103. {
  104.     int i;
  105.     for (i = 0; i < 50; i++) off_free(off_alloc());
  106. }
  107.  
  108. /****************************************************************************
  109. *                off_schedule
  110. * Inputs:
  111. *    long offtime: time to turn note off
  112. *    int voice: the midi channel
  113. *    int pitch: the pitch
  114. * Effect: 
  115. *    schedules a note to be turned off
  116. * Assumes:
  117. *    note_offs will be called frequently to actually turn off notes
  118. ****************************************************************************/
  119.  
  120. void off_schedule(offtime, voice, pitch)
  121.     long offtime;
  122.     int voice, pitch;
  123. {
  124.     off_type off, ptr, prv;
  125.     /* allocate off */
  126.     if ((off = free_off) == NULL) {
  127.     off = off_alloc();
  128.     } else free_off = off->next;
  129.  
  130.     if (off == NULL) {
  131.     fprintf(stderr, "out of space for note off events");
  132.     musicterm();
  133.     exit(1);
  134.     }
  135.  
  136.     off->when = offtime;
  137.     off->voice = voice;
  138.     off->pitch = pitch;
  139.     /* insert into list of off events */
  140.     ptr = off_events;
  141.     if (ptr == NULL || offtime <= ptr->when) {
  142.     off->next = ptr;
  143.     off_events = off;
  144.     } else {
  145.     while (ptr != NULL && offtime > ptr->when) {
  146.         prv = ptr;
  147.         ptr = ptr->next;
  148.     }
  149.     prv->next = off;
  150.     off->next = ptr;
  151.     }
  152. /*
  153.  *    printf("off_schedule(%ld, %d, %d): \n", offtime, voice, pitch);
  154.  *    for (ptr = off_events; ptr != NULL; ptr = ptr->next) {
  155.  *    printf("    %ld: %d, %d\n", ptr->when, ptr->voice, ptr->pitch);
  156.  *    }
  157.  */
  158. }
  159.