0

I need to delete pause in wav file (pause - area with amplitudes<16 and length>N samples). Don't really understand how to work with samples, and how to read sample by sample from file. Below is my code:

#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 4000

typedef struct header {
    char chunk_id[4];
    int chunk_size;
    char format[4];
    char subchunk1_id[4];
    int subchunk1_size;
    short int audio_format;
    short int num_channels;
    int sample_rate;
    int byte_rate;
    short int block_align;
    short int bits_per_sample;
    short int extra_param_size;
    char subchunk2_id[4];
    int subchunk2_size;
} header;
typedef struct header* header_p;

void change_file(char * input, int N){
    FILE * infile = fopen(input, "rb");
    FILE * outfile = fopen("outfile.wav", "wb");
    short int inbuff16[BUFSIZE];
    header_p meta = (header_p)malloc(sizeof(header));

    if (infile) {
        fread(meta, 1, sizeof(header), infile);
        fwrite(meta, 1, sizeof(header), outfile);

        while (!feof(infile)) {
                fread(inbuff16, 1, BUFSIZE, infile);      
                //delete pause here
                fwrite(inbuff16, 1, BUFSIZE, outfile);
        }
    }
    if (infile) { fclose(infile); }
    if (outfile) { fclose(outfile); }
    if (meta) { free(meta); }
}

int main (int argc, char const *argv[]){
    char infile[] = "track.wav";
    change_file(infile, 20);
    return 0;
}
luminousmen
  • 1,971
  • 1
  • 18
  • 24
  • 1
    You can count "silent" samples, and when pause is detected, set the output stream position to the beginning of the pause. Or you can read the entire file into memory, and process it there. The latter is probably a better approach overall. – void_ptr Jun 04 '15 at 22:09
  • The structure tells you how large each sample is and how many there are, but how to *process* samples is a bit broad. If I recall correctly, it mainly depends on the `audio_format`; some formats are well documented, some are not, some can be processed with simple code and others cannot. – Jongware Jun 04 '15 at 22:42

1 Answers1

0

You must not use !feof(infile) expression in the while loop.

For details:

Why is “while ( !feof (file) )” always wrong?

Community
  • 1
  • 1
Hsyn
  • 46
  • 1
  • 8