Press the sound at the beginning when using Lame

I use lame to convert a WAV file (extracted from an Audio CD) to MP3. The result of the conversion is just fine, except that at the very beginning of the file there is one single click sound. A click takes almost 0.5 seconds to the song itself.

char *input_file = argv[1];
char *output_file = argv[2];

FILE *pcm = fopen(input_file, "rb");
FILE *mp3 = fopen(output_file, "wb+");

size_t nread;
int ret, nwrite;

const int PCM_SIZE = 1152;
const int MP3_SIZE = 1152;

short pcm_buffer[PCM_SIZE * 2];
unsigned char mp3_buffer[MP3_SIZE];

lame_t lame = lame_init();

// Can not put these lines at the end of conversion
id3tag_set_title(lame, "Still reminds me");
id3tag_set_artist(lame, "Anggun");

lame_set_VBR(lame, vbr_mt);
lame_set_VBR_quality(lame, 2);

ret = lame_init_params(lame);

do {
    nread = fread(pcm_buffer, sizeof(short), PCM_SIZE * 2, pcm);

    if (nread != 0) {
        // Is this the cause of the single click?
        int nsamples = nread / 2;
        short buffer_l[nsamples];
        short buffer_r[nsamples];

        int j = 0;
        int i = 0;
        for (i = 0; i < nsamples; i++) {
            buffer_l[i] = pcm_buffer[j++];
            buffer_r[i] = pcm_buffer[j++];

        }

        nwrite = lame_encode_buffer(lame, buffer_l, buffer_r, nsamples,
                mp3_buffer, MP3_SIZE);

    } else {
        nwrite = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);

    }

    fwrite(mp3_buffer, nwrite, 1, mp3);
} while (nread != 0);


lame_close(lame);

fclose(mp3);
fclose(pcm);

What's happening? What am I missing here?

+5
source share
3 answers

, , Dave L, . wav, , , 44 . pcm, , 4096 . wav (, , 44- ),

fseek(pcm,44,0);

, . wav , .

( , 4096b), .

+6

? .

FWIW, 1024 (.. , ). 'click' . , , , .

+2

, , , . 44 - . wav 80 . , wav , . , , .., .

, . , Lame ( get_audio.c parse_wave_header(lame_global_flags * gfp, FILE * sf)

, , , .

0
source

All Articles