**** Works now ****
Hi I am trying to make this program play a song from a .txt file of music that is written.
It was working up until I modified the program to be able to play different speeds of notes.
Code:
// Include the standard input/output header file and declare all constants
#include<stdio.h>
#include<string.h>
#include<windows.h>
#define FILENAME "music.txt"
#define N 50
int main(void)
{
int k=0, length, speed[N];
// Frequency of notes
double A=440.00, B=493.88, C=523.25, D=587.33, E=659.26, F=698.46, G=783.99;
double a=466.16, c=554.37, d=622.25, f=739.99, g=830.61;
HANDLE hConsole;
char *myString;
FILE *music;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
printf("The program will play music in one octive from A to G.\nTo play a"
" note use the upper case letter. For a sharp use the lower case"
" letter. If you would like a small break use the 'space'.\nYou must "
"create the entire song in a txt file called 'music.txt'\n");
music = fopen("music.txt", "r");
// Checking to make sure the file is there
if(music==NULL)
printf("file not found\n");
else
{
// Scanning the file for the music notes and length of note
while(fscanf(music, "%c%d\n", &myString[k], &speed[k])==2)
{
printf("%c %i\n", myString[k], speed[k]);
k++;
}
// Checking length of string
length = strlen(myString);
// Setting the length of each note into miliseconds
for(k=0;k<length;k++)
{
if(speed[k]==1)
speed[k]=100;
if(speed[k]==2)
speed[k]=200;
if(speed[k]==3)
speed[k]=300;
if(speed[k]==4)
speed[k]=400;
}
// Playing the music
for(k=0;k<length;k++)
{
if(myString[k]=='a')
Beep(a,speed[k]);
if(myString[k]=='c')
Beep(c,speed[k]);
if(myString[k]=='d')
Beep(d,speed[k]);
if(myString[k]=='f')
Beep(f,speed[k]);
if(myString[k]=='g')
Beep(g,speed[k]);
if(myString[k]=='A')
Beep(A,speed[k]);
if(myString[k]=='B')
Beep(B,speed[k]);
if(myString[k]=='C')
Beep(C,speed[k]);
if(myString[k]=='D')
Beep(D,speed[k]);
if(myString[k]=='E')
Beep(E,speed[k]);
if(myString[k]=='F')
Beep(F,speed[k]);
if(myString[k]=='G')
Beep(G,speed[k]);
if(myString[k]==' ')
sleep(speed[k]);
}
// Closing the open file
fclose(music);
}
system("pause");
return (0);
}
and the .txt file that I am using at the moment looks like this
(I have tried putting spaces in between the letter and number with no success)
Code:
P4
E1
E1
E4
P2
E1
E1
E4
P2
E2
G2
C2
D1
E3
P3
F2
F2
F2
F2
F2
E2
E2
E1
E1
E1
P2
D2
D2
E2
D2
G4
maybe this post is getting lengthy but when the program worked, in the "beep" statement instead of "speed[k]" it was set at 200 mili seconds. and in the txt file there where no numbers. The tune should sound something like jingle bells
Any help would be greatly appreciated.
Thanks
*** I figured it out... needed to change my space char to a letter for pause... and a \n in my fscanf ***