/* wavfile.h */ struct WavHdr { char riff[4]; /* "RIFF" */ unsigned long file_dim; /* filesize */ char wave_format[8]; /* "WAVEfmt " */ unsigned long reserved1; /* 16L */ unsigned short int reserved2; /* 1 */ unsigned short int channels; /* numero di canali */ unsigned long samp_rate; /* samplerate */ unsigned long fc; /* byte al secondo */ unsigned short int reserved3; /* numero di byte del campione (reserved3*bit_num/8) */ unsigned short int bit_num; /* numero di bytet del campione */ char data[4]; /* "data" */ unsigned long samp_num; /* numero totale di campioni */ }; // Stampa le info estratte da un header void printWaveInfo (struct WavHdr header) { //printf ("RIFF: %10s\n", header.riff); printf ("File dim: %10ld\n", header.file_dim); //printf ("Wave format: %10s\n",header.wave_format); printf ("Reserved1: %10ld\n", header.reserved1); printf ("Reserved2: %10d\n", header.reserved2); printf ("Channels: %10d\n", header.channels); printf ("Samp rate: %10ld\n", header.samp_rate); printf ("FC: %10ld\n", header.fc); printf ("Reserved3: %10d\n", header.reserved3); printf ("Bit num: %10d\n", header.bit_num); //printf ("Data: %10s\n", header.data); printf ("Samp num: %10ld\n", header.samp_num); printf ("\n"); } // Legge un file wave *filename // mette l'header in *hrd e il contenuto in **buffer // Ritorna il numero di campioni o -1 in caso d'errore int readWaveFile (char *filename, struct WavHdr *hdr, short int **buffer) { FILE *fp; int samples; int read; fp = fopen (filename,"rb"); // "rb": read, bynary if (fp == NULL) { printf ("ERROR reading %s\n", filename); return -1; } // Leggo l'header in *hdr fread ((void *)hdr, sizeof(*hdr), 1 , fp); samples = hdr->samp_num / 2; // Leggo i campioni in **buffer (puntatore a puntatore) *buffer = (short int *) calloc (samples, 2); read = fread (*buffer, 2 , samples, fp); //printf ("FREAD: %d\n",read); // chiudo il file fclose (fp); return read; } // Scrive **buffer su un file wave *filename // usa *hdr per scrivere l'header // Ritorna il numero di campioni scritti o -1 in caso d'errore int writeWaveFile (char *filename, struct WavHdr *hdr, short int **buffer) { FILE *fp; int samples; int wrotenum; fp = fopen (filename,"wb"); if (fp == NULL) { printf ("ERROR writing %s\n", filename); return -1; } samples = hdr->samp_num / 2; // Scrivo l'header contenuto in hdr fwrite ( (void *) hdr, sizeof(*hdr), 1 , fp); // Poi scrivo il buffer wrotenum = fwrite ((void *) *buffer, 2 , samples, fp); // Chiudo il file fclose (fp); printf ("wrote %d bytes ",wrotenum*2 + sizeof (*hdr)); return wrotenum; }