gcc towav.c -o towav e Voilá!
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc, char ** argv){
if(argc < 4){
printf("Usage:\n");
printf("%s
exit(0);
}
char * filename = malloc(strlen(argv[1]));
strcpy(filename, argv[1]);
struct stat st;
stat(filename, &st);
uint32_t size = st.st_size; //limita o tamanho do arquivo!
printf("filesize: %d\n", size);
// Read file data
char * buffer = 0;
FILE * f = fopen (filename, "rb");
buffer = malloc(size);
fread (buffer, 1, size, f);
fclose (f);
char * newfilename = malloc(strlen(argv[1] + 4));
strcpy(newfilename, filename);
strcat(newfilename, ".wav");
FILE *fp = fopen(newfilename,"wb+");
fwrite("RIFF", 1, 4, fp);
int newsize = size + 32; // file size - 8
fwrite(&newsize, 1, sizeof(newsize), fp);
fwrite("WAVE", 1, 4, fp);
fwrite("fmt ", 1, 4, fp);
int flag = 16; // chunk size
fwrite(&flag, 1, 4, fp);
flag = 1; // compression code
fwrite(&flag, 1, 2, fp);
flag = 1; // number of channels
fwrite(&flag, 1, 2, fp);
flag = atoi(argv[2]); // sample rate
fwrite(&flag, 1, 4, fp);
flag = flag * atoi(argv[3]) / 8; // bytes per second
fwrite(&flag, 1, 4, fp);
flag = atoi(argv[3]) / 8; // block align
fwrite(&flag, 1, 2, fp);
flag = atoi(argv[3]); // significant bits per sample
fwrite(&flag, 1, 2, fp);
fwrite("data", 1, 4, fp);
fwrite(&size, 1, 4, fp);
fwrite(buffer, 1, size, fp);
fclose(fp);
}
Algumas referências:
- http://www.sonicspot.com/guide/wavefiles.html
- https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
O dmesg ficou muito bom tocado a 10025 com 8 bits!
Um comentário:
divertido esse negócio ai flávio...
Postar um comentário