/* Music on PC demonstration program */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#include <bios.h>
#include <io.h>

#include "musiclib.h"


void beep (unsigned freq, unsigned length)
{
   static lastfreq = 0;

   /* nosound (); */

   if (freq == lastfreq)
     {
       nosound ();
       delay (10);
     }

   sound (freq);
   delay (length);
   lastfreq = freq;
}

void playnote (unsigned note, unsigned octave, unsigned length, unsigned staccato)
{
   unsigned freq;  /* frequency of the note to be played            */
   int diff;  /* number of notes between the note to be played */
                   /* and the LA of the 3rd octave                  */

   if (note == 13)
      delay (length);  /* rest period */
         else
            {  /* audible note */
               if (octave > BASEOCTAVE)
                  diff = (OCTAVESIZE - BASENOTENUM) + note + (octave - BASEOCTAVE - 1) * OCTAVESIZE;
                     else
                        if (octave < BASEOCTAVE)
                           diff = - (BASENOTENUM + (OCTAVESIZE - note) + (BASEOCTAVE - octave - 1) * OCTAVESIZE);
                              else
                                 diff = note - BASENOTENUM;   /* note is in the base octave */



               freq = (unsigned) (BASENOTE * pow (K, diff));

               beep (freq, length * (1 - (staccato / 10)));
               delay (length * (staccato / 10));
            }  /* audible note */
}

int playnotefile (char *notefilename)
{
   NOTEREC *currentnote;
   FILE *notefile;

   if (notefile = fopen (notefilename, "rb"))
      {
         while ((! feof (notefile)) && (bioskey (1) == 0))
            {
               fread (currentnote, sizeof (NOTEREC), 1, notefile);

               if (! feof (notefile))  {
                 playnote ((unsigned) (currentnote -> note),
                           (unsigned) (currentnote -> octave),
                           (unsigned) (currentnote -> length),
                           (unsigned) (currentnote -> staccato));

               }
            }

         fclose (notefile);
         return 1;
      }
         else
            return 0;  /* error opening music file */
}

unsigned get_note_freq (unsigned note, unsigned octave)
{
   int diff;
   unsigned freq;

   if (note == 13)
      freq = 0;
         else {
               if (octave > BASEOCTAVE)
                  diff = (OCTAVESIZE - BASENOTENUM) + note + (octave - BASEOCTAVE - 1) * OCTAVESIZE;
                     else
                        if (octave < BASEOCTAVE)
                           diff = - (BASENOTENUM + (OCTAVESIZE - note) + (BASEOCTAVE - octave - 1) * OCTAVESIZE);
                              else
                                 diff = note - BASENOTENUM;   /* note is in the base octave */



               freq = (unsigned) (BASENOTE * pow (K, diff));

            }

   return freq;
}


SOUNDREC *read_note_file (char *fname, SOUNDREC **notebuf, size_t *buf_size)
{
   FILE *notefile;
   NOTEREC current_note;
   unsigned note_count;
   unsigned note_num;

   if (notefile = fopen (fname,"rb"))  {
         note_count = (unsigned) filelength (fileno (notefile)) / sizeof (NOTEREC);

         if (*notebuf = (SOUNDREC *) calloc (note_count, sizeof (SOUNDREC))) {
               fread (&current_note, sizeof (NOTEREC), 1, notefile);
               note_num = 0;

               while (! feof (notefile))  {
                     (*notebuf) [note_num].frequency = get_note_freq (current_note.note, current_note.octave);
                     (*notebuf) [note_num].time = current_note.length * (1 - current_note.staccato / 10);
                     (*notebuf) [note_num].end_delay = current_note.length * (current_note.staccato / 10);

                     note_num++;
                     fread (&current_note, sizeof (NOTEREC), 1, notefile);
                  }
            }


         fclose (notefile);
         *buf_size = note_count;
      }
         else  {
               *buf_size = 0;
               *notebuf = NULL;
            }

  return (*notebuf);
}

