I dusted off this little code snippet from the good old DOS days. It will answer some of your questions on how to calculate the musical scales. If you need any more details, get friendly with a musician!
/*********************** play.c ***********************
**
** Experimentation with sound:
** play(int octave,int note,int duration)
** Written in Turbo C V.2.0 by vegaseat 6/15/88
**
*********************************************************/
/************** note.h ***********/
#define P 0 /* pause */
#define C 1
#define CS 2 /* C sharp */
#define D 3
#define DS 4
#define E 5
#define F 6
#define FS 7
#define G 8
#define GS 9
#define A 10
#define AS 11
#define B 12
#define EN 75 /* eighth note */
#define QN 150 /* quarter note */
#define HN 300 /* half note */
#define FN 600 /* full note */
/************************************/
void play(int octave,int note,int duration);
void british(void); /* Westminster Bells */
void main(void) /* test play() */
{
british();
getch();
}
void play(int octave,int note,int duration)
/* play note (C=1 to B=12), in octave (1-8), and duration (msec)
include NOTE.H for note values */
{
int k;
double frequency;
if (note == 0) { /* pause */
delay(duration);
return;
}
frequency = 32.625;
for (k = 0; k < octave; k++) /* compute C in octave */
frequency *= 2;
for (k = 0; k < note; k++) /* frequency of note */
frequency *= 1.059463094; /* twelve root of 2 */
delay(5); /* delay between keys */
sound((int) frequency); /* sound the note */
delay(duration); /* for correct duration */
nosound();
}
void british(void) /* Westminster Bells, sort of */
{
play(4,E,HN);
play(4,C,HN);
play(4,D,HN);
play(3,G,HN+QN); play(3,P,QN);
play(3,G,HN);
play(4,D,HN);
play(4,E,HN);
play(4,C,HN+QN);
}
/*
For XP/NT that don't allow port outputs there is a WIN32 API call:
The Beep function generates simple tones on the speaker. The function
is synchronous; it does not return control to its caller until the
sound finishes.
BOOL Beep(
DWORD dwFreq, // sound frequency, in hertz
DWORD dwDuration // sound duration, in milliseconds
);
*/ vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416