Hey Kashif!
I’m not sure how to do this properly, but after writing the code I was working on, here is the output from the terminal:
Terminal output.png (5.1 KB)
Here’s a text version as well:
RIFF: RIFF
File size: 352836
WAVE: WAVE
fmt: fmt
previous Bytes size: 16
Type of format: 1 PCM
Format Channel: 2
Sample rate: 44100
Sample rate math: 176400
Mono/Stereo: 4
Bits per sample: 16
/////DATA///// 1635017060
File Size Data: 352800
I think it worked fine? It’s matching the table quite well
I had to set the sample rate to be a 32-bit integer instead of 32-byte integer, so I think it was a typo after all? (Unless I’m just lost)
Please note that this is my first time ever trying something like this. I believe my code isn’t the best (it feels unnecessarily long), so I’m sorry if it’s confusing to read. Here’s the code I wrote, in case it helps you with anything:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef uint8_t BYTE;
typedef int16_t BYTE_2;
int main(int argc, char *argv[])
{
// Check command line arguments
if (argc != 4)
{
printf("Usage: ./alone Input_file Output_file Factor(Number)\n");
return 1;
}
// Open files
FILE *input = fopen(argv[1], "r");
if (input == NULL)
{
printf("Can't open file\n");
return 2;
}
FILE *output = fopen(argv[2], "w");
if (output == NULL)
{
printf("Can't create a file \n");
return 3;
}
// Copy and output RIFF
BYTE RIFF[4];
fread(&RIFF, sizeof(RIFF), 1, input);
fwrite(&RIFF, sizeof(RIFF), 1, input);
printf("RIFF: ");
for (int i = 0; i < 4; i++)
{
printf("%c", RIFF[i]);
}
printf("\n");
// File size
int file_size;
fread(&file_size, sizeof(file_size), 1, input);
fwrite(&file_size, sizeof(file_size), 1, input);
printf("File size: %i\n", file_size);
// Modify the rest of the file
float factor = atof(argv[3]);
// "WAVE"
BYTE WAVE[4];
fread(&WAVE, sizeof(WAVE), 1, input);
fwrite(&WAVE, sizeof(WAVE), 1, input);
printf("WAVE: ");
for (int i = 0; i < 4; i++)
{
printf("%c", WAVE[i]);
}
printf("\n");
// "fmt"
BYTE fmt[4];
fread(&fmt, sizeof(fmt), 1, input);
fwrite(&fmt, sizeof(fmt), 1, input);
printf("fmt: %s\n", &fmt[0]);
// Length of previous
int previous;
fread(&previous, sizeof(previous), 1, input);
fwrite(&previous, sizeof(previous), 1, input);
printf("previous Bytes size: %i\n", previous);
// Format type
short format;
fread(&format, sizeof(format), 1, input);
fwrite(&format, sizeof(format), 1, input);
printf("Type of format: %i", format);
if (format == 1)
printf(" PCM\n");
else
printf("\n");
// Format Channel
fread(&format, sizeof(format), 1, input);
fwrite(&format, sizeof(format), 1, input);
printf("Format Channel: %i\n", format);
// Sample Rate
int Sample_Rate;
fread(&Sample_Rate, sizeof(Sample_Rate), 1, input);
fwrite(&Sample_Rate, sizeof(Sample_Rate), 1, input);
printf("Sample rate: %i\n", Sample_Rate);
// Sample Rate Math:
fread(&Sample_Rate, sizeof(Sample_Rate), 1, input);
fwrite(&Sample_Rate, sizeof(Sample_Rate), 1, input);
printf("Sample rate math: %i\n", Sample_Rate);
// Mono/Stereo
short M_S;
fread(&M_S, sizeof(M_S), 1, input);
fwrite(&M_S, sizeof(M_S), 1, input);
printf("Mono/Stereo: %i\n", M_S);
// Bits per sample
fread(&M_S, sizeof(M_S), 1, input);
fwrite(&M_S, sizeof(M_S), 1, input);
printf("Bits per sample: %i\n", M_S);
// Data
fread(&Sample_Rate, sizeof(Sample_Rate), 1, input);
fwrite(&Sample_Rate, sizeof(Sample_Rate), 1, input);
printf("/////DATA///// %i\n", Sample_Rate);
// File size Data
fread(&Sample_Rate, sizeof(Sample_Rate), 1, input);
fwrite(&Sample_Rate, sizeof(Sample_Rate), 1, input);
printf("File Size Data: %i\n", Sample_Rate);
// Write the new Wav file
BYTE_2 buffer;
while(fread(&buffer, sizeof(buffer), 1, input))
{
buffer *= factor;
fwrite(&buffer, sizeof(buffer), 1, output);
}
// Close files
fclose(input);
fclose(output);
}
It’s supposed to print the header file in the terminal, then change the volume of the WAV file by multiplying it with the variable “factor” (I was just experimenting to learn stuff).
Thanks!