CS50 Course - Volume -

2.7k views Asked by At

I would like to ask a question regarding a one lab task from CS 50's course - Introduction to CS. I do not like to consult my "homework" as I am trying to do everything on my own, but I would like to be honest - I hit a wall over here. My code compiles well and produces an expected outcome. The aim of this task is to create a short c program, that will open a wmv file, then copy a header to the output file, and then multiply each sample by provided factor. My program deals with all of these tasks - as far as I can test (I have even compared input and output files with Audacity). However, tests indicate that audio is not correctly altered compared to the provided factor and expected outcome. I am providing a prt of source code below. You can just indicate what I should be looking for, as honestly - I have no idea. For me - everything is just fine.

float factor = atof(argv[3]);

uint8_t HEADER [HEADER_SIZE]; //const int HEADER_SIZE = 44;
int16_t buffer;
int16_t* bufferp = &buffer;
fread(HEADER, sizeof(uint8_t), HEADER_SIZE, input);
fwrite(HEADER, sizeof(uint8_t), HEADER_SIZE, output);

while(feof(input) == 0)
{
    fread(&buffer, sizeof(int16_t), 1, input);
    if (*bufferp * factor >= MAX) //const int MAX = 32767; <<Prevents overflow>>
        {
            *bufferp = MAX;
        }
    else
        {
            *bufferp = *bufferp * factor;
        }
    fwrite(&buffer, sizeof(int16_t), 1, output);
}

// Close files
fclose(input);
fclose(output);
return 0;

Thanks for all your help!

EDIT 30.01.2021 : Properly working version no. 1:

while (fread(&buffer, sizeof(int16_t), 1, input) != 0)
{
    if (buffer * factor >= MAX) //This deals with integral overflow.
    {
        buffer = MAX;
    }
    else
    {
        buffer = buffer * factor;
    }
    fwrite(&buffer, sizeof(int16_t), 1, output);
}
0

There are 0 answers