Skip to content

Instantly share code, notes, and snippets.

@MarsTechHAN
Created November 29, 2024 11:12
Show Gist options
  • Save MarsTechHAN/d6e51d39c3a0bbc00acea41e24d4d4f1 to your computer and use it in GitHub Desktop.
Save MarsTechHAN/d6e51d39c3a0bbc00acea41e24d4d4f1 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#pragma pack(push, 1)
typedef struct {
char riff[4]; // "RIFF"
uint32_t chunkSize; // File size
char wave[4]; // "WAVE"
char fmt[4]; // "fmt "
uint32_t subchunk1Size; // PCM = 16
uint16_t audioFormat; // PCM = 1
uint16_t numChannels; // Number of channels
uint32_t sampleRate; // Sample rate
uint32_t byteRate; // Byte rate
uint16_t blockAlign; // Block alignment
uint16_t bitsPerSample; // Bits per sample
} WavHeader;
#pragma pack(pop)
int main() {
FILE *inputFile = fopen("input.wav", "rb");
if (!inputFile) {
perror("Unable to open input file");
return 1;
}
WavHeader header;
fread(&header, sizeof(WavHeader), 1, inputFile);
if (strncmp(header.riff, "RIFF", 4) != 0 || strncmp(header.wave, "WAVE", 4) != 0) {
fprintf(stderr, "Not a valid WAV file\n");
fclose(inputFile);
return 1;
}
// Modify sample rate to 32000
header.sampleRate = 32000;
header.byteRate = header.sampleRate * header.numChannels * header.bitsPerSample / 8;
FILE *outputFile = fopen("output.wav", "wb");
if (!outputFile) {
perror("Unable to open output file");
fclose(inputFile);
return 1;
}
// Write modified header
fwrite(&header, sizeof(WavHeader), 1, outputFile);
// Copy the remaining data from input file to output file
char buffer[4096];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), inputFile)) > 0) {
fwrite(buffer, 1, bytesRead, outputFile);
}
fclose(inputFile);
fclose(outputFile);
// Print completion messages in English and Japanese
printf("Sample rate modification completed, output saved to output.wav\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment