home *** CD-ROM | disk | FTP | other *** search
/ Sound Sensations! / sound_sensations.iso / miscprog / mvsrc / musiclib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-30  |  4.1 KB  |  146 lines

  1. /* Music on PC demonstration program */
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <dos.h>
  7. #include <bios.h>
  8. #include <io.h>
  9.  
  10. #include "musiclib.h"
  11.  
  12.  
  13. void beep (unsigned freq, unsigned length)
  14. {
  15.    static lastfreq = 0;
  16.  
  17.    /* nosound (); */
  18.  
  19.    if (freq == lastfreq)
  20.      {
  21.        nosound ();
  22.        delay (10);
  23.      }
  24.  
  25.    sound (freq);
  26.    delay (length);
  27.    lastfreq = freq;
  28. }
  29.  
  30. void playnote (unsigned note, unsigned octave, unsigned length, unsigned staccato)
  31. {
  32.    unsigned freq;  /* frequency of the note to be played            */
  33.    int diff;  /* number of notes between the note to be played */
  34.                    /* and the LA of the 3rd octave                  */
  35.  
  36.    if (note == 13)
  37.       delay (length);  /* rest period */
  38.          else
  39.             {  /* audible note */
  40.                if (octave > BASEOCTAVE)
  41.                   diff = (OCTAVESIZE - BASENOTENUM) + note + (octave - BASEOCTAVE - 1) * OCTAVESIZE;
  42.                      else
  43.                         if (octave < BASEOCTAVE)
  44.                            diff = - (BASENOTENUM + (OCTAVESIZE - note) + (BASEOCTAVE - octave - 1) * OCTAVESIZE);
  45.                               else
  46.                                  diff = note - BASENOTENUM;   /* note is in the base octave */
  47.  
  48.  
  49.  
  50.                freq = (unsigned) (BASENOTE * pow (K, diff));
  51.  
  52.                beep (freq, length * (1 - (staccato / 10)));
  53.                delay (length * (staccato / 10));
  54.             }  /* audible note */
  55. }
  56.  
  57. int playnotefile (char *notefilename)
  58. {
  59.    NOTEREC *currentnote;
  60.    FILE *notefile;
  61.  
  62.    if (notefile = fopen (notefilename, "rb"))
  63.       {
  64.          while ((! feof (notefile)) && (bioskey (1) == 0))
  65.             {
  66.                fread (currentnote, sizeof (NOTEREC), 1, notefile);
  67.  
  68.                if (! feof (notefile))  {
  69.                  playnote ((unsigned) (currentnote -> note),
  70.                            (unsigned) (currentnote -> octave),
  71.                            (unsigned) (currentnote -> length),
  72.                            (unsigned) (currentnote -> staccato));
  73.  
  74.                }
  75.             }
  76.  
  77.          fclose (notefile);
  78.          return 1;
  79.       }
  80.          else
  81.             return 0;  /* error opening music file */
  82. }
  83.  
  84. unsigned get_note_freq (unsigned note, unsigned octave)
  85. {
  86.    int diff;
  87.    unsigned freq;
  88.  
  89.    if (note == 13)
  90.       freq = 0;
  91.          else {
  92.                if (octave > BASEOCTAVE)
  93.                   diff = (OCTAVESIZE - BASENOTENUM) + note + (octave - BASEOCTAVE - 1) * OCTAVESIZE;
  94.                      else
  95.                         if (octave < BASEOCTAVE)
  96.                            diff = - (BASENOTENUM + (OCTAVESIZE - note) + (BASEOCTAVE - octave - 1) * OCTAVESIZE);
  97.                               else
  98.                                  diff = note - BASENOTENUM;   /* note is in the base octave */
  99.  
  100.  
  101.  
  102.                freq = (unsigned) (BASENOTE * pow (K, diff));
  103.  
  104.             }
  105.  
  106.    return freq;
  107. }
  108.  
  109.  
  110. SOUNDREC *read_note_file (char *fname, SOUNDREC **notebuf, size_t *buf_size)
  111. {
  112.    FILE *notefile;
  113.    NOTEREC current_note;
  114.    unsigned note_count;
  115.    unsigned note_num;
  116.  
  117.    if (notefile = fopen (fname,"rb"))  {
  118.          note_count = (unsigned) filelength (fileno (notefile)) / sizeof (NOTEREC);
  119.  
  120.          if (*notebuf = (SOUNDREC *) calloc (note_count, sizeof (SOUNDREC))) {
  121.                fread (¤t_note, sizeof (NOTEREC), 1, notefile);
  122.                note_num = 0;
  123.  
  124.                while (! feof (notefile))  {
  125.                      (*notebuf) [note_num].frequency = get_note_freq (current_note.note, current_note.octave);
  126.                      (*notebuf) [note_num].time = current_note.length * (1 - current_note.staccato / 10);
  127.                      (*notebuf) [note_num].end_delay = current_note.length * (current_note.staccato / 10);
  128.  
  129.                      note_num++;
  130.                      fread (¤t_note, sizeof (NOTEREC), 1, notefile);
  131.                   }
  132.             }
  133.  
  134.  
  135.          fclose (notefile);
  136.          *buf_size = note_count;
  137.       }
  138.          else  {
  139.                *buf_size = 0;
  140.                *notebuf = NULL;
  141.             }
  142.  
  143.   return (*notebuf);
  144. }
  145.  
  146.