Last active
January 26, 2021 05:04
-
-
Save harieamjari/8893ee24c2084e35a29e3daf5cfe6a0e to your computer and use it in GitHub Desktop.
Add a delay line to a s16le mono 44100 wav file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE | |
Version 1, October 2013 | |
Copyright (c) 2020 Al-buharie Amjari <[email protected]> | |
Everyone is permitted to copy and distribute verbatim or modified copies | |
of this license document, and changing it is allowed as long as the name | |
is changed. | |
DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE TERMS | |
AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION: | |
0. You just DO WHAT THE FUCK YOU WANT TO. | |
1. Do not hold the author(s), creator(s), developer(s) or distributor(s) | |
liable for anything that happens or goes wrong with your use of the work. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define DELAYLINE 22050 /* Half a second */ | |
char wav_struct[] = { | |
'R', 'I', 'F', 'F', | |
0, 0, 0, 0, /* chunksize */ | |
'W', 'A', 'V', 'E', | |
'f', 'm', 't', ' ', /* subchunk1id */ | |
16, 0, 0, 0, /* subchunk1size */ | |
1, 0, /* audioformat */ | |
1, 0, /* numchannels */ | |
68, 172, 0, 0, /* samplerate 44100 */ | |
136, 88, 1, 0, /* byterate 88200 */ | |
2, 0, /* blockalign */ | |
16, 0, /* bitspersample */ | |
'd', 'a', 't', 'a', /* subchunk2id */ | |
0, 0, 0, 0 /* subchunk2size */ | |
}; | |
int main(int argc, char *argv[]){ | |
if (argc < 3){ | |
fprintf(stderr, "usage: %s [wavfile] [decay]\n" | |
"example: %s file.wav 2\n", argv[0], argv[0]); | |
return 1; | |
} | |
FILE *fpin = fopen(argv[1], "rb"); | |
if (!fpin){ | |
fprintf(stderr, "%s: No such file or directory\n", argv[1]); | |
return 1; | |
} | |
char data_tag[4]; | |
/* seek to subchunk2size */ | |
fseek(fpin, 36, SEEK_SET); | |
do { | |
fread(data_tag, sizeof(char), 4, fpin); | |
if (!strncmp(data_tag, "data", 4)) break; | |
fseek(fpin, -3, SEEK_CUR); | |
} while (1); | |
uint32_t subchunk2size; | |
uint32_t numsamples; | |
fread(&subchunk2size, sizeof(uint32_t), 1, fpin); | |
numsamples = (subchunk2size << 1); | |
int16_t *pcm_in = malloc(sizeof(int16_t)*numsamples); | |
if (pcm_in == NULL){ | |
fprintf(stderr, "Allocating failed\n"); | |
return 1; | |
} | |
fread(pcm_in, sizeof(int16_t), numsamples, fpin); | |
fclose(fpin); | |
FILE *fpout = fopen("write.wav", "wb"); | |
if (fpout == NULL) { | |
fprintf(stderr, "Couldn't create write.wav\n"); | |
return 1; | |
} | |
fwrite(wav_struct, sizeof(wav_struct), 1, fpout); | |
uint32_t delayline = DELAYLINE; | |
int16_t s_temp; | |
for (uint32_t i = 0; i < numsamples-delayline; i++){ | |
s_temp = | |
pcm_in[i+delayline] += pcm_in[i]/atoi(argv[2]); | |
fwrite(&s_temp, sizeof(int16_t), 1, fpout); | |
} | |
uint32_t length = 32+subchunk2size; | |
fseek(fpout, 4, SEEK_SET); | |
fwrite(&length, sizeof(uint32_t), 1, fpout); | |
fseek(fpout, 40, SEEK_SET); | |
fwrite(&subchunk2size, sizeof(uint32_t), 1, fpout); | |
fclose(fpout); | |
free(pcm_in); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment