home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TUNE.ZIP / TESTTUNE.C
Text File  |  1993-02-13  |  6KB  |  138 lines

  1. /*************************************************************************
  2.  * Testtune.c  Source to demonstrate how to play the tune files created  *
  3.  * with the OS/2 PMTUNE program.  The tune files are described by the    *
  4.  * note below from IBM.  Each element of the tune is delimited by a pipe *
  5.  * symbol "|" .                                                          *
  6.  *                                                                       *
  7.  * Provided by Bryan Walker, WalkerWerks with the permission of IBM.     *
  8.  *                                                                       *
  9.  * WARNING: This is a simple test file so it does not do error trapping  *
  10.  *          be sure and enter a Tempo on the command line.               *
  11.  *************************************************************************
  12.  
  13.  There are two ways of enabling other programs to "play" the alarm tunes,
  14.  either by "printing" a Tune out (appears in DosBeep 'c' code format) and
  15.  building the code into a program or by coding up a special "play" tune
  16.  routine to operate on the tune file(s) directly.
  17.  
  18.  Tune file consists of the following fields - repeated 36 times:-
  19.  
  20.   1) Tune Title.
  21.   2) Tune Tempo.
  22.   3) Tune Notes ( 20 pairs of numbers representing Note Type and Pitch ).
  23.                  Note Types less than 10 are real notes. Note Types 10 or
  24.                  more are rests.
  25.  
  26.  File read into a series of arrays:-
  27.       Titles,
  28.       Tempo   = SHORT  g_sTuneTempo[ 36 ]
  29.       Notes   = SHORT  g_sTuneNote[ 36 ][ 20 ]
  30.       Pitch   = SHORT  g_sTunePitch[ 36 ][ 20 ]
  31.  
  32.  Hope this is what you wanted,
  33.  
  34.  Jeff Kerr
  35. */
  36.  
  37.  
  38. #define INCL_DOS
  39. #include <os2.h>
  40. main(int argc, char *argv[], char *envp[])
  41. {
  42.  
  43. /* Default Tune */
  44.  
  45. SHORT  g_sTunePitch[ 20 ] = {18,17,16,15,16,15,14,13,14,13,12,11,12,13,12,11,6,6,6,6} ;
  46. SHORT  g_sTuneNote[ 20 ] = {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,10,10,10,10} ;
  47.  
  48. /*Blue Danube*/
  49. /*
  50. SHORT  g_sTunePitch[ 20 ] = {17,17,57,13,13,6,6,6,6,50,50,6,6,6,6,6,6,6,6,6} ;
  51. SHORT  g_sTuneNote[ 20 ] = {4,4,4,4,4,12,4,4,12,4,4,10,10,10,10,10,10,10,10,10} ;
  52. */
  53. SHORT  g_sTuneTempo ;
  54. static  SHORT    ls_sDuration[ 16 ] =
  55.          {
  56.            1024, 768, 512, 384, 256, 192, 128, 96, 64, 32,
  57.            1024, 512, 256, 128, 64,  32
  58.          };
  59.  
  60. static  SHORT    ls_sFrequency[ 63 ] =
  61.          {
  62.            784, 698, 660, 588, 523, 494, 440, 392, 349, 330,
  63.            294, 262, 247, 220, 196, 175, 165, 147, 131, 123,
  64.            110, 740, 660, 622, 554, 494, 466, 415, 370, 330,
  65.            311, 277, 247, 233, 208, 185, 165, 156, 139, 123,
  66.            117, 104, 830, 740, 698, 622, 554, 523, 466, 415,
  67.            370, 349, 311, 277, 262, 233, 208, 185, 175, 156,
  68.            139, 131, 117
  69.          };
  70.  
  71. SHORT      l_sI;
  72. SHORT      l_sJ;
  73. double tempo_multiplier = 1;
  74.  
  75. g_sTuneTempo = atoi(argv[1]) ;
  76.  
  77. if(g_sTuneTempo >= 300)
  78.     g_sTuneTempo = 299 ;
  79. else if (g_sTuneTempo < 1)
  80.     g_sTuneTempo = 1 ;
  81.  
  82. /**********************************************************************
  83.  * ls_sDuration above gives the duration for a tempo of 200.  Also    *
  84.  * The Tune Editor assumes each move of 100 is                        *
  85.  * an equivelant 100% change in the tempo (i.e. 100 is twice as slow  *
  86.  * as 200 and 300 is twice as fast).  To calculate this I altered the *
  87.  * original Tempo formula provided by IBM from the duration of the    *
  88.  * selected note times the tempo divided by 100 to the below formula  *
  89.  * that starts with a baseline 200 assumption.                        *
  90.  * Bryan Walker WalkerWerks                                           *
  91.  **********************************************************************/
  92.  
  93. if(g_sTuneTempo < 200)
  94.    tempo_multiplier += (((200.00 - (double)g_sTuneTempo) / 100.00));
  95. else if (g_sTuneTempo > 200)
  96.    tempo_multiplier -= ((((double)g_sTuneTempo - 200.00)/100.00));
  97.  
  98.  
  99. /************************************************************************/
  100. /* Search backwards along tune looking for last none "rest" type note.  */
  101. /************************************************************************/
  102. for ( l_sI=19; l_sI>0; l_sI-- )
  103.    {
  104.    if ( g_sTuneNote[ l_sI ] < 10 )
  105.      break;
  106.    }
  107.  
  108. /************************************************************************/
  109. /* Loop to "play" tune.                                                 */
  110. /************************************************************************/
  111. for ( l_sJ=0; l_sJ<= l_sI; l_sJ++ )
  112.    {
  113.      /**********************************************************************/
  114.      /* if note type is greater than 9 (ie: it's a rest) then use duration */
  115.      /* with a very high frequency - results in silence.                   */
  116.      /* Otherwise play note as normal.                                     */
  117.      /**********************************************************************/
  118.     if ( g_sTuneNote[ l_sJ ] > 9 )
  119.        {
  120.        /*
  121.          Changed from DosBeep(0x7FFF, duration) to DosSleep because long
  122.          pauses allowed the speaker transition from off to on to interject
  123.          a click.
  124.        */
  125.        DosSleep((USHORT)((double)ls_sDuration[ g_sTuneNote[ l_sJ ] ] * tempo_multiplier));
  126.        }
  127.     else
  128.        {
  129.        DosBeep( ls_sFrequency[ g_sTunePitch[ l_sJ ] ],
  130.           (USHORT)((double)ls_sDuration[ g_sTuneNote[ l_sJ ] ] * tempo_multiplier));
  131.        }
  132.     /**********************************************************************/
  133.     /* Insert a short silence in between notes                            */
  134.     /**********************************************************************/
  135.     DosBeep( 0x7FFF, (USHORT) (8 * tempo_multiplier)) ;
  136.    }
  137. }          
  138.