home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / TUNEPL.ZIP / tuneplyr.c < prev    next >
C/C++ Source or Header  |  1992-11-12  |  6KB  |  219 lines

  1. /************************************************************************
  2.  
  3.       Module Name........: $modname$
  4.       Module Description.: Player for 'Tune Editor' files
  5.       Module Date........: $date$
  6.       Written By.........: Steve Horne / CoralSoft, Inc.
  7.                            305 Judson Dr. 
  8.                            Mobile, AL  36608
  9.                            (205) 344-2251
  10.       Version............: $version$
  11.       Release............: $release$
  12.  
  13. -------------------------------------------------------------------------
  14.  
  15.    Description
  16.    -----------
  17.    $notes$
  18.    This VIO application will play a tune from a PMDiary
  19.    data file.  These files are created with the 'Tune Editor' mini-app
  20.    located in the 'Productivity' folder.  The files created by the
  21.    Tune Editor have an extension of '$$a'.  The full path/filename must
  22.    be supplied to these routines.
  23.    $notesend$
  24.  
  25.    $log$
  26.  
  27. -------------------------------------------------------------------------
  28.  
  29.    The C and C++ structures and code in this document have been created
  30.    by CoralSoft, Inc. and are considered proprietary and confidential.
  31.    This information may not be distributed by any means, electronic or
  32.    mechanical, without the prior written consent of CoralSoft, Inc.
  33.    No warranties are either EXPRESSED or IMPLIED.
  34.    Copyright (c) 1992, CoralSoft, Inc.
  35.  
  36. ************************************************************************/
  37. #define  INCL_BASE
  38. #define  INCL_NOPM
  39. #include <string.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <errno.h>
  43. #include <ctype.h>
  44. #include <os2.h>
  45. #include "tuneplyr.h"
  46.  
  47. ULONG tuneFreqs[63] = {
  48. /* Naturals */
  49.    784L, 698L, 659L, 587L, 523L, 494L, 440L, 392L, 349L, 330L, 294L, 262L,
  50.       247L, 220L, 196L, 175L, 165L, 147L, 131L, 123L, 110L,
  51. /* Flats */
  52.    740L, 659L, 622L, 554L, 494L, 466L, 415L, 370L, 330L, 311L, 277L, 247L,
  53.       233L, 208L, 185L, 165L, 156L, 139L, 123L, 117L, 104L,
  54. /* Sharps */
  55.    831L, 740L, 698L, 622L, 554L, 523L, 466L, 415L, 370L, 349L, 311L, 277L,
  56.       262L, 233L, 208L, 185L, 175L, 156L, 139L, 131L, 117L
  57.    };
  58.  
  59. const double durMap[] =
  60.    {  0.25, 0.33333, 0.5, 0.66667, 1.0, 1.33333, 2.0, 2.66667, 4.0, 8.0,
  61.       0.25, 0.5, 1.0, 2.0, 4.0, 8.0 };
  62.  
  63. typedef struct _TuneTitleLine {
  64.    BYTE tuneNo;
  65.    BYTE colonSpace[2];
  66.    BYTE tuneName[25];
  67.    BYTE vertBar;
  68.    BYTE filler[10];
  69.    } TuneTitleLine;
  70.  
  71. typedef struct _TuneTempoLine {
  72.    BYTE tempo[4];
  73.    BYTE vertBar;
  74.    BYTE filler[34];
  75.    } TuneTempoLine;
  76.  
  77. typedef struct _TuneNoteLine {
  78.    BYTE barDivisor[2];
  79.    BYTE aspace;
  80.    BYTE noteIdx[2];
  81.    BYTE vertBar;
  82.    BYTE filler[33];
  83.    } TuneNoteLine;
  84.  
  85. typedef union _TuneInBuff {
  86.    BYTE buffer[39];
  87.    TuneTitleLine title;
  88.    TuneTempoLine tempo;
  89.    TuneNoteLine note;
  90.    } TuneInBuff;
  91.  
  92. int readTune(FILE *inFile, BYTE tuneNo, PTUNENOTE *base, PBYTE tuneName);
  93.  
  94. int createTune(PSZ tuneFile, BYTE tuneNo, PTUNENOTE *tuneBase, PBYTE tuneName)
  95. {
  96. int rc;
  97. FILE *inFile;
  98.  
  99.    inFile= fopen(tuneFile, "r");
  100.    if (inFile == NULL)
  101.       return errno;
  102.    tuneNo= toupper(tuneNo);
  103.    rc= readTune(inFile, tuneNo, tuneBase, tuneName);
  104.    fclose(inFile);
  105.    if (rc)
  106.       return rc;
  107.    return 0;
  108.  
  109. }
  110.  
  111. int readTune(FILE *inFile, BYTE tuneNo, PTUNENOTE *base, PBYTE tuneName)
  112. {
  113. TuneInBuff inBuff;
  114. BOOL foundTune;
  115. int divisor, noteIdx;
  116. double length;
  117. PTUNENOTE nextNote, lastNote;
  118. USHORT tempo;
  119.  
  120.    *base= nextNote= lastNote= NULL;
  121.    while (!feof(inFile))
  122.       {
  123.       foundTune= FALSE;
  124.       while (!foundTune)
  125.          {
  126.          memset(inBuff.buffer, 0, sizeof(inBuff.buffer));
  127.          if (fgets(inBuff.buffer, sizeof(inBuff.buffer), inFile) == NULL)
  128.             return ferror(inFile);
  129.          if (inBuff.title.vertBar == '|')
  130.             if (inBuff.title.tuneNo == tuneNo)
  131.                {
  132.                foundTune= TRUE;
  133.                if (tuneName)
  134.                   memcpy(tuneName, inBuff.title.tuneName, 25);
  135.                }
  136.          }
  137.       memset(inBuff.buffer, 0, sizeof(inBuff.buffer));
  138.       if (fgets(inBuff.buffer, sizeof(inBuff.buffer), inFile) == NULL)
  139.          return -1;
  140.       tempo= atoi(inBuff.tempo.tempo);
  141.       while (TRUE)
  142.          {
  143.          memset(inBuff.buffer, 0, sizeof(inBuff.buffer));
  144.          if (fgets(inBuff.buffer, sizeof(inBuff.buffer), inFile) == NULL)
  145.             break;
  146.          if (inBuff.note.vertBar != '|')
  147.             break;
  148.          if (nextNote)
  149.             {
  150.             nextNote->next= (PTUNENOTE) malloc(sizeof(TUNENOTE));
  151.             nextNote= nextNote->next;
  152.             }
  153.          else
  154.             {
  155.             nextNote= (PTUNENOTE) malloc(sizeof(TUNENOTE));
  156.             *base= nextNote;
  157.             }
  158.          divisor= atoi(inBuff.note.barDivisor);
  159.          noteIdx= atoi(inBuff.note.noteIdx);
  160.          length= tempo;
  161.          length= 60000.0 / length;
  162.          length/= durMap[divisor];
  163.          nextNote->freq= tuneFreqs[noteIdx];
  164.          nextNote->dur= length;
  165.          if (divisor > 9)
  166.             nextNote->rest= TRUE;
  167.          else
  168.             {
  169.             nextNote->rest= FALSE;
  170.             lastNote= nextNote;
  171.             }
  172.          nextNote->next= NULL;
  173.          }
  174.       if (lastNote)
  175.          {
  176.          if (lastNote->next)
  177.             {
  178.             closeTune(lastNote->next);
  179.             lastNote->next= NULL;
  180.             }
  181.          }
  182.       else
  183.          {
  184.          closeTune(*base);
  185.          return -2;
  186.          }
  187.       return 0;
  188.       }
  189.    return -3;
  190. }
  191.  
  192. void closeTune(PTUNENOTE base)
  193. {
  194.    if (base->next)
  195.       closeTune(base->next);
  196.    free(base);
  197.    return;
  198. }
  199.  
  200. void playTune(PTUNENOTE base)
  201. {
  202. PTUNENOTE nextNote;
  203.  
  204.    if (!base)
  205.       return;
  206.  
  207.    nextNote= base;
  208.    while (nextNote)
  209.       {
  210.       if (nextNote->rest)
  211.          DosSleep(nextNote->dur);
  212.       else
  213.          DosBeep(nextNote->freq, nextNote->dur);
  214.       nextNote= nextNote->next;
  215.       }
  216.    return;
  217. }
  218.  
  219.