home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Sound Sensations!
/
sound_sensations.iso
/
miscprog
/
mvsrc
/
music.c
< prev
next >
Wrap
Text File
|
1990-12-23
|
3KB
|
138 lines
/* Music on PC demonstration program */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#define BASENOTE 440.00 /* frequency of la in octave 3 */
#define BASEOCTAVE 3
#define K 1.0595 /* k^12=2 -- constant of the geometric sequence of note frequencies */
#define OCTAVESIZE 12 /* number of notes in octave */
#define BASENOTENUM 11 /* number of LA */
typedef struct {
char octave;
char note;
char stacatto;
unsigned length;
} NOTEREC;
/* function prototypes */
displaynotes ();
playnotes ();
displaynote (NOTEREC *currentnote);
playnote (unsigned note, unsigned octave, unsigned length, unsigned stacatto);
beep (unsigned freq, unsigned length);
main (int argc, char *argv [])
{
int i;
if ()
playnotes ();
fcloseall ();
exit (0);
}
displaynotes ()
{
NOTEREC *currentnote;
FILE *notefile;
if (notefile = fopen ("D:\\TC\\PROGRAMS\\NOTES.MUS","rb")) {
while (! feof (notefile))
{
fread (currentnote, sizeof (NOTEREC), 1, notefile);
displaynote (currentnote);
}
fclose (notefile);
}
}
playnotes ()
{
NOTEREC *currentnote;
FILE *notefile;
int notecount;
if (notefile = fopen ("D:\\TC\\PROGRAMS\\NOTES.MUS","rb")) {
notecount = 0;
while ((! feof (notefile)) && (bioskey (1) == 0))
{
fread (currentnote, sizeof (NOTEREC), 1, notefile);
playnote ((unsigned) (currentnote -> note),
(unsigned) (currentnote -> octave),
(unsigned) (currentnote -> length),
(unsigned) (currentnote -> stacatto));
notecount++;
if (notecount == 116)
notecount = notecount;
}
fclose (notefile);
}
}
displaynote (NOTEREC *currentnote)
{
printf ("\nOctave: %d", (int) currentnote -> octave);
printf ("\nNote: %d", (int) currentnote -> note);
printf ("\nStacatto: %d", (int) currentnote -> stacatto);
printf ("\nLength: %d", (int) currentnote -> length);
}
playnote (unsigned note, unsigned octave, unsigned length, unsigned stacatto)
{
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 - stacatto);
delay (stacatto);
} /* audible note */
}
beep (unsigned freq, unsigned length)
{
nosound ();
sound (freq);
delay (length);
nosound ();
}