home *** CD-ROM | disk | FTP | other *** search
/ Da Capo / da_capo_vol1.bin / programs / amiga / midi / midi_playground / sourcecode / iofunctions.c < prev    next >
C/C++ Source or Header  |  1993-01-23  |  3KB  |  162 lines

  1. /**************************************************************************
  2. * iofunctions.c:
  3. *        Functions for input/output in a variety of formats.    
  4. *        Part of MP, the MIDI Playground.
  5. *
  6. * Author:    Daniel Barrett
  7. * Version:    See the file "version.h".
  8. * Copyright:    None!  This program is in the Public Domain.
  9. *        Please share it with others.
  10. ***************************************************************************/
  11.  
  12.     
  13. #include "mp.h"
  14. #include "midi.h"
  15.  
  16.  
  17. /* Read data from MIDI.  Return the value of the next MIDI byte.  Because
  18.  * reading byte-by-byte from the MIDI port is inefficient, we read as
  19.  * much as we can and keep it in a static buffer. */
  20.     
  21. MIDI_RESULT FromMidi(FILE *in, MIDI_VALUE *value)
  22. {
  23.     static long bytesWaiting = 0L;    /* # bytes waiting at serial port. */
  24.     static UBYTE buf[BUFSIZ];    /* Static buffer. */
  25.     static long currentByte = 0L;    /* Index of next byte to return. */
  26.  
  27.     if (currentByte >= bytesWaiting)
  28.     {
  29.         bytesWaiting = FastSerialRead(buf);
  30.         currentByte = 0L;
  31.     }
  32.  
  33.     if (bytesWaiting == CTRL_C_NO_BYTES)
  34.     {
  35.         bytesWaiting = currentByte = 0L;
  36.         return(RESULT_STOP);
  37.     }
  38.     else if (bytesWaiting <= 0)
  39.     {
  40.         bytesWaiting = currentByte = 0L;
  41.         return(RESULT_ERROR);
  42.     }
  43.     else
  44.     {
  45.         *value = buf[currentByte++];
  46.         return(RESULT_OK);
  47.     }
  48. }
  49.  
  50.  
  51. /* Write a byte to MIDI.  It is inefficient to write 1 byte at a time, but
  52.  * we want instant transmission.  I will rewrite this eventually. */
  53.     
  54. BOOL ToMidi(FILE *dummy, MIDI_VALUE value)
  55. {
  56.     PrepareToWriteMidi(&value, 1);
  57.     return(DoTheIO() == 1L);
  58. }
  59.  
  60. /* Read 1 byte from text.  We use a finite state automaton.
  61.  * If an error occurs, assign the value of the last character read
  62.  * to *answer. */
  63.     
  64. MIDI_RESULT FromText(FILE *in, MIDI_VALUE *value)
  65. {
  66.     STATE_T state = STATE_NORMAL;
  67.     int c;
  68.     
  69.     *value = 0L;
  70.  
  71.     while ((state != STATE_SUCCESS) && (state != STATE_ERROR)
  72.     &&     (state != STATE_OVERFLOW)
  73.     &&     ((c = getc(in)) != EOF))
  74.  
  75.         state = NewState(c, state, value);
  76.  
  77.     if (state == STATE_SUCCESS)
  78.         return(RESULT_OK);
  79.     else if (c == EOF && state == STATE_NORMAL)
  80.         return(RESULT_STOP);
  81.     else if (c == EOF)
  82.         return(RESULT_ERROR);
  83.     else if (state == STATE_OVERFLOW)
  84.         return(RESULT_OVERFLOW);
  85.     else
  86.     {
  87.         *value = c;
  88.         return(RESULT_ERROR);
  89.     }
  90. }
  91.  
  92.  
  93. /* Write 1 MIDI value as text. */
  94.     
  95. BOOL ToText(FILE *out, MIDI_VALUE value)
  96. {
  97.     static long byt = 0L;
  98.  
  99.     fprintf(out, "<%6ld>   ", ++byt);
  100.     PrintNumber(value, out);
  101. }
  102.  
  103.  
  104. /* Read 1 MIDI value from a binary file.  Inefficient. */
  105.     
  106. MIDI_RESULT FromBinary(FILE *in, MIDI_VALUE *value)
  107. {
  108.     int c;
  109.     if ((c = getc(in)) != EOF)
  110.     {
  111.             *value = (MIDI_VALUE)c;
  112.         return(RESULT_OK);
  113.     }
  114.  
  115.     return(RESULT_STOP);
  116. }
  117.  
  118.  
  119. /* Write 1 MIDI value to a binary file.  Inefficient. */
  120.     
  121. BOOL ToBinary(FILE *out, MIDI_VALUE value)
  122. {
  123.     return(putc(value, out) != EOF);
  124. }
  125.  
  126.     
  127. /****************************************************************************
  128. * Skipping input.
  129. ****************************************************************************/
  130.  
  131.     
  132. /* Skip to the next valid text input. */
  133.  
  134. void SkipText(FILE *in, MIDI_VALUE value)
  135. {
  136.     register int c;
  137.  
  138.     if (isspace(value))
  139.         return;
  140.  
  141.     while ((c = getc(in)) != EOF && !isspace(c))
  142.         ;
  143.     if (c != EOF)
  144.         ungetc(c, in);
  145. }
  146.  
  147.  
  148. /* Skip to the next valid MIDI input by clearing the serial port buffer. */
  149.     
  150. void SkipMidi(FILE *dummy, MIDI_VALUE junk)
  151. {
  152.     ResetSerialPort();
  153. }
  154.  
  155.  
  156. /* Skip to the next valid binary value.  This is currently just a stub. */
  157.     
  158. void SkipBinary(FILE *in, MIDI_VALUE junk)
  159. {
  160.     return;
  161. }
  162.