简体   繁体   中英

C Reading from file into an equation

 else if(choice == 'p')
        {
            textFilePointer = fopen("/Users/jonnymaguire/Documents/Uni Work/Audio Programming /iap/iapProj/Builds/MacOSX/build/Debug/textp15.txt", "r");

            if(textFilePointer == NULL)
            {
                printf("!Error Opening File!");
            }

            while(!feof(textFilePointer))
            {
                float frequency;

                fscanf(textFilePointer, " %d\n", &note);
                printf(" %d\n\n", note);

                frequency = 440 * pow(2, (note-69) /12.0);

                aserveOscillator(0, frequency, 1, 0);
                aserveSleep(500);

            }

This part of my program reads 16 numbers from a txt file, adds them into the 'note' part of the equation to convert them into a pitch, and then sends it to the serveOscillator which plays 16 notes at the set pitch. Aserve sleep dictates how long each note plays for (500ms) but the 16th does not stop ringing out and i need it to stop after 500ms!

Instead of testing feof (see comments above) you should be testing that the file field was read correctly. And you turn off the oscillator with another call.

int note;
float frequency;
while(1 == fscanf(textFilePointer, " %d\n", &note)) {
    printf(" %d\n\n", note);
    frequency = 440 * pow(2, (note-69) /12.0);
    aserveOscillator(0, frequency, 1, 0);
    aserveSleep(500);
}
aserveOscillator(0, 0, 0, 0);       // turn off osc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM