Music playing program

This is a discussion on Music playing program within the C Programming forums, part of the General Programming Boards category; **** Works now **** Hi I am trying to make this program play a song from a .txt file of ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    6

    Music Playing Program ***problem solved***

    **** 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 ***
    Last edited by corey.heffernan; 04-07-2011 at 07:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  2. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 01:55 AM
  3. Replies: 2
    Last Post: 09-16-2009, 07:00 AM
  4. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 02:42 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21