0

Source file:


#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

// Number of bytes in .wav header
const int HEADER_SIZE = 44;

int main(int argc, char *argv[])
{
    // Check command-line arguments
    if (argc != 4)
    {
        printf("Usage: ./volume input.wav output.wav factor\n");
        return 1;
    }

    // Open files and determine scaling factor
    FILE *input = fopen(argv[1], "r");
    if (input == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }

    FILE *output = fopen(argv[2], "w");
    if (output == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }

    float factor = atof(argv[3]);

    // TODO: Copy header from input file to output file

    uint8_t header[HEADER_SIZE];
    fread(header, HEADER_SIZE, 1, input);
    fwrite(header, HEADER_SIZE, 1, output);
    int16_t buffer;
    while (fread(&buffer, sizeof(int16_t), 2, input))
    {
        buffer = buffer * factor;
        fwrite(&buffer, sizeof(int16_t), 2, output);
    }


    // Close files
    fclose(input);
    fclose(output);
}

The file clearly is altered when I plug

$ ./volume input.wav output.wav 2.0

but my check50 returns affirming none of the files are being correctly being altered, even though it's clear for me that they are actually being modified when the program runs.

My problem is, if buffer is being wrongly altered by the factor, how do I fix this?

2 Answers 2

1

According to the spec:

Your program should then read the rest of the data from the WAV file, one 16-bit (2-byte) sample at a time.

This program is reading (and writing!) two 2-byte samples at a time. Perhaps review the fread doc again, with a concentration on the nmemb argument.

0

your fread is reading 32 bits at a time

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .