0
#include "aservelibs/aservelib.h"
#include <stdio.h>
#include <math.h>
#include <string.h>

FILE *textFilePointer;
FILE *textFilePointer2;


typedef struct
{
    int notes;
    int velocity;
}NoteData;


void notetofile (NoteData input, int seqlen, int reclayer);

int main()
{

    int count, count2, note, vel = 0;
    char choice = 'y';
    struct layers;
    int reclayer = 1;
    int playlayer = 1;
    char choice2 = 'y', choice3 = 'y';
    int seqlen = 16;
    NoteData input;




    printf("Welcome To Jonny Maguire's Midi record and playback application. Please Select one of the following options...\n\n");
    aserveSay("Welcome To Jonny Maguires Midi record and playback application. Please Select one of the following options");
    aserveSleep(8000);
    while(choice != 'x')
    {



        printf("r to Record\np to Playback\nx to exit the program\n");
        aserveSay("choose r, to record a sequence, p, to playback your recording. Or select x, at any time to exit the program");

        scanf(" %c", &choice);

        if(choice == 'r')
        {
            while(choice2 != 'n')
            {
                aserveSay("you have chosen to record, enter the no. of notes you wish to record");
                printf("You have chosen to record, enter the no. of notes you wish to record \n\n");
                scanf(" %d", &seqlen);
                printf("Please play %d notes", seqlen);



                    textFilePointer = fopen("recording1.txt", "w");

                    textFilePointer2 = fopen("recording2.txt", "w");



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



                    //Recording 16 note data into txt file

                    notetofile(input, seqlen, reclayer);
                    printf("Would you like to record another layer?");
                    scanf(" %c", &choice2);



                }
                reclayer++;
            }
        }
            else if (choice == 'p')
            {

                while(choice3 != 'n')
                {
                    //If P is selected, playback of the txt file
                    printf("which sequence would like to playback? 1, 2 or both (3)?");
                    scanf(" %d", &playlayer);


                        textFilePointer = fopen("recording1.txt", "r");

                       // textFilePointer2 = fopen("recording2.txt", "r");



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

                    //read until end of file and convert frequency
                    if (playlayer == 1)
                    {
                    while(!feof(textFilePointer))
                    {
                        float frequency;
                        float amplitude = vel/127.0;

                        fscanf(textFilePointer, " %d %d\n", &input.notes, &input.velocity);
                        printf(" %d %d\n\n", input.notes, input.velocity);

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

                        aserveOscillator(0, frequency, amplitude, 0);
                        aserveSleep(500);
                        aserveOscillator(0, 0, 0, 0);
                    }
                    }
                }
                fclose(textFilePointer);
                playlayer++;
            }
        }

    return 0;
}

//function to write notes to file
void notetofile(NoteData input, int seqlen, int reclayer)
{
    for (int count = 1; count <= seqlen;)
    {

        input.notes = aserveGetNote();
        input.velocity = aserveGetVelocity();



        //only note on messages are sent to file
        if(input.velocity > 0)
        {
            printf("reclayer = %d\n", reclayer);
            if(reclayer == 1)
            {
                printf("test");
              fprintf(textFilePointer, " %d %d\n", input.notes, input.velocity);
                fprintf(textFilePointer, "test");
            }
            else if(reclayer == 2)
            {
                fprintf(textFilePointer2, " %d %d\n", input.notes, input.velocity);

            }

            printf("%d %d\n", input.notes, input.velocity);
            count++;
        }
    }

}

This program is meant to read midi notes, write them to a txt file and then read from file into an oscillator. fprintf is not writing to file in the 'notestofile' function. It maybe because I have two textFilePointers open at the same time.

Neo
  • 203
  • 1
  • 11
  • 1
    you forgot to ask a question. What kind of things did you try to find out what goes wrong? – Marcus Müller Mar 22 '15 at 17:39
  • Related? http://stackoverflow.com/questions/29107259/whats-wrong-with-my-function-or-main – R Sahu Mar 22 '15 at 17:44
  • Are you (re-)opening the files in a `while` loop? That's probably not what you want. Also, consider making the files local variables that you pass to the functions. (And there's [`feof`](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong), which isn't relatied to your writing problems, but might be an issue when scanning values.) – M Oehm Mar 22 '15 at 17:48
  • 4
    Your file open/close management is dreadful. You never close the files you open during recording **at all**, and repeatedly open, but close only once, the first file during playback. This program leaks `FILE*` like a sieve leaks water. – WhozCraig Mar 22 '15 at 17:52

1 Answers1

0

When you write to the 2 files recording1.txt and recording2.txt, you don't append so theses files are reset each time you want to write. If you want to append you have to use mode "a" (for append) instead of "w".

And when you are are writing theses files, both file descriptors textFilePointer and textFilePointer2 are never closed and the buffer is not explicitly flushed so you don't see the content in the file while your process is alive. A call to fcloseon theses file descriptors will flushed write buffers like this in your code:

            textFilePointer = fopen("recording1.txt", "w");
            textFilePointer2 = fopen("recording2.txt", "w");

            if(textFilePointer == NULL or textFilePointer2 == NULL)
            {
                printf("Error Opening File!");
            }
            else
            {
                //Recording 16 note data into txt file
                notetofile(input, seqlen, reclayer);
                printf("Would you like to record another layer?");
                scanf(" %c", &choice2);

                //Added fclose calls to opened files:
                fclose( textFilePointer );
                fclose( textFilePointer2 );
                //TODO Check both fclose return values
            }

If you don't close files, it is a file descriptors leak and on most Linux systems the limit of maximum opened files is set to 1024, type ulimit -n to see it.

You can see opened descriptors on a living process to see a leak with the lsof command or by listing /proc/<pid>/fd directory.

Sylvain Bugat
  • 7,704
  • 3
  • 29
  • 30