Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: https://www.instagram.com/ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Why is sizeof(HEADER_SIZE) = 4 when it was initialized as 44? [closed]
Given below is my solution to CS50's Week 4: Volume. The problem I was trying to solve was to read a .wav file and change its volume based on a comandline argument 'factor'.
CS50's Week 4: Volume
Problem:
When I was testing this code I initially used
fread(header, HEADER_SIZE, 1, input);
My code would compile without error and an output file would be produced for the following:
./volume input.wav output.wav 1.0
Problem:
fread(header, HEADER_SIZE, 1, input);
./volume input.wav output.wav 1.0
But a currupted output file would be produced if I changed the volume factor to anything else other than 1.0.
// Modifies the volume of an audio file
#include stdint.h
#include stdio.h
#include stdlib.h
// Number of bytes in .wav header
const int HEADER_SIZE = 44;
void copy_header(FILE *input, FILE *output);
void copy_samples(FILE *input, FILE *output, float factor);
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]);
copy_header(input, output);
copy_samples(input, output, factor);
// Close files
fclose(input);
fclose(output);
}
void copy_header(FILE *input, FILE *output)
{
uint8_t header[HEADER_SIZE];
fread(header, sizeof(HEADER_SIZE), 1, input);
fwrite(header, sizeof(HEADER_SIZE), 1, output);
printf("header: %s; header_size: %lu; HEADER_s: %lu\n", header, sizeof(header), sizeof(HEADER_SIZE));
}
void copy_samples(FILE *input, FILE *output, float factor)
{
int16_t buffer;
while (fread(&buffer, sizeof(int16_t), 1, input) != 0)
{
buffer = buffer * factor;
fwrite(&buffer, sizeof(int16_t), 1, output);
}
}
// Modifies the volume of an audio file
#include stdint.h
#include stdio.h
#include stdlib.h
// Number of bytes in .wav header
const int HEADER_SIZE = 44;
void copy_header(FILE *input, FILE *output);
void copy_samples(FILE *input, FILE *output, float factor);
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]);
copy_header(input, output);
copy_samples(input, output, factor);
// Close files
fclose(input);
fclose(output);
}
void copy_header(FILE *input, FILE *output)
{
uint8_t header[HEADER_SIZE];
fread(header, sizeof(HEADER_SIZE), 1, input);
fwrite(header, sizeof(HEADER_SIZE), 1, output);
printf("header: %s; header_size: %lu; HEADER_s: %lu\n", header, sizeof(header), sizeof(HEADER_SIZE));
}
void copy_samples(FILE *input, FILE *output, float factoSource of the question:
https://stackoverflow.com/questions/79033153
Question and source license information:
https://meta.stackexchange.com/help/licensing
https://stackoverflow.com/
Best YouTube to MP3 Converter
Tube MP3 is the leading converter which allows you to convert YouTube videos to MP3 files with just a few clicks. It supports high quality MP3 up to 320kbps. Enjoy listening to your favorite YouTube songs in offline mode.