YouTube MP3 Converter

YouTube Video to MP3 Converter

Thumbnail for Why is sizeof(HEADER_SIZE) = 4 when it was initialized as 44? [closed]

Why is sizeof(HEADER_SIZE) = 4 when it was initialized as 44? [closed]

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/

🎵

Unlimited Downloads

Without the download limit, we are the best! Convert YouTube videos anytime, anywhere, no matter where you are. Your favorite videos will be ready for download in just a few seconds

⚡️

Convert YouTube to MP4

Convert and download YouTube videos with Tube MP3 fast, easy and secure! Get free music with our YouTube Converter.

📱

Most platforms supported

Most platforms supported including Windows, Android, iPhone and Mac.

🛡️

Various Quality Selections

Tube MP3 supports 5 different quality, including 64, 128, 192, 256 and 320kbps. You can select any of them, the most downloaded quality is 128kbps. The default quality of 128kbps is recommended because it sounds great and does not take so much space.

🔄

No Registration Required

It's not necessary to create an account to convert YouTube videos to MP3 format. Our Online Converter is free for anyone, without any restrictions.

🆓

High Compatibility

Tube MP3 is optimized for all browsers such as Chrome, Microsoft Edge, Firefox, Safari and Opera.

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.

YouTube converter, YouTube downloader, YouTube to mp3, YouTube mp3, YouTube to mp4, YouTube video converter, convert YouTube, download YouTube, YT1s, YouTube to MP3, Youtube Downloader, MP3, MP4, 3GP, 320kbps, snappea, Convert YouTube to MP3 Free, Tube Mp3, Youtube Conversor, Converter Mp3, y2meta