4

I have added LibFlac in xcode project . Then I added decode/main.c from Libflac in my project. I passed infile.flac and run executable of project but it is giving following error

decoding: FAILED state: FLAC__STREAM_DECODER_END_OF_STREAM logou

t

Here is main.c

int main(int argc, char *argv[])
{
    FLAC__bool ok = true;
    FLAC__StreamDecoder *decoder = 0;
    FLAC__StreamDecoderInitStatus init_status;
    FILE *fout;

    const char *infile = "infile.flac";
    const char *outfile = "outfile.wav";

    /*
    if(argc != 3) {
        fprintf(stderr, "usage: %s infile.flac outfile.wav\n", argv[0]);
        return 1;
    }
    */

    if((fout = fopen("infile.flac", "wb")) == NULL) {
        fprintf(stderr, "ERROR: opening %s for output\n", argv[2]);
        return 1;
    }

    if((decoder = FLAC__stream_decoder_new()) == NULL) {
        fprintf(stderr, "ERROR: allocating decoder\n");
        fclose(fout);
        return 1;
    }

    (void)FLAC__stream_decoder_set_md5_checking(decoder, true);

    init_status = FLAC__stream_decoder_init_file(decoder, infile, write_callback, metadata_callback, error_callback, /*client_data=*/fout);
    if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
        fprintf(stderr, "ERROR: initializing decoder: %s\n", FLAC__StreamDecoderInitStatusString[init_status]);
        ok = false;
    }

    if(ok) {
        ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
        fprintf(stderr, "decoding: %s\n", ok? "succeeded" : "FAILED");
        fprintf(stderr, "   state: %s\n", FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
    }

    FLAC__stream_decoder_delete(decoder);
    fclose(fout);

    return 0;
}

Please help me. why I am getting this error ?

Abizern
  • 146,289
  • 39
  • 203
  • 257
iProgrammer
  • 3,099
  • 3
  • 33
  • 59

1 Answers1

3

fopening your input file with "wb" will truncate your infile on opening it. That can't be what you want, right? I think you really mean;

if((fout = fopen(outfile, "wb")) == NULL) {

There seems to be some confusion how the FLAC sample works.

FLAC__stream_decoder_init_file

opens the file you give it the filename to for decoding and sets up callbacks for the decoding.

FLAC__stream_decoder_process_until_end_of_stream

decodes the file and for every decoded frame it calls the write_callback function provided in the call to FLAC__stream_decoder_init_file with the parameter given as last parameter to it.

In other words, all the work of writing the file is done in write_callback. That's where you are provided with the decoded data and you should generate and write the output file, frame by frame. If you look at the sample at http://flac.cvs.sourceforge.net/viewvc/flac/flac/examples/c/decode/file/main.c?view=markup which seems to be what you've copied to start with, that's exactly what it does.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • I want to convert flac to wav and here I should pass infile not outfile – iProgrammer Jan 09 '12 at 09:39
  • You're passing your input file name to FLAC__stream_decoder_init_file, that method will open the input file for you using the name you supplied. fout is in your example only used for passing to the callbacks which should - as far as I understand your problem - only write to the output file. – Joachim Isaksson Jan 09 '12 at 09:45
  • Also, if you _really_ want to open the input file there, you should open it using "rb" for read, not "wb" for truncate and overwrite. – Joachim Isaksson Jan 09 '12 at 09:47
  • ok I used "rb " also but still giving me error.If you have done this before Please help me, I am stuck in this since last 2 weeks. – iProgrammer Jan 09 '12 at 09:50
  • Is your flac file the right size or is it truncated since the last run to 0 bytes? Also, are you getting the same error? Since I don't have your complete code I can't compile and test myself. – Joachim Isaksson Jan 09 '12 at 09:56
  • my flac size is 55mb and If you can come to chat discussion I could provide you my code – iProgrammer Jan 09 '12 at 09:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6544/discussion-between-iphonedeveloper-and-joachim-isaksson) – iProgrammer Jan 09 '12 at 10:00