home *** CD-ROM | disk | FTP | other *** search
/ Audio 4.94 - Over 11,000 Files / audio-11000.iso / amiga / midi / obrst103.lha / OberSuite-1.03 / SourceCode / tomidi.c < prev    next >
C/C++ Source or Header  |  1993-01-23  |  3KB  |  120 lines

  1. /**************************************************************************
  2. * tomidi.c:    Functions for sending patch data to MIDI.
  3. *        A part of OberSuite for the Commodore Amiga.
  4. *
  5. * Author:    Daniel Barrett, barrett@cs.umass.edu.
  6. * Version:    1.0.
  7. * Copyright:    None!  This program is in the Public Domain.
  8. *        Please share it with others.
  9. ***************************************************************************/
  10.  
  11. #include "decl.h"
  12.  
  13.  
  14. /*
  15.  * Send the patch data from the PATCHINFO structure to MIDI.
  16.  * Return TRUE on success (else FALSE).
  17.  */
  18.  
  19. BOOL PutPatchToMidi(PATCHINFO *pi)
  20. {
  21.     if (!FigureThingsOutFromHeader(pi))
  22.         return(FALSE);
  23.     else if (!VerifyAllPatches(pi))
  24.         return(FALSE);
  25.  
  26.     AdjustPatchNumber(pi);
  27.     return(RepeatedlySend(pi));
  28. }
  29.  
  30.  
  31. /*
  32.  * Keep trying to send the data from the PATCHINFO structure to MIDI.
  33.  * Try a maximum of NUM_RETRIES times.
  34.  * Return TRUE if successful (else FALSE).
  35.  */
  36.  
  37. BOOL RepeatedlySend(PATCHINFO *pi)
  38. {
  39.     int numTries = 0;    /* How many retries so far? */
  40.     int patchesSent = 0;    /* How many patches sent successfully? */
  41.     long offset = 0L;    /* Where does the data start? */
  42.  
  43.     ResetSerialPort();
  44.  
  45.     while ((patchesSent < pi->numPatches) && (numTries < NUM_RETRIES))
  46.     {
  47.  
  48.    /* Attempt to send the patch data. */
  49.  
  50.         PutToMidi(pi, offset);
  51.  
  52.    /* If the user pressed ^C, goodbye! */
  53.  
  54.         if (pi->actualSize == CTRL_C_NO_BYTES)
  55.         {
  56.             ErrorMsg(ERROR_CTRLC);
  57.             return(FALSE);
  58.         }
  59.  
  60.    /*
  61.     * If the correct amount of data was sent, get ready to send the next
  62.     * patch.
  63.     */
  64.         else if (pi->actualSize == pi->rightSize)
  65.         {
  66.             patchesSent++;
  67.             if (OUTPUT_ALLOWED)
  68.                 PrintPatchInfo(pi, offset);
  69.             numTries = 0;
  70.             offset += pi->rightSize;
  71.         }
  72.  
  73.    /*
  74.     * The data was not sent successfully.  If we haven't already retried
  75.     * too many times, try again.
  76.     */
  77.         else if (++numTries < NUM_RETRIES)
  78.         {
  79.             if (ERR_OUTPUT_ALLOWED)
  80.                 fprintf(stderr, "  <%d> Retry #%d...\n", 
  81.                     patchesSent, numTries);
  82.  
  83.             ResetSerialPort();
  84.         }
  85.     }
  86.  
  87.    /* Did we send the data in fewer than NUM_RETRIES attempts? */
  88.  
  89.     return((BOOL)(numTries < NUM_RETRIES));
  90. }
  91.  
  92.  
  93. /*
  94.  * Send the patch data found at address pi->data + offset.
  95.  * Save the number of bytes sent in pi->actualSize.
  96.  */
  97.  
  98. void PutToMidi(PATCHINFO *pi, long offset)
  99. {
  100.     PrepareToWriteMidi(pi->data + offset, pi->rightSize);
  101.     pi->actualSize = DoTheIO();
  102. }
  103.  
  104.  
  105. /*
  106.  * If the user supplied a patch number, use it.
  107.  * Otherwise, use the patch number found at pi->data[BYTE_PATCHNUMBER].
  108.  */
  109.  
  110. void AdjustPatchNumber(PATCHINFO *pi)
  111. {
  112.     if (pi->numPatches == 1)
  113.     {
  114.         if (pi->patchNum != DEFAULT_PATCH_NUM)
  115.             pi->data[BYTE_PATCHNUMBER] = pi->patchNum;
  116.         else 
  117.             pi->patchNum = pi->data[BYTE_PATCHNUMBER];
  118.     }
  119. }
  120.