Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Created July 14, 2020 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harieamjari/87155e1408d7ceed229b0bc491aa3652 to your computer and use it in GitHub Desktop.
Save harieamjari/87155e1408d7ceed229b0bc491aa3652 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
char wav_data[] = {
'R', 'I', 'F', 'F',
0, 0, 0, 0,
'W', 'A', 'V', 'E',
'f', 'm', 't', ' ',
16, 0, 0, 0, /*subchunk1size*/
1, 0, /*audio format*/
1, 0, /*num channels*/
68, 172, 0, 0, /*sample rate 44100*/
136, 88, 1, 0, /*byte rate 88200*/
2, 0, /*block align*/
16, 0, /*bits per sample*/
'd', 'a', 't', 'a',/*subchunk2id*/
0, 0, 0, 0 /*subchunk2size*/
};
int main(){
int numsamples;
uint32_t length;
uint32_t subchunk2size;
FILE *fp = fopen("rand.wav", "wb");
if (!fp) {perror("rand.wav"); return 1;}
fwrite(wav_data, sizeof(wav_data), 1, fp);
int16_t buff;
while (1){
fread(&buff, sizeof(int16_t), 1, stdin);
if (feof(stdin)) break;
fwrite(&buff, sizeof(int16_t), 1, fp);
numsamples++;
}
length = (uint32_t)ftell(fp)-(uint32_t)8;
fseek(fp, 4, SEEK_SET);
fwrite(&length, sizeof(uint32_t), 1, fp);
subchunk2size = numsamples*2;
fseek(fp, 40, SEEK_SET);
fwrite(&subchunk2size, sizeof(uint32_t), 1, fp);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment